简体   繁体   English

同一台计算机上两个应用程序之间的串行通信(RS232)(串行回送)

[英]Serial communication (RS232) between two applications on the same computer (serial loop back)

I have an application (App1) which just gives input data from serial port. 我有一个应用程序(应用),它刚刚从串口给出输入数据。 I don't have any external device which sends input data through serial port to the application, so I want to make a java application (App2) which can communicating through serial port with first app (App1) for sending input data to App1. 我没有任何外部设备通过串行端口将输入数据发送到应用程序,因此我想制作一个Java应用程序(App2),该应用程序可以通过串行端口与第一个应用程序(App1)通信,以将输入数据发送到App1。 But I don't know how can communicating with serial ports between two applications. 但是我不知道如何与两个应用程序之间的串行端口通信。 It's like local network communication with socket programming on 127.0.0.1 IP (Loop back). 就像使用127.0.0.1 IP(回送)上的套接字编程进行本地网络通信一样。 I know there are libraries like RXTX and jSSC but I could not find anything like loop back with serial port in their documentations. 我知道有像RXTX和jSSC这样的库,但是在它们的文档中找不到像使用串行端口环回这样的库。 Can you give me a solution? 你能给我解决办法吗? Thank you. 谢谢。

Simple diagram: |App2| 简单图:| App2 | =======(serial communication)======> |App1| =======(串行通信)======> | App1 |

No JAVA content in this answer, just years of bitter experience... 这个答案没有JAVA内容,只有多年的痛苦经验...

There's a bit more to serial communications than meets the eye if you're used to sockets. 如果您习惯使用套接字,那么串行通信还有很多其他功能。

Null Modem Cable 空调制解调器电缆

You'll need two serial ports on you computer, one for each application. 您的计算机上需要两个串行端口,每个应用程序一个。 You will need a null modem cable to join them together. 您将需要使用零调制解调器电缆将它们连接在一起。 There is no internal loopback for serial ports, you have to go via a cable. 串行端口没有内部环回,必须通过电缆连接。

Baud, Parity, etc 波特率,奇偶校验等

Then you have to pick a baud rate, number of stop bits, and what parity to use. 然后,您必须选择波特率,停止位的数量以及要使用的奇偶校验。 You even have to say how big a word is - 5, 7 or 8 bits (5 is very historic, 7 less so, 8 is the norm these days). 您甚至不得不说一个单词有多大-5、7或8位(5是非常有历史意义的,少了7,8是最近的标准)。 These have to match either end otherwise the applications won't be able to communicate. 这些必须匹配任何一端,否则应用程序将无法通信。 Each application has to set up their serial port. 每个应用程序都必须设置其串行端口。

Handshaking 握手

You then have to decide what handshaking mechanism to use - XON/XOFF, hardware or none. 然后,您必须决定使用哪种握手机制-XON / XOFF,硬件或不使用。 XON/XOFF is no good if you're sending binary data, but needs only RX/TX/GND wires. 如果要发送二进制数据,则XON / XOFF不好,但仅需要RX / TX / GND线。 Hardware is better if correctly wired and correctly set up in the applications. 如果在应用程序中正确接线并正确设置,则硬件会更好。 A lot of crummy cheap USB serial port adapters don't do hardware flow control properly. 许多廉价的廉价USB串行端口适配器无法正确执行硬件流控制。 No handshaking at all is easiest to get going, also needing only RX/TX/GND, but is hardest to live with in software; 根本不容易握手,也只需要RX / TX / GND,但是最难在软件中使用。 if one application isn't reading whilst the other is writing, the data gets lost. 如果一个应用程序不读取而另一应用程序正在写入,则数据会丢失。

Synchronisation 同步化

Then you have the synchronisation problem. 然后您有同步问题。 You have to write your applications so that they can synchronise with each other. 您必须编写应用程序,以便它们可以彼此同步。 If you have chosen no handshaking, there's nothing to stop app2 sending before app1 is ready to listen. 如果您没有选择握手,那么在app1准备收听之前,没有什么可以阻止app2发送。 Sure, you can start app1 before app2, but that's kinda unsatisfactory, and doesn't work if you have two way communications (data going in both directions). 当然,您可以在app2之前先启动app1,但这还是不能令人满意的,并且如果您进行双向通信(数据双向传输),则无法正常工作。 You can't start both apps first. 您不能先启动两个应用程序。

Start up 启动

Then there's the problem that even if you are using hardware handshaking in your applications, there's no guarantee that the serial ports are set up by the OS that way before you run them. 然后就是一个问题,即使您在应用程序中使用硬件握手,也无法保证在运行串行端口之前操作系统已通过这种方式设置了串行端口。 For example, you may start app2 first, and the serial port for app1 may be saying "clear to send" because the OS has app1's serial port set up that way before you've had a chance to run app1. 例如,您可能首先启动app2,而app1的串行端口可能会说“清除发送”,因为在您有机会运行app1之前,操作系统已经以这种方式设置了app1的串行端口。 App2 thinks that it can send and does so, before app1 starts up, configures the port, and only then does app2 realise it's send data too early, and data has been lost. App2认为它可以发送消息,并且可以这样做,直到app1启动,配置端口,然后App2才意识到它发送数据的时间太早,并且数据已经丢失。

In short, you need some synchronisation protocol between apps involved in two way communications so that they can be sure that the app the other end is really there and really listening. 简而言之,您需要在涉及双向通信的应用程序之间使用一些同步协议,以便他们可以确保另一端的应用程序确实存在并且正在监听。

This is a lot harder than using a socket. 这比使用套接字要难得多。 With a TCP socket, the stacks ensure that data only flows if there's something the other end ready to read it. 使用TCP套接字,栈可确保仅当另一端准备读取数据时,数据才流动。

You could have a "start now" button on the applications' user interfaces. 可能在应用程序的用户界面上有一个“立即开始”按钮。 The user can then tell each application that the other is running and that the cable is plugged in. 然后,用户可以告诉每个应用程序另一个正在运行,并且电缆已插入。

Conclusions 结论

Serial ports are much more annoying to use than a socket! 串行端口比套接字更令人讨厌!

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

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