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?
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) < (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 < 10:
nowhr = '0' + str(time.localtime().tm_hour)
else:
nowhr = str(time.localtime().tm_hour)
if time.localtime().tm_min < 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 < 10:
nowmon = '0' + str(time.localtime().tm_mon)
else:
nowmon = str(time.localtime().tm_mon)
if time.localtime().tm_mday < 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]
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),
d = parse_is(sample)
s.write (“sample”)
print "ADO = ", (d[‘AD0’]/1023.010), “V” #Value has calibration factor from Excel change with every unit
#print "AD1 = ", d[‘AD1’]/1023.010, “V” (1-0.102)
#print "AD2 = ", d[‘AD2’]/1023.010, “V”
#print "AD3 = ", d[‘AD3’]/1023.010, “V”
s.close()
@Ambes, why did you use sser = open(self.serialPort, ‘r’) instead of os.open?