Linux下C语言如何向串口写一个带回车换行的字符串?

牧牛童子 2014-12-01 12:37:11
说明:
串口已经读写方式打开,并且可以正常读取数据;这个过程没问题。
用write(fd,str_cmd_buf, strlen(str_cmd_buf));返回值0,表示正确;

但是就是串口那边没反应。(就像 命令没生效一样)。
尝试过strcpy(str_cmd_buf,"\r\n") 也尝试过在串尾追加str_cmd_buf[?]=0x0d和0x0a。也不行。

若用 echo "str_cmd_buf" >> 串口 ;立即成功,串口有回应;
又用system调用echo命令,也能成功,有回应。

就是write不行。何故?

...全文
696 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
牧牛童子 2014-12-01
  • 打赏
  • 举报
回复
补充一下: 是想写入一个 带回车换行符的命令行(字符串)
mymtom 2014-12-01
  • 打赏
  • 举报
回复
应该加"\r\n",先回车后换行
牧牛童子 2014-12-01
  • 打赏
  • 举报
回复
谢谢,返回值是 字符串的长度。返回不是0. 是我的疏忽。(笔误,本是表达反回是正常的) 我的字符串strx[ ]有11有效字符,我在字符串数组后 strx[11]='\n' ; strx[12]='\r' ; 最后返回值是13. 是正确的。 但就是命令下去后,没见生效。 会不会是 我在后面加的 \n \r 不正确 ?
梦想照旧实现 2014-12-01
  • 打赏
  • 举报
回复
楼上正解, write(fd,str_cmd_buf, strlen(str_cmd_buf));返回值0,表示正确; --- 返回0表示没写进去
mymtom 2014-12-01
  • 打赏
  • 举报
回复
用write(fd,str_cmd_buf, strlen(str_cmd_buf));返回值0,表示正确; ==== 返回值不正确啊, WRITE(2) Linux Programmer’s Manual WRITE(2) NAME write - write to a file descriptor SYNOPSIS #include <unistd.h> ssize_t write(int fd, const void *buf, size_t count); DESCRIPTION write() writes up to count bytes from the buffer pointed buf to the file referred to by the file descriptor fd. The number of bytes written may be less than count if, for example, there is insufficient space on the underly- ing physical medium, or the RLIMIT_FSIZE resource limit is encountered (see setrlimit(2)), or the call was interrupted by a signal handler after having written less than count bytes. (See also pipe(7).) For a seekable file (i.e., one to which lseek(2) may be applied, for example, a regular file) writing takes place at the current file offset, and the file offset is incremented by the number of bytes actually written. If the file was open(2)ed with O_APPEND, the file offset is first set to the end of the file before writing. The adjustment of the file offset and the write operation are performed as an atomic step. POSIX requires that a read(2) which can be proved to occur after a write() has returned returns the new data. Note that not all file systems are POSIX conforming. RETURN VALUE On success, the number of bytes written is returned (zero indicates nothing was written). On error, -1 is returned, and errno is set appropriately. If count is zero and fd refers to a regular file, then write() may return a failure status if one of the errors below is detected. If no errors are detected, 0 will be returned without causing any other effect. If count is zero and fd refers to a file other than a regular file, the results are not specified.
赵4老师 2014-12-01
  • 打赏
  • 举报
回复
void HexDump(char *buf,int len,int addr) {
    int i,j,k;
    char binstr[80];

    for (i=0;i<len;i++) {
        if (0==(i%16)) {
            sprintf(binstr,"%08x -",i+addr);
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        } else if (15==(i%16)) {
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
            sprintf(binstr,"%s  ",binstr);
            for (j=i-15;j<=i;j++) {
                sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
            }
            printf("%s\n",binstr);
        } else {
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        }
    }
    if (0!=(i%16)) {
        k=16-(i%16);
        for (j=0;j<k;j++) {
            sprintf(binstr,"%s   ",binstr);
        }
        sprintf(binstr,"%s  ",binstr);
        k=16-k;
        for (j=i-k;j<i;j++) {
            sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
        }
        printf("%s\n",binstr);
    }
}
牧牛童子 2014-12-01
  • 打赏
  • 举报
回复
char file_str[256]={0}; strcpy(file_str,"./kao"); char str_x[32]={0}; strcpy(str_x,"$CFGSYS,h10"); strcat(str_x,"\r\n"); int fd=OpenDev(file_str); int y=write(fd,str_x,strlen(str_x)); cout<<"writeString: <<str_x << strLen: "<<y<<endl; --------------------------------- 运行后,返回长度是13. 说明 \r\n 算为2个字符已经加到字符串里了。 但是 我 cat kao时,除了那个字符串,还换行 并且显示 \n 。这个 \n不应该在文件中以字符显示出来的啊?可能问题就在这里。
牧牛童子 2014-12-01
  • 打赏
  • 举报
回复
引用 9 楼 luciferisnotsatan 的回复:
你确定串口那边的程序,看到你发了“\r\n”,会把你发的一行字符串当命令来运行?
我将这个字符串,写到文件里了,能看到 回车换行的效果。 但在串口里,我没法确认啊。 若用 “\\r\\n” 会不会当成4个字符?处理?
luciferisnotsatan 2014-12-01
  • 打赏
  • 举报
回复
你确定串口那边的程序,看到你发了“\r\n”,会把你发的一行字符串当命令来运行?
luciferisnotsatan 2014-12-01
  • 打赏
  • 举报
回复
window "\r\n" linux "\n" mac "\r" 这个是听说的,没自己试过
牧牛童子 2014-12-01
  • 打赏
  • 举报
回复
引用 5 楼 mymtom 的回复:
应该加"\r\n",先回车后换行
是这么搞的,不行。我这样搞的strcat(str_cmd_buf,"\r\n"); 是不是应该写成这样?strcat(str_cmd_buf,"\\r\\n") ; 电脑在家里,忘了昨晚写了1个斜杠还是2个斜杠。

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧