C 串行编程:发送器和接收器
因此,我在一个项目中使用两个Xbee ZB(S2B)将数据从一个传输到另一个。它是一个8数据位、无奇偶校验、1停止位系统(8N1) 我有两个问题 1。由于我使用的是笔记本电脑USB适配器的RS232(DB9连接器)接口,波特率为B230400时,系统调用fwrite/fread/fopen/fclose是否比使用写/读/开/关更好?(我假设fread()无法正常工作,因为据我所知,它没有波特率配置。) 2.我有一台计算机运行一个程序(发射机程序)和另一个程序(接收机程序)。这两个程序都需要能够读写,因为我希望能够从发送端和接收端了解不同的信息集 像接收端(如果我看到字母Q,停止并关闭端口)或发送端(等待读取端发送字母G开始写入,写入完成后关闭) 在非规范输入处理下使用()中的一些代码,我尝试设置一个基本模板,用于从USB端口的任意一侧进行写入和读取。我的问题是: *发送和接收程序的termios结构(newtio)是否需要不同的配置设置(c_cflag、c_iflag、c_oflag、c_lflag)* 我的代码:C 串行编程:发送器和接收器,c,serial-port,xbee,termios,C,Serial Port,Xbee,Termios,因此,我在一个项目中使用两个Xbee ZB(S2B)将数据从一个传输到另一个。它是一个8数据位、无奇偶校验、1停止位系统(8N1) 我有两个问题 1。由于我使用的是笔记本电脑USB适配器的RS232(DB9连接器)接口,波特率为B230400时,系统调用fwrite/fread/fopen/fclose是否比使用写/读/开/关更好?(我假设fread()无法正常工作,因为据我所知,它没有波特率配置。) 2.我有一台计算机运行一个程序(发射机程序)和另一个程序(接收机程序)。这两个程序都需要能够读
#include <stdio.h>
#include <strings.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <termios.h>
int open_port(char *path, int modes)
{
int fd; // File descriptor for port
fd = open(path, modes); // Open the path
if(fd == -1) return -1; // Could not be opened
printf("Port %s opened successfully.\n", path);
return fd; // Return fd
}
int setBaud(struct termios *termios_p, int baud)
{
int ires = 0; int ores = 0;
switch(baud)
{
0:
ires = cfsetispeed(&termios_p, B0);
ores = cfsetispeed(&termios_p, B0);
break;
50:
ires = cfsetispeed(&termios_p, B50);
ores = cfsetispeed(&termios_p, B50);
break;
75:
ires = cfsetispeed(&termios_p, B75);
ores = cfsetispeed(&termios_p, B75);
break;
110:
ires = cfsetispeed(&termios_p, B110);
ores = cfsetispeed(&termios_p, B110);
break;
134:
ires = cfsetispeed(&termios_p, B134);
ores = cfsetispeed(&termios_p, B134);
break;
150:
ires = cfsetispeed(&termios_p, B150);
ores = cfsetispeed(&termios_p, B150);
break;
200:
ires = cfsetispeed(&termios_p, B200);
ores = cfsetispeed(&termios_p, B200);
break;
300:
ires = cfsetispeed(&termios_p, B300);
ores = cfsetispeed(&termios_p, B300);
break;
600:
ires = cfsetispeed(&termios_p, B600);
ores = cfsetispeed(&termios_p, B600);
break;
1200:
ires = cfsetispeed(&termios_p, B1200);
ores = cfsetispeed(&termios_p, B1200);
break;
1800:
ires = cfsetispeed(&termios_p, B1800);
ores = cfsetispeed(&termios_p, B1800);
break;
2400:
ires = cfsetispeed(&termios_p, B2400);
ores = cfsetispeed(&termios_p, B2400);
break;
4800:
ires = cfsetispeed(&termios_p, B4800);
ores = cfsetispeed(&termios_p, B4800);
break;
9600:
ires = cfsetispeed(&termios_p, B9600);
ores = cfsetispeed(&termios_p, B9600);
break;
19200:
ires = cfsetispeed(&termios_p, B19200);
ores = cfsetispeed(&termios_p, B19200);
break;
38400:
ires = cfsetispeed(&termios_p, B38400);
ores = cfsetispeed(&termios_p, B38400);
break;
57600:
ires = cfsetispeed(&termios_p, B57600);
ores = cfsetispeed(&termios_p, B57600);
break;
115200:
ires = cfsetispeed(&termios_p, B115200);
ores = cfsetispeed(&termios_p, B115200);
break;
230400:
ires = cfsetispeed(&termios_p, B230400);
ores = cfsetispeed(&termios_p, B230400);
break;
default:
ires = cfsetispeed(&termios_p, B9600);
ores = cfsetispeed(&termios_p, B9600);
break;
}
if(ires == -1 | ores == -1) return -1; // ISpeed or OSpeed could not be set
else { printf("Baud set successfully to %d\n", baud); return 0; }
}
char* readIt(int fd)
{
char buf;
char *tmp;
tmp = calloc(2, sizeof(char);
if( read(fd, buf, 1) == 1 ) { tmp[0] = buf; tmp[1] = 0; } // Return char in tmp[0]
else { tmp[0] = 0x45; tmp[1] = 0x52; } // Return ER in char meaning error
return tmp;
}
int writeIt(int fd, char *str, int bytes)
{
if( write(fd, str, bytes) == bytes ) return 0; // Write success
else return -1; // Failed write
}
int main (int argc, char **argv)
{
int usb;
volatile int STOP = 0;
if(argc == 1) { printf("Must enter usb port path.\n"); exit(EXIT_FAILURE); } // No second argument
else if (argc > 1) { usb = open_port(argv[1], O_RDWR | O_NOCTTY ); }
struct termios oldterm, newterm;
tcgetattr(fd, &oldtio); // Save old terminal settings
bzero(&newtio, sizeof(newtio)); // Clear out struct
if( setBaud(&newtio, 230400) != 0 ) { printf("Baud could not be set\n"); exit (EXIT_FAILURE); } // Set up baud rate
newtio.c_cflag = CS8 | CREAD | CLOCAL; // 8 data bits, enable receiver, local line
newtio.c_cflag &= ~PARENB | ~CSTOPB; // No parity, one stop bit
newtio.c_iflag &= ~(IXON | IXOFF | IXANY); // Disable software flow control
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0; // No timeout clock for read
newtio.c_cc[VMIN] = 1; // Wait for one character before reading another
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &newtio); // Set fd with new settings immediately
/* Do writing or reading from port in here - readIt or writeIt*/
tcsetattr(fd, TCSANOW, &oldtio); // Set fd with old settings immediately
close(fd); // Close fd
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int open_端口(字符*路径,int模式)
{
int fd;//端口的文件描述符
fd=打开(路径,模式);//打开路径
if(fd==-1)返回-1;//无法打开
printf(“端口%s已成功打开。\n”,路径);
return fd;//return fd
}
int setBaud(结构termios*termios\u p,int波特)
{
int ires=0;int ores=0;
开关(波特率)
{
0:
ires=cfsetispeed(&termios_p,B0);
ores=cfsetispeed(&termios_p,B0);
打破
50:
ires=cfsetispeed(&termios_p,B50);
ores=cfsetispeed(&termios_p,B50);
打破
75:
ires=cfsetispeed(&termios_p,B75);
ores=cfsetispeed(&termios_p,B75);
打破
110:
ires=cfsetispeed(&termios_p,B110);
ores=cfsetispeed(&termios_p,B110);
打破
134:
ires=cfsetispeed(&termios_p,B134);
ores=cfsetispeed(&termios_p,B134);
打破
150:
ires=cfsetispeed(&termios_p,B150);
ores=cfsetispeed(&termios_p,B150);
打破
200:
ires=cfsetispeed(&termios_p,B200);
ores=cfsetispeed(&termios_p,B200);
打破
300:
ires=cfsetispeed(&termios_p,B300);
ores=cfsetispeed(&termios_p,B300);
打破
600:
ires=cfsetispeed(&termios_p,B600);
ores=cfsetispeed(&termios_p,B600);
打破
1200:
ires=cfsetispeed(&termios_p,B1200);
ores=cfsetispeed(&termios_p,B1200);
打破
1800:
ires=cfsetispeed(&termios_p,B1800);
ores=cfsetispeed(&termios_p,B1800);
打破
2400:
ires=cfsetispeed(&termios_p,B2400);
ores=cfsetispeed(&termios_p,B2400);
打破
4800:
ires=cfsetispeed(&termios_p,B4800);
ores=cfsetispeed(&termios_p,B4800);
打破
9600:
ires=cfsetispeed(&termios_p,B9600);
ores=cfsetispeed(&termios_p,B9600);
打破
19200:
ires=cfsetispeed(&termios_p,B19200);
ores=cfsetispeed(&termios_p,B19200);
打破
38400:
ires=cfsetispeed(&termios_p,B38400);
ores=cfsetispeed(&termios_p,B38400);
打破
57600:
ires=cfsetispeed(和termios_p,B57600);
ores=cfsetispeed(和termios_p,B57600);
打破
115200:
ires=cfsetispeed(&termios_p,B115200);
ores=cfsetispeed(&termios_p,B115200);
打破
230400:
ires=cfsetispeed(&termios_p,B230400);
ores=cfsetispeed(&termios_p,B230400);
打破
违约:
ires=cfsetispeed(&termios_p,B9600);
ores=cfsetispeed(&termios_p,B9600);
打破
}
if(ires==-1 | ores==-1)返回-1;//无法设置ISpeed或OSpeed
else{printf(“成功地将波特设置为%d\n”,波特);返回0;}
}
字符*readIt(int-fd)
{
焦炉;
char*tmp;
tmp=calloc(2,sizeof(char);
if(read(fd,buf,1)==1){tmp[0]=buf;tmp[1]=0;}//返回tmp[0]中的字符
else{tmp[0]=0x45;tmp[1]=0x52;}//返回字符中的ER表示错误
返回tmp;
}
int writeIt(int fd,char*str,int字节)
{
if(write(fd,str,bytes)=bytes)返回0;//写入成功
else返回-1;//写入失败
}
int main(int argc,字符**argv)
{
int usb;
挥发性int STOP=0;
if(argc==1){printf(“必须输入usb端口路径。\n”);exit(exit_FAILURE);}//无第二个参数
如果(argc>1){usb=open_-port(argv[1],O_-RDWR | O_-NOCTTY)}
结构术语旧术语,新术语;
tcgetattr(fd,&oldtio);//保存旧的终端设置
bzero(&newtio,sizeof(newtio));//清除结构
如果(setBaud(&newtio,230400)!=0){printf(“无法设置波特率”);退出(退出失败)}//设置波特率
newtio.c|u cflag=CS8 | CREAD | CLOCAL;//8个数据位,启用接收器,本地线路
newtio.c_cflag&=~PARENB | ~CSTOPB;//无奇偶校验,一个停止位
newtio.c|u iflag&=~(IXON | IXOFF | IXANY);//禁用软件流控制
newtio.c_of lag=0;
newtio.c_lflag=0;
牛顿