Dennis, my english is not too good...
About first question this is the implementation of SER class (this is
freeware from the source
www.telit.it):
#Telit Extensions
#
#Copyright © 2004, DAI Telecom S.p.A.
#All rights reserved.
#
#Redistribution and use in source and binary forms, with or without
#modification, are permitted provided that the following conditions
#are met:
#
#Redistribution s of source code must retain the above copyright notice,
#this list of conditions and the following disclaimer.
#
#Redistribution s in binary form must reproduce the above copyright
#notice, this list of conditions and the following disclaimer in
#the documentation and/or other materials provided with the
distribution.
#
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS
#IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
#TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
#PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
#CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
#EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
#PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
#LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
#NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
import time
import serial
freeser = serial.Serial(' COM4', 9600, timeout=0, rtscts=1)
def send(string):
global freeser
freeser.write(s tring)
result = 1
return result
def receive(timeout ):
global freeser
sectimeout = int((timeout+5)/10)
time.sleep(sect imeout)
string = freeser.read(51 2)
return string
def read():
global freeser
string = freeser.read(51 2)
return string
def sendbyte(byte):
global freeser
string = chr(byte)
freeser.write(s tring)
result = 1
return result
def receivebyte(tim eout):
global freeser
sectimeout = int((timeout+5)/10)
time.sleep(sect imeout)
string = freeser.read(1)
if string == '':
result = -1
else:
result = ord(string)
return result
def readbyte():
global freeser
string = freeser.read(1)
if string == '':
result = -1
else:
result = ord(string)
return result
def SetSpeed(speed) :
global freeser
if speed == '9600':
freeser.setBaud rate(9600)
result = 1
elif speed == '19200':
freeser.setBaud rate(19200)
result = 1
elif speed == '115200':
freeser.setBaud rate(115200)
result = 1
else:
result = -1
return result
This is the serial class
class Serial(serialut il.FileLike):
def __init__(self,
port, #number of device, numbering
starts at
#zero. if everything fails, the
user
#can specify a device string,
note
#that this isn't portable
anymore
baudrate=9600, #baudrate
bytesize=EIGHTB ITS, #number of databits
parity=PARITY_N ONE, #enable parity checking
stopbits=STOPBI TS_ONE, #number of stopbits
timeout=None, #set a timeout value, None to
wait forever
xonxoff=0, #enable software flow control
rtscts=0, #enable RTS/CTS flow control
):
"""initiali ze comm port"""
self.timeout = timeout
if type(port) == type(''): #strings are taken directly
self.portstr = port
else:
self.portstr = device(port)
try:
self.hComPort = win32file.Creat eFile(self.port str,
win32con.GENERI C_READ | win32con.GENERI C_WRITE,
0, # exclusive access
None, # no security
win32con.OPEN_E XISTING,
win32con.FILE_A TTRIBUTE_NORMAL |
win32con.FILE_F LAG_OVERLAPPED,
None)
except Exception, msg:
self.hComPort = None #'cause __del__ is called anyway
raise serialutil.Seri alException, "could not open port: %s"
% msg
# Setup a 4k buffer
win32file.Setup Comm(self.hComP ort, 4096, 4096)
#Save original timeout values:
self.orgTimeout s = win32file.GetCo mmTimeouts(self .hComPort)
#Set Windows timeout values
#timeouts is a tuple with the following items:
#(ReadIntervalT imeout,ReadTota lTimeoutMultipl ier,
# ReadTotalTimeou tConstant,Write TotalTimeoutMul tiplier,
# WriteTotalTimeo utConstant)
if timeout is None:
timeouts = (0, 0, 0, 0, 0)
elif timeout == 0:
timeouts = (win32con.MAXDW ORD, 0, 0, 0, 0)
else:
#timeouts = (0, 0, 0, 0, 0) #timeouts are done with
WaitForSingleOb ject
timeouts = (0, 0, int(timeout*100 0), 0, 0)
win32file.SetCo mmTimeouts(self .hComPort, timeouts)
#win32file.SetC ommMask(self.hC omPort, win32file.EV_RX CHAR |
win32file.EV_TX EMPTY |
# win32file.EV_RX FLAG | win32file.EV_ER R)
#~ win32file.SetCo mmMask(self.hCo mPort,
#~ win32file.EV_RX CHAR | win32file.EV_RX FLAG |
win32file.EV_ER R)
win32file.SetCo mmMask(self.hCo mPort, win32file.EV_ER R)
# Setup the connection info.
# Get state and modify it:
comDCB = win32file.GetCo mmState(self.hC omPort)
comDCB.BaudRate = baudrate
if bytesize == FIVEBITS:
comDCB.ByteSize = 5
elif bytesize == SIXBITS:
comDCB.ByteSize = 6
elif bytesize == SEVENBITS:
comDCB.ByteSize = 7
elif bytesize == EIGHTBITS:
comDCB.ByteSize = 8
if parity == PARITY_NONE:
comDCB.Parity = win32file.NOPAR ITY
comDCB.fParity = 0 # Dis/Enable Parity Check
elif parity == PARITY_EVEN:
comDCB.Parity = win32file.EVENP ARITY
comDCB.fParity = 1 # Dis/Enable Parity Check
elif parity == PARITY_ODD:
comDCB.Parity = win32file.ODDPA RITY
comDCB.fParity = 1 # Dis/Enable Parity Check
if stopbits == STOPBITS_ONE:
comDCB.StopBits = win32file.ONEST OPBIT
elif stopbits == STOPBITS_TWO:
comDCB.StopBits = win32file.TWOST OPBITS
comDCB.fBinary = 1 # Enable Binary Transmission
# Char. w/ Parity-Err are replaced with 0xff (if fErrorChar is
set to TRUE)
if rtscts:
comDCB.fRtsCont rol = win32file.RTS_C ONTROL_HANDSHAK E
comDCB.fDtrCont rol = win32file.DTR_C ONTROL_HANDSHAK E
else:
comDCB.fRtsCont rol = win32file.RTS_C ONTROL_ENABLE
comDCB.fDtrCont rol = win32file.DTR_C ONTROL_ENABLE
comDCB.fOutxCts Flow = rtscts
comDCB.fOutxDsr Flow = rtscts
comDCB.fOutX = xonxoff
comDCB.fInX = xonxoff
comDCB.fNull = 0
comDCB.fErrorCh ar = 0
comDCB.fAbortOn Error = 0
win32file.SetCo mmState(self.hC omPort, comDCB)
# Clear buffers:
# Remove anything that was there
win32file.Purge Comm(self.hComP ort,
win32file.PURGE _TXCLEAR |
win32file.PURGE _TXABORT |
win32file.PURGE _RXCLEAR |
win32file.PURGE _RXABORT)
#print win32file.Clear CommError(self. hComPort) #flags, comState
=
self._overlappe dRead = win32file.OVERL APPED()
self._overlappe dRead.hEvent = win32event.Crea teEvent(None, 1,
0, None)
self._overlappe dWrite = win32file.OVERL APPED()
self._overlappe dWrite.hEvent = win32event.Crea teEvent(None, 0,
0, None)
def __del__(self):
self.close()
def close(self):
"""close port"""
if self.hComPort:
#Restore original timeout values:
win32file.SetCo mmTimeouts(self .hComPort, self.orgTimeout s)
#Close COM-Port:
win32file.Close Handle(self.hCo mPort)
self.hComPort = None
def setBaudrate(sel f, baudrate):
"""change baudrate after port is open"""
if not self.hComPort: raise portNotOpenErro r
# Setup the connection info.
# Get state and modify it:
comDCB = win32file.GetCo mmState(self.hC omPort)
comDCB.BaudRate = baudrate
win32file.SetCo mmState(self.hC omPort, comDCB)
def inWaiting(self) :
"""returns the number of bytes waiting to be read"""
flags, comstat = win32file.Clear CommError(self. hComPort)
return comstat.cbInQue
def read(self, size=1):
"""read num bytes from serial port"""
if not self.hComPort: raise portNotOpenErro r
if size > 0:
win32event.Rese tEvent(self._ov erlappedRead.hE vent)
flags, comstat = win32file.Clear CommError(self. hComPort)
if self.timeout == 0:
n = min(comstat.cbI nQue, size)
if n > 0:
rc, buf = win32file.ReadF ile(self.hComPo rt,
win32file.Alloc ateReadBuffer(n ), self._overlappe dRead)
win32event.Wait ForSingleObject (self._overlapp edRead.hEvent,
win32event.INFI NITE)
read = str(buf)
else:
read = ''
else:
rc, buf = win32file.ReadF ile(self.hComPo rt,
win32file.Alloc ateReadBuffer(s ize), self._overlappe dRead)
n = win32file.GetOv erlappedResult( self.hComPort,
self._overlappe dRead, 1)
read = str(buf[:n])
else:
read = ''
return read
def write(self, s):
"""write string to serial port"""
if not self.hComPort: raise portNotOpenErro r
#print repr(s),
if s:
err, n = win32file.Write File(self.hComP ort, s,
self._overlappe dWrite)
if err: #will be ERROR_IO_PENDIN G:
# Wait for the write to complete.
win32event.Wait ForSingleObject (self._overlapp edWrite.hEvent,
win32event.INFI NITE)
def flushInput(self ):
if not self.hComPort: raise portNotOpenErro r
win32file.Purge Comm(self.hComP ort, win32file.PURGE _RXCLEAR |
win32file.PURGE _RXABORT)
def flushOutput(sel f):
if not self.hComPort: raise portNotOpenErro r
win32file.Purge Comm(self.hComP ort, win32file.PURGE _TXCLEAR |
win32file.PURGE _TXABORT)
def sendBreak(self) :
if not self.hComPort: raise portNotOpenErro r
import time
win32file.SetCo mmBreak(self.hC omPort)
#TODO: how to set the correct duration??
time.sleep(0.02 0)
win32file.Clear CommBreak(self. hComPort)
def setRTS(self,lev el=1):
"""set terminal status line"""
if not self.hComPort: raise portNotOpenErro r
if level:
win32file.Escap eCommFunction(s elf.hComPort,
win32file.SETRT S)
else:
win32file.Escap eCommFunction(s elf.hComPort,
win32file.CLRRT S)
def setDTR(self,lev el=1):
"""set terminal status line"""
if not self.hComPort: raise portNotOpenErro r
if level:
win32file.Escap eCommFunction(s elf.hComPort,
win32file.SETDT R)
else:
win32file.Escap eCommFunction(s elf.hComPort,
win32file.CLRDT R)
def getCTS(self):
"""read terminal status line"""
if not self.hComPort: raise portNotOpenErro r
return MS_CTS_ON & win32file.GetCo mmModemStatus(s elf.hComPort)
!= 0
def getDSR(self):
"""read terminal status line"""
if not self.hComPort: raise portNotOpenErro r
return MS_DSR_ON & win32file.GetCo mmModemStatus(s elf.hComPort)
!= 0
def getRI(self):
"""read terminal status line"""
if not self.hComPort: raise portNotOpenErro r
return MS_RING_ON & win32file.GetCo mmModemStatus(s elf.hComPort)
!= 0
def getCD(self):
"""read terminal status line"""
if not self.hComPort: raise portNotOpenErro r
return MS_RLSD_ON & win32file.GetCo mmModemStatus(s elf.hComPort)
!= 0
#Nur Testfunktion!!
if __name__ == '__main__':
print __name__
s = Serial(0)
About second question I have to read a string of data from the modem
serial port but I was using .readbyte to semplify the code. In effect
..readbyte doesn't return a string type. I repeat the test with .read
method but I discovered another strange behaviour. If the string is not
'sended' to a DTE (serial not connected ot other) the .read method read
the string(s) still not 'sent'. In other words it seems that the rx and
tx serial buffer are shared.
Thanks for your time.
Dario.