simple_client
1.33 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
#!/usr/bin/env python3
import serial
import select
import sys
if len(sys.argv) != 2:
print ("Correct usage: script, serial_port")
exit()
port = str(sys.argv[1])
scom = serial.Serial(port=port,
baudrate=115200,
timeout=1,
xonxoff=False,
rtscts=False,
dsrdtr=True)
# maintains a list of possible input streams
stream_list = [sys.stdin, scom]
while True:
""" There are two possible input situations. Either the
user wants to give manual input to send to other people,
or the server is sending a message to be printed on the
screen. Select returns from sockets_list, the stream that
is reader for input. So for example, if the server wants
to send a message, then the if condition will hold true
below.If the user wants to send a message, the else
condition will evaluate as true"""
read_fd, write_fd, error_fd = select.select(stream_list,[],[])
for fd in read_fd:
if fd == scom:
message = scom.read(2048).decode('utf8')
print("Receive:" + message)
else:
message = sys.stdin.readline()
scom.write(message.encode('utf8'))
sys.stdout.write("<You>")
sys.stdout.write(message)
sys.stdout.flush()
scom.close()