-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_rs232.py
44 lines (39 loc) · 988 Bytes
/
test_rs232.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
#!/usr/bin/env python3
import sys
import serial
import time
import seeed_python_rpi.core as rt
param1 = sys.argv[1]
# enable the rs232 for test
rt.rs232_or_rs485 = "RS232"
# init the serial
ser = serial.Serial(
port='/dev/ttyS0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
if param1 == "send":
counter=0
try:
print("rs232 starts now!\n")
ser.write("rs232 starts now!\n".encode())
while 1:
ser.write(("Write counter:{}\n".format(counter)).encode())
time.sleep(1)
counter += 1
except KeyboardInterrupt:
exit()
elif param1 == "receive":
try:
print("Start receiving data now!\n")
while 1:
x=ser.readline()
if x != b'':
print(x)
except KeyboardInterrupt:
exit()
else:
print('param input error,try again with send or receive')