简体   繁体   English

从串行(FTDI)输入到php / python脚本

[英]Input from Serial (FTDI) to a php/python script

I and some other people are working on a project that includes using an Arduino with an Ubuntu Server (running Ubuntu 10.04 64 bit). 我和其他一些人正在从事一个项目,该项目包括将Arduino与Ubuntu服务器(运行Ubuntu 10.04 64位)一起使用。 Currently we have the device connected and we can see the Arduino putting data in /dev/ttyUSB0. 目前,我们已连接设备,并且可以看到Arduino将数据放入/ dev / ttyUSB0中。 I can successfully cat it to another file. 我可以成功将其保存到另一个文件。

We have a MySQL database that this information will be translated to, via either a python or php script. 我们有一个MySQL数据库,此信息将通过python或php脚本转换为该数据库。 I need to know how to get the input from the serial port to be the input for that script. 我需要知道如何从串行端口获取输入作为该脚本的输入。 The device will be responding at least 20 times a second. 设备将至少每秒响应20次。 The script essentially just needs to take whatever response it gets and insert the corresponding row to the MySQL database. 脚本实际上只需要采取任何响应,并将相应的行插入MySQL数据库即可。

Has anyone done this before who could help out? 有谁做过这件事之前谁能帮忙?

It seems you are doing just fine. 看来您做得很好。 You could directly open /dev/ttyUSB0 like a file in your code, but as write and read access should be done at a certain pace (serial baudrate, ...), it may be problematic (but still possible: I never tried it, but you can configure the TTY to directly write in it). 您可以像在代码中的文件一样直接打开/dev/ttyUSB0 ,但是由于写入和读取访问应该以一定的速度进行(串行波特率,...),因此可能会出现问题(但仍然可能:我从未尝试过) ,但您可以将TTY配置为直接写入)。

The missing link is you have to access /dev/ttyUSB0 like a serial port. 缺少的链接是您必须像串行端口一样访问/dev/ttyUSB0

You mentionned Python: with it you can use PySerial . 您提到了Python:有了它,您可以使用PySerial It also makes your code more portable toward other operating systems. 它还使您的代码可移植到其他操作系统。 A quick apt-get install python-serial or apt-get install python3-serial should work. 快速的apt-get install python-serialapt-get install python3-serial应该可以工作。

You have some examples in the Arduino playground : 您在Arduino操场上有一些示例:

import serial
ser = serial.Serial('/dev/ttyUSB0', 9600)
while 1:
    ser.readline()

There are plenty others in the PySerial introduction . PySerial简介中还有很多其他内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM