How can I use a pseudo terminal in python to emulate a serial port?

I am creating a python application using twisted that reads lines from a serial port. In order to (unit) test this application without connecting the actual device to the serial port (also on a PC without the actual serial port), I would like to create a python script / app that installs a virtual serial port and writes to it, so a twisted application can connect to the other end of the virtual serial port and read from it. That way I can write some unittests.

I have found that this is possible using pseudo-terminals in Linux. I also found a working example script at https://askubuntu.com/questions/9396/virtual-serial-port-for-testing-purpose .

I would like to change this script to a class on which I can call the write method to write data to the serial port, and then check the twisted application.

In this example script, a lot of material is being done with the poll and selection and the linux stty command, which I really don't understand. I hope someone can fill in the gap in my knowledge or give some clues.

Greetings

Dolph.

+4
source share
3 answers

You do not need pty to check your protocol. You do not even need any file descriptor. Follow the guidelines at http://twistedmatrix.com/documents/current/core/howto/trial.html , especially in the Protocol Testing section.

+2
source

In addition to what Jean-Paul Calderon said (which was the correct answer), I also made the following script in python using socat.

This can be imported and instantiated into the interpreter, and then you can use the writeLine method to write data to the serial port (vritual), which connects via socat to another (virtual) serial port on which another twisted application can happen. But, as Jean-Paul Calderone said: if you just want you to want this, you really don't need to do this. Just read the documents he mentioned.

import os, subprocess, serial, time from ConfigParser import SafeConfigParser class SerialEmulator(object): def __init__(self,configfile): config=SafeConfigParser() config.readfp(open(configfile,'r')) self.inport=os.path.expanduser(config.get('virtualSerialPorts','inport')) self.outport=os.path.expanduser(config.get('virtualSerialPorts','outport')) cmd=['/usr/bin/socat','-d','-d','PTY,link=%s,raw,echo=1'%self.inport,'PTY,link=%s,raw,echo=1'%self.outport] self.proc=subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE) time.sleep(3) self.serial=serial.Serial(self.inport) self.err='' self.out='' def writeLine(self,line): line=line.strip('\r\n') self.serial.write('%s\r\n'%line) def __del__(self): self.stop() def stop(self): self.proc.kill() self.out,self.err=self.proc.communicate() 
+3
source

The best approach is probably to use a software null modem emulator.

You can get it from github for linux and sourceforge for windows.

On linux, it is called tty0tty, and you just type

to do

build everything. Then you will need to enter

sudo insmod module / tty0tty.ko

to install the virtual driver and

./PTS/tty0tty

to launch an application that opens you 2 virtual ports: / dev / pts / 4 and / dev / pts / 6.

Then you can open the serial port / dev / pts / 4 in your python test modules and open / dev / pts / 6 in your application.

In your python unit test, you simply enter something like:

 import serial ser = serial.Serial('/dev/pts/4', 19200) 
0
source

All Articles