Logan Dickon
Logan Dickon

Reputation: 11

Sending ASCII Commands to a TSI 5310 Flowmeter from a Teensy 3.5 with (Arduino IDE)

Introduction:

Okay so to start I just want to say that the sensor does send its data when commanded as I've tested this on Python connected to a COMPORT on a pc. I will include the Python Code I created that works with the sensor, so that all information is available to you guys. I also will include a link to the PJRC Forum that I've asked the same question on, because I've already gotten responses on the issue, but it still persists, and I want you guys to have what they've said at your disposal.

(Python Code & PJRC Link will be at the very bottom of the post)

Problem:

So, my problem is I cannot figure out how to properly send ASCII commands from the Teensy 3.5 and in return read the output of the Flowmeter with the Teensy 3.5. I am afraid that the hardware is connected wrong or I'm just going about something wrong.

The Serial Console will stay blank meaning nothing is available to be read in

What I've Tried - Software:

This is basic code I was given that should work for my use:

char s;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  while (!Serial && (millis() < 5000)) {};
  Serial1.begin(115200);
  delay(1000);
  Serial1.print("?\r\n");
}

void loop() {
  // put your main code here, to run repeatedly:
  while (Serial1.available()){
    s = Serial1.read();
    Serial.print(s);
  }

}

What I've Tried - Hardware:

Image of TSI FlowMeter 5130 w/Cables

Black Wire - USB_C to USB_A - connected to a 5v power supply
Blue/White Wire - USB_A to MALE DB9

Image of Cables that connect the Flowmeter & Teensy 3.5

Blue/White Wire - Male DB9
Tan Serial Gender Converter - Female DB9 to Female DB9
Black Converter Board - Male DB9 to 4-Wire TTL (Red - VCC, Yellow - Transmit, Blue - Receive, Black - GND)

Image of RS232 to TTL Wiring

Yellow Wire - Teensy Transmit Pin 1
Blue Wire - Teensy Receive Pin 0
Red Wire - Currently Set to 5v, but I've tried 3.3v to no avail
Black Wire - GND

Image of LEDs Wired into Rx/Tx of Teensy to watch for data being sent

Blue LED - (Yellow - Teensy Receive Pin 0, Orange - GND)
Green LED - (Red - Teensy Transmit Pin 1, Brown - GND)

Image - 5v Power Supply

White Wire - Teensy 5v
Purple Wire - Teensy GND

Python Code:

import serial
import time

index = 0
total = 0
i = 0
avg = 0

# Serial Connection
time.sleep(.5)
ser = serial.Serial(
        port="COM2", baudrate = 115200,
        parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS, timeout=1)

# Write ASCII Commands To TSI 5300 Flow Sensor
ser.write(b'?\r\n')                                         # Ask Sensor if it is getting a signal (Returns "OK")
ser.write(b'SUS\r\n')                                       # Set Flow type to SLPM (Returns "OK")
ser.write(b'SG0\r\n')                                       # Set Flow Gas to Air (Returns "OK")
ser.write(b'SSR0005\r\n')                                   # Set Sample Rate to 5ms (Returns "OK")
ser.write(b'LPZ\r\n')                                       # Zero Low Pressure Sensor

# Read serial output to remove all 'OK's from buffer
while (i <= 4):
    OK = ser.readline()                                     # Read one line of serial and discard it
    print(OK)
    i += 1

# Ask for 5 Flow readings
ser.write(b'DAFxxxxx0005\r\n')                              # Read 5 sensor Flow Reading
ser.readline()                                              # Read one line of serial data and discard it
byte = ser.readline()                                       # Read one line of serial data and store it
print("Unfiltered Bytes: " + str(byte))
string = byte.decode('utf-8')                               # Convert from BYTE to STRING
array = string.split(',')                                   # Convert from STRING to STRING ARRAY
print("String Array of all 5 readings: " + str(array))

# Convert each element of the ARRAY to FLOAT then add them together
for data in array:
    index += 1 
    data = float(data)
    total += data

avg = total / index # Find the average Flow in LPM
print("Average Flow Rate: " + str(avg) + " LPM")
time.sleep(1)

ser.close()

PJRC LINK:

https://forum.pjrc.com/threads/69679-Sending-ASCII-Commands-to-a-Teensy-3-5-Via-RS232-to-TTL-Converter

Upvotes: 0

Views: 217

Answers (2)

Logan Dickon
Logan Dickon

Reputation: 11

I found the solution! It didn't matter which serial it was on (serial1 or serial2), however the problem is I had to start the teensy before the flowmeter and give the flowmeter 20sec to boot up before letting the teensy send any commands! This sensor is so slow though, it takes 50 seconds to fully boot up to the test screen! I just used a 5v relay to delay the flowmeter turning on. Thanks for your help! Image of code and output

Upvotes: 0

luni64
luni64

Reputation: 333

Yes, you should be able to connect it to the second USB port of the Teensy. This port acts as Host. Whether it works of course depends on which USB interface your flowmeter implements. If it implements some standard (e.g. CDC aka virtual serial or some HID interface) the USB Host lib can probably communicate with it. If they did a proprietary interface you would need to write a corresponding driver first...

I assume they implemented a CDC interface. You can easily check: if you connect the flowmeter to a PC a COM Port (Windows) should appear in the device manager.

Upvotes: 0

Related Questions