gpt4 book ai didi

c++ - 如何在 C++ 中为 Raspberry PI USB UART 连接设置 termios 参数?

转载 作者:行者123 更新时间:2023-12-02 10:04:45 25 4
gpt4 key购买 nike

我有一个 RPI 3 和一个 LoRa USB 模块,我正在用 C++ 编写一些代码来连接设备的端口。我能够连接到端口(在 udev 规则中分配为 ttyUSBPort1)。但是,当我向端口发送数据时,出现错误。我只是对 termios 和端口通信知之甚少,无法确定这是否是问题所在(是的,我已阅读手册页)。

LoRa 模块是一个 RN2903 设备,以下是引用表上的 UART 接口(interface)说明:

All of the RN2903 module’s settings and commands are transmitted over UART using the ASCII interface. All commands need to be terminated with < CR >< LF > (spaces added for formatting) and any replies they generate will also be terminated by the same sequence. The default settings for the UART interface are 57600 bps, 8 bits, no parity, 1 Stop bit, no flow control.



发送命令时,我可以看到设备通过监视端口以“invalid_parameter”响应
sudo cat /dev/ttyUSBPort1
我假设我的某些 termios 标志设置不正确,或者写入命令设置不正确。这是我设置端口的代码:
int openPort(void) {
struct termios tty;
memset(&tty, 0, sizeof tty);
if ((usb_port = open(device, O_RDWR))>=0) {// | O_NOCTTY | O_SYNC
std::cout << "DEVICE OPENED: " << device << " handle number: " << usb_port << std::endl;
} else {
fprintf(stderr, "unable to open serial device");
return -1;
}
if(tcgetattr(usb_port, &tty) != 0) {
printf("Error %i \n", errno);
}
cfsetispeed(&tty, B57600);
cfsetospeed(&tty, B57600);

tty.c_cflag &= ~PARENB; // Make 8n1
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;

tty.c_cflag &= ~CRTSCTS; // no flow control
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout

tcflush( usb_port, TCIFLUSH );
if (tcsetattr(usb_port, TCSANOW, &tty) != 0) {
printf("Error %i\n", errno);
}
return usb_port;
}

这是从设备获取版本信息的调用命令:
void radioCMD(string tmp) {
string tmp2 = tmp + "\r\n";
int n = tmp2.length();
char cmd[n];
strcpy(cmd, tmp2.c_str());
std::cout << write(usb_port, cmd, sizeof(cmd)) << " " << cmd << "Writing to " << usb_port << " Delay: " << delay << " Command Size: " << sizeof(cmd) << std::endl;
}
void setupRadio() {
radioCMD("sys get ver");
usleep(delay);
}

写入控制台 std::cout 时,我看到:
13 sys get ver
Writing to 3 Delay: 200000 Command Size: 13

表明消息确实被正确写入。

设备的 cat 输出应以如下方式响应(来自数据表):

2.3.6.1 sys get ver Response: RN2903 X.Y.Z MMM DD YYYY HH:MM:SS, where X.Y.Z is the firmware version, MMM is month, DD is day, HH:MM:SS is hour, minutes, seconds (format: [HW] [FW] [Date] [Time]). [Date] and [Time] refer to the release of the firmware. This command returns the information related to the hardware platform, firmware version, release date and time-stamp on firmware creation. Example: sys get ver



我实际得到的是“invalid_param\r\n”,如果调用中的某些内容不正确,这是设备的适当响应。

有什么想法我可能会在这里出错吗?

编辑

感谢 Ted 为我指明了正确的方向并简化了我的代码。缺少两个 termios 标志。一旦我设置了这些(最后两个),它就可以正常工作了。
tty.c_cflag     &=  ~PARENB;            // Make 8n1
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~CRTSCTS; // no flow control
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
***ADDITIONAL TWO FLAGS THAT FIXED IT****
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
tty.c_oflag &= ~OCRNL; // Prevent conversion of newline to carriage return/line feed

新的写调用函数:
void radioCMD(string cmd) {
cmd += "\r\n";
write(usb_port, cmd.c_str(), cmd.size());
}

最佳答案

这将创建一个 VLA那是一个char太短而无法适应 C 字符串的空终止符:

void radioCMD(string tmp) {
string tmp2 = tmp + "\r\n";
int n = tmp2.length();
char cmd[n]; // should be n+1 for strcpy to work
strcpy(cmd, tmp2.c_str()); // undefined behavior \0 is written out of bounds
write(usb_port, cmd, sizeof(cmd)); // sizeof(cmd) should be replaced by n
}

更好的选择是使用 std::memcpy而不是 std::strcpy复制没有空终止符的 C 字符串 - 并避免 VLA :s。

更好的选择是使用 std::string你直接作为参数得到:
void radioCMD(string cmd) {
cmd += "\r\n";
write(usb_port, cmd.c_str(), cmd.size());
}

这可能不是唯一的问题,但作为 std::strcpy当前使您的程序具有未定义的行为,这是一个很好的起点。

关于c++ - 如何在 C++ 中为 Raspberry PI USB UART 连接设置 termios 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60832967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com