How to control DCR/RTC/CTS of the serial port using python on CPX4 ?

Hi All,

I am trying to write a python script that is controlling serial port on ConnectPort X4. I configured the serial to act as a realport. Can connect, read and write to the serial. At the moment I am using standard ‘os’ module to open/write/read serial port.

Now I want to control DCR or RTC or CTS line. Is there an easy way to do so? Did anybody try it before?

Can I use pyserial? Would the embeded python on X4 support the pyserial?

Any advice would be appreciated.

Many thanks,
Chris

There is also this page on Digi’s wiki:

http://www.digi.com/wiki/developer/index.php/Connect_Port_Serial_Port_Access

Hi Chris,

You should use termios module.

import termios

tcattrs = termios.tcgetattr(serialfd)
tcattrs[2] |= termios.CRTSCTS

Please, let me know if you have any question.

Thanks.

Best regards,


Miguel Ángel Pérez Ruiz

If the desire is to individually control output signals (as opposed to enabling flow control), the functions “tcgetstats” and “tcsetsignals” in the termios module are appropriate.

Note that the device is a DTE only, so RTS and DTR are the only available output signals (and thus controllable). Signals such as DCD, DSR, and CTS are input only.

Thanks for both of you for the replies.
This might help me a bit. Let me try it and I will let you know if I succeed.

Thanks,
Chris

Hi Lynn,

I was on that page. We had a wee problem with the external device.

Now, I am facing a problem how to control RTS from the python script? The rtstoggle state is on, so in some way I should be able to access the RTS but how?

Another question is, how to use pyserial on CPX4?

Thanks,
Chris

Ultimately the interface for the port is defined (limited) by the underlying RTOS driver model exposed, and Digi’s Python makes it look like a UNIX file. So pyserial would need to be ported/customized to run on the Digi, and although that has been discussed, it is not planned.

You must turn OFF hardware flow control (would be on the Web UI page). rtstoggle should not be on, or that will block your access to RTS. Although I haven’t tried it yet, you should be able to just use the MS_RTS constant and tssetsignals() to raise or lower RTS.

Ultimately any RS-232 ‘expert’ is wise to have a volt-meter on hand and learn how to measure the signals directly - it can even tell you which ports are DTE verse DCE by which pins are driven to +/-V (are transmitters) and which float (are receivers).

Thanks Lynn. This clarifies a lot of things for me.

Regards,
Chris

I finally get it running. For those who are interested I included a code snippet (unfortunate indentation is removed) and corresponding output.

Code

import os, termios
import time

serialport = ‘/com/0’
serialfd = ‘’

def checkIfConsIsSet(stats, const, cname):
if stats & const:
print ‘%s is set’ % cname
return True
else:
print ‘%s is NOT set’ % cname
return False

try:
print “Openning serial port…”
serialfd = os.open(serialport, os.O_RDWR | os.O_NONBLOCK)
if serialfd:
print "Serial port settings: ", termios.tcgetattr(serialfd)
print “Constants: MS_DCD=%d, MS_RTS=%d” % (termios.MS_DCD, termios.MS_RTS)
# Getting serial stats
estat, mstat = termios.tcgetstats(serialfd)
print "1 termios stats: ", (estat, mstat)
checkIfConsIsSet(mstat, termios.MS_RTS, ‘MS_RTS’)
print ‘>>> Toggling MS_RTS’
termios.tcsetsignals(serialfd, termios.MS_RTS)
estat, mstat = termios.tcgetstats(serialfd)
print "2. termios stats, mstat: ", (estat, mstat)
checkIfConsIsSet(mstat, termios.MS_RTS, ‘MS_RTS’)
print ‘Waiting 5 secs…’
time.sleep(5)
print ‘>>> Reseting the MS_RTS…’
tmp = mstat & ( not termios.MS_RTS )
termios.tcsetsignals(serialfd, tmp)
estat, mstat = termios.tcgetstats(serialfd)
print "3. termios stats: ", (estat, mstat)
checkIfConsIsSet(mstat, termios.MS_RTS, ‘MS_RTS’)
print “Closing serial port…”
os.close(serialfd)
except:
print “Error: Problem occured while openning socket”

Output

Openning serial port…
Serial port settings: [0, 0, 48, 0, 115200, 115200, [‘\x11’, ‘\x13’, ‘\x00’, ‘\x00’, ‘\x00’, ‘\x00’, ‘\x00’, ‘\x00’, ‘\x00’]]
Constants: MS_DCD=128, MS_RTS=2
1 termios stats: (0, 128)
MS_RTS is NOT set
>>> Toggling MS_RTS
2. termios stats, mstat: (0, 130)
MS_RTS is set
Waiting 5 secs…
>>> Reseting the MS_RTS…
3. termios stats: (0, 128)
MS_RTS is NOT set
Closing serial port…

This code is controlling the RTS. Please also refer to [1].

Many thanks again for the help.
Chris

[1] http://www.digi.com/wiki/developer/index.php/Digi_Python_Programmer's_Guide#termios