# Does anyone know how to install the PySerial module onto a ConnectPort  TS?

**URL:** <https://forums.digi.com/t/does-anyone-know-how-to-install-the-pyserial-module-onto-a-connectport-ts/12732>\
**Category:** Python\
**Tags:** serial-port, gateway, python\
**Created:** [March 27, 2014, 2:41pm UTC](https://forums.digi.com/t/does-anyone-know-how-to-install-the-pyserial-module-onto-a-connectport-ts/12732 "2014-03-27T14:41:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fayezbee](https://avatars.discourse-cdn.com/v4/letter/f/a5b964/32.png) [@Fayezbee](https://forums.digi.com/u/Fayezbee)\
**Post date:** [March 27, 2014, 2:41pm UTC](https://forums.digi.com/t/does-anyone-know-how-to-install-the-pyserial-module-onto-a-connectport-ts/12732/1 "2014-03-27T14:41:15Z")

</div>

Hi there, I’ve been trying to run a Python script to read data from a serial port of a Digi ConnectPort TS1W MEI. I keep getting an error saying the serial module is not found. Can anyone advise as to what I should do?

---

<div class="post-metadata">

**Author:** ![Ambes](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@Ambes](https://forums.digi.com/u/Ambes)\
**Post date:** [March 28, 2014, 8:33am UTC](https://forums.digi.com/t/does-anyone-know-how-to-install-the-pyserial-module-onto-a-connectport-ts/12732/2 "2014-03-28T08:33:19Z")

</div>

Hi I am also looking for similar solution.  
However I used simple os.open command to open the serial port and then os.write and os read … to write/read. the main limitation (atleast i couldnot resolve yet) is time out. If it get no connection on the other end it hangs out for ever…  
sample of my code:

import time  
import os, sys  
#import termios

#####Define it as class #############################################  
sser = 0  
class ConnectSerialToGps:  
#####FUNCTIONS#############################################  
#dgpslog(‘INFO’, 'GPS-SERIAL serialGPS.ConnectSerialToGps called ')  
def **init** (self,serialPort,minstolog,localdirpath):  
self.serialPort = serialPort  
self.minstolog = minstolog  
self.localdirpath = localdirpath  
def open\_serial\_port(self):  
try:  
global sser  
sser = open(self.serialPort, ‘r’)  
#print “termios =”, termios.tcgetattr(sser)  
return True  
#sser.close()  
except:  
return False  
print("Unexpected error checking termios: ", sys.exc\_info()[0])

```
def close_serial_port(self):
    global sser
    try:
        sser.close()
        print("serial port clsoed ... ")
    except:
        print("Unexpected error closing serial port: ", sys.exc_info()[0])
        

def collect_data_for_x_mins(self):
    try:
        global sser
        nowyyyymmddhhmm = self.now_ymdhmin_string()
        textfilepath = self.localdirpath + nowyyyymmddhhmm
        text_file = open(textfilepath, 'w')            
        nowmins = (time.localtime().tm_hour * 60 + time.localtime().tm_min)
        try:
                while (time.localtime().tm_hour * 60 + time.localtime().tm_min) &lt; (nowmins + self.minstolog):
                    print "Writing one batch started 1440 bits"
                    text_file.write(sser.read(1440)) #115200bits
                    #text_file.write(sser.read(32768)) #taken from RTKlib 32768
                text_file.close() #close the serial connection and text file
                print "Writing to a file completed"
        except:
                text_file.close() #close the serial connection and text file
                print("Unexpected writing to a file: ", sys.exc_info()[0])
            
    except:
        print("Unexpected error writing to a file ", sys.exc_info()[0])

def now_ymdhmin_string(self):
    #change this day to string of yyyymmddhhmm format
    if time.localtime().tm_hour &lt; 10:
            nowhr = '0' + str(time.localtime().tm_hour)
    else:
            nowhr = str(time.localtime().tm_hour)
    if time.localtime().tm_min &lt; 10:
            nowmin = '0' + str(time.localtime().tm_min)
    else:
            nowmin = str(time.localtime().tm_min)
    return self.now_ymd_strig() + nowhr + nowmin
    
def now_ymd_strig(self):
    #change today to string of yyyymmdd form
    nowyear = str(time.localtime().tm_year)
    if time.localtime().tm_mon &lt; 10:
            nowmon = '0' + str(time.localtime().tm_mon)
    else:
            nowmon = str(time.localtime().tm_mon)
         
    if time.localtime().tm_mday &lt; 10:
            nowday = '0' + str(time.localtime().tm_mday)
    else:
            nowday = str(time.localtime().tm_mday)
    return nowmon + nowday

```

pdp=‘ASY/00’  
locdirpath = ‘/usb/’

d

minutestolog = 2

srgps = ConnectSerialToGps(pdp,minutestolog,locdirpath)

if srgps.open\_serial\_port():  
print(“Port “, pdp,” is Open: “)  
srgps.collect\_data\_for\_x\_mins()  
srgps.close\_serial\_port()  
else:  
print(“Serial port”, pdp,” not open”)  
[/color]

---

<div class="post-metadata">

**Author:** ![jgrano](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@jgrano](https://forums.digi.com/u/jgrano)\
**Post date:** [July 31, 2014, 11:16pm UTC](https://forums.digi.com/t/does-anyone-know-how-to-install-the-pyserial-module-onto-a-connectport-ts/12732/3 "2014-07-31T23:16:33Z")

</div>

I went a round about way to get serial read/write capabilities on a connectport x4. I used the Digi ESP program to write the serial\_loopback example to the router and pulled the library from there.

My code is a littler different but you should be able to modify it. I’m taking data from a remote AIO device and trying to write the data to the serial port on the connectport which will be attached to an arduino. CRC implantation is next on my list.

import sys, os  
sys.path.insert(0, os.path.join(os.path.abspath(‘.’), ‘\_serial\_loopback-1.zip’))  
sys.path.insert(0, os.path.join(os.path.abspath(‘.’), ‘dpdebug.zip’))

import serial  
#import termios  
from xbee import ddo\_get\_param, ddo\_set\_param  
from io\_sample import parse\_is

s = serial.Serial(‘/com/0’, 9600, timeout = 10)

addr = ‘00:13:a2:00:40:a8:ae:88!’

##Enabling Ten volt mode on terminals 1 and 2  
ddo\_set\_param(addr, ‘D8’, 4)  
ddo\_set\_param(addr, ‘D4’, 5)  
ddo\_set\_param(addr, ‘D6’, 5)

##Enabling terminals 1 and 2 for analog input  
ddo\_set\_param(addr, ‘D0’, 2)  
ddo\_set\_param(addr, ‘D1’, 2)

##Applying changes and writing to flash  
ddo\_set\_param(addr, ‘AC’)  
ddo\_set\_param(addr, ‘WR’)

##Taking a sensor sample, printing the raw binary string to STDOUT  
sample = ddo\_get\_param(addr, ‘IS’)

print ‘sample(%d)’ % len(sample),  
for by in sample:  
print ‘%02X’ % ord(by),

print

d = parse\_is(sample)

s.write (“sample”)

print "ADO = ", (d[‘AD0’]/1023.0_10), “V” #Value has calibration factor from Excel change with every unit  
 #print "AD1 = ", d[‘AD1’]/1023.0_10, “V” (1-0.102)  
#print "AD2 = ", d[‘AD2’]/1023.0_10, “V”  
 #print "AD3 = ", d[‘AD3’]/1023.0_10, “V”

s.close()

---

<div class="post-metadata">

**Author:** ![jrussell](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jrussell](https://forums.digi.com/u/jrussell)\
**Post date:** [January 26, 2015, 10:36pm UTC](https://forums.digi.com/t/does-anyone-know-how-to-install-the-pyserial-module-onto-a-connectport-ts/12732/4 "2015-01-26T22:36:52Z")

</div>

@Ambes, why did you use sser = open(self.serialPort, ‘r’) instead of os.open?
