-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathser2ppm.py
65 lines (49 loc) · 1.26 KB
/
ser2ppm.py
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
# simple test program
# generating packets
# for the arduino board
# specify port number and a series of channel values
# for ppm output
import sys, serial
import time
BAUDRATE = 115200
# serial port number
# on windows python port numbering starts from 0: COM68 -> 67
# on linux: /dev/ttyUSB0
if (len(sys.argv) < 2):
print 'usage: python',sys.argv[0],'/dev/ttyUSB0 1500 1000'
exit(1)
port = int(sys.argv[1])
channels = sys.argv[2:]
count = 5 + len(channels) * 2
packet = bytearray()
packet.append(0x01) # start byte
packet.append(count) # length
packet.append(0x01) # type
try: # test for string to int cast
for ch in channels:
ch = int(ch)
b = (ch & 0x000000ff)
packet.append(b)
b = (ch & 0x0000ff00) >> 8
packet.append(b)
except Exception, e:
print e
exit(1)
checksum = 0
for p in packet:
checksum += p
packet.append(checksum & 0x000000FF) # leave only lower checksum byte
packet.append(0x04) # end byte
for b in packet:
sys.stdout.write(format(b, '02x'))
sys.stdout.write(' ')
try:
ser = serial.Serial(port, BAUDRATE)
ser.flushInput()
#ser = serial.Serial(port, BAUDRATE, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=0)
except Exception, e:
print e
exit(1)
ser.write(packet)
ser.flush()
ser.close()