在 C++ linux 中将 STRINGS 写入串口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9340388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing STRINGS to serial port in C++ linux
提问by haikalpribadi
I know this question is scattered all over the internet, but still, nothing is getting me completely there yet. I want to write data to a serial port in C++ (linux) for a a Propeller board. Program works fine when taking input from the console, but when I write strings to it always return: ERROR - Invalid command
from the device. I tried creating array of char
with Hex values then it worked. here's a working code, below. But how will i be able to just provide a string variable of command and send it to the serial port? perhaps, how do you I convert it to hex values if it's the only way? Thanks everyone
我知道这个问题散布在整个互联网上,但仍然没有什么能让我完全了解。我想将数据写入 Propeller 板的 C++ (linux) 中的串行端口。从控制台获取输入时程序工作正常,但是当我向它写入字符串时,它总是返回:ERROR - Invalid command
从设备。我尝试char
使用十六进制值创建数组,然后它起作用了。这是一个工作代码,如下。但是我如何能够只提供命令的字符串变量并将其发送到串行端口?也许,如果这是唯一的方法,我如何将其转换为十六进制值?谢谢大家
note: the loop is to use user input from console. What i need is a way to send a string variable to the serial port.
注意:循环是使用来自控制台的用户输入。我需要的是一种将字符串变量发送到串行端口的方法。
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
int main(int argc,char** argv){
struct termios tio;
struct termios stdio;
int tty_fd;
fd_set rdset;
unsigned char c='D';
printf("Please start with %s /dev/ttyS1 (for example)\n",argv[0]);
memset(&stdio,0,sizeof(stdio));
stdio.c_iflag=0;
stdio.c_oflag=0;
stdio.c_cflag=0;
stdio.c_lflag=0;
stdio.c_cc[VMIN]=1;
stdio.c_cc[VTIME]=0;
tcsetattr(STDOUT_FILENO,TCSANOW,&stdio);
tcsetattr(STDOUT_FILENO,TCSAFLUSH,&stdio);
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); // make the reads non-blocking
memset(&tio,0,sizeof(tio));
tio.c_iflag=0;
tio.c_oflag=0;
tio.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information
tio.c_lflag=0;
tio.c_cc[VMIN]=1;
tio.c_cc[VTIME]=5;
tty_fd=open(argv[1], O_RDWR | O_NONBLOCK);
cfsetospeed(&tio,B115200); // 115200 baud
cfsetispeed(&tio,B115200); // 115200 baud
tcsetattr(tty_fd,TCSANOW,&tio);
//char str[] = {'V','E','R','\r'};
//the above str[] doesn't work although it's exactly the same as the following
char str[] = {0x56, 0x45, 0x52, 0x0D};
write(tty_fd,str,strlen(str));
if (read(tty_fd,&c,1)>0)
write(STDOUT_FILENO,&c,1);
while (c!='q')
{
if (read(tty_fd,&c,1)>0) write(STDOUT_FILENO,&c,1); // if new data is available on the serial port, print it out
if (read(STDIN_FILENO,&c,1)>0)
if(c!='q')
write(tty_fd,&c,1); // if new data is available on the console, send it to the serial port
}
close(tty_fd);
}
回答by rve
Are you sure you should end with '\r'? When entering text from console the return key will result in a '\n' character (on Linux) and not '\r'
你确定你应该以'\r'结尾吗?当从控制台输入文本时,返回键将导致一个 '\n' 字符(在 Linux 上)而不是 '\r'
Also error checking is missing on most functions (open()
, fcntl()
, etc.). Maybe one of these functions fail. To find out how to check for errors read the man page (for example man 2 open
for the open()
command. In case of open()
the man page explains it returns -1 when it could not open the file/port.
此外错误检查缺少的大部分功能(open()
,fcntl()
,等)。也许这些功能之一失败了。要了解如何检查错误阅读man页面(例如man 2 open
对于open()
命令,在的情况下,open()
该名男子页解释了它返回-1,当它无法打开文件/端口。
After your edit you wrote:
编辑后你写道:
char str[] = {0x56, 0x45, 0x52, 0x0D};
write(tty_fd,str,strlen(str));
which is wrong. strlen
expects a '\0' terminated string which str is obviously not so now it sends your data and whatever there is in memory until it sees a '\0'. You need to add 0x00
to your str
array.
这是错误的。strlen
期望一个 '\0' 终止的字符串,而 str 显然不是这样,现在它会发送您的数据和内存中的任何内容,直到它看到一个 '\0'。您需要添加0x00
到您的str
数组中。
回答by haikalpribadi
I'm happy to solve my own solution but yet disappointed to not have seen the trivial matter much sooner. char
by default are signed
in c++, which makes it holding the range -128 to 127. However, we are expecting the ASCII values which are 0 to 255. Hence it's as simple as declaring it to be unsigned char str[]
and everything else should work. Silly me, Silly me.
我很高兴解决我自己的解决方案,但对没有早点看到微不足道的事情感到失望。char
默认情况下是signed
在 c++ 中,这使得它保持范围 -128 到 127。但是,我们期望 ASCII 值是 0 到 255。因此它就像声明它一样简单,unsigned char str[]
其他一切都应该工作。傻我,傻我。
Still, Thank you everyone for helping me!!!
不过还是谢谢大家的帮助!!!