Facebook
From Diminutive Meerkat, 5 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 189
  1. import codecs
  2. import threading
  3. from tkinter import *
  4. import serial
  5.  
  6. def GetFromQue(self): #pobieranie z kolejki
  7.     while self.RxBuf.empty() == False:
  8.         # emb.log('before GetFromQue',self.BytesInQueue)
  9.         if self.RxState == self.oenum.LOOKING_FOR_BEG:
  10.             self.RxFrame = ''
  11.             self.RxFrame = self.RxFrame + chr(self.RxBuf.get())
  12.             self.BytesInQueue = self.BytesInQueue - 1
  13.             if (ord(self.RxFrame[0]) != 0x06 and ord(self.RxFrame[0]) != 0x15 and ord(self.RxFrame[0]) != 0x02 and ord(
  14.                     self.RxFrame[0]) != 0x03 and ord(self.RxFrame[0]) != 0x3F):
  15.                 while self.RxBuf.empty() == False:  # trochę niep
  16.                     a = self.RxBuf.get()
  17.                     self.BytesInQueue = self.BytesInQueue - 1
  18.                     emb.log('?', a)
  19.                     if (a == 0x06 or a == 0x15 or a == 0x3F or a == 0x02 or a == 0x03):
  20.                         self.RxFrame = ''
  21.                         self.RxFrame = self.RxFrame + chr(a)
  22.                         break  # tuuuuuuuuuuuuuuuuuuuuuuu
  23.             if (ord(self.RxFrame[0]) == 0x06 or ord(self.RxFrame[0]) == 0x15):
  24.                 self.ReceiveFrame()
  25.                 # return
  26.             if (ord(self.RxFrame[0]) == 0x02 or ord(self.RxFrame[0]) == 0x03 or ord(self.RxFrame[0]) == 0x3F):
  27.                 self.RxState = self.oenum.LOOKING_FOR_LEN
  28.                 self.startTimer102(10)
  29.  
  30.         if self.RxState == self.oenum.LOOKING_FOR_LEN:
  31.             if self.RxBuf.empty() == False:
  32.                 self.RxFrame = self.RxFrame + chr(self.RxBuf.get())  # LEN or MS cought
  33.                 self.BytesInQueue = self.BytesInQueue - 1
  34.                 if (ord(self.RxFrame[0]) == 0x3F):
  35.                     self.RxState = self.oenum.LOOKING_FOR_BEG
  36.                     # self.startTimer102(10)
  37.                     self.stopTimer102()
  38.                     self.ReceiveFrame()
  39.                     # return
  40.                 else:  # a==0x02 or a==0x03:
  41.                     self.RxState = self.oenum.LOOKING_FOR_CC
  42.                     self.startTimer102(10)
  43.  
  44.         if self.RxState == self.oenum.LOOKING_FOR_CC:
  45.             if self.RxBuf.empty() == False:
  46.                 self.RxFrame = self.RxFrame + chr(self.RxBuf.get())  # CC cought
  47.                 self.BytesInQueue = self.BytesInQueue - 1
  48.                 if (ord(self.RxFrame[1]) != 0):
  49.                     self.RxState = self.oenum.DATA_COLLECTING
  50.                     self.startTimer102(10)
  51.                     self.Data_Collecting_i = ord(self.RxFrame[1])
  52.                 else:
  53.                     self.RxState = self.oenum.LOOKING_FOR_CHECK_1BYTE
  54.                     self.startTimer102(10)
  55.  
  56.         if self.RxState == self.oenum.DATA_COLLECTING:
  57.             while self.Data_Collecting_i:
  58.                 if self.RxBuf.empty() == False:
  59.                     self.RxFrame = self.RxFrame + chr(self.RxBuf.get())  # CC cought
  60.                     self.BytesInQueue = self.BytesInQueue - 1
  61.                     self.Data_Collecting_i = self.Data_Collecting_i - 1
  62.                     self.startTimer102(10)
  63.             # for i in range (0, ord(self.RxFrame[1])):
  64.             #    if self.RxBuf.empty()==False:
  65.             #       self.RxFrame=self.RxFrame+chr(self.RxBuf.get())# CC cought
  66.  
  67.             if ((len(self.RxFrame) - 3) == ord(self.RxFrame[1])):  # po co
  68.                 self.RxState = self.oenum.LOOKING_FOR_CHECK_1BYTE  # chyba tylko po to
  69.                 self.startTimer102(10)
  70.         if self.RxState == self.oenum.LOOKING_FOR_CHECK_1BYTE:
  71.             if self.RxBuf.empty() == False:
  72.                 self.RxFrame = self.RxFrame + chr(self.RxBuf.get())  # CHECK_1BYTE cought
  73.                 self.BytesInQueue = self.BytesInQueue - 1
  74.                 self.RxState = self.oenum.LOOKING_FOR_CHECK_2BYTE
  75.                 self.startTimer102(10)
  76.  
  77.         if self.RxState == self.oenum.LOOKING_FOR_CHECK_2BYTE:
  78.             if self.RxBuf.empty() == False:
  79.                 self.RxFrame = self.RxFrame + chr(self.RxBuf.get())  # CHECK_2BYTE cought
  80.                 self.BytesInQueue = self.BytesInQueue - 1
  81.                 CheckSum = 0x10000
  82.                 for i in range(1, 3 + ord(self.RxFrame[1])):
  83.                     CheckSum = CheckSum + ord(self.RxFrame[i])
  84.                 CheckSum = CheckSum - 0x10000
  85.                 if ((CheckSum % 256 == ord(self.RxFrame[ord(self.RxFrame[1]) + 3])) and (
  86.                         int(CheckSum / 256) == ord(self.RxFrame[ord(self.RxFrame[1]) + 4]))):
  87.                     self.stopTimer102()  # tu też co by zdazyc przetwarzac
  88.                     self.ReceiveFrame()
  89.                 else:
  90.                     emb.log('Fr Err')  # ,CheckSum%256,'i',self.RxFrame[ord(self.RxFrame[1])+3])
  91.                 self.RxState = self.oenum.LOOKING_FOR_BEG
  92.                 self.stopTimer102()
  93. global otworz
  94. def retrieve_input(): #odczyt i konwersja
  95.     global ascii_to_hex
  96.     inputValue = textBox.get("1.0","end-1c")
  97.     asci=(inputValue)
  98.     try:
  99.         #print(bytearray.fromhex(asci).decode())
  100.         ascii_to_hex=str((codecs.encode(asci.encode("ascii"), "hex")))
  101.         print(ascii_to_hex[1:])
  102.  
  103.  
  104.     except:
  105.         print("To nie jest kod hexadecymalny")
  106.  
  107. def serial_ports(): #Wyswietlanie dostepnych portow
  108.     global port,result
  109.     ports = ['COM%s' % (i + 1) for i in range(256)]
  110.     result = []
  111.     for port in ports:
  112.         try:
  113.             s = serial.Serial(port)
  114.             s.close()
  115.             result.append(port)
  116.         except (OSError, serial.SerialException):
  117.             pass
  118.     return result
  119.  
  120. def otworz():
  121.     global ser
  122.     try:
  123.         ser = serial.Serial(port=result[0], baudrate=57600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, timeout=2)  # otwarcie portu
  124.         if(ser.isOpen()):
  125.             ser.setRTS(True)
  126.             a=ser.inWaiting()
  127.             print("Port otwarto")
  128.  
  129.  
  130.         else:
  131.             print("Port jest już otwarty")
  132.  
  133.  
  134.     except:
  135.         print("Błąd, nie można otworzyć tego portu lub port jest już otwarty")
  136.  
  137. def read_all(port, chunk_size=200):
  138.     """Read all characters on the serial port and return them."""
  139.     if not port.timeout:
  140.         raise TypeError('Port needs to have a timeout set!')
  141.  
  142.     read_buffer = b''
  143.  
  144.     while True:
  145.         # Read in chunks. Each chunk will wait as long as specified by
  146.         # timeout. Increase chunk_size to fail quicker
  147.         byte_chunk = port.read(size=chunk_size)
  148.         read_buffer += byte_chunk
  149.         if not len(byte_chunk) == chunk_size:
  150.             break
  151.  
  152.     return read_buffer
  153.  
  154.  
  155. global zamknij
  156.  
  157.  
  158. def zamknij():
  159.     try:
  160.         if (ser.isOpen()):
  161.             ser.close()
  162.             print("Port zamknięto")
  163.  
  164.     except:
  165.         print("Port nie jest otwarty")
  166.  
  167. def worker():
  168.     print("Worker")
  169.  
  170.     threads=[]
  171.     for i in range(5):
  172.         t=threading.Thread(target=worker)
  173.         threads.append((t))
  174.         t.start()
  175.  
  176. okno = Tk()
  177.  
  178. topFrame = Frame(okno)
  179.  
  180. bottomFrame = Frame(okno)
  181.  
  182. info = Label(okno, text="")
  183. open_button = Button(okno, text="Otwórz port", fg="green", command=otworz)
  184.  
  185. close_button = Button(okno, text="Zamknij port", fg="red", command=zamknij)
  186. ports = Label(okno, text=serial_ports())
  187. textBox = Text(okno, height=1, width=50)
  188. textBox.pack()
  189.  
  190. buttonCommit=Button(okno, height=1, width=10, text="wyslij", command=lambda: retrieve_input())
  191. buttonCommit.pack()
  192. open_button.pack()
  193. close_button.pack()
  194. info.pack()
  195. #entry1.pack()
  196. variable = StringVar(okno)
  197. variable.set("Porty")
  198.  
  199. w = OptionMenu(okno, variable, serial_ports())
  200. #print(worker())
  201. w.pack()
  202.  
  203.  
  204. okno.mainloop()
  205.  
  206.