From cca17d4428aedcafb1a7d1582041462cd6dac45e Mon Sep 17 00:00:00 2001 From: Mustafa Sakarya Date: Sat, 7 Aug 2010 03:16:58 +0300 Subject: [PATCH] Serial port data integrity Incoming and outgoing data is packaged --- gui/com.py | 198 +++++++++++++++++++++++++++++++++++++++------------------- gui/gui.glade | 38 ++++++++++- gui/main.py | 33 ++++++++-- 3 files changed, 198 insertions(+), 71 deletions(-) rewrite gui/com.py (64%) diff --git a/gui/com.py b/gui/com.py dissimilarity index 64% index 7910404..840761f 100755 --- a/gui/com.py +++ b/gui/com.py @@ -1,65 +1,133 @@ -from bzrlib.doc_generate.autodoc_bash_completion import preamble -#!/usr/bin/env python -try: - import serial -except: - print("Please install PySerial\n") - print("http://pyserial.sourceforge.net/index.html \n") - -class COM: - def __init__(self,read_cbf=None): - self.serial=serial.Serial() - self.port="/dev/ttyUSB0" - self.baudrate=115200 - - self.read_cbf=read_cbf - self.preamble=[0x55,0xAA] - self.address=[73] - def connect(self): - print("Connecting to "+self.port) - self.serial.port=self.port - self.serial.baudrate=self.baudrate - try: - self.serial.open() - except: - print("Can't open serial port") - def disconnect(self): - print("Closing "+self.port) - if self.serial.isOpen(): - self.serial.close() - def write(self,data): - txpack=self.pack(data[:]) - if self.serial.isOpen(): - self.serial.write(txpack) - else: - print(txpack) - print(self.depack(txpack)) - def pack(self,data=None): - size=[len(data)] - sum=0 - for d in data: - sum=(sum+d)%256 - sum=[256-sum] - txpack=self.preamble[:]+self.address[:]+size[:]+data[:]+sum[:] - return txpack[:] - def depack(self,data=None): - pack_size=len(data) - rxdata=None - if(pack_size>5): - preamble=data[0:2] - address=data[2:3] - if((preamble==self.preamble)and(address==self.address)): - size=data[3] - if (pack_size==(size+5)): - sum=0 - rxdata=data[4:4+size] - for d in rxdata: - sum=(sum+d)%256 - sum=256-sum - if(sum!=data[pack_size-1]): - rxdata=None - - return rxdata - -if __name__=="__main__": - com=COM() +#!/usr/bin/env python +try: + import serial +except: + print("Please install PySerial") + print("http://pyserial.sourceforge.net/index.html") +import time +import threading +from collections import deque + +class TTimer(threading.Thread): + def __init__(self, func,interval=1): + threading.Thread.__init__(self) + self.func=func + self.interval=interval + self.enabled=False + self.cnt=0 + def run(self): + while 1: + if self.enabled: + self.func(self) + if time: + time.sleep(self.interval) + def kill(self): + self.enabled=False + self._Thread__stop() + +class COM: + def __init__(self,read_cbf=None): + self.serial=serial.Serial(timeout=0) + self.port="/dev/ttyUSB0" + self.baudrate=57600 + self.read_cbf=read_cbf + self.preamble=['A','U'] + self.rxbuff=deque() + self.timer=TTimer(self.t1f,0.1) + self.timer.start() + def connect(self): + print("Connecting to "+self.port) + ser=self.serial + self.serial.port=self.port + self.serial.baudrate=self.baudrate + try: + self.serial.open() + except: + print("Can't open serial port") + if ser.isOpen(): + self.timer.enabled=True + ser.setRTS(0) + ser.setDTR(0) + self.rxstep=0 + self.rxcnt=0 + self.nsum=0 + self.rxsize=0 + def disconnect(self): + print("Closing "+self.port) + if self.serial.isOpen(): + self.serial.close() + self.timer.enabled=False + def t1f(self,kendi): #poll rx data + rxbuff=self.rxbuff + rxdata=self.serial.readline() + for d in rxdata: + rxbuff.appendleft(d) + length=len(rxbuff) + if length>4: #enough data available? + self.depack() + def depack(self): + rxbuff=self.rxbuff + step=self.rxstep + cnt=self.rxcnt + nsum=self.nsum + size=self.rxsize + pr=self.preamble + while((step<3 and len(rxbuff)>0) or(step==3 and len(rxbuff)>size)): + if(step==0): + temp=rxbuff.pop() + if(temp==pr[0]): + step=step+1 + elif (step==1): + temp=rxbuff.pop() + if(temp==pr[1]): + step=step+1 + else: + step=0 + elif (step==2): + temp=ord(rxbuff.pop()) + size=temp -ord('A') #********* + if(size<64): + step=step+1 + else: + step=0 + cnt=0 + elif (step==3): + if(size==cnt): + temp=ord(rxbuff[-size-1]) + if(temp==(255-nsum)): + tempbuff=[] + for i in range(size): + tempbuff.append(rxbuff.pop()) + self.read_cbf(tempbuff) + step=0 + nsum=0 + cnt=0 + else: + nsum = (nsum+ord(rxbuff[-cnt-1]))%256 + cnt=cnt+1 + def write(self,data): + if(isinstance(data,str)): + l=[] + for d in data: + l.append(ord(d)) + data=l[:] + txpack=self.pack(data[:]) + if self.serial.isOpen(): + self.serial.write(str(bytearray(txpack))) + else: + for d in txpack: + self.rxbuff.appendleft(d) + print(txpack) + self.depack() + def pack(self,data=None): + size=[len(data)+ord('A')] #****** + sum=0 + for d in data: + sum=(sum+d)%256 + sum=[255-sum] + txpack=self.preamble[:]+size[:]+data[:]+sum[:] + return txpack[:] + def close(self): + self.timer.kill() +if __name__=="__main__": + com=COM() diff --git a/gui/gui.glade b/gui/gui.glade index c2234a3..f08c960 100644 --- a/gui/gui.glade +++ b/gui/gui.glade @@ -138,10 +138,43 @@ - + + True + True + automatic + automatic + + + 200 + 200 + True + True + console_text + + + + + 1 + 2 + 2 + 3 + GTK_FILL + GTK_FILL + - + + True + label + + + 1 + 2 + 1 + 2 + GTK_FILL + GTK_FILL + @@ -416,4 +449,5 @@ + diff --git a/gui/main.py b/gui/main.py index 9fafea4..2f9606c 100755 --- a/gui/main.py +++ b/gui/main.py @@ -4,6 +4,7 @@ pygtk.require("2.0") import sys import gtk from com import COM +import re class Main: def __init__(self): @@ -17,14 +18,29 @@ class Main: self.pd_entry=builder.get_object("pd_entry") self.pd_cb=builder.get_object("pd_cb") self.pd_cb_liststore=builder.get_object("pd_cb_liststore") - + self.l_message=builder.get_object("l_message") + self.statusbar=builder.get_object("statusbar1") + self.console_view=builder.get_object("console_view") + self.console_text=builder.get_object("console_text") builder.connect_signals(self) - self.com=COM() + self.com=COM(read_cbf=self.read_cbf) self.tbconnect.set_active(True) self.pd_init() - def on_btsend_clicked(self, widget, data=None): - self.com.write([1,2,3]) + def read_cbf(self,data): + if(data[0]=='s'): + if(len(data)>12): + #self.l_message.set_text(''.join(data)) + str=''.join(data) + str=re.split('[^0-9]', str) + self.statusbar.push(0,''.join(str)+' seconds') + if(data[0]=='$'): + str=''.join(data) + ct=self.console_text + enditer = ct.get_end_iter() + ct.insert(enditer, str) + def on_btsend_clicked(self, widget, data=None): + self.com.write("id?\r") def on_tbconnect_clicked(self, widget, data=None): if widget.get_active(): self.com.connect() @@ -56,6 +72,10 @@ class Main: def on_pd_close_bt_clicked(self, widget, data=None): self.preferences_dialog.hide() def on_window1_destroy(self, widget, data=None): + try: + self.com.close() + except: + a=0 gtk.main_quit() def on_aboutdialog1_show(self, widget, data=None): self.about.show() @@ -94,6 +114,11 @@ class Configure: conf=Configure() if __name__=="__main__": + gtk.gdk.threads_init() app=Main() app.window.show() + gtk.gdk.threads_enter() gtk.main() + gtk.gdk.threads_leave() + #gtk.threads_enter() + #gtk.threads_leave() -- 2.11.4.GIT