Linux Virtual Serial Ports

tatteredpotato

Diamond Member
Jul 23, 2006
3,934
0
76
I'm working on a simple serial application in Java using the RXTX library. For development, I would like to create a simple "device emulator" that will output serial messages that I can then read in with my client application. I've done this before easily on Windows with some special software to create two virtual serial ports and have them linked internally.

After some searching I've discovered the "socat" utility that can accomplish the same thing using:

Code:
socat -d -d pty,raw,echo=0, pty,raw,echo=0

This creates two devices in "/dev/pts/1" and "/dev/pts/2". The problem for me is that RXTX doesn't detect these devices at runtime. I read it might be helpful to rename these devices... however I'm not sure how to do that exactly.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
What does it detect? Could you just create symlinks like /dev/ttyV1 and /dev/ttyV2 to the pts devices and see if picks them up?
 

tatteredpotato

Diamond Member
Jul 23, 2006
3,934
0
76
What does it detect? Could you just create symlinks like /dev/ttyV1 and /dev/ttyV2 to the pts devices and see if picks them up?

So diving into the source code for RXTX I discovered this:

Code:
String[] Temp = {
	"ttyS", // linux Serial Ports
	"ttySA", // for the IPAQs
	"ttyUSB" // for USB frobs
};
CandidatePortPrefixes=Temp;

So after I symlinked to a port-name with a valid prefix (such as ttyUSB01) it is detected at least. My previous attempts at symlinking only added the "tty" prefix, which was not enough.
 

tatteredpotato

Diamond Member
Jul 23, 2006
3,934
0
76
Now RXTX detects the device, however when I attempt to open the port I get the following error:

RXTX fhs_lock() Error: creating lock file: /var/lock/LCK..ttyUSB01: File exists

I get the impressions that I may be opening the wrong devices from my understanding that error means another process has the device open (possibly socat?).
 

tatteredpotato

Diamond Member
Jul 23, 2006
3,934
0
76
Application error on my part that I was blaming on other things... I've gotten the loopback adapters to work using socat and tested using minicom (and successfully opened in RXTX).

The entire process (in case someone has this same issue) is:

1) Use socat to make the two adapters:
Code:
socat -d -d pty,raw,echo=0, pty,raw,echo=0
2) Look at the output from socat... for me this was:
Code:
2011/03/10 16:07:53 socat[2712] N PTY is /dev/pts/1
2011/03/10 16:07:53 socat[2712] N PTY is /dev/pts/2
2011/03/10 16:07:53 socat[2712] N starting data transfer loop with FDs [3,3] and [5,5]
Note the two devices created (/dev/pts/1 and /dev/pts/2 for me).

3) Symlink to a location that RXTX will recognise (/dev/ttyS*, /dev/ttyUSB*, etc)
Code:
sudo ln -s /dev/pts/1 /dev/ttyUSB01
sudo ln -s /dev/pts/2 /dev/ttyUSB02
4) I tested using two instances of minicom (one on each port)
Code:
minicom -D /dev/ttyUSB01
minicom -D /dev/ttyUSB02
Typing into one terminal should cause those characters to appear in the other terminal.
 

coopsnoop

Junior Member
Sep 9, 2011
1
0
0
Hi there,

you don't know how much it means the code you posted...
I'm trying to do the same thing and after following your procedure I get an error from the java application saying
"RXTX fhs_lock() Error: creating lock file: /var/lock/LCK..ttyUSB02: File exists"
You said you made an error on the application end causing the locking, what was that?
Thanks so much