Serial ports are for distinct machines to communicate with each other, not for IPC within the same machine. You can configure serial hardware for loopback, but the highest data rates supported by serial port hardware do not come anywhere close to the speed of any modern interconnect -- not USB or eSATA (for other interfaces with "serial" in their names) nor network interconnects such as ethernet (even wireless). Serial port speed is not even in the same solar system as a FIFO's.
As far as other characteristics go,
- serial ports will be presented to the system as device files, and FIFOs also will be presented as files
- as such, each can be opened concurrently by multiple unrelated processes, for both reading and writing
- however, you need special privileges to create a serial port special file, plus actual hardware behind it for it to be useful, whereas anyone can make a FIFO
- communication through serial ports is bi-directional; it can be full-duplex, but half-duplex modes are available as well.
- FIFOs are unidirectional, but you can use them in pairs if necessary. In principle, one process could both write to and read from a FIFO, but it would need to be very careful if it wanted to avoid consuming its own messages and to avoid deadlocking.
Bottom line: for bidirectional IPC within one machine, FIFOs are far superior to serial ports. You should also consider a socket interface.
Answer from John Bollinger on Stack OverflowWhy doesn't the below work?
# in one terminal:
echo "asdf" > /dev/ttyUSB0
# in another terminal, this hangs and does nothing
cat < /dev/ttyS0
Because, as a rule, serial ports don't buffer data. If there's no client app to receive the bytes landing on the serial port, they will simply be discarded.
As an experiment, try launching minicom or cu or another serial terminal program on the receiving computer, then run the echo command again on the transmitting computer. Assuming the baud rate and framings line up, you should see "asdf" appear at the destination.
I want to be able to simply send bytes into one end and come out the other end, and vice versa. Why doesn't the below work?
I think it's because you simply need to switch the order: start the listener first and then send the data (just like you did in your own pipe example--you started listening first):
# Absolutely first, configure each port using `stty`, as you already
# do:
# set both serial ports to 9600 8n1
# `-parenb` means no parity,
# `-cstopb` means 1 stop bit
# cs8 means 8 bits at a time
stty -F /dev/ttyUSB0 cs8 -cstopb -parenb 9600
stty -F /dev/ttyS0 cs8 -cstopb -parenb 9600
# THEN, do in this order
# first, in the receiving terminal, start listening
cat < /dev/ttyS0
# then, in a separate terminal for sending, send the data
echo "asdf" > /dev/ttyUSB0
I am pretty confident my answer is procedurally correct, meaning: if you blindly follow it, it will work.
But, the question still remains: why? Why do you have to start listening first? Doesn't the Linux kernel buffer for you? If so, shouldn't the data just be sitting there in the buffer ready to be read? Clearly it isn't, else writing first and reading second would work. But, it doesn't.
I'm guessing here, but I think the answer here is that when the driver receives data, it first checks to see if the receiving pseudo-file at /dev/ttyUSB0 is in an opened state, held open by another process. If not, the driver discards the data, since no one is there to receive it. If the file is open, it allows the process which has the file open to read the buffered data.
Key differences between sending over a serial port and sending over an inter-process-communication (IPC) pipe
This is different from your pipe example where you make a pipe with mkfifo. I just checked, and in the pipe example, you can either start listening in one terminal first or send to the pipe in the other terminal first. It doesn't matter. It works in both cases.
If you write first, the write blocks and waits until a process reads from the pipe. If you read first, the read blocks and waits until a process writes to the pipe. This is confirmed by the Linux documentation for C's mkfifo() function, which the bash mkfifo is almost certainly based on. See man 3 mkfifo (emphasis added):
Once you have created a FIFO special file in this way, any process can open it for reading or writing, in the same way as an ordinary file. However, it has to be open at both ends simultaneously before you can proceed to do any input or output operations on it. Opening a FIFO for reading normally blocks until some other process opens the same FIFO for writing, and vice versa.
This is different from how serial ports behave, however, probably because the serial ports on each end are expected to be controlled from different machines, unlike FIFO pipes, whose read and write ends are expected to be controlled from the same machine. In the latter case, the kernel can easily track when one process tries to read and another tries to write, since it controls both ends of the pipe...hence, it allows either to block.
In the serial port case, however, the kernel has no idea if another device is listening or going to send, so it won't block on sending. To be clear: when writing to a pipe, the write is blocked until a listener is present. But, when writing to a serial port, the computer has no means of checking if a listener is present, so it never blocks! It just sends. Instead of blocking if no listener is present, data sent over serial while no-one is listening is just lost. Again, this is different from pipes, where my experiment and the documentation above both confirm that a process trying to send data over a pipe is blocked until a listener is present to read that data.
References and "see also":
- I'm actively learning as I write this, documenting various findings in my eRCaGuy_dotfiles repo here, in case you're interested: serial_terminals_README.md.
- My demo code: eRCaGuy_hello_world/bash/ipc_pipe_fifo.sh
man 3 mkfifo: https://man7.org/linux/man-pages/man3/mkfifo.3.html
You set Host pipe. It means you need to fill pipe name like this: \.\pipe\VirtualMachineName
Moreover, as Robin Hood mentioned you need to uncheck Connect to existing pipe/socket it helps don't create a pipe manually. After launch Virtual machine you can check created pipe via pipelist application. You should see VirtualMachineName on the list.
C:\Windows\system32>pipelist64.exe
...
pipe_returnb2848f45-49cf-444b-85a1-04af7fe5606e 1 128
mojo.9004.8732.10216662671524970515 1 1
mojo.9004.9560.12163195199288806074 1 1
mojo.9004.9560.2084235546261883787 1 1
VirtualMachineName 1 1
You can read what is pipe here. A pipe isn't a simple file.
it is not an existing port, uncheck connect to existing pipe, so it creates a new one.
Thanks
I guess:
copy com1: somefile.log
There is also con: and nul: special device as I remember. But that was true for times when DOS was more popular. Probably now you have to create some strange file/folder with extension where in curly brakets is placed some scary long sequence of numbers and letters :)
I find more usefull this one:
type com1: >> data.log
The main advantages versus a copy is that you can append data to an existing file and you can open the file with another application (notepad) to see the contents.
I cannot test this end to end (no arduino anywhere near), but since serialport seems to implement a Readable stream, I'd try using scramjet like this:
const {StringStream} = require('scramjet');
serialport.on('open', () => console.log('open');
serialport.pipe(new StringStream) // pipe the stream to scramjet StringStream
.lines('\n') // split per line
.each( // send message per every line
data => io.sockets.emit('message',data)
);
Scramjet would sort the readline issue for you.
I tested this code with the serial port device that uses RS-232 protocol. Its advantage is that you don't need install the third package from npm.
var recVal = '';
mySerial.on('data', function(data) {
if(data.indexOf('\n') != -1) {
io.emit('serial:data', {
value: recVal
});
console.log("Data: ", recVal.toString());
recVal = '';
} else {
recVal = recVal.concat(data);
}
});