在编写这个串行测试程序时,我错过了什么?

What have i missed when writing this serial test program

本文关键字:错过了 什么 测试程序      更新时间:2023-10-16

我在linux中的rs232通信遇到了严重的问题,所以我编写了这个测试程序,以确保它不是我的程序的其他部分干扰串行通信。

程序但是不工作,所以我担心这是串行端口代码的问题。

我有一台运行centos程序的笔记本电脑,它连接到一台运行windows xp超级终端的电脑。根据错误检查,代码执行正常,但在超级终端中没有显示任何内容。

我试图实现的串行孔设置是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端使用超级终端连接,可以来回传递数据吗?

相关文章: