LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Virtual serial port? (http://www.linuxquestions.org/questions/programming-9/virtual-serial-port-4175430645/)

reszj 10-05-2012 04:28 AM

Virtual serial port?
 
Hi,
I have a little .c program which read data from a serial a port.
If I use a PC where is a serial port with the machine connected to the port than I can read the data.
The problem is that on my laptop, I don't have serial port. Can I simulate a serial port where I send some data and my .c program will read from there like he read from a real serial port.
I need this option because I want to test my .c program on my laptop because I can't access the PC with the serial port and with the machine which is connected to the port.

Thanks for reply :)

michaelk 10-05-2012 06:39 AM

No actual experience using it.

http://www.linuxquestions.org/questi...-linux-797851/
http://tty0tty.sourceforge.net/

Wim Sturkenboom 10-05-2012 08:59 AM

What you're looking for are pseudo terminals. See man pty

The BSD style ones are no longer supported; life was very easy with them. If you have an old distro (RH7 or so) they are still there. I personally find the unix98 style ones far more complicated.

Use tty0tty as indicated above; if you use the code from the linuxquestions link, it will throw a warning, so you might be better of with the sourceforge link (not checked).

Code:

wim@i3-2120:~/tty0tty$ gcc -Wall -O2 -D_GNU_SOURCE tty0tty.c -o tty0tty
tty0tty.c: In function ‘main’:
tty0tty.c:66:32: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
tty0tty.c:68:32: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
wim@i3-2120:~/tty0tty$ ls -l
total 16
-rwxrwxr-x 1 wim wim 8896 Oct  5 15:34 tty0tty
-rw-rw-r-- 1 wim wim 1075 Oct  5 15:34 tty0tty.c

Once you have an executable tty0tty, you can start it

Code:

wim@i3-2120:~/tty0tty$ ./tty0tty
(/dev/pts/2) <=> (/dev/pts/3)

Keep it running!

Use a terminal program (e.g minicom) to use one of the two (e.g /dev/pts/2). Use your application to use the other one (e.g. /dev/pts/3).

Code:

wim@i3-2120:~$ minicom -p /dev/pts/2
Note:
you might need to configure minicom so it does not send ATZ commands

jefro 10-05-2012 09:54 PM

Be careful on laptops and many desktops. Some have legacy parts or code that makes some apps think there really is a com1 and com2 installed.\

I think you need to test this in a virtual machine maybe or a real system that is configured like you need. Also test it with higher com port numbers such as one might find in a usb to serial adapter.


All times are GMT -5. The time now is 11:07 AM.