从一个串口读取原始字节 [英] Reading raw bytes from a serial port

查看:525
本文介绍了从一个串口读取原始字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从通过IEC 870-5-101的win32协议模拟器在Linux上运行32位C语言编写一个程序发送一个串口读取原始字节。

I'm trying to read raw bytes from a serial port sent by a IEC 870-5-101 win32 protocol simulator with a program written in C running on Linux 32bit.

它的工作罚款字节值一样为0x00 - 0x7F的。但是,对于从开始0x80到0xAF执行值高位是错误的,例如:

It's working fine for byte values like 0x00 - 0x7F. But for values beginning from 0x80 to 0xAF the high bit is wrong, e.g.:

0x7F -> 0x7F //correct
0x18 -> 0x18 //correct
0x79 -> 0x79 //correct
0x80 -> 0x00 //wrong
0xAF -> 0x2F //wrong
0xFF -> 0x7F //wrong

现在周围挖了两天后,我不知道,是什么导致了这一点。

After digging around for two days now, I have no idea, what's causing this.

这是我的串行端口的配置:

This is my config of the serial port:

    cfsetispeed(&config, B9600);
    cfsetospeed(&config, B9600);

    config.c_cflag |= (CLOCAL | CREAD);

    config.c_cflag &= ~CSIZE;                               /* Mask the character size bits */
    config.c_cflag |= (PARENB | CS8);                       /* Parity bit Select 8 data bits */

    config.c_cflag &= ~(PARODD | CSTOPB);                   /* even parity, 1 stop bit */


    config.c_cflag |= CRTSCTS;                              /*enable RTS/CTS flow control - linux only supports rts/cts*/


    config.c_iflag &= ~(IXON | IXOFF | IXANY);              /*disable software flow control*/ 

    config.c_oflag &= ~OPOST;                               /* enable raw output */
    config.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);      /* enable raw input */

    config.c_iflag &= ~(INPCK | PARMRK);                    /* DANGEROUS no parity check*/
    config.c_iflag |= ISTRIP;                               /* strip parity bits */
    config.c_iflag |= IGNPAR;                               /* DANGEROUS ignore parity errors*/

    config.c_cc[VTIME] = 1;                                 /*timeout to read a character in tenth of a second*/

我从串口读取与

*bytesread = read((int) fd, in_buf, BytesToRead);

右键后,此操作in_buf包含错误的字节,所以我想有什么毛病我的配置,这是一个Win32 DCB结构的端口。

Right after this operation "in_buf" contains the wrong byte, so I guess there's something wrong with my config, which is a port from a win32 DCB structure.

感谢您的任何想法!

推荐答案

根据你的例子中,只有8位(最高位)是错误的,它由是始终为0。你在你的线路规程设置ISTRIP是错的在Linux中,这将导致此。 ISTRIP并不像在C code索赔的评论,剥离校验位。它除去第八位数据。

Based on your examples, only the 8th bit (the high bit) is wrong, and it's wrong by being always 0. You are setting ISTRIP in your line discipline on the Linux side, and that would cause this. ISTRIP does not, as the comment in the C code claims, strip parity bits. It strips the 8th data bit.

如果设置了ISTRIP,有效输入字节应先剥去七位;否则,所有8位应该被处理。 IEEE标准1003.1 2004年版,第11章,通用终端接口

If ISTRIP is set, valid input bytes shall first be stripped to seven bits; otherwise, all eight bits shall be processed. IEEE Std 1003.1, 2004 Edition, chapter 11, General Terminal Interface

这篇关于从一个串口读取原始字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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