LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-25-2010, 01:37 PM   #1
archShade
Member
 
Registered: Mar 2006
Location: Delft NL
Distribution: Debian; Slackware; windows 7
Posts: 218

Rep: Reputation: 53
How to create a virtual serial/COM port in Linux


I'm currently working on a project for university that requires communication, via an RS232 link between a piece of hardware (I have designed and am currently testing) and some software(which need testing).

I was hoping to have my hardware finished by now so I could get on with testing the software over Easter.

The hardware does not work yet but I would still like to test the software.

Does any one know if there a way to make a virtual serial port that I could put data on and read data off while my program accesses it as a regular serial port so I can check my code?

I am using Ubuntu 9.10 and am happy to give any more information on request.

PS: I'm sorry if this is in the wrong place I did not know what this type of question came under so I put it in genral
 
Old 03-25-2010, 08:16 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,666

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by archShade View Post
I'm currently working on a project for university that requires communication, via an RS232 link between a piece of hardware (I have designed and am currently testing) and some software(which need testing).

I was hoping to have my hardware finished by now so I could get on with testing the software over Easter.

The hardware does not work yet but I would still like to test the software.

Does any one know if there a way to make a virtual serial port that I could put data on and read data off while my program accesses it as a regular serial port so I can check my code?

I am using Ubuntu 9.10 and am happy to give any more information on request.

PS: I'm sorry if this is in the wrong place I did not know what this type of question came under so I put it in genral
Offhand, no, but I'm sure there's a way to do it. However, if I had to test a serial device, I'd grab a cheap USB to Serial converter, and jump pins 2&3 together, to create a loopback. Your software should then be able to transmit/receive....
 
Old 03-25-2010, 08:35 PM   #3
archShade
Member
 
Registered: Mar 2006
Location: Delft NL
Distribution: Debian; Slackware; windows 7
Posts: 218

Original Poster
Rep: Reputation: 53
I had thought about using a loop back but the data I wish to simulate coming in is not the same as that going out. I suppose I could get two serial ports and connect RX of one to TX of the other and vice versa.

Would still be nice if I could do it with a virtual port.
 
Old 03-26-2010, 09:07 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,666

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by archShade View Post
I had thought about using a loop back but the data I wish to simulate coming in is not the same as that going out. I suppose I could get two serial ports and connect RX of one to TX of the other and vice versa.

Would still be nice if I could do it with a virtual port.
Very, but the time spent in getting that going, would be much more than plugging in a USB device. Thought about the different data thing after I posted...you could always get two, and a null-modem adapter, and connect it to another PC, running a terminal emulator. That way, you could see the data you're sending come up on the screen, and type in data to go BACK to your program.
 
Old 03-26-2010, 06:42 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
I think part of the answer lies in how you intend to create a simulation of the device, and whether the objective is to test the communication channel itself, or to test the 'conversation', the data exchange irrespective of the serial interface. If the latter, you can probably create a useful and simple to deploy simulation by exchanging data through pipes or message queues. A process simulating the device reads and writes data in real time, responding to queries from the project software. If the communications itself is the object of the test, then a second serial port driven by the simulation code seems like a good approach. Simply using a loopback on a single port doesn't sound like it will work, since it is still a single port, which only a single process can open and use.
How you craft your simulator might make as much of a project as the original project. Usually devices do something that relates to real-world events and circumstances, and modeling these is the subject of a lot of science.
--- rod
 
Old 03-26-2010, 07:20 PM   #6
archShade
Member
 
Registered: Mar 2006
Location: Delft NL
Distribution: Debian; Slackware; windows 7
Posts: 218

Original Poster
Rep: Reputation: 53
First off thanks for the and help.

Seaching the Internet I found this program http://sourceforge.net/projects/ttypatch/ but I keep getting this error.

Code:
could not open /dev/ptyp0
Any way this seems to be more complex and time consuming than I thought so I have decided to modify the code so it writes to a file (I have called serverTX) and reads from another (serverRX) the simulated device reads writes to the opposite files.

I think this should show the logic of my project which is what needs to be done (I only have to demonstrate the validity of my idea)

I plan to try this over the weekend wish me luck
 
Old 06-11-2010, 09:02 AM   #7
lcgamboa
LQ Newbie
 
Registered: Jun 2010
Location: Brasil
Distribution: debian
Posts: 1

Rep: Reputation: 6
Hi,

ttypatch use legacy ptys (bsd style), disabled for security questions in new kernels. You can recompile the kernel with this option enabled and the ttypatch will work.
Other option is use the ptys UNIX_98 style, supported in new kernels.

The code below simulate an serial data connection without control handshake using pty UNIX_98 style, this code work to connect two gtkterm, replacing ttypatch for my tests.


/*compile with : gcc -Wall -O2 -D_GNU_SOURCE tty0tty.c -o tty0tty */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>


int
ptym_open(char *pts_name, char *pts_name_s , int pts_namesz)
{
char *ptr;
int fdm;

strncpy(pts_name, "/dev/ptmx", pts_namesz);
pts_name[pts_namesz - 1] = '\0';

fdm = posix_openpt(O_RDWR | O_NONBLOCK);
if (fdm < 0)
return(-1);
if (grantpt(fdm) < 0)
{
close(fdm);
return(-2);
}
if (unlockpt(fdm) < 0)
{
close(fdm);
return(-3);
}
if ((ptr = ptsname(fdm)) == NULL)
{
close(fdm);
return(-4);
}

strncpy(pts_name_s, ptr, pts_namesz);
pts_name[pts_namesz - 1] = '\0';

return(fdm);
}


int main(void)
{
char master1[1024];
char slave1[1024];
char master2[1024];
char slave2[1024];

int fd1;
int fd2;

char c1,c2;

fd1=ptym_open(master1,slave1,1024);

fd2=ptym_open(master2,slave2,1024);

printf("(%s) <=> (%s)\n",slave1,slave2);


while(1)
{
if(read (fd1,&c1,1) == 1) write(fd2,&c1,1);
usleep(20);
if(read (fd2,&c2,1) == 1) write(fd1,&c2,1);
usleep(20);
};

close(fd1);
close(fd2);

return EXIT_SUCCESS;
}
 
1 members found this post helpful.
Old 06-12-2010, 03:49 AM   #8
234pramod
LQ Newbie
 
Registered: May 2010
Posts: 1

Rep: Reputation: 0
same kind of problem

This s regarding my project
I am trying to send data over wifi using socket.
I am working Ubuntu.
The data to be sent is received from a device over serial port.
I want to create a virtual serial
port which will read data from a file and present it in way serial port will present for a given baud rate.
I got same error and also we while running auto testtty.c
the error is couldnt open ttyp0
Also i checked /de there is no file called ptyp0 then how can it open(there is file named ptmx,i think its pseudo terminal)
So can u please tell how to recomplile with kernel permission changed or what should be make file.
This is what make file i have.
[/quote]# Makefile for ttypatch-1.0
# Copyright (c) Daniel G. Clemmensen. Licensed under the GPL.
#
# trivial makefile, generated manually. Needs to be replaced by full-up
# autoocnf/automake system. We simply make ttypatch and testty.
# This will probably work on most Linux distros, but that's all
#
all: ttypatch testty autotesttty
ttypatch: ttypatch.c
gcc -DSTDC_HEADERS=1 -DHAVE_SYS_WAIT_H=1 -DHAVE_FCNTL_H=1 -DHAVE_SYS_IOCTL_H=1 -DHAVE_UNISTD_H=1 -DRETSIGTYPE=void -DHAVE_SELECT=1 -DHAVE_STRERROR=1 -I. -I. -g -O2 ttypatch.c -o ttypatch

testty: testty.c
gcc -DSTDC_HEADERS=1 -DHAVE_SYS_WAIT_H=1 -DHAVE_FCNTL_H=1 -DHAVE_SYS_IOCTL_H=1 -DHAVE_UNISTD_H=1 -DRETSIGTYPE=void -DHAVE_SELECT=1 -DHAVE_STRERROR=1 -I. -I. -g -O2 testty.c -o testty

autotesttty: autotesttty.c
gcc -DSTDC_HEADERS=1 -DHAVE_SYS_WAIT_H=1 -DHAVE_FCNTL_H=1 -DHAVE_SYS_IOCTL_H=1 -DHAVE_UNISTD_H=1 -DRETSIGTYPE=void -DHAVE_SELECT=1 -DHAVE_STRERROR=1 -I. -I. -g -O2 autotesttty.c -o autotesttty

[/quote]

Also can u please tell me how i can open a ttyp0 slave in other user program and read data from it.

Last edited by 234pramod; 06-12-2010 at 03:52 AM. Reason: additional information
 
Old 06-12-2010, 09:51 AM   #9
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,666

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by 234pramod View Post
This s regarding my project
I am trying to send data over wifi using socket.
I am working Ubuntu.
The data to be sent is received from a device over serial port.
I want to create a virtual serial
port which will read data from a file and present it in way serial port will present for a given baud rate.
I got same error and also we while running auto testtty.c
the error is couldnt open ttyp0
Also i checked /de there is no file called ptyp0 then how can it open(there is file named ptmx,i think its pseudo terminal)
So can u please tell how to recomplile with kernel permission changed or what should be make file.
This is what make file i have.

Also can u please tell me how i can open a ttyp0 slave in other user program and read data from it.
Open your own thread for your own question, please, and spell out your words. Don't tack yours onto the bottom of someone elses. And if you read the above thread, you'd see code that lets you work with a virtual serial port.
 
  


Reply

Tags
port, serial



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Virtualbox winxp virtual machine just can't access serial port xmrkite Linux - Virtualization and Cloud 5 11-11-2009 10:52 PM
Serial Port Programming/virtual Device MikeGra Programming 3 04-16-2006 03:35 PM
How to create a virtual serial connection through bluetooth? RHLinuxGUY Linux - Wireless Networking 1 03-31-2006 03:25 PM
Virtual serial port over TCP/IP ericcarlson Linux - Software 0 11-10-2004 05:39 AM
how to create ppp server in RH 7.2 as small ISP thro serial port pentium5 Linux - General 5 07-06-2002 06:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:20 AM.

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