从一个串口读取原始字节

编程入门 行业动态 更新时间:2024-10-26 00:21:03
本文介绍了从一个串口读取原始字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从通过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

更多推荐

从一个串口读取原始字节

本文发布于:2023-10-24 09:27:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523578.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:串口   字节   原始

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!