在编写这个串行测试程序时,我错过了什么?
What have i missed when writing this serial test program
我在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端使用超级终端连接,可以来回传递数据吗?
相关文章:
- 我的C++合并排序代码不起作用。我在这里错过了什么?
- 我是否为邪恶刽子手的构造函数错过了什么?
- C++:将向量传递给函数,然后在main中调用函数.错过了什么
- 随机访问迭代器:我错过了什么?
- 在这个while循环中我错过了什么?
- 我是否错过了什么,或者虚拟呼叫的性能并不像人们所说的那样糟糕
- 我在变量上收到 3 个 C4703 错误,我认为我已经正确初始化了,但我不确定我错过了什么
- 如果这不是 boost::lockfree::d etail::freelist 中的错误,我在这里错过了什么
- 尝试使用类,但未打印任何内容.不会生成任何错误.我错过了什么吗?
- 尝试实现通过引用传递的向量以与二叉树一起使用,我错过了什么
- 此代码的输出是什么?我在这里错过了什么吗?
- 下面显示的代码片段在 Coliru 和 Ideone 中编译,但根据 iso § 8.5 p6,它不应该,还是我错过了什么?
- AFAIK,下面的代码不应该编译,但它可以在 clang 和 GCC 中编译。我在这里错过了什么?
- 仅在标头中定义时内联的函数.我错过了什么吗?
- 我错过了什么吗?我一直在输出"No file found!"
- 安全布尔成语 - 我是否找到了更简单的方法,还是我错过了什么
- 我错过了什么
- 无法完成Rice(Ruby)的简单示例.我错过了什么
- 为什么包括警卫对我没有影响?我是不是错过了什么
- 据我所知,下面的函数不是constexpr,而是用clang和g++编译的代码.我错过了什么