Linux C++ Serial Writing:只写一个字符
Linux C++ Serial Writing: Only one character written
我正在尝试修改此处编写的代码:Linux C 串行端口读取/写入,以便我可以通过串行(带有 USB 适配器(连接控制 LED 闪光灯单元。但是,当我尝试将 12 个命令写入设备,然后使用 GTKterm 和示波器检查 LED 状态时,设备似乎只接收第一个项,即它正在接收命令("10000000000"(。我相信端口设置是正确的(尽管我可能完全错误(,并正确附加了以下在GTKterm中运行的命令的图像!http://oi59.tinypic.com/27wrexx.jpg。有谁知道为什么会发生这种情况?非常感谢山 姆
我的代码:
int flasher::allon(){
int USB = open( "/dev/ttyUSB0", O_RDWR| O_NOCTTY );
struct termios tty;
struct termios tty_old;
memset (&tty, 0, sizeof tty);
/* Error Handling */
if ( tcgetattr ( USB, &tty ) != 0 )
{
cout << "Error " << errno << " from tcgetattr: " << strerror(errno) << endl;
}
/* Save old tty parameters */
tty_old = tty;
/* Set Baud Rate */
cfsetospeed (&tty, (speed_t)B9600);
cfsetispeed (&tty, (speed_t)B9600);
/* Setting other Port Stuff */
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] = 1; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
/* Make raw */
cfmakeraw(&tty);
/* Flush Port, then applies attributes */
tcflush( USB, TCIFLUSH );
if ( tcsetattr ( USB, TCSANOW, &tty ) != 0)
{
cout << "Error " << errno << " from tcsetattr" << endl;
}
unsigned char cmd[] = "111111111111 rn";
int n_written = 0;
do {
n_written += write( USB, &cmd[n_written], 1 );
}
while (cmd[n_written-1] != 'r' && n_written > 0);
int n = 0;
char buf = ' ';
/* Whole response*/
std::string response;
do
{
n = read( USB, &buf, 1 );
response.append( &buf );
}
while( buf != 'r' && n > 0);
if (n < 0)
{
cout << "Error reading: " << strerror(errno) << endl;
}
else if (n == 0)
{
cout << "Read nothing!" << endl;
}
else
{
cout << "Response: " << response<<endl;
}
return 0;
}
由于这个原因,你的代码肯定不能工作
string
有一个追加函数,该函数接受 char*,但它需要一个以 null 结尾的字符串。你的 buf 只是一个字符,所以如果 read(( 确实在其中放了一个字符,则无法保证内存中它后面的内容,因此您没有适当的以 null 结尾的字符串,而是未定义的行为。
您可能应该提供一个超过 1 个字符的缓冲区,然后使用一个需要长度的 append
版本,传入 n。
否则替换
response.append( &buf );
跟
response.push_back( buf );
这可能有效,但效率可能低于使用多字符缓冲区。在附加之前,您可能也应该检查read
的结果。就代码而言,如果read
失败,您仍然会追加。
此语句,如果有效,应切换 while 子句的顺序
while (cmd[n_written-1] != 'r' && n_written > 0);
如果n_written不是> 0,则 LHS 是未定义的行为。所以
while ( n_written > 0 && cmd[n_written-1] != 'r');
您确定这是终止循环的正确条件吗?我认为r
是某种"消息结束"角色。
如果write()
返回 -1,则不一定会将n_written
推到 0 或低于 0。
相关文章:
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 我已经建立了递归关系,它找到了两个字符串之间最长的连续公共字符串,我怎么能跳过其中一个字符串中的一个字符
- 使用动态分配将 char* 复制到另一个字符**
- 如何将一个结构的字符数组复制到结构的另一个字符数组中?
- 如何在不知道对应关系的情况下在字符串中搜索字符并将其分配给另一个字符?
- 如何检查一个字符是否与字符数组中的另一个字符匹配?
- C++:使用另一个字符将一个字符大写
- 我可以得到一个字符 * 到一个 std::sregex_iterator 匹配 str() 吗?
- 使用连续分隔符和空最后一个字符进行拆分
- 如何比较文件中包含的下一个字符
- 如何检查字符串中的最后一个字符是否是某个字符并将其从字符串中删除?(C++)
- 如何从文本文件中一次读取一个字符
- 将一个字符替换为字符串中更多数量的字符,而不删除C++中的其他字母
- 读取输入后,输出丢失了一个字符
- 比较最后一个字符,不区分大小写(带谓词?
- 如果一个字符,比如说"9",我减去"0",我在结果上使用 is 数字,我是真还是假
- 为什么 std::istream::gcount 返回的字符比预期的多一个字符
- 使用 fstream 在空格分隔文件中查看下一项(不仅仅是一个字符)的方法
- 使用 char 将一个字符数组分配给另一个字符数组时出现奇怪的行为
- 在弦乐器中删除最后一个字符