C++使用 Write() 串行端口仅响应一次

发布于 2024-08-08 10:39:32 字数 954 浏览 3 评论 0原文

下面的所有代码都有效。我的设备响应,C,7 是重置。当我第二次运行它时,它没有响应。如果我手动关闭和打开设备,然后再次运行此脚本,它就会起作用。但如果我第二次按下按钮运行脚本则不会。

RS232:57600,8,N,1

有什么想法吗?是否需要更多信息来解决此问题?

*此外,当我开始工作时,我将不得不使用 read() 函数来获取设备响应。根据下面的代码,有谁知道我需要使用的正确格式?抱歉,我是 C++ 新手...我更喜欢 PHP。

*我也不知道 1024 是否正确,但它似乎很有效......

#include <termios.h>

int fd;
struct termios options;
fd=open("/dev/tty.KeySerial1", O_RDWR | O_NOCTTY | O_NDELAY);
fcntl(fd, F_SETFL, 0);
tcgetattr(fd,&options);
options.c_ispeed=57600;
options.c_ospeed=57600;
options.c_cflag |= (CLOCAL | CREAD);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_cflag &= ~CSTOPB;
options.c_lflag &= ~ECHO;
options.c_oflag &= ~ECHO;
options.c_oflag &= ~OPOST;
options.c_cflag |= CS8;
options.c_cflag |= CRTSCTS;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] =10;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&options);

write(fd, "C,7\r\n", 1024);

close(fd);

All the code below works. My device responds, C,7 is a reset. When I run this the second time it doesn't respond. If I manually turn my device off and on, then run this script again it works. But not if I press the button to run the script the second time.

RS232: 57600,8,N,1

Any ideas?? Is there any more information needed to solve this?

*Also when I get this working I'm going to have to use the read() function to get the devices responses. Does anyone know the correct format I need to use, based on the below code? Sorry I'm new to C++...I'm more of a PHP guy.

*I also don't know if 1024 is right, but it seems to work so eh...

#include <termios.h>

int fd;
struct termios options;
fd=open("/dev/tty.KeySerial1", O_RDWR | O_NOCTTY | O_NDELAY);
fcntl(fd, F_SETFL, 0);
tcgetattr(fd,&options);
options.c_ispeed=57600;
options.c_ospeed=57600;
options.c_cflag |= (CLOCAL | CREAD);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_cflag &= ~CSTOPB;
options.c_lflag &= ~ECHO;
options.c_oflag &= ~ECHO;
options.c_oflag &= ~OPOST;
options.c_cflag |= CS8;
options.c_cflag |= CRTSCTS;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] =10;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&options);

write(fd, "C,7\r\n", 1024);

close(fd);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手心的海 2024-08-15 10:39:32

1024实际上可能是你的问题。 write() 函数的第三个参数指示要写入的字节数:

ssize_t write(int fildes, const void *buf, size_t nbyte);

请参阅 write() 手册页 了解详细信息。

在您的情况下,该数字应为 5,因为您要发送 5 个字符('C' '、' '7' '\r' 和 '\n')。

通过提供值 1024,您实际上会通过串行通道发送另外 1019 个垃圾字符。

更新:

read() 函数具有几乎相同的参数:

ssize_t read(int fildes, void *buf, size_t nbyte);

请注意,您必须提供一个可写缓冲区作为第二个参数。例如,要读取 12 个字节,您可以使用:

char someData[12];
read(fd, someData, 12);

我不太确定如何确定需要读取的字符数,但是函数返回的 ssize_t 数字会告诉您有多少个字符实际上读过。

The 1024 may in fact be your problem. The third paramter to the write() function indicates the number of bytes to be written:

ssize_t write(int fildes, const void *buf, size_t nbyte);

See the man page for write() for details.

In your case, the number should be 5, since you are sending 5 characters ('C' ',' '7' '\r' and '\n').

By providing a value of 1024, you are actually sending another 1019 garbage characters over the serial channel.

update:

The read() function has almost the same arguments:

ssize_t read(int fildes, void *buf, size_t nbyte);

Note that you must provide a writable buffer as the second parameter. For example, to read 12 bytes you would use:

char someData[12];
read(fd, someData, 12);

I'm not quite sure how to determine the number of characters you need to read, but the ssize_t number returned by the function will tell you how many were actually read.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文