[英]Serial communication of XBee on Linux COM using C
我已经将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 (某种超级终端)上显示。 另外我还必须在Xbee上发送/接收十六进制字符串以进行传输,因为它们以API模式进行通信。
我该如何解决这个问题?
您没有等待足够长的时间来等待XBee模块输入字节。 尝试将sleep(1)
移到read()
之前,然后将其增加到几秒钟。 在尝试致电read之前,您甚至可能还没有以9600波特的速度发送完数据! 请记住, write()
正在将数据传递给操作系统,并且已对其进行缓冲以通过串行连接发送出去。
在典型的应用程序中,您会有一个循环,在该循环中您尝试定期read()
,然后处理来自远程端的任何内容。 请记住,您将看到它成块,并且不能仅将其打印为字符串。 您通常会在字符串上缺少空终止符,并且如果您拥有二进制数据,则可能会使您的终端会话混乱。
在使用它时,请考虑将XBee模块配置为115,200bps而不是9600bps。它将响应速度更快。
您可能想看看我投入了大量工作的这个开源ANSI C XBee主机库 。 它可以针对POSIX系统,并包括用于从原始串行到API模式甚至是某些ZigBee处理(ZDP端点和ZCL)通信的多层。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.