mthrossell
mthrossell

Reputation: 27

Reading unexpected bytes using pyserial, \x004

I am attempting to communicate with a laser system over USB. The manufacturer has provided documentation on how to use serial communication to gather information from and send commands to the laser. I'm using Python 3.12.7 along with pySerial in order to communicate with the laser.

Once connected, every few seconds the laser sends out a "system status word" that consists of 16 bytes. The various bits that make up the bytes each indicate the status of various aspects of the system. Based on the documentation from the laser manufacturer I know what the system status word should look like, but what I read from the device in Python is not entirely what I expect.

First thing I did was to download a terminal, CoolTerm, to see what to expect from the laser. The laser sends the following packet for the system status word:

AA AA 00 01 00 10 00 11 00 03 20 00 34 00 C5 BD

I can decompose this per the manufacturer's documentation, and it all makes sense, so I am confident the terminal is providing correct data.

Depending on the method I use to read the bytes and construct the list I get different results. My first iteration of code is the following:

import serial

class LPY707G:
    def __init__(self):
        
        self.consettings = serial.Serial(port = None, baudrate = 9600,
                                         bytesize = serial.EIGHTBITS,
                                         parity = serial.PARITY_NONE,
                                         stopbits = serial.STOPBITS_ONE,
                                         timeout = 30)
        
        self.consettings.port = 'COM7'
        
    def connect(self):
        try:
            self.consettings.open()
            self.is_connect = 1
            print("Successfully connected")
            return self.is_connect
        except Exception as e:
            print("Serial Instrument could not be identified.")
            print(e)
            self.is_connect = 0
            return self.is_connect
        
    def disconnect(self):
        try:
            self.consettings.close()
            self.is_connect = 0
            print("Successfully disconnected")
            return self.is_connect
        except Exception as e:
            print(e)
            self.is_connect = 1
            print("Still connected")
            return self.is_connect
        
    def read_bytes(self):
        try:
            readbytes = self.consettings.read(16)
            return readbytes
        except Exception as e:
            print(e)

if __name__ == "__main__":
    instrument = LPY707G()
    instrument.connect()
    print(instrument.read_bytes())
    instrument.disconnect()

The data it prints is: b'\xaa\xaa\x00\x01\x00\x10\x00\x11\x00\x03 \x004\x00\xc5\xbd'

The first 10 bytes are what I expect, as are the final 3 bytes, but the output between those, ' \x004', just doesn't make any sense to me.

The next thing I tried was to collect each byte one at a time and make a list. I changed the read_bytes function and added a for loop.

def read_bytes(self):
        try:
            readbytes = self.consettings.read(1)
            return readbytes
        except Exception as e:
            print(e)

if __name__ == "__main__":
    instrument = LPY707G()
    instrument.connect()
    bytepacket = []
    for i in range(16):
        indvbyte = instrument.read_bytes()
        bytepacket.append(indvbyte)
    print(bytepacket)
    instrument.disconnect()

This method prints: [b'\xaa', b'\xaa', b'\x00', b'\x01', b'\x00', b'\x10', b'\x00', b'\x11', b'\x00', b'\x03', b' ', b'\x00', b'4', b'\x00', b'\xc5', b'\xbd']

Again, first 10 and last 3 bytes are what I expect, but now the 3 bytes in between are shown and they seem to make sense with what was printed with the first method, but still not what I expect to see.

Finally, I tried creating a bytearray, but the results were essentially the same as the first method.

if __name__ == "__main__":
    instrument = LPY707G()
    instrument.connect()
    bytepacket = bytearray()
    for i in range(16):
        indvbyte = instrument.read_bytes()
        bytepacket += indvbyte
    print(bytepacket)
    
    instrument.disconnect()

This method prints: bytearray(b'\xaa\xaa\x00\x01\x00\x10\x00\x11\x00\x03 \x004\x00\xc5\xbd')

I cannot figure out what ' \x004' means, or why it is being sent/read.

Upvotes: 1

Views: 38

Answers (0)

Related Questions