使用 (Arduino IDE) 从 Teensy 3.5 向 TSI 5310 流量计发送 ASCII 命令
简介:
好的,首先我想说的是,传感器确实会在收到命令时发送数据,因为我已经在连接到 PC 上 COMPORT 的 Python 上对此进行了测试。我将包含我创建的与传感器配合使用的Python代码,以便你们可以使用所有信息。我还将添加一个指向 PJRC 论坛 的链接,我曾在该论坛上提出过同样的问题,因为我已经收到了有关该问题的回复,但问题仍然存在,我希望你们能够了解他们所说的话由你掌握。
(Python 代码和 PJRC 链接将位于帖子的最底部)
问题:
所以,我的问题是我无法弄清楚如何从 Teensy 3.5 正确发送 ASCII 命令并作为回报读取Teensy 3.5 流量计的输出。我担心硬件连接错误或者我只是做错了什么。
串行控制台将保持空白,这意味着没有任何内容可供读取
我尝试过的 - 软件:
这是我得到的基本代码,应该适合我的使用:
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);
}
}
我尝试过的 - 硬件:
Black Wire - USB_C to USB_A - connected to a 5v power supply
Blue/White Wire - USB_A to MALE DB9
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)
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
连接到 Teensy 的 Rx/Tx 的 LED 图像,用于监视正在发送的数据
Blue LED - (Yellow - Teensy Receive Pin 0, Orange - GND)
Green LED - (Red - Teensy Transmit Pin 1, Brown - GND)
White Wire - Teensy 5v
Purple Wire - Teensy GND
Python 代码:
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 链接:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您应该能够将其连接到 Teensy 的第二个 USB 端口。该端口充当主机。当然,它是否有效取决于您的流量计采用的 USB 接口。如果它实现了一些标准(例如 CDC 又名虚拟串行或一些 HID 接口),USB 主机库可能可以与其通信。如果他们做了一个专有的接口,你需要先编写一个相应的驱动程序......
我假设他们实现了一个 CDC 接口。您可以轻松检查:如果将流量计连接到 PC,COM 端口 (Windows) 应出现在设备管理器中。
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.
我找到了解决方案!它位于哪个串行(serial1或serial2)上并不重要,但问题是我必须在流量计之前启动teensy,并在让teensy发送任何命令之前给流量计20秒的启动时间!不过这个传感器太慢了,需要 50 秒才能完全启动到测试屏幕!我只是用了一个5v继电器来延迟流量计的开启。感谢您的帮助!

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!
