- Main
- Tutorials
- Library
- Samples

- File List
- Globals

serial_sample.py
Go to the documentation of this file.
1 '''
2 Copyright (C) 2019 Moxa Inc. All rights reserved.
3 SPDX-License-Identifier: Apache-2.0
4
5 Serial Python Sample Application
6
7 Date Author Comment
8 2019-02-11 William Chang Created it
9 2020-09-15 KevinYK Lin Modify it
10 '''
11
12
22
23 from ioThinx_4530 import ioThinx_4530_API
24 import serial
25 import argparse
26 import os
27
28 BUF_LEN = 4096
29 SERIAL_MODE_RS232 = 0
30 SERIAL_SLOT_0 = 0
31 SERIAL_PORT_0 = 0
32
33
34
35 def main():
36 parser = argparse.ArgumentParser(description="Miscellaneous sample program.")
37 parser.add_argument("-b", "--baudrate", dest="serial_baudrate", type=int, default=115200)
38 parser.add_argument("-s", "--slot", dest="serial_slot", type=int, default=SERIAL_SLOT_0)
39 parser.add_argument("-p", "--port", dest="serial_port", type=int, default=SERIAL_PORT_0)
40 parser.add_argument("-m", "--mode", dest="serial_mode", type=int, default=SERIAL_MODE_RS232)
41 args = parser.parse_args()
42
43 serial_slot = args.serial_slot
44 serial_port = args.serial_port
45 serial_mode = args.serial_mode
46 serial_baudrate = args.serial_baudrate
47 print("serial slot = {}".format(serial_slot))
48 print("serial port = {}".format(serial_port))
49 print("serial mode = {}".format(serial_mode))
50 print("serial baudrate = {}".format(serial_baudrate))
51
52 device = ioThinx_4530_API.ioThinx_4530_API()
53 ioThinx_4530_API.ioThinx_4530_API().ioThinx_Serial_SetInterface(serial_slot, serial_port, serial_mode)
54 interface =ioThinx_4530_API.ioThinx_4530_API().ioThinx_Serial_GetInterface(serial_slot, serial_port)
55 print("interface ={}".format(interface))
56 devname_array = str(ioThinx_4530_API.ioThinx_4530_API().ioThinx_Serial_GetDevName(serial_slot, serial_port))
57
58 DevName = ""
59 for ele in devname_array:
60 if(ele == "\x00"):
61 break
62 DevName += ele
63 print("DevName ={}".format(DevName))
64 # baudrate,flow control,parity,data bits,stop bit
65 # please refer to pyserial documents
66 fd = serial.Serial(DevName, serial_baudrate, timeout=1)
67
68 print("fd = {}".format(fd))
69 print("Start Serial echo.")
70
71 while True:
72 buf = fd.readline()
73 print(len(buf))
74 print(buf)
75 if len(buf) > 0:
76 fd.write(buf)
77 if 'quit' in buf.decode():
78 break
79
80 print("Stop Serial echo.")
81 fd.close()
82
83
84 if __name__ == '__main__':
85 main()
ioThinx_Serial_SetInterface
IOTHINX_ERR ioThinx_Serial_SetInterface(uint32_t slot, uint32_t port, uint32_t interface)
serial_sample.main
def main()
Definition: serial_sample.py:35
ioThinx_Serial_GetInterface
IOTHINX_ERR ioThinx_Serial_GetInterface(uint32_t slot, uint32_t port, uint32_t *interface)
ioThinx_Serial_GetDevName
IOTHINX_ERR ioThinx_Serial_GetDevName(uint32_t slot, uint32_t port, uint8_t *name)
format
char const int const cJSON_bool format
Definition: cJSON.h:161