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

**URL:** <https://forums.digi.com/t/how-to-control-dcr-rtc-cts-of-the-serial-port-using-python-on-cpx4/2637>\
**Category:** Python\
**Created:** [July 24, 2009, 1:15pm UTC](https://forums.digi.com/t/how-to-control-dcr-rtc-cts-of-the-serial-port-using-python-on-cpx4/2637 "2009-07-24T13:15:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris.r](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@chris.r](https://forums.digi.com/u/chris.r)\
**Post date:** [July 24, 2009, 1:15pm UTC](https://forums.digi.com/t/how-to-control-dcr-rtc-cts-of-the-serial-port-using-python-on-cpx4/2637/1 "2009-07-24T13:15:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lynnl](https://avatars.discourse-cdn.com/v4/letter/l/22d042/32.png) [@lynnl](https://forums.digi.com/u/lynnl)\
**Post date:** [July 24, 2009, 4:49pm UTC](https://forums.digi.com/t/how-to-control-dcr-rtc-cts-of-the-serial-port-using-python-on-cpx4/2637/2 "2009-07-24T16:49:45Z")

</div>

There is also this page on Digi’s wiki:

[http://www.digi.com/wiki/developer/index.php/Connect\_Port\_Serial\_Port\_Access](http://www.digi.com/wiki/developer/index.php/Connect_Port_Serial_Port_Access)

---

<div class="post-metadata">

**Author:** ![mperez](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@mperez](https://forums.digi.com/u/mperez)\
**Post date:** [July 24, 2009, 2:26pm UTC](https://forums.digi.com/t/how-to-control-dcr-rtc-cts-of-the-serial-port-using-python-on-cpx4/2637/3 "2009-07-24T14:26:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![52637p](https://avatars.discourse-cdn.com/v4/letter/5/c6cbf5/32.png) [@52637p](https://forums.digi.com/u/52637p)\
**Post date:** [July 24, 2009, 2:48pm UTC](https://forums.digi.com/t/how-to-control-dcr-rtc-cts-of-the-serial-port-using-python-on-cpx4/2637/4 "2009-07-24T14:48:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chris.r](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@chris.r](https://forums.digi.com/u/chris.r)\
**Post date:** [July 24, 2009, 3:14pm UTC](https://forums.digi.com/t/how-to-control-dcr-rtc-cts-of-the-serial-port-using-python-on-cpx4/2637/5 "2009-07-24T15:14:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chris.r](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@chris.r](https://forums.digi.com/u/chris.r)\
**Post date:** [July 27, 2009, 9:48am UTC](https://forums.digi.com/t/how-to-control-dcr-rtc-cts-of-the-serial-port-using-python-on-cpx4/2637/6 "2009-07-27T09:48:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lynnl](https://avatars.discourse-cdn.com/v4/letter/l/22d042/32.png) [@lynnl](https://forums.digi.com/u/lynnl)\
**Post date:** [July 27, 2009, 1:27pm UTC](https://forums.digi.com/t/how-to-control-dcr-rtc-cts-of-the-serial-port-using-python-on-cpx4/2637/7 "2009-07-27T13:27:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chris.r](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@chris.r](https://forums.digi.com/u/chris.r)\
**Post date:** [July 27, 2009, 1:32pm UTC](https://forums.digi.com/t/how-to-control-dcr-rtc-cts-of-the-serial-port-using-python-on-cpx4/2637/8 "2009-07-27T13:32:37Z")

</div>

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

Regards,  
Chris

---

<div class="post-metadata">

**Author:** ![chris.r](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@chris.r](https://forums.digi.com/u/chris.r)\
**Post date:** [July 28, 2009, 1:27pm UTC](https://forums.digi.com/t/how-to-control-dcr-rtc-cts-of-the-serial-port-using-python-on-cpx4/2637/9 "2009-07-28T13:27:48Z")

</div>

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](http://www.digi.com/wiki/developer/index.php/Digi_Python_Programmer%27s_Guide#termios)
