TWE-Lite serial communication app Byte mode setting (send in Python)

Introduction

I will remember the part that I stumbled upon when sending and receiving the Monowireless TWE-Lite serial communication application "Byte mode".

environment

Windows 10 MONOSTIC TWI-LITE DSP(TWELITE R)

Anconda python 3.7 pyserial

For reference, there is an example (standard mode) using TWE-Lite on this site. (Because I am using Python2, I need to be careful when handling Byte.) Agricultural IT human resources development text

Software that should be installed (confirmation of serial communication)

Teraterm Realterm (for sending and receiving binary)

Rewriting to serial communication application

Please refer to the following web page. Reference: Evaluation and development environment TWELITE STAGE (1) TWELITE STAGE-Download Twilight Stage → In my case Windows

(2) Connect the TWELITE R or MONO STICK to be rewritten to the USB port. → In the case of TWELITE R: Check the position of the yellow jumper pin near the USB terminal (Jumper 2 of the 3 pins near the USB terminal. Otherwise, power will not be supplied to the DIP and a recognition error will occur. ) Reference: TWELITE R-Twilighter

(3) Start and rewrite TWELITE_stage.exe (Windows) in the MWSTAGE folder → Select 2. App rewrite The screen moves to the TWELITE programmer. → Select 3. TWELITE APPS Build & Rewrite Select App_Uart

(4) Rewriting contents in interactive mode Return to TWELITE STAGE and select interactive mode Change Set Device ID Base unit: 121 = 0x79 Slave unit: 120 = 0x78 (default) Press "i" Enter "121" or "0" Enter an uppercase "S" (Shift + S) to save your changes.

(5) Test in chat mode Chat Mode Select serial connection in Teraterm Refer to the COM port to which the master unit and slave unit are connected. Set the speed to "115200" in the serial port setting and connection of Teraterm settings (otherwise this setting will not work)

tw01.PNG

Connection example with TeraTerm COM3 is a slave unit, COM4 is a master unit tw03.PNG

(6) ASCII mode Omitted this time Settings enter interactive mode Press "m". Enter "a" Type an uppercase "S" (Shift + S) to save your changes.

(7) Binary mode Use Realterm to check communication. Realterm settings Baud:115200 Port: 4 Specify 3 for the other Display As:Hex(space) tw011.PNG tw010.PNG

Transmission from the slave unit to the master unit Send "0xA5 0x5A 0x80 0x05 0x00 </ u> 0x00 0x11 0x22 0x33 0x78"

  • Enter the above hexadecimal number in Serial Numbers in the Send tag of Realterm. Master unit Received "A 5 5A 80 05 78 00 11 22 33 78 04"

From master unit to slave unit 「0xA5 0x5A 0x80 0x05 0x78 0x00 0x11 0x22 0x33 0x78」 Cordless handset Received "A 5 5A 80 05 00 00 11 22 33 00 04"

Example of actual communication: 3 on the left is a slave unit, 4 on the right is a master unit TW013.PNG

"0xA5 0x5A 0x80 0x05 0x00 </ u> ..." Specify the destination with the 5th byte number 00: Master unit, 78: Slave unit

Send with Python (Pyserial)

Send it in Python and monitor it with Realterm Python=3.7,  Install Pyserial (If you are using Anaconda, it is better to install with conda) Reference: How to create an Anaconda virtual environment (conda-forge, Jupyter notebook)

import serial

#Master unit: COM4, slave unit: COM3
#Send from master unit to slave unit Specify 0x78
data=[0xA5, 0x5A, 0x80, 0x05, 0x78, 0x00, 0x11, 0x22, 0x33] 

#Send from slave unit to master unit Specify 0x00'
# data=[0xA5, 0x5A, 0x80, 0x05, 0x00, 0x00, 0x11, 0x22, 0x33] 

#Checksum calculation
chs = 0
for i in data[4:]:
    print(hex(i))
    chs = chs ^ i
print('check sum xor:',hex(chs))
data.append(chs)

# borate 115200
b_data = bytearray(data)
#When sending from the master unit to the slave unit. Use COM3 when switching from a slave unit to a master unit.
with serial.Serial('COM4', 115200) as ser:
    print('---')
    ser.write(b_data)
    print('---')

Execution example tw015.PNG

Recommended Posts