您的位置:首页 > 编程语言

ubuntu下串口编程备忘

2017-02-15 10:22 531 查看
弄了一下串口,一个小问题多折腾了下,备忘。
软件环境:
zl@zhanglong:~$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.4 LTS"zl@zhanglong:~$uname -a
Linux zhanglong 3.2.0-58-generic #88-Ubuntu SMP Tue Dec 3 17:37:58 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
硬件环境:
    另有一台win7,有串口及读写串口的工具软件。两机通过交叉串口线相连

代码如下:
点击(此处)折叠或打开#include <stdio.h>

#include <string.h>

#include <unistd.h>

#include <fcntl.h> 

#include <errno.h>

#include <termios.h>

/**

 * 写/读数据

 **/

int main(int argc, char* argv[])

{

    int i;

    int fd; /* File descriptor for the port */

    int iRet;

    char buf[1024];

    fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);

    if (fd < 0) { /** error **/

        printf("[%s-%d] open error!!!\n", __FILE__, __LINE__);

        goto err1;

    } 

    //fcntl(fd, F_SETFL, FNDELAY);

    fcntl(fd, F_SETFL, 0);

    iRet = write(fd, "a1b2c3\r", 7);

    if (iRet < 0) {

        printf("[%s-%d] write error!!!\n", __FILE__, __LINE__);

    }

    iRet = read(fd, buf, 1024);

    for(i = 0; i < iRet; i++) {

        if((i & 0xf) == 0) {

            printf("\n");

        }

        printf("0x%02x ", buf[i]);

        fflush( fflush(stdout));

    }

    printf("\n");

    close(fd);

err1:

    return 0;

}
编译运行此代码后发现:
      win7能够收到ubuntu发出的数据,但win7发出的数据,自己意外收到了,而ubuntu却收不到。或有时能收到数据(如win7以十六进制发4567890a时,ubuntu能收到数据0x45 0x67 0xffffff89 0x0a)。
  将char buf[1024]数组改成unsigned char buf[1024]后,0xffffff89变成了0x89。
  win7机器上单独发以十六进制,先发"7890a",ubuntu无打印输出,再发"457890a",ubuntu无打印输出,然后发"4567890a"时,ubuntu上的程序打印收到的数据,并且前面几次发出的都收到了。
  网上发现一说法,如果不是终端,使用Raw Mode方式来通讯。偿试将串口设置成Raw Mode,再读数据。

    修改后的代码如下:

点击(此处)折叠或打开#include <stdio.h>

#include <string.h>

#include <unistd.h>

#include <fcntl.h> 

#include <errno.h>

#include <termios.h>

/**

 * 读数据

 **/

int main(int argc, char* argv[])

{

    int i;

    int fd; /* File descriptor for the port */

    int iRet;

    struct termios options_old, options;

    unsigned char buf[1024];

    fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);

    if (fd < 0) { /** error **/

        printf("[%s-%d] open error!!!\n", __FILE__, __LINE__);

        goto err1;

    } 

    //fcntl(fd, F_SETFL, FNDELAY);

    fcntl(fd, F_SETFL, 0);

    /*********************************************************/

    /** * Get the current options for the port... **/

    tcgetattr(fd, &options);

    options_old = options;

    /*** Set the baud rates to 9600... **/

// cfsetispeed(&options, B9600);

// cfsetospeed(&options, B9600);

    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/

    //options.c_oflag |= OPOST; /** 加工过的输出 **/

    options.c_oflag &= ~OPOST; /** 选择原始输出 **/

    /*** Enable the receiver and set local mode... **/

// options.c_cflag |= (CLOCAL | CREAD);

    /*** Set the new options for the port... **/

    tcsetattr(fd, TCSANOW, &options);

    /*********************************************************/

    iRet = write(fd, "a1b2c3\r", 7);

    if (iRet < 0) {

        printf("[%s-%d] write error!!!\n", __FILE__, __LINE__);

    }

    iRet = read(fd, buf, 1024);

    for(i = 0; i < iRet; i++) {

        if((i & 0xf) == 0) {

            printf("\n");

        }

        printf("0x%02x ", buf[i]);

        fflush(stdout);

    }

    printf("\n");

    tcsetattr(fd, TCSANOW, &options_old);

    close(fd);

err1:

    return 0;

}
简单测试,发现win7每次发出数据,ubuntu一端都可以收到。并且win7一端没有再收到自己发出的数据。
     但win7一次发出很长一段数据(如456789abcdef)时,ubuntu只收到前面的数据(0x34 0x35 0x36 0x37 0x38 0x39 0x61 0x62)。估计是串口速度慢,read()系统调用等不了那么长时间,接收到一部分数据后就返回了。
               测试循环执行read()系统调用,并打印收到的数据,确实可以收到更多数据。

至此,问题已经清晰:ubuntu下的/dev/ttyS0设备打开时,默认设置了终端相关的特性,会根据收到不同的数据做出不同的反应。如果用作纯粹的数据传输通道,需要进行设置,去除终端相关特性设定。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: