simple_client 1.33 KB
#!/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()