'''
example name：Serial communication
version：v1.0
date：2020.3
Author：01Studio
Forum: forum.01studio.org
Description: Serial send and receive message
'''

import serial
import time

# initialize serial
com = serial.Serial("/dev/ttyS0", 115200)

#send message
com.write(b'Hello 01Studio!')

while True:
    
    # get the buf number of character (int type)
    count = com.inWaiting()
    
    if count != 0:
        # read and print
        recv = com.read(count)
        print(recv)
        
        #send the message back
        com.write(recv)
        
        # clean buf
        com.flushInput()
        
    # delay 100ms
    time.sleep(0.1)