0

I am trying to establish a serial connection via an RS232 port on the PR4000 controller from MKS. This controller is connected to a pressure gauge, and I try to read the pressure from my PC with the following script:

import time
import serial
import bitarray
ba = bitarray.bitarray()
# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
    port='COM7',
    baudrate=9600,
    parity=serial.PARITY_ODD,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.SEVENBITS
)

#ser.isOpen()

print ('Enter your commands below.\r\nInsert "exit" to leave the application.')

data_in=0
while True :
    data_in = input(">> ")
    if data_in == 'exit':
        ser.close()
        break
    else:
        ser.write((data_in).encode('utf-8'))
        out = ''
        time.sleep(0.1)
        while ser.inWaiting() > 0:
            out = ser.read(ser.inWaiting()).decode('utf8')
        if out != '':
            print (out)

This code is inspired from this post :

Full examples of using pySerial package

you can find the doc of the controller here :

https://www.idealvac.com/files/manuals/PR4000_InstructionManual.pdf

The interface chapter start at page 43.

basically, the RS interface works with an requests and answers syntax.

example of answer :

RT,ON : set the remote on the controller. ?RT : ask for the state of the remote mode.

I managed to establish the connection with hyper terminal

But with python, I've tried to enter the commands and I can't have any answers, the serial buffer is empty.

Do you think the problem is in the format of the requests ?

sawdust
  • 16,103
  • 3
  • 40
  • 50
  • A command to the device needs to be terminated with a carriage return character. Suggest you append a CR character (i.e. `'\r'`) to `data_in` before it is sent. Refer to the code that inspired your version for an example. *"basically, the RS interface works with an requests and answers syntax"* -- It's a *command - response* dialog or protocol (not a *"syntax"*). – sawdust Dec 01 '21 at 01:45
  • Hi ! Thank you for the answer. I'm now able to start communication (using the RT,ON and RT,OFF command to activate and deactivate the remote mode.). Now I have another problem, When I send a request starting with "?" (?RT to see if remote is on or off, or "?AV1" to get the actual value) the response is not what I expect, when i decode it I don't have intelligible (for example when I sent ?DT, instead of b'HELLO', I get something like b'HQ = \x02 \x02 \x02' tha I decode in : 'HQ = \x02 \x02 \x02'. maybe the controller encodes the response in another format than utf-8 ? – adrien pillet Dec 02 '21 at 09:48
  • *"for example when I sent ?DT, ..."* -- Seems like you're misusing that command. (1) You need to send the command with the end-of-line terminator, e.g. `?DT\r`. (2) That command apparently responds with the previously assigned (or current) display text, which in your situation seems to be `HQ ...`. You would only get a `"Hello"` response if you had previously issued a `DT,Hello\r` command. This is all clearly documented in the manual. *"maybe the controller encodes the response in another format..."* -- Request the ID string (a known response) to resolve your doubt. – sawdust Dec 03 '21 at 00:26
  • Sorry I misspoke, I am sending the "?DT\r" request and not "DT?" only. However, I found the problem in my code, I had modified the parameters of the serial connection (Stopbit = 2 instead of Stopbits=1) It works now. Thanks for your patience and your answers ! Adrien – adrien pillet Dec 03 '21 at 08:08

1 Answers1

0

Do you think the problem is in the format of the requests ?

A command to the device needs to be terminated with a carriage return character. Suggest you append a CR character (i.e. '\r') to data_in before it is sent.
Refer to the code that inspired your version for an example.

sawdust
  • 16,103
  • 3
  • 40
  • 50