0

I am using a Teensy 3.6 to communicate with an external instrument via a serial connection. However, I am not able to get the instrument to respond to my commands. The idea is to send a command from the computer via the serial monitor to the Teensy, which then can send it to the instrument (I know that the Teensy is not needed with this configuration but this is just for testing of a more complex set-up). The instrument uses an RS232 connector so I use this RS232 shifter to connect it to the Teensy. The serial communication settings of the instrument are: 38.4k baud rate, 8 data bits, 1 stop bit, no parity and a carriage return character should mark the end of the command string.

A simple example of my code to read the input from the serial monitor and send a command to the instrument:

#define FILTser Serial1
char Command;

void setup() {
  Serial.begin(9600);
  FILTser.begin(38400);
  Serial.println("<Teensy is ready>");
}

void loop() {
    Command = 'X';
    recvCmd();
    
    // Start sampling command
    if (Command == 'S') {
       FILTser.write("pumpctl=1\r");
       FILTser.write('\n');
       Serial.println("Sampling started");
    }    
}

void recvCmd() {
    if (Serial.available() > 0) {
        Command = Serial.read();
    }
}

If I read the output from the Teensy on the Serial1 port with Putty I get the message correctly. If I send the same command directly to the instrument with Putty or Python I get the instrument to respond as I expect. This means that the instrument is working and the serial output from the Teensy is also fine so I really don't understand what could be wrong.

I know that the instrument is not responding because I would expect a pump to turn on when it receives the command. Additionally, I tried to read the answer of the instrument on the serial port and I was receiving a -1 but I would expect OK for a good command and ERR if the command is not recognized.
To read the instrument answer on the serial port I tried using both a single character with Response = FILTser.read(); and also a full string using the code from here.

An example of the Python code that I use to send commands to the instrument is the following:

import datetime
import serial
import io
import time

# =============================================================================
# Configure serial port
# =============================================================================
ser = serial.Serial()
ser.baudrate = 38400
ser.port = 'COM5'
ser.timeout = 1

#%% Define FILT functions
def FILT_send(commnd):
    ser.write(commnd.encode('ascii'))
    time.sleep(0.5)
    value_read = ser.readline().decode('ascii')
    return(value_read)

def FILT_control():
    global q
    user_input = input('Type:\n \
    - S to start sampling\n \
    - E to end sampling\n \
    - Q to quit the program\n')
    
    if user_input == 'S':
        FILT_send('pumpctl=1\r')
        print('Sampling started')
        
    if user_input == 'E':
        FILT_send('pumpctl=0\r')
        print('Sampling ended')

    if user_input == 'Q':
        print('The end')
        q = False
            
#%% Main
ser.open()
q = True
while q:
    FILT_control()
ser.close()
1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Juraj
    Aug 21, 2021 at 5:33

1 Answer 1

3

The Sparkfun RS232 shifter is not a great device. It relies on whatever it's connected to (on the 9-pin port) providing the negative rail for the EIA-232 voltage levels.

That's fine when you connect to something that has those voltages available, such as a computer, but many "devices" don't - they will themselves have a similar shifter inside and will rely on whatever they are plugged in to for providing the needed negative voltage.

Put the two together and no-one will provide that negative voltage.

Instead you should use a MAX232 (or MAX3232 for 3.3V operation) based converter. This has a built-in voltage doubler and voltage inverter to convert the 5V into +/-10V to give a proper EIA-232 compliant signal which the device should then be happy with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.