简体   繁体   English

串口通讯一对一

[英]Serial Communication one to one

If this is a stupid question, please don't mind me. 如果这是一个愚蠢的问题,请不要介意我。 But I spent some time trying to find the answer but I couldn't get anything solid. 但我花了一些时间试图找到答案,但我无法得到任何可靠的东西。 Maybe this is a hardware question, but I figured I'd try here first. 也许这是一个硬件问题,但我想我先试试这里。

Does Serial Communication only work one to one? 串行通信只能一对一工作吗? The reason this came up is because I had an arduino board listening for communication on its serial port. 之所以出现,是因为我有一个arduino板在其串口上监听通信。 I had a python script feed bytes to the port as well. 我也有一个python脚本提供字节到端口。 However, whenever I opened up the arduino's serial monitor, the connection with the python script failed. 但是,每当我打开arduino的串行监视器时,与python脚本的连接都会失败。 The serial monitor also connects to the serial port for communication for its little text input field. 串行监视器还连接到串行端口,以便为其小文本输入字段进行通信。

So what's the deal? 那是什么交易? Does serial communication only work between a single client and a single server? 串行通信仅在单个客户端和单个服务器之间工作吗? Is there a way to get multiple clients writing to the server? 有没有办法让多个客户端写入服务器? I appreciate your suggestions. 我感谢你的建议。

Well, your question can be quite wide, so I'm going to layer my answer: 那么,你的问题可能非常广泛,所以我将分层答案:

  • On the hardware side, the same pair of wires can work be shared with many devices. 在硬件方面,同一对导线可以与许多设备共享。 It is mostly a question of electronics (maintaining the signal in the good voltage range), and not having all devices writing to the serial port at the same time (or you'll get wreckage). 这主要是电子设备的问题(将信号保持在良好的电压范围内),并且没有所有设备同时写入串行端口(或者你会得到残骸)。

  • On the software side, on the host, yes you can share the same serial connection to a device with multiple processes. 在软件方面,在主机上, 是的,可以与具有多个进程的设备共享相同的串行连接。 But that's not straight forward. 但这不是直截了当的。 I'll assume you're using an unix (macos or linux): 我假设你使用的是unix(macos或linux):

    • in unix, everything is a file, your serial connection is one too: /dev/ttyACM0 on linux, for example. 在unix中,一切都是文件,你的串行连接也是一个:例如,linux上的/dev/ttyACM0
    • When you have a process opening that file, it will block it (using ioctl , iirc) so no other process can mess with that file too. 当你有一个打开该文件的进程时,它会阻止它(使用ioctl ,iirc),所以没有其他进程也可以搞乱该文件。
    • Then, you can input and output to that file using the process that opened it, that's all. 然后,您可以使用打开它的进程输入和输出到该文件,这就是全部。

But hopefully, it is still possible to share the connection between processes. 但希望,仍然可以共享进程之间的连接。 One of them would simply be to use the tee command, that will be able to get input from one process, and give it back output, and copy the output to another process. 其中一个就是使用tee命令,它将能够从一个进程获取输入,并将其返回输出,并将输出复制到另一个进程。 You can also do it from within python, by duplicating the file descriptor. 您也可以通过复制文件描述符在python中完成。

To easily output stuff that can be redirected the unix way (using pipes), you can use socat : http://www.dest-unreach.org/socat/ 要轻松输出可以重定向unix方式的东西(使用管道),你可以使用socathttp//www.dest-unreach.org/socat/

here's an usage example: 这是一个用法示例:

socat -,raw,echo=0,escape=0x0f /dev/ttyACM0,raw,echo=0,crnl

you may want to tweak it for your needs. 你可能想根据自己的需要调整它。

Edit: I forgot about RS-485, which 'jdr5ca' was smart enough to recommend. 编辑:我忘了RS-485,'jdr5ca'很聪明,可以推荐。 My explanation below is restricted to RS-232, the more "garden variety" serial port. 我在下面的解释仅限于RS-232,更多“花园品种”串口。 As 'jdr5ca' points out, RS-485 is a much better alternative for the described problem. 正如'jdr5ca'指出的那样,RS-485是解决上述问题的更好选择。

Original: To expand on zmo's answer a bit, it is possible to share serial at the hardware level, and it has been done before, but it is rarely done in practice. 原文:为了稍微扩展zmo的答案, 可以在硬件级别共享串口,并且之前已经完成,但实际上很少完成。

Likewise, at the software driver level, it is again theoretically possible to share, but you run into similar problems as the hardware level, ie how to "share" the link to prevent collisions, etc. 同样,在软件驱动程序级别,理论上可以再次共享,但是您遇到与硬件级别类似的问题,即如何“共享”链接以防止冲突等。

A "typical" setup would be two serial (hardware) devices attached to each other 1:1. “典型”设置是两个以1:1相互连接的串行(硬件)设备。 Each would run a single software process that would manage sending/receiving data on the link. 每个都运行一个软件过程,管理链接上的发送/接收数据。

If it is desired to share the serial link amongst multiple processes (on either side), the software process that manages the link would also need to manage passing the received data to each reading process (keeping track of which data each process had read) and also arbitrate which sending process gets access to the link during "writes". 如果希望在多个进程之间共享串行链路(在任何一方),管理链路的软件进程还需要管理将接收到的数据传递给每个读取进程(跟踪每个进程读取的数据)和还可以在“写入”期间仲裁哪个发送进程可以访问链接。

If there are multiple read/write processes on each end of the link, the handshaking/coordination of all this gets deep as some sort of meta-signaling arrangement may be needed to coordinate the comms between the process on each end. 如果在链路的每一端有多个读/写过程,则所有这些的握手/协调变深,因为可能需要某种元信令安排来协调每端上的过程之间的通信。

Either a real mess or a fun challenge, depending on your needs and how you view such things. 无论是真正的混乱还是有趣的挑战,取决于您的需求以及您如何看待这些事物。

Multiple clients (eg Arduinos) communicating with one server (eg a desktop computer) is commonly done with the serial variant: 与一个服务器(例如台式计算机)通信的多个客户端(例如Arduinos)通常使用串行变量:

RS-485 RS-485

This is a simple method widely used in industrial settings where you want to have many devices connected to one computer via one serial port. 这是一种在工业环境中广泛使用的简单方法,您希望通过一个串行端口将许多设备连接到一台计算机。 This type of arrangement is also called multi-drop, because one cable strings around a building with Tees that tap in and drop lines to each device. 这种类型的布置也称为多点布置,因为在建筑物周围有一根电缆串,其中有T型接入并将线路丢弃到每个设备。

The hardware for this is widely available. 其硬件广泛可用。 You can buy USB serial adapters that provide the hardware interface for a computer. 您可以购买USB串行适配器,为计算机提供硬件接口。 Programmatically the port looks just like an RS232 port. 以编程方式,端口看起来就像一个RS232端口。 For the Arduino you would just add a transceiver chip. 对于Arduino,您只需添加一个收发器芯片。 A sea of serial transceivers exists, eg 存在大量的串行收发器,例如

Example computer USB adapter with 485 interface Sample RS485 transceiver chip from Element14 具有485接口的示例计算机USB适配器 来自Element14的示例RS485收发器芯片

All the devices hang on the same bus listening at the same time. 所有设备挂在同一总线上同时监听。 A simple communication protocol used is just add a device address before every command. 使用的简单通信协议只是在每个命令之前添加设备地址。 For example: 例如:

  • 001SETLIGHT1 <- tells Arduino "001" to turn on the light 001SETLIGHT1 < - 告诉Arduino“001”打开灯
  • 013SETLIGHT0 <- tells "013" to turn off the light 013SETLIGHT0 < - 告诉“013”关灯

Any device hanging on the cable ignores commands that do not start with their address. 挂在电缆上的任何设备都会忽略不以其地址开头的命令。 When a device responds, it prepends its address. 当设备响应时,它会预先设置其地址。

  • 001SETLIGHT1DONE <- response from device "001" that the command has been received and executed 001SETLIGHT1DONE < - 来自设备“001”的响应,表示已接收并执行该命令

The address in the response lets the receiving party know which device was talking. 响应中的地址使接收方知道哪个设备正在通话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM