Linux环境下串口数据转换为TCP/IP

发布于 2024-07-13 05:45:36 字数 101 浏览 4 评论 0原文

我需要从Linux系统的串口获取数据并将其转换为TCP/IP发送到服务器。 这很难做到吗? 我有一些基本的编程经验,但对 Linux 的经验不多。 有没有开源应用程序可以做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

深海夜未眠 2024-07-20 05:45:36

在 Linux 中您不需要编写程序来执行此操作。 只需通过 netcat

netcat www.example.com port </dev/ttyS0 >/dev/ttyS0

只需替换地址和端口信息即可。 另外,您可能使用不同的串行端口(即更改 /dev/ttyS0 部分)。 您可以使用 sttysetserial 命令用于更改串行端口的参数(波特率、奇偶校验、停止位等)。

You don't need to write a program to do this in Linux. Just pipe the serial port through 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 stty or setserial commands to change the parameters of the serial port (baud rate, parity, stop bits, etc.).

梦里寻她 2024-07-20 05:45:36

我通过 Google 搜索一个非常相似的问题(通过 TCP/IP 从 Linux 客户端使用服务器上的串行端口)偶然发现了这个问题,因此,尽管这不是确切原始问题的答案,但其中一些代码可能对原始海报有用,我认为:

  • Making a Linux box with a serial port Listen on the TCP port to share the modem: ser2net
  • 从另一个 Linux 工作站使用此“共享”调制解调器:remtty

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:

  • Making a Linux box with a serial port listen on the TCP port to share the modem: ser2net
  • Using this "shared" modem from another Linux workstation: remtty
み零 2024-07-20 05:45:36

您可以使用 socat 创建 LAN 串行 (SOL) 连接。 它可用于将 ttyS“转发”到另一台计算机,使其显示为本地计算机,或者您可以通过 TCP/IP 端口访问它。

You can create a serial-over-LAN (SOL) connection by using socat. It can be used to 'forward' a ttyS to another machine so it appears as a local one or you can access it via a TCP/IP port.

〃安静 2024-07-20 05:45:36

大多数现代 Linux 发行版上都已经提供了您需要的所有工具。

正如一些人指出的那样,您可以通过 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"
}

请务必更改 /dev/ttyS0 以匹配您尝试连接的串行设备。

All the tools you would need are already available to you on most modern distributions of 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:

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.

我的黑色迷你裙 2024-07-20 05:45:36

TCP 到串行 Systemd 服务

当您的 Linux 计算机运行 systemd(大多数情况下)时,您可以创建一个简洁的服务来通过 TCP(telnet)提供(USB)串行设备。

安全警告:通过 TCP 公开串行设备存在安全风险。

在此示例中,我使用:

  • TCP 端口 5900
  • 串行设备 /dev/ttyUSB0 (使用 2 次)
  • 串行速度 115200 Bps

您可以在下面的示例中编辑这些。 查看 sttync (netcat) 了解更多选项。

以下所有命令均假设您是用户 root。 如果您没有执行: sudo su -

目录 /etc/systemd/system 应该已经存在。 如果没有,您的系统可能没有运行 Systemd。

创建文件 /etc/systemd/system/tcp2serial.service ,其内容为:

[Unit]
Description=TCP to Serial

[Service]
TTYPath=/dev/ttyUSB0
ExecStartPre=/usr/bin/stty -F /dev/ttyUSB0 speed 115200
ExecStart=/usr/bin/nc -k -l 5900
StandardInput=tty
StandardOutput=tty
Restart=always

[Install]
WantedBy=default.target

创建文件后,您可以执行 systemctl start tcp2serial 来启动服务。

从同一网络中的另一台 Linux 计算机,您可以使用 telnet [server] 5900 连接到它。 要退出 telnet,请按 Ctrl+] 并输入 quit Enter

编辑服务文件后,执行这两个命令命令:

systemctl daemon-reload
systemctl restart tcp2serial

要使服务在引导时启动,请执行:

systemctl enable tcp2serial

TCP to Serial Systemd service

When your Linux machine runs systemd (most do), you can create a neat service to make an (USB) serial device available over TCP (telnet).

SECURITY WARNING: Exposing a serial device over TCP is a security risk.

In this example I am using:

  • TCP Port 5900
  • Serial device /dev/ttyUSB0 (used 2 times)
  • Serial speed 115200 Bps

You can edit these in the example below. Check the manual of stty and nc (netcat) for more options.

All commands below assume you are user root. If you aren't execute: sudo su -

The directory /etc/systemd/system should already exist. If it doesn't your system probably is not running Systemd.

Create the file /etc/systemd/system/tcp2serial.service with contents:

[Unit]
Description=TCP to Serial

[Service]
TTYPath=/dev/ttyUSB0
ExecStartPre=/usr/bin/stty -F /dev/ttyUSB0 speed 115200
ExecStart=/usr/bin/nc -k -l 5900
StandardInput=tty
StandardOutput=tty
Restart=always

[Install]
WantedBy=default.target

When you created the file you can execute systemctl start tcp2serial to start the service.

From another Linux computer in the same network you can connect to it using telnet [server] 5900. To exit telnet press Ctrl+] and type quit Enter

When you have edited the service file, execute these two commands:

systemctl daemon-reload
systemctl restart tcp2serial

To make the service start on boot execute:

systemctl enable tcp2serial
还给你自由 2024-07-20 05:45:36

使用 netcat 在服务器中打开一个端口并开始侦听:

nc -lvp port number

在您正在读取串行端口的计算机上,以 root 身份使用 netcat 发送它:

nc <IP address> portnumber < /dev/ttyACM0

如果您想将数据存储在服务器上,您可以将数据重定向到文本文件。

首先创建一个要保存数据的文件:

touch data.txt

然后开始保存数据

nc -lvp port number > data.txt

Open a port in your server with netcat and start listening:

nc -lvp port number

And on the machine you are reading the serial port, send it with netcat as root:

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
迷荒 2024-07-20 05:45:36

我有同样的问题。

我不太确定开源应用程序,但我已经测试了命令行 Serial over Linux 以太网 以及...它对我有用。

还要感谢梅加登法官的指示。

I had the same problem.

I'm not quite sure about open source applications, but I have tested command line Serial over Ethernet for Linux and... it works for me.

Also thanks to Judge Maygarden for the instructions.

羅雙樹 2024-07-20 05:45:36

我认为你的问题不太清楚。 这里有几个关于如何捕获进入 Linux 串行端口的数据的答案,但也许您的问题是相反的?

如果您需要捕获来自 Linux 串行端口的数据并将其发送到服务器,有几个小硬件小玩意可以做到这一点,从简单的串行打印服务器开始,例如 Lantronix 小发明

不,我与 Lantronix 没有任何关系。

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?

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.

No, I'm not affiliated with Lantronix in any way.

梦幻的味道 2024-07-20 05:45:36

我已经为这个问题苦苦挣扎了几天。

对我来说,问题源于 VirtualBox/Ubuntu。 我的机器上有很多 USB 串行端口。 当我尝试将其中一个分配给虚拟机时,它破坏了所有虚拟机 - 即主机和其他虚拟机不再能够使用其 USB 串行设备。

我的解决方案是在我衣柜里的上网本上设置一个独立的串行服务器。

我尝试了 ser2net,它可以将串行端口放在电线上,但 remtty 不起作用。 我需要将端口作为虚拟机上的 tty 获取。

socat 工作得很好。

这里有很好的说明:

使用远程 tty (tty over TCP) 的示例索卡特

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.

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.

socat worked perfectly.

There are good instructions here:

Example for remote tty (tty over TCP) using socat

Hello爱情风 2024-07-20 05:45:36

您可能会找到 PerlPython 对于从串行端口获取数据很有用。
要将数据发送到服务器,如果服务器是(比方说)HTTP 应用程序甚至是流行的数据库,那么解决方案可能很简单。 如果它是一些自定义/专有的 TCP 应用程序,那么解决方案就不那么容易了。

You might find Perl or Python useful 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文