Serial communication 8N1: how to know start and stop bit format?

179 Views Asked by At

Unfortunately, I am totally unfamiliar with serial communication, but I have a temperature controller which I want to control using python + pyserial.

I managed to connect to the device via a COM-Port. Then my next approach was to set the new temperature via the serial.write command. However, nothing happened.

My questions:

  1. Do I need to send a startbit before and a stopbit following my „set new temperature“ command?
  2. If a startbit/stopbit is needed, how does it typically look like?
  3. How would the corresponding startbit/stopbit serial.write(„…“) command look like?

The code:

import serial

ser = serial.Serial()
ser.baudrate = 9600
ser.port = „COM3“
ser.timeout = 5
ser.stopbits = 1

ser.open()
#Serial<id=0x233e6e2e1a0, open=True>(port=„COM3“, baudrate=9600, bytesize=8, parity=„N“, stopbits=1, timeout=5, xonxoff=False, rtscts=False, dsrdtr=False)

ser.readline()
#b““
ser.write(bytes.fromhex(“01 06 00 00 00 69 49 E4“)) #<—- do i need to add start and stopbits here?

ser.readline()
#b““

Things I tried so far:

First I used a serial sniffer and monitored the COM-Port while utilizing the manufacturers software to change the temperature. The sniffer told me the command to change the temperature to 10.5°C was: 01 06 00 00 00 69 49 E4.

In addition the sniffer tells me, when sending a new temperature via the original software, that it returns and confirms the „hex code“: 01 06 00 00 00 69 49 E4.

When utilizing serial.write(bytes.fromhex(01 06 00 00 00 69 49 E4)) in python with pyserial and subsequently using serial.readline() it returns an empty byte. Although I would expect to get my hex code back. Ultimately the temperature setting should be confirmed on the display of the device, but it is not.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ general available device information: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ The Device is a PID-PID temperature controller from the vendor Microptik and is called MTDC600. MTDC600 Specification sheet. To use the device with the provided software one has to install the uart bride driver CH341. CH341 manual. Unfortunately i do not know how to get useful information out of that CH341 manual.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Additional information: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

One thing that seems odd to me is when i use the sniffer to monitor the com port while utilising the original manufacturers software it monitors all write/read commands seperately. Screenshot of the sniffer monitoring the com port while sending commands through the manufacturers software

However, when i send through commands via pyserial it rarely sends them seperately. It seems like sometimes they pile up as shown in the image below:

Screenshot of the sniffer monitoring the com port while sending commands through pyserial

Sniffer Protocol while setting temperature. Besides setting the temperature (red circle) the continuous background can be seen (marked in black circle)

Sniffer Protocol of the device response while setting the temperature. Shown is the response (black circle) of the continuous write from the screenshot 5 and from setting the temperature (red circle).

1

There are 1 best solutions below

11
Lee-xp On

This is a good way to send the block of bytes to set the new temperature.

import serial
import time

ser = serial.Serial('COM3', 9600, write_timeout=1)
time.sleep(3)   # Wait for Arduino to reboot

bytes_sent = ser.write([0x01, 0x06, 0x00, 0x00, 0x00, 0x69, 0x49, 0xE4])
print(f"Sent {bytes_sent} bytes")
print("Receiving:")
while True:
    bytes_in = ser.read()
    print(f"{bytes_in.hex()}", end=' ')

ser.close()

Basically you are sending a list of hex bytes. You shouldn't need to change any of the defaults on the port such as stop bits etc. I have included a 3 second pause before sending the data because I have been testing this with an Arduino, and opening the port has the unfortunate side-effect of rebooting the Arduino. So you probably will not need that line.

At the end we display any bytes received, as they arrive.