How to out put HEX array with Pyserial

import serial
import array
ser=serial.Serial('COM3',38400,timeout=1)

"""
To turn on all LED on 255
FF FF 00 01 FF FF FF FF FF FF FF 0D
To turn on at half
FF FF 00 01 7F 7F 7F 7F 7F 7F 00 0D
To turn off
FF FF 00 01 00 00 00 00 00 00 00 0D

"""
LedOn=[0xFF,0xFF,0x00,0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x0D]
Test=[0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x50,0x51,0x0D]
ser.write(array.array('B',LedOn).tostring())
ser.close()

the command array can still be a 0x type to indicate hex array.
but for pyserial, it take char, if you want to print out hex it need to be \x41 (which is an "A")
if you have an arry say: My=[0x41,0x42,0x43] need to pass to pyserial write as '\x41\x42\x43'
good thing is there is a array function to do so:
array.array('B', My.tostring()
which will out put
'\x41\x42\x43'

留言

這個網誌中的熱門文章

Heltec ESP32+OLED+Lora, hardware testing

micro SD card for ESP32, on lolin32 with OLED and heltec 32 lora oled

AIS0. understanding AIS NMEA 0183: How it's coded