Usb to tcp linux

Converting serial port data to TCP/IP in a Linux environment

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?

You might find this useful for the TCP/IP-part, and the serial ports you might get a lot of help here. Keep in mind, everything in Linux is «a file», so you could probably «cat» the serial port.

10 Answers 10

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 

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.).

Typo: change «< cat /dev/ttyS0" to just "< /dev/ttyS0". Function: Might put it in a shell script while : do netcat . done If there is danger of the file I/O ending, e.g. if it's a modem, loses carrier, etc.

I changed the snippet per Liudvikas. I didn’t get a chance to check it last night because of the 3 week old baby at the house!

It would be nice if someone can edit this answer and explain about the logic behind the < and >, what happend first ? how that works ? maybe it looks trivial, but i need to make some redirections to logfiles also and find that challanging .

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
Читайте также:  Linux port open local

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.

According to this page, socat doesn’t directly support RFC 2217, but it provides a patch to support it.

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:

Be sure to change the /dev/ttyS0 to match the serial device you are attempting to interface with.

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

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

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:

And then start saving data

nc -lvp port number > data.txt 

This will work but it requires access to terminal on the machine that hosts the port. A more robust solution is to use ser2net where the server acts like a regular server for accessing remote resource. Of course this way is more «secure» as you need to physically connect using SSH to the remote system first before you’re able to access the serial port but at the same time it is far less convenient than ser2net. Kind of depends on what you need.

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.

I am -1-ing this answer as it looks like an ad for commercial product. Maybe it is maybe it isn’t — I don’t really know however things like accessing resources by remote on Linux is provided pretty much out of the box and no fancy user interface is needed to do it.

Читайте также:  Yandex browser системные рамки linux

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:

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 

Источник

USB/IP

USB/IP Project aims to develop a general USB device sharing system over IP network. To share USB devices between computers with their full functionality, USB/IP encapsulates «USB I/O messages» into TCP/IP payloads and transmits them between computers.

Installation

Usage

Server setup

The server should have the physical USB device connected to it, and the usbip_host USB/IP kernel module loaded. Then start and enable the USB/IP systemd service usbipd.service . The daemon will accept connections on TCP port 3240.

List the connected devices:

Bind the required device. For example, to share the device having busid 1-1.5:

After binding, the device can be accessed from the client.

Binding with systemd service

In order to make binding persistent following systemd template unit file can be used:

/etc/systemd/system/usbip-bind@.service
[Unit] Description=USB-IP Binding on bus id %I After=network-online.target usbipd.service Wants=network-online.target Requires=usbipd.service #DefaultInstance=1-1.5 [Service] Type=simple ExecStart=/usr/bin/usbip bind -b %i RemainAfterExit=yes ExecStop=/usr/bin/usbip unbind -b %i Restart=on-failure [Install] WantedBy=multi-user.target

So, e.g., to share the device having busid 1-1, one should start/enable usbip-bind@1-1.service .

Client setup

Make sure the vhci-hcd kernel module is loaded.

Then list devices available on the server:

$ usbip list -r server_IP_address 

Attach the required device. For example, to attach the device having busid 1-1.5:

$ usbip attach -r server_IP_address -b 1-1.5

Disconnecting devices

A device can be disconnected only after detaching it on the client.

$ usbip detach -p port_number 

Unbind the device on the server:

$ usbip unbind -b busid 

Note: USB/IP by default requires port 3240 to be open. If a firewall is running, make sure that this port is open. For detailed instruction on configuring the firewall, go to Category:Firewalls.

Читайте также:  Linux make set path

Tips and tricks

Binding by vendor/device ID

If bus ids are inconsistent and dynamically assigned at each system boot, binding by vendor/device ID can be used alternatively:

/etc/systemd/system/usbip-bind@.service
[Unit] Description=USB-IP Binding device id %I After=network-online.target usbipd.service Wants=network-online.target Requires=usbipd.service [Service] Type=simple ExecStart=/bin/sh -c "/usr/sbin/usbip bind --$(/usr/sbin/usbip list -p -l | grep '#usbid=%i#' | cut '-d#' -f1)" RemainAfterExit=yes ExecStop=/bin/sh -c "/usr/sbin/usbip unbind --$(/usr/sbin/usbip list -p -l | grep '#usbid=%i#' | cut '-d#' -f1)" Restart=on-failure [Install] WantedBy=multi-user.target

So, e.g., to share the device having vendor/device ID 0924:3d68, one should start/enable usbip-bind@0924:3d68.service .

Note: Such a binding method cannot work correctly for multiple devices with the same vendor/device ID.

Then client setup will be like this:

$ usbip attach -r server_IP_address --$(/usr/sbin/usbip list -p -r server_IP_address | grep '#usbid=0924:3d68#' | cut '-d#' -f1)

Note: If the previous command fails, check if -p flag in usbip list -p -r server_IP_address is working properly. If not, use the following line instead:

$ usbip attach -r server_IP_address -b $(/usr/sbin/usbip list -p -r server_IP_address | grep '0924:3d68' | cut '-d:' -f1 | awk '')
c:\> for /f "tokens=1 delims=:, " %a in ('usbip list -r server_IP_address ^| findstr /r /c:"0924:3d68"') do start usbip attach -r server_IP_address -b %a

Sharing devices configured with files in /etc/..

usbip-systemd AUR provides systemd service files for binding by vendor/device id and for connecting by hostname and vendor/device id.

Server setup

For each device create a device.conf in /etc/usbip/bind-devices/ with USBIP_DEVICE set to the vendor/product id, e.g.:

/etc/usbip/bind-devices/example-device.conf

To bind a cofigured device start/enable the service usbip-bind@example-device.service

Client setup

For each host/device create a device.conf in /etc/usbip/remote-devices/ with HOST set and USBIP_DEVICE set to the vendor/product id, e.g.:

/etc/usbip/remote-devices/example-device.conf
USBIP_HOST=example-host USBIP_DEVICE=0924:3d68

Make sure your server is running and the configured device is bound, then start or stop the service usbip@example-device.service

See also

Источник

Оцените статью
Adblock
detector