Combined Example - Serial
This is an example for performing a self-loop-back test on RXD (#2 pin) and
TXD (#3 pin) are connected with the serial port.
Example 1 : Self-loop-back test example
# serial port open
# if D-SUB (9pin) is connected: port="COM"
# if USB is connected with USB to Serial: port="COM_USB"
ser = serial_open(port="COM_USB", baudrate=115200, bytesize=DR_EIGHTBITS, parity=DR_PARITY_NONE, stopbits=DR_STOPBITS_ONE)
wait(1)
# SEND DATA : "123ABC"
res = serial_write(ser, b"123ABC") # b means byte type
wait(1)
# READ DATA
res, rx_data = serial_read(ser)
# RXD and TXD are H/W connected res=6 (byte) rx_data = b"123ABC" are recevied
tp_popup("res ={0}, rx_data={1}".format(res, rx_data))
# close corresponding serial port
serial_close(ser)
Received data is collected as is and the result is outputted as a TP pop-up message.
If executed properly, it outputs a result of res=6 rx_data = b’123ABC’.
Example 2 : Various packet transmission examples
Transmission packet: “MEAS_START” +data1[4byte]+data2[4byte]
data1: Converts integer to 4 bytes ex) 1 → 00000001
data2: Converts integer to 4 bytes ex) 2 → 00000002
ex) In case data1=1, data2=2: “MEAS_START”+00000001+00000002
Actual Packet: 4D4541535F53544152540000000100000002
Received packet: res=18, rx_data=“MEAS_START”+00000001+00000002
Extract rxd1 : Convert 10th to 14th byte into integer
Extract rxd2 : Convert 14th to 18th byte into integer
ser = serial_open(port="COM_USB", baudrate=115200, bytesize=DR_EIGHTBITS, parity=DR_PARITY_NONE, stopbits=DR_STOPBITS_ONE)
wait(1)
send_data = b"MEAS_START" # b means byte type
data1 =1
data2 =2
send_data += (data1).to_bytes(4, byteorder='big')
send_data += (data2).to_bytes(4, byteorder='big')
# SEND DATA
res = serial_write(ser, send_data)
wait(1)
# READ DATA
# RXD, TXD are connected by H/W, so send_data is received as it is
res, rx_data = serial_read(ser)
tp_popup("res ={0}, rx_data={1}".format(res, rx_data))
rxd1 = int.from_bytes(rx_data[10:10+4], byteorder='big', signed=True)
rxd2 = int.from_bytes(rx_data[14:14+4], byteorder='big', signed=True)
tp_popup("res={0}, rxd1={1}, rxd2={2}".format(res, rxd1, rxd2))
#Close the serial port
serial_close(ser)
Connect the USB to serial device to the USB port and send byte type send_data.
Since RXD(2pin) and TXD(3pin) are connected to receive the transmitted data as it is,
res = 18, rx_data has the same packet as send_data.
Extract rxd1 : Convert 10th to 14th byte into integer
Extract rxd2 : Convert 14th to 18th byte into integer
The end result will be res=18, rxd1=1, rxd2=2