LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to create a virtual serial/COM port in Linux (http://www.linuxquestions.org/questions/linux-general-1/how-to-create-a-virtual-serial-com-port-in-linux-797851/)

archShade 03-25-2010 01:37 PM

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

TB0ne 03-25-2010 08:16 PM

Quote:

Originally Posted by archShade (Post 3912044)
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....

archShade 03-25-2010 08:35 PM

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.

TB0ne 03-26-2010 09:07 AM

Quote:

Originally Posted by archShade (Post 3912596)
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.

theNbomr 03-26-2010 06:42 PM

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

archShade 03-26-2010 07:20 PM

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

lcgamboa 06-11-2010 09:02 AM

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;
}

234pramod 06-12-2010 03:49 AM

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.

TB0ne 06-12-2010 09:51 AM

Quote:

Originally Posted by 234pramod (Post 4000952)
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.


All times are GMT -5. The time now is 11:20 PM.