Linux环境下串口数据转TCP/IP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/484740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting serial port data to TCP/IP in a Linux environment
提问by
I need to get data from the serial port of a Linux system and convert it to TCP/IP to send to a server. Is this difficult to do? I have some basic programming experience, but not much experience with Linux. Is there an open source application that do this?
我需要从 Linux 系统的串行端口获取数据并将其转换为 TCP/IP 以发送到服务器。这很难做到吗?我有一些基本的编程经验,但对 Linux 的经验不多。是否有一个开源应用程序可以做到这一点?
回答by Bash
You might find Perlor Pythonuseful to get data from the serial port. To send data to the server, the solution could be easy if the server is (let's say) an HTTP application or even a popular database. The solution would be not so easy if it is some custom/proprietary TCP application.
您可能会发现Perl或Python对从串行端口获取数据很有用。要将数据发送到服务器,如果服务器是(比方说)HTTP 应用程序甚至流行的数据库,则解决方案可能很简单。如果它是一些自定义/专有的 TCP 应用程序,解决方案就不会那么容易了。
回答by Judge Maygarden
You don't need to write a program to do this in Linux. Just pipethe serial port through netcat:
您无需编写程序即可在 Linux 中执行此操作。只需通过netcat管道串行端口:
netcat www.example.com port </dev/ttyS0 >/dev/ttyS0
Just replace the address and port information. Also, you may be using a different serial port (i.e. change the /dev/ttyS0
part). You can use the sttyor setserialcommands to change the parameters of the serial port (baud rate, parity, stop bits, etc.).
只需替换地址和端口信息。此外,您可能正在使用不同的串行端口(即更改/dev/ttyS0
部件)。您可以使用stty或setserial命令来更改串口的参数(波特率、奇偶校验、停止位等)。
回答by Shannon Nelson
I think your question isn't quite clear. There are several answers here on how to catch the data coming into a Linux's serial port, but perhaps your problem is the other way around?
我觉得你的问题不是很清楚。这里有几个关于如何捕获进入 Linux 串行端口的数据的答案,但也许您的问题是相反的?
If you need to catch the data coming out of a Linux's serial port and send it to a server, there are several little hardware gizmos that can do this, starting with the simple serial print server such as this Lantronix gizmo.
如果您需要捕获来自 Linux 串行端口的数据并将其发送到服务器,有几个小的硬件小工具可以做到这一点,从简单的串行打印服务器开始,例如这个Lantronix 小工具。
No, I'm not affiliated with Lantronix in any way.
不,我与 Lantronix 没有任何关联。
回答by Andrew Y
I stumbled upon this question via a Google search for a very similar one (using the serial port on a server from a Linux client over TCP/IP), so, even though this is not an answer to exact original question, some of the code might be useful to the original poster, I think:
我通过 Google 搜索了一个非常相似的问题(通过 TCP/IP 使用来自 Linux 客户端的服务器上的串行端口)偶然发现了这个问题,因此,即使这不是确切原始问题的答案,一些代码可能对原始海报有用,我认为:
回答by Garsia
I had the same problem.
我有同样的问题。
I'm not quite sure about open source applications, but I have tested command line Serial over Ethernet for Linuxand... it works for me.
我不太确定开源应用程序,但我已经测试了 Linux 的命令行串行以太网,并且......它对我有用。
Also thanks to Judge Maygarden for the instructions.
还要感谢 Maygarden 法官的指导。
回答by Indie
回答by Tereus Scott
I have been struggling with the problem for a few days now.
几天来我一直在努力解决这个问题。
The problem for me originated with VirtualBox/Ubuntu. I have lots of USB serial ports on my machine. When I tried to assign one of them to the VM it clobbered all of them - i.e. the host and other VMs were no longer able to use their USB serial devices.
我的问题起源于VirtualBox/Ubuntu。我的机器上有很多 USB 串口。当我尝试将其中一个分配给 VM 时,它破坏了所有这些——即主机和其他 VM 不再能够使用它们的 USB 串行设备。
My solution is to set up a stand-alone serial server on a netbook I happen to have in the closet.
我的解决方案是在我碰巧在壁橱里的上网本上设置一个独立的串行服务器。
I tried ser2net and it worked to put the serial port on the wire, but remtty did not work. I need to get the port as a tty on the VM.
我尝试了 ser2net,它可以将串行端口连接到线路上,但是 remtty 不起作用。我需要将端口作为 VM 上的 tty。
socat worked perfectly.
socat 工作得很好。
There are good instructions here:
这里有很好的说明:
回答by user3247282
Open a port in your server with netcat and start listening:
使用 netcat 在您的服务器中打开一个端口并开始侦听:
nc -lvp port number
And on the machine you are reading the serial port, send it with netcat as root:
在您正在读取串行端口的机器上,以 root 身份使用 netcat 发送它:
nc <IP address> portnumber < /dev/ttyACM0
If you want to store the data on the server you can redirect the data to a text file.
如果要将数据存储在服务器上,可以将数据重定向到文本文件。
First create a file where you are saving the data:
首先创建一个文件来保存数据:
touch data.txt
And then start saving data
然后开始保存数据
nc -lvp port number > data.txt
回答by Dwight Spencer
All the tools you would need are already available to you on most modern distributions of Linux.
您需要的所有工具都已在大多数现代 Linux 发行版上提供。
As several have pointed out you can pipe the serial data through netcat. However you would need to relaunch a new instance each time there is a connection. In order to have this persist between connections you can create a xinetd service using the following configuration:
正如一些人指出的那样,您可以通过 netcat 管道传输串行数据。但是,每次有连接时,您都需要重新启动一个新实例。为了在连接之间保持这种状态,您可以使用以下配置创建 xinetd 服务:
service testservice
{
port = 5900
socket_type = stream
protocol = tcp
wait = yes
user = root
server = /usr/bin/netcat
server_args = "-l 5900 < /dev/ttyS0"
}
Be sure to change the /dev/ttyS0
to match the serial device you are attempting to interface with.
请务必更改/dev/ttyS0
以匹配您尝试与之连接的串行设备。