我在linux上进行rs232通信时遇到了严重麻烦,因此我编写了此测试程序,以确保它不会干扰程序的其他部分。
但是该程序无法正常工作,因为我担心这是问题所在的串行端口代码。
我有一台带centos的笔记本电脑运行该程序,该笔记本电脑与运行超级终端的Windows XP的计算机相连。代码会根据错误检查正常执行,但在huperterminal中什么都没有显示。
我要达到的串行毛孔设置是115200波特率,8个数据位,1个停止位和标记奇偶校验。
这是程序:
#include <termios.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <ncurses.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
int port, serial, i;
unsigned long nobw;
char buf[10];
struct termios options;
port = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (port == -1)
perror("open_port: Unable to open /dev/ttyS0 - ");
else
printf("port open \n");
tcgetattr(port, &options); // get current settings
cfsetispeed(&options, 115200); // set baud rate
cfsetospeed(&options, 115200); // set baud rate
options.c_cflag &= ~CSIZE; // Mask the character size bits
options.c_cflag |= CS8; // 8 bit data
options.c_cflag &= ~PARENB; // set parity to no
options.c_cflag &= ~PARODD; // set parity to no
options.c_cflag |= CSTOPB;//set mark parity by using 2 stop bits
options.c_cflag |= (CLOCAL | CREAD);
options.c_oflag &= ~OPOST;
options.c_lflag &= 0;
options.c_iflag &= 0; //disable software flow controll
options.c_oflag &= 0;
tcsetattr(port, TCSANOW, &options);// save the settings
ioctl(port, TIOCMGET, &serial);
serial |= TIOCM_DTR; // set DTR to high
ioctl(port, TIOCMSET, &serial);
for(i = 0; i < 10; i++)
{
buf[i] = i;
}
for(i = 0; i < 10; i++)
{
errno = 0;
nobw = write(port, buf, 1);
if(nobw == -1)
perror("WriteComm:");
else
printf("sent character %d \n", i);
}
return 0;
}
这些都是从实习生的教程中完成的,我几乎不知道自己在做什么,您能看到我哪里出问题了吗?
如果有人知道如何进行空间奇偶校验,也将不胜感激。
最佳答案
也许值得一看,如果没有您的代码,安装程序是否可以工作? :)
如果您在Linux端使用minicom进行连接,而在Windows端使用超级终端进行连接,则可以来回传递数据吗?
关于c++ - 编写此串行测试程序时我错过了什么,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7660446/