如何通过RS232从连接到PC的串行设备读取数据? [英] How to read data from serial device which is connected to PC by RS232 ?

查看:796
本文介绍了如何通过RS232从连接到PC的串行设备读取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<errno.h>
#include<windows.h>
#include<termios.h>
#include<unistd.h>
#include<string.h>
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>




int set_attributes (int fd, int speed, int parity)
{
        struct termios tty;
        if (tcgetattr (fd, &tty) != 0)
        {
                perror("error\n");
                return -1;
        }

        cfsetospeed (&tty, speed);
        cfsetispeed (&tty, speed);

        tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;

        tty.c_iflag &= ~IGNBRK;
        tty.c_lflag = 0;

        tty.c_oflag = 0;
        tty.c_cc[VMIN]  = 0;
        tty.c_cc[VTIME] = 5;

        tty.c_iflag &= ~(IXON | IXOFF | IXANY);

tty.c_cflag |= (CLOCAL | CREAD);

        tty.c_cflag &= ~(PARENB | PARODD);

 tty.c_cflag |= parity;
        tty.c_cflag &= ~CSTOPB;
        tty.c_cflag &= ~CRTSCTS;

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
        {
                perror("error in setting attributes\n");
                return -1;
        }
        return 0;
}


int set_blocking (int fd, int should_block)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &tty) != 0)
        {
                perror("error in setting attributes\n");
                return -1;
        }

        tty.c_cc[VMIN]  = should_block ? 1 : 0;
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
                perror("error in setting attributes\n");
}

int main()
{

    char *portname = "/dev/tty";
    int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
    if (fd < 0)
    {
            perror("error\n");
            return -1;
    }

    set_attributes (fd, B9600, 0);
    set_blocking (fd, 0);

    while (1){
             char data[20];
            char buf[100];
            sleep(10);
            int n = read (fd, buf, sizeof buf);
            printf(" the character read from serial port is = %s",buf);
            printf("\n");
        }
        close(fd);
    }

推荐答案

像Jochen所说 - 你的问题非常稀少。 termios代码看起来合法。



以下是一些建议:



1.使设备名称为a命令行参数,例如。使用argv [1]而不是文字。



2.当读取返回零字节时不要打印任何东西。



3.不要打印可能不是nul终止的字符串(提示:buf不是nul终止的)。



4。期望你的读取返回你必须拼凑起来的片段以获得完整的消息。



5.添加一个control-C(或control-break)信号处理程序。 br />


6.高级:用select替换while循环。
Like Jochen says - your question is very sparse. The termios code looks legit.

Here are some suggestions:

1. make the device name a command line parameter eg. use argv[1] instead of a literal.

2. don't print anything when the read returns zero bytes.

3. don't print a string that might not be nul terminated (hint: "buf" is not nul terminated).

4. expect your reads to return fragments which you must piece together to get a complete message.

5. add a control-C (or control-break) signal handler.

6. advanced: replace the while loop with a select.


这篇关于如何通过RS232从连接到PC的串行设备读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆