-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice.py
More file actions
71 lines (59 loc) · 1.87 KB
/
device.py
File metadata and controls
71 lines (59 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import serial
import time
import re
import time
import os
import sys
class SerialDevice(serial.Serial):
def __init__(self, tx_log = False, rx_log = False, *args, **kwargs):
self.tx_log = tx_log
self.rx_log = rx_log
serial.Serial.__init__(self, *args, **kwargs)
#self.setTimeout(0.1)
#self.setWriteTimeout(1.0)
def read(self, size = 1):
data = serial.Serial.read(self, size)
if self.rx_log:
sys.stdout.write(data)
return data
def readline(self):
data = serial.Serial.readline(self)
if self.rx_log:
sys.stdout.write(data)
return data
def write(self, data):
serial.Serial.write(self, data)
if self.tx_log:
sys.stdout.write(data)
return
def trx(self, omsg, imsg, delay = 0, trx_wait = 2):
m = None
#imsg = r'[ -+]\d+\.\d+[Ee][-+][0-9]{2}'
if omsg: self.write(omsg)
start = time.clock()
line = ''
m = None
while True:
line = line + self.readline()
print(line)
#line = r'+5.0002334E-01'
if '------\r\n' in line:
line = ''
#m = _sre.SRE_Match('10.0')
break
m = re.search(imsg, line,re.M) # pause here is no result
#print(type(m))
if m:
#print(m)
break
now = time.clock()
#print(type(now - start),now - start,(now - start) > trx_wait)
if (now - start) > trx_wait:
#raise Exception("trx timeout")
print("trx timeout")
break
time.sleep(delay)
if ',' in line: unit = line.split(',')[1].strip()
return m,unit