Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-05-2012, 04:28 AM   #1
LQ Newbie
Registered: Sep 2012
Posts: 2

Rep: Reputation: Disabled
Lightbulb Virtual serial port?

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
Old 10-05-2012, 06:39 AM   #2
Registered: Aug 2002
Posts: 14,761

Rep: Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478
No actual experience using it.
Old 10-05-2012, 08:59 AM   #3
Wim Sturkenboom
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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).

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

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).

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

Last edited by Wim Sturkenboom; 10-05-2012 at 09:00 AM.
Old 10-05-2012, 09:54 PM   #4
LQ Guru
Registered: Mar 2008
Posts: 15,164

Rep: Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143Reputation: 2143
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Virtual disk/node port for serial buffer balaqemu Linux - Newbie 1 10-21-2011 11:07 AM
How to create a virtual serial/COM port in Linux archShade Linux - General 8 06-12-2010 09:51 AM
access virtual box serial port from host wykthorr Linux - Virtualization and Cloud 2 04-05-2010 11:42 PM
Serial Port Programming/virtual Device MikeGra Programming 3 04-16-2006 03:35 PM
Virtual serial port over TCP/IP ericcarlson Linux - Software 0 11-10-2004 05:39 AM

All times are GMT -5. The time now is 12:45 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration