Serial Connector Software Interaction

Suppose you want to offer an interface for other programmers, which allows them to write code like this:

# connect to remote Linux device >>> conn = myClass('/dev/ttyUSB0', 115200, '8N1') >>> conn.login('myname', 'mypass') >>> output = conn.command('ls -al') >>> print output total 3 drwxr-xr-x 49 myname myname 4096 Jun 21 15:13 . drwxr-xr-x 4 root root 4096 Mar 20 14:43 .. drwxr-xr-x 49 myname myname 1005 Jun 14 11:23 .vimrc >>> output2 = conn.command('cd ..') >>> print output2 >>> 

How could you implement it?

Current state

I first thought of pyserial , but it seems to treat the serial connection just as a file, similar to an object, and not like a terminal. I found from it the source code that pyserial itself uses termios , which at least seems to allow some terminal configuration options. But what structure allows terminal-like IO? I’m just a newbie in the whole world of embedded systems in general, but so far it seems to me that an IO terminal through a serial connection should be a normal everyday problem in this environment, and there should already be a structure that performs β€œhard work.” But so far then I could not find him.

Background

Currently, most people in my company are checking their topics for developing an embedded system manually. But we want to switch to a more automatic script with a lot of unittest like scripts. Since we already have an interface like UART for our embedded systems, I would like to give the authors of these test scripts the opportunity to write code more intuitively, since in any case they could interact with devices through a minicomputer or screen.

+8
source share
2 answers

I would strongly consider using Twisted and projects using Twisted for Python projects talking to terminals. I saw a terminal screen scraper written in Twisted, and at least one telnet public client on GitHub - https://github.com/fjogstad/twisted-telnet-client .

I'm not sure what terminal interface you are trying to talk to, but at least here is an example of a terminal emulator in Twisted: https://launchpad.net/python-tvi955 .

The good thing about using an asynchronous structure like Twisted would be that you could test one test server at the same time on multiple virtual machines or physical machines.

+2
source

DO NOT REQUEST A SPECIAL QUESTION, but most likely you found it on Google to get the following:

If you really want to interact with the serial i / f interface , try using: pyserial ( pip install pyserial ):

here is an example :

 import serial ser = serial.Serial( port='/dev/ttyUSB1', baudrate=9600, parity=serial.PARITY_ODD, stopbits=serial.STOPBITS_TWO, bytesize=serial.SEVENBITS ) ser.open() ser.write("bla bla" + '\r') 
-one
source

All Articles