- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我已将我的 XBee 模块(通过 UART 进行通信)连接到我的 PC。我从一个远程节点将数据无线传输到我的 XBee。
我在 Linux 终端上接收字节时遇到问题。虽然我能够将数据传输到远程节点。找到我在 Linux 终端上编写的以下代码以进行串行通信。但是为了调试,我将 XBee 配置为环回。(TXD-RXD 短路)
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
//Initialize serial port
int initport(int fd)
{
int portstatus = 0;
struct termios options;
// Get the current options for the port...
tcgetattr(fd, &options);
// Set the baud rates to 9600...
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
// Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
//options.c_cflag |= SerialDataBitsInterp(8); /* CS8 - Selects 8 data bits */
options.c_cflag &= ~CRTSCTS; // Disable hardware flow control
options.c_iflag &= ~(IXON | IXOFF | IXANY); // Disable XON XOFF (for transmit and receive)
//options.c_cflag |= CRTSCTS; /* Enable hardware flow control */
options.c_cc[VMIN] = 1; //Minimum characters to be read
options.c_cc[VTIME] = 2; //Time to wait for data (tenths of seconds)
options.c_oflag &=~OPOST;
options.c_iflag &=~(ICANON | ECHO | ECHOE | ISIG);
// Set the new options for the port...
tcsetattr(fd, TCSANOW, &options);
//Set the new options for the port...
tcflush(fd, TCIFLUSH);
if (tcsetattr(fd, TCSANOW, &options) == -1)
{
perror("On tcsetattr:");
portstatus = -1;
}
else
portstatus = 1;
return (portstatus);
}
int open_port(void)
{
int fd; /* File descriptor for the port */
fd = open("/dev/ttyUSB1", O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd == -1)
{
/* Could not open the port. */
perror("Open_port: Unable to open /dev/ttyUSB0 --- \n");
}
else
{message print....};
return (fd);
}
int main(void)
{
int i;
unsigned char write_buf[] =
{0x7E, 0x00, 0x16, 0x10, 0x01, 0x00, 0x13,
0xA2, 0x00, 0x40, 0xE4, 0x22, 0x64, 0xFF,
0xFE, 0x00, 0x00, 0x41, 0x42, 0x43, 0x44,
0x41, 0x42, 0x43, 0x43, 0x7F};
int serial_fd = open_port();
if (serial_fd == -1)
printf("Error opening serial port /dev/ttyUSB0 \n");
else
{
printf("Serial Port /dev/ttyUSB0 is Open\n");
if (initport(serial_fd) == -1)
{
printf("Error Initializing port");
close(serial_fd);
return 0;
}
int n = write(serial_fd, &write_buf, sizeof write_buf);
if (n < 0)
fputs("write() failed!\n", stderr);
else
{
printf("Successfully wrote %d bytes\n", sizeof write_buf);
for (i=0; i<n; i++)
{
printf("%c ",write_buf[i]);
}
}
}
usleep(200000);
char read_buf[128];
int n1 = read(serial_fd, read_buf, sizeof read_buf);
// sleep(1);
if (n1 < 0)
fputs("Read failed!\n", stderr);
else
{
printf("Successfully read from serial port -- %s\n With %d Bytes", read_buf,n1);
}
// sleep(.5);
// usleep(500000);
printf("\n\nNow closing serial port /dev/ttyUSB0 \n\n");
close(serial_fd);
return 0;
}
当我执行这段代码时,它确实显示收到了 1 个字节。我在 read() 之前引入了延迟。但它显示在 Tera Term 上(有点像 super 终端)。此外,我必须在 Xbee 上发送/接收十六进制字符串才能传输,因为它们以 API 模式进行通信。
我该如何解决这个问题?
最佳答案
您等待字节从 XBee 模块传入的时间不够长。尝试将您的 sleep(1)
移动到 read()
之前,并可能将其增加到几秒钟。在尝试调用 read 之前,您可能甚至还没有完成以 9600 波特发送数据!请记住,write()
正在将数据传递给操作系统,并且它经过缓冲以通过串行连接发送出去。
在一个典型的应用程序中,您会有一个循环,在该循环中您尝试定期read()
,然后处理从远程端传入的任何内容。请记住,您会以 block 的形式看到它,并且不能只将其打印为字符串。您通常会在字符串中遗漏一个空终止符,如果您有二进制数据,它可能会扰乱您的终端 session 。
当您使用它时,请考虑将您的 XBee 模块配置为 115,200bps 而不是 9600。它的响应速度会快得多。
你可能想看看这个 Open Source ANSI C XBee Host Library我付出了很多努力。它可以针对 POSIX 系统,包括许多层,用于从原始串行到 API 模式,甚至一些 ZigBee 处理(ZDP 端点和 ZCL)的通信。
关于c - 使用 C 在 Linux COM 上进行 XBee 串行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33450159/
我喜欢 smartcase,也喜欢 * 和 # 搜索命令。但我更希望 * 和 # 搜索命令区分大小写,而/和 ?搜索命令遵循 smartcase 启发式。 是否有隐藏在某个地方我还没有找到的设置?我宁
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 10年前关闭。 Improve this qu
从以下网站,我找到了执行java AD身份验证的代码。 http://java2db.com/jndi-ldap-programming/solution-to-sslhandshakeexcepti
似乎 melt 会使用 id 列和堆叠的测量变量 reshape 您的数据框,然后通过转换让您执行聚合。 ddply,从 plyr 包看起来非常相似..你给它一个数据框,几个用于分组的列变量和一个聚合
我的问题是关于 memcached。 Facebook 使用 memcached 作为其结构化数据的缓存,以减少用户的延迟。他们在 Linux 上使用 UDP 优化了 memcached 的性能。 h
在 Camel route ,我正在使用 exec 组件通过 grep 进行 curl ,但使用 ${HOSTNAME} 的 grep 无法正常工作,下面是我的 Camel 路线。请在这方面寻求帮助。
我正在尝试执行相当复杂的查询,在其中我可以排除与特定条件集匹配的项目。这是一个 super 简化的模型来解释我的困境: class Thing(models.Model) user = mod
我正在尝试执行相当复杂的查询,我可以在其中排除符合特定条件集的项目。这里有一个 super 简化的模型来解释我的困境: class Thing(models.Model) user = mod
我发现了很多嵌入/内容项目的旧方法,并且我遵循了在这里找到的最新方法(我假设):https://blog.angular-university.io/angular-ng-content/ 我正在尝试
我正在寻找如何使用 fastify-nextjs 启动 fastify-cli 的建议 我曾尝试将代码简单地添加到建议的位置,但它不起作用。 'use strict' const path = req
我正在尝试将振幅 js 与 React 和 Gatsby 集成。做 gatsby developer 时一切看起来都不错,因为它发生在浏览器中,但是当我尝试 gatsby build 时,我收到以下错
我试图避免过度执行空值检查,但同时我想在需要使代码健壮的时候进行空值检查。但有时我觉得它开始变得如此防御,因为我没有实现 API。然后我避免了一些空检查,但是当我开始单元测试时,它开始总是等待运行时异
尝试进行包含一些 NOT 的 Kibana 搜索,但获得包含 NOT 的结果,因此猜测我的语法不正确: "chocolate" AND "milk" AND NOT "cow" AND NOT "tr
我正在使用开源代码共享包在 iOS 中进行 facebook 集成,但收到错误“FT_Load_Glyph failed: glyph 65535: error 6”。我在另一台 mac 机器上尝试了
我正在尝试估计一个标准的 tobit 模型,该模型被审查为零。 变量是 因变量 : 幸福 自变量 : 城市(芝加哥,纽约), 性别(男,女), 就业(0=失业,1=就业), 工作类型(失业,蓝色,白色
我有一个像这样的项目布局 样本/ 一种/ 源/ 主要的/ java / java 资源/ .jpg 乙/ 源/ 主要的/ java / B.java 资源/ B.jpg 构建.gradle 设置.gr
如何循环遍历数组中的多个属性以及如何使用map函数将数组中的多个属性显示到网页 import React, { Component } from 'react'; import './App.css'
我有一个 JavaScript 函数,它进行 AJAX 调用以返回一些数据,该调用是在选择列表更改事件上触发的。 我尝试了多种方法来在等待时显示加载程序,因为它当前暂停了选择列表,从客户的 Angul
可能以前问过,但找不到。 我正在用以下形式写很多语句: if (bar.getFoo() != null) { this.foo = bar.getFoo(); } 我想到了三元运算符,但我认
我有一个表单,在将其发送到 PHP 之前我正在执行一些验证 JavaScript,验证后的 JavaScript 函数会发布用户在 中输入的文本。页面底部的标签;然而,此消息显示短暂,然后消失...
我是一名优秀的程序员,十分优秀!