How to create a dummy "pipe" pseudo-serial device on Linux? - linux

I need to write a program (C++) that uses a serial port to communicate with another device. The other device isn't even built yet so I need a software emulator For various reasons there is no point going into here, the software emulator needs to run on a different machine. I would like to send the data via UDP from the software emulator to the machine my program is running on and have it received by another serial interface type program that in some way acts as a serial port (serial tty device).
I also want to test my program in an automated fashion in a VM. Ideally my test program would also use UDP and would use the same UDP-serial interface program to forward the data back and forwards between my test program and the program under test.
Time is very tight. I don't really have time to learn to write and install kernel level device drivers.
I would be very grateful for any pointers as to how I can create some sort of "pipe" or "loopback" pseudo-serial device.
I am working on Linux.

Credit to meuh for his tip-off.
socat UDP:127.0.0.1:5001,bind=127.0.0.1:5000 \
PTY,link=/dev/ttyS0,raw,echo=0,waitslave
This listens on UDP port 5000 on the loopback network interface. All data received is sent to the virtual serial device at /dev/ttyS0. All data received on the virtual serial device is sent to UDP address 127.0.0.1:5001.
The IP address can be remote.
The command must be run as root, as must the process connecting to the serial port. To avoid this use a different file path, e.g. /tmp/ttyS99.
Apparently the file path specified must not already exist. However my PC has /dev/ttyS0 all the way to /dev/ttyS31 despite not having any serial ports, and using /dev/ttyS0 works fine. I suppose if I actually had a real serial port this wouldn't work.

Related

Does Linux serial port device support poll()?

I want to know whether Linux kernel device driver of serial port device, e.g. /dev/tty1, /dev/tty2, support poll() operation?
Or tell me where is the kernel source code of serial port device?
In my case, some sensors will send data to ARM CPU via serial port each second. And I think one way is to use a timer for periodically read from the serial port. Another way should use poll(), read it when data is ready.
Unless your serial port is a special case, using poll() or select() would seamlessly work.
If your serial port was controlled via USB, you would have to take care about what happens if the USB to serial is disconnected, but this does not seem to be the case for you.

will printf() conflict with the 232 serial communcation in embedded linux system?

I'm having an embedded linux device. I'm using ttyO2 as my console.
However, at the same time my MCU need to perform RS232 communicate with a device through ttyO2.
Now lets say if the MCU and the device are communicating, and I type some characters in the console terminal, or there runs another thread that will invoke function printf() , will that conflicts with the 232 communication? is the printf() outputting to the ttyO2?
Thanks
I'm using ttyO2 as my console.
so you configured serial port as console, printf() output will be redirected to console. i.e serial port.
If your board is communicating over RS232 with an external device, then then board and the device are connected with a serial cable. How can you also connect the the serial terminal?
In any case, using the same serial port as console and as a communication port is a bad idea, because there are a lot of things that can be printed: the console I/O, the kernel debug, other programs output to stdout and so on.... Do you think you can have a stable communication with all that "junk" on the wire?

Sniffing IOCTL and serial port communication

I'm trying to reverse the protocol used by an early nineties logic analyzer an its PC software.
The device is connected via RS-232 (propietary wiring) and communicates with a DOS program, successfully running on DOSBOX.
I'm able to control the device with the original software but it would be useful to autimathe the downloading of data from the analyzer using a custom program but to do so I need to know what is going on the serial port.
Ineed to know what mode is the serial port set, while I know for sure the datarate is either 1200 or 9600 bps (configurable on the device) I don't know the flow control (I guess it is RTS/CTS).
I also need to tap into the conversation between the prgram and the device without disturbing their communication.
Reading the serial port with another program (cutecom/minicom) prevents the emulator from receiving the data from the hardware.
So, summing up, what I need to know is:
What configuration is set on /dev/ttyS0 (via IOCTL calls, i think)
What goes on between program and device.
I was thinking in programming a library which acts as a proxy for the standard c library (via LD_PRELOAD) but there must be an easier way to do this.
You can use slsnif (Serial Line SNIFfer).
http://linux.die.net/man/1/slsnif
Here's a link to the sourceforge project so you can download it. I don't believe it comes with any modern distributions but I could be wrong so check your distro's software repository first.
http://sourceforge.net/projects/slsnif/
I use ttyrpld for tty sniffing. I ported it to PPC and run it on 2.6.32. It logs all of the tty traffic on the board to files, one per tty. Works well.

Establishing a simulated bluetooth connection

I'd like to establish a simulated Bluetooth connection between two Java programs running on the same machine. I am writing to program that uses BlueCove's BT library to connect to a hardware device; I pass the bluetooth address (ex. "btspp://1C659DF6B5AC:1;master=false;encrypt=false;authenticate=false", which contains the device's mac address) to my program to connect.
I would like to write another java program that emulates the hardware device so that I can do testing (my hardware is not currently working). Is there a way to simulate a bluetooth device with either a mac address or some other kind of bluetooth address which can send data over to my program?
By emulating the hardware you are possibly doing more work than needed, is it possible to create a mock connection "before" the BT library has been invoked (or for you to remove it temporarily)?
By doing this you can craft some code which behaves like the connection, but doesn't require you writing device drivers.

how to read from and write to a serial port (ttys0) from another pc via ethernet (eth0) on Ubuntu/Debian?

Well, there are two pc's connected via Wi-Fi and one of those (let's call it A) has a serial port printer (in ttys0) and a measure serial port device (in ttys1) connected too. So B needs to read some values from the mesure device connected on A and then write to the printer connected on A using a network connection.
ser2net is a good solution for this
aptitude install ser2net
Then edit the config in /etc/ser2net.conf
You can then make a tcp connection to a defined port and be connected to the serial port on the remote computer - very useful.
I don't know if Ubuntu has anything built-in, but you could run a couple of daemons using netcat. Of course, if you want it to be secure, you'd need to do a little more work.

Resources