简体   繁体   中英

Serial port routing in Linux

After reading about serial ports, virtual serial ports and such, I need a little advice to see if this is even possible. I've set up two serial ports on a Linux machine (running Ubuntu). I'd like to route the two serial ports together. Is this even possible?

The two serial ports are automatically started through the /etc/init/ttyXXX.conf getty scripts. I'd like it so that when the first serial port receives a character, it outputs that character straight away to the second serial port, and also the vice versa.

Any easy way to do this through a program or bash scripts?

The idea is that both serial ports should be able to access the linux machine with commands. However, it would be nice to be able to see the outputs of the commands regardless of which port you are attached to. For example, if port 1, logged on as root, sends "echo testing", I'd like for port 2 to see the output, but also able to see that port 1 sent the command.

Thanks

A small Perl script like this might do what you're hoping, though I'm not quite sure what you're asking, so please comment if it's not working the way you'd hope. I've only got it going one way because I think they'd just keep sending the same character back and forth if it were two way. You might also need to change the port paths near the top to whatever yours are.

Just save it as serial.pl or similar, make it executable and run it.

#!/usr/bin/perl

use strict;
use warnings;

use Device::SerialPort;

my $port1_path = '/dev/tty1';
my $port2_path = '/dev/tty2';

my $port1 = Device::SerialPort->new($port1_path);
$port1->databits(8);
$port1->baudrate(19200);
$port1->parity("none");
$port1->stopbits(1);

my $port2 = Device::SerialPort->new($port2_path);
$port2->databits(8);
$port2->baudrate(19200);
$port2->parity("none");
$port2->stopbits(1);

while ($in = $port1->input) {
    $port2->write($in);
}

It is possible to connect two serial ports between each other, with a crossover cable (so that the input of one port is connected to the output of the other port).

Assuming that you ports are correctly configured (drivers installed and loaded) and that your crossover cable is connected between your ports, you can type the following commands in two terminals:

Terminal 1: listen in the output port

$ tail -f /dev/ttyXXX

Terminal 2: write to the input port

$ echo "Hello!" > /dev/ttyYYY

If the two ports are correctly connected, the message "Hello!" will be displayed in terminal 1.

The hardest part is often to know which hardware port correspond to which device file.

If you just wanted to connect the two serial ports, you could use

socat /dev/ttyS0,raw,echo=0,crnl /dev/ttyS1,raw,echo=0,crnl

(see http://technostuff.blogspot.com/2008/10/some-useful-socat-commands.html )

But, since you want to interact with the command interpreter, I think you'll need to write a Perl script that

  1. Opens both serial ports
  2. Uses select to wait until one of the ports has some input for you
  3. Pass that input to the shell
  4. Write the output of the shell command back to both serial ports

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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