使用POSIX访问半双工串行端口

时间:2022-08-26 19:53:51

I'm asked to read from and write to a half-duplex serial connection using POSIX calls (more specifically, writing in C on Linux 2.6.x). I'm having slight troubles finding detailed information on that particular model (most pages concentrate on full-duplex) and as I am getting slight anomalies when reading, I wanted to check whether maybe I am doing something wrong here.

我被要求使用POSIX调用来读取和写入半双工串行连接(更具体地说,在Linux 2.6.x上用C语言编写)。我有轻微的麻烦找到有关该特定型号的详细信息(大多数页面都集中在全双工),而且当我在阅读时遇到轻微的异常时,我想检查一下我是否在做错了。

With a half-duplex serial connection, I can only read or write. This is not a problem, as there is no unsolicited incoming data on the line - the only time any packages are sent to me (for reading) is when I asked for them beforehand.

使用半双工串行连接,我只能读或写。这不是问题,因为线路上没有未经请求的传入数据 - 任何包裹发送给我(用于阅读)的唯一时间是我事先要求它们。

So what my code does is to write() to the port whenever something needs to be sent. Should this data result in a response (something I know beforehand), I simply read(). There are no special functions I am calling - but maybe I should? And is this approach correct? I.e. write when the line is free?

所以我的代码所做的是在需要发送内容时将write()写入端口。如果这些数据导致响应(事先我知道),我只需阅读()。我没有特别的功能 - 但也许我应该这样做?这种方法是否正确?即当线路空闲时写?

1 个解决方案

#1


I would read the Linux kernel source documentation, there may be a text file about the serial driver; if not, you could read through the actual driver code to see what it does (it's not as scary as it sounds, I promise!)

我会阅读Linux内核源文档,可能有一个关于串口驱动程序的文本文件;如果没有,你可以阅读实际的驱动程序代码,看看它做了什么(它不像听起来那么可怕,我保证!)

#1


I would read the Linux kernel source documentation, there may be a text file about the serial driver; if not, you could read through the actual driver code to see what it does (it's not as scary as it sounds, I promise!)

我会阅读Linux内核源文档,可能有一个关于串口驱动程序的文本文件;如果没有,你可以阅读实际的驱动程序代码,看看它做了什么(它不像听起来那么可怕,我保证!)