Package: seeed-python-reterminal
Python Library for reTerminalThis is a Python library which enables you to use the onboard hardware on thereTerminalandreTerminal Bridge. Currently theaccelerometer, user LEDs, user buttons and buzzercan be accessed using this Python library on reTerminal, and thefan, RS232,  RS485, CANcan be accessed by using this Python library on reTerminal Bridge.InstallationFrom PyPITo install the latest release from PyPIsudo pip3 install seeed-python-reterminalFrom SourceTo install from source, clone this repositorygit clone https://github.com/Seeed-Studio/Seeed_Python_ReTerminalInstall the librarycd Seeed_Python_ReTerminal
sudo pip3 install .UsageUser LEDs Testimportseeed_python_reterminal.coreasrtimporttimeprint("STA ON, USR OFF")rt.sta_led=Truert.usr_led=Falsetime.sleep(1)print("STA OFF, USR ON")rt.sta_led=Falsert.usr_led=Truetime.sleep(1)print("STA RED, USR OFF")rt.sta_led_green=Falsert.sta_led_red=Truert.usr_led=Falsetime.sleep(1)print("STA OFF, USR OFF")rt.sta_led=Falsert.usr_led=FalseBuzzer Testimportseeed_python_reterminal.coreasrtimporttimeprint("BUZZER ON")rt.buzzer=Truetime.sleep(1)print("BUZZER OFF")rt.buzzer=FalseUser Buttons Testimportseeed_python_reterminal.coreasrtimportseeed_python_reterminal.buttonasrt_btndevice=rt.get_button_device()whileTrue:foreventindevice.read_loop():buttonEvent=rt_btn.ButtonEvent(event)ifbuttonEvent.name!=None:print(f"name={str(buttonEvent.name)}value={buttonEvent.value}")Accelerometer Testimportseeed_python_reterminal.coreasrtimportseeed_python_reterminal.accelerationasrt_acceldevice=rt.get_acceleration_device()whileTrue:foreventindevice.read_loop():accelEvent=rt_accel.AccelerationEvent(event)ifaccelEvent.name!=None:print(f"name={str(accelEvent.name)}value={accelEvent.value}")Accelerometer and Buttons Testimportasyncioimportseeed_python_reterminal.coreasrtimportseeed_python_reterminal.accelerationasrt_accelimportseeed_python_reterminal.buttonasrt_btnasyncdefaccel_coroutine(device):asyncforeventindevice.async_read_loop():accelEvent=rt_accel.AccelerationEvent(event)ifaccelEvent.name!=None:print(f"accel name={str(accelEvent.name)}value={accelEvent.value}")asyncdefbtn_coroutine(device):asyncforeventindevice.async_read_loop():buttonEvent=rt_btn.ButtonEvent(event)ifbuttonEvent.name!=None:print(f"name={str(buttonEvent.name)}value={buttonEvent.value}")accel_device=rt.get_acceleration_device()btn_device=rt.get_button_device()asyncio.ensure_future(accel_coroutine(accel_device))asyncio.ensure_future(btn_coroutine(btn_device))loop=asyncio.get_event_loop()loop.run_forever()The Following Test Should Work With Reterminal Bridgefan Testimportseeed_python_reterminal.coreasrtimporttimeprint("FAN ON")rt.fan=Truetime.sleep(1)print("FAN OFF")rt.fan=FalseRS232 Testimportsysimportserialimporttimeimportseeed_python_reterminal.coreasrtparam1=sys.argv[1]# enable the rs232 for testrt.rs232_or_rs485="RS232"# init the serialser=serial.Serial(port='/dev/ttyS0',baudrate=9600,parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS,timeout=1)ifparam1=="send":counter=0try:print("rs232 starts now!\n")ser.write("rs232 starts now!\n".encode())while1:ser.write(("Write counter:{}\n".format(counter)).encode())time.sleep(1)counter+=1exceptKeyboardInterrupt:exit()elifparam1=="receive":try:print("Start receiving data now!\n")while1:x=ser.readline()ifx!=b'':print(x)exceptKeyboardInterrupt:exit()else:print('param input error,try again with send or receive')Note::When we use the test script of RS232/RS485/CAN.We need to pass a parameter to them.Take the RS232 for example:python3 test_rs232.py send # test the send(TX) function of RS232
python3 test_rs232.py receive # test the receive(RX) function of RS232RS485 Testimportsysimportserialimporttimeimportseeed_python_reterminal.coreasrtparam1=sys.argv[1]# enable the rs485 for testrt.rs232_or_rs485="RS485"# init the serialser=serial.Serial(port='/dev/ttyS0',baudrate=9600,parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS,timeout=1)ifparam1=="send":counter=0# enable the rs485 for txrt.rs485_tx_rx_stat="TX"try:print("rs485 starts now!\n")ser.write("rs485 starts now!\n".encode())while1:ser.write(("Write counter:{}\n".format(counter)).encode())time.sleep(1)counter+=1exceptKeyboardInterrupt:exit()elifparam1=="receive":# enable the rs485 for rxrt.rs485_tx_rx_stat="RX"try:print("Start receiving data now!\n")while1:x=ser.readline()ifx!=b'':print(x)exceptKeyboardInterrupt:exit()else:print('param input error,try again with send or receive')CAN Test# NOTICE: please make sure you have pip3 install python-can#         before you use this test script# import the libraryimportcanimportsysimporttimeparam1=sys.argv[1]# create a bus instance# many other interfaces are supported as well (see documentation)bus=can.Bus(interface='socketcan',channel='can0',receive_own_messages=True)ifparam1=="send":# send a messagecounter=0print("can send starts now!\n")try:whileTrue:message=can.Message(arbitration_id=123,is_extended_id=True,data=[0x11,0x22,counter])bus.send(message,timeout=0.2)time.sleep(1)counter+=1exceptKeyboardInterrupt:exit()elifparam1=="receive":# iterate over received messagestry:formsginbus:print(f"{msg.arbitration_id:X}:{msg.data}")exceptKeyboardInterrupt:exit()else:print('param input error,try again with send or receive')Note:Please make sure your CAN interface is working before run this script.
If not. You will get the error log with "Network is down". And you can
enable the can with "sudo ip link set can0 up type can bitrate 500000".API Referenceusr_led: Turn on/off green USR LEDrt.usr_led=True#Turn on green USR LEDrt.usr_led=False#Turn off green USR LEDsta_led_red: Turn on/off red STA LEDrt.sta_led_red=True#Turn on red STA LEDrt.sta_led_red=False#Turn off red STA LEDsta_led_green: Turn on/off green STA LEDrt.sta_led_green=True#Turn on green STA LEDrt.sta_led_green=False#Turn off green STA LEDNote:If red STA LED is on during this time, the green STA LED will turn on over the red STA LEDsta_led: Turn on/off green STA LEDrt.sta_led=True#Turn on green STA LEDrt.sta_led=False#Turn off green STA LEDNote:If red STA LED is on during this time, the green STA LED will turn on and the red STA LED will turn offbuzzer: Turn on/off buzzerrt.buzzer=True#Turn on buzzerrt.buzzer=False#Turn off buzzerget_button_device(): Obtain information about the buttons including all the events supported by themdevice=rt.get_button_device()ButtonEvent(): Calls the ButtonEvent() and returns the EVENTbuttonEvent=rt_btn.ButtonEvent(event)get_acceleration_device(): Obtain information about the accelerometer including all the events supported by itdevice=rt.get_acceleration_device()AccelerationEvent(): Calls the AccelerationEvent() and returns the EVENTaccelEvent=rt_accel.AccelerationEvent(event)fan: Turn on/off fanrt.fan=True# Turn on fanrt.fan=False# Turn off fanrs232_or_rs485: Open the RS232 or RS485rt.rs232_or_rs485="RS232"# open the RS232rt.rs232_or_rs485="RS485"# open the RS485rs485_tx_rx_stat: Switch the function between send(TX) and receive(Rx) of RS485rt.rs485_tx_rx_stat="TX"# enable the send(TX) of RS485rt.rs485_tx_rx_stat="RX"# enable the receive(RX) of RS485