Teensy Bord与使用python的系统之间的串行通信

问题描述 投票:1回答:1

我在物联网领域还很陌生。我正在设置一个带有Teensy的传感器,以读取其数据并通过串行通信传输到使用python的系统,我正在读取数据并将其存储到数据库中。

我面临的问题是当我使用arduino串行监视器]检查程序时,我得到的是[[疯狂的采样速度,例如在40毫秒内完成10k的读数]但是当我尝试使用python它甚至没有给我带来超过[[1000每秒的读数,并且如果没有数据库代码也只有每秒读取200个样本。我有什么办法可以提高此采样率,还是必须设置用于通过串行通信的任何其他参数?这是我的teensy代码:

int i; elapsedMillis sinceTest1; void setup() { Serial.begin(2000000); // USB is always 12 Mbit/sec i = 0; delay(5000); Serial.println("Setup Called"); Serial.flush(); } void loop() { if (i == 0 || i == 500000) { Serial.println(sinceTest1); } Serial.println(i); //Serial.println(Serial.baud()); i++; }

对于python:

import serial
import pymysql
from datetime import datetime
import time
import signal
import sys


class ReadLine:
    def __init__(self, s):
        self.buf = bytearray()
        self.s = s

    def readline(self):
        i = self.buf.find(b"\n")
        if i >= 0:
            r = self.buf[:i+1]
            self.buf = self.buf[i+1:]
            return r
        while True:
            i = max(1, min(2048, self.s.in_waiting))
            data = self.s.read(i)
            i = data.find(b"\n")
            if i >= 0:
                r = self.buf + data[:i+1]
                self.buf[0:] = data[i+1:]
                return r
            else:
                self.buf.extend(data)


ser = serial.Serial(
    port='COM5',\
    baudrate=2000000,\
    #baudrate=9600,\
    #parity=serial.PARITY_NONE,\
    #stopbits=serial.STOPBITS_ONE,\
    #bytesize=serial.EIGHTBITS,\
        #timeout=0
        )

print("connected to: " + ser.portstr)
count=1
#this will store the line
line = []

#database connection
connection = pymysql.connect(host="localhost", user="root", passwd="", database="tempDatabase")
cursor = connection.cursor()


checker = 0

rl = ReadLine(ser)
while True:

   time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
   print(time)
   print(checker)
   print(rl.readline())


   insert1 = ("INSERT INTO tempinfo(value,test,counter) VALUES('{}','{}','{}');".format(33.5, time,checker)) #.format(data[0])
   insert2 = ("INSERT INTO urlsync(textvalue,sync) VALUES('http://www.myname.com/value.php?&value={}&time={}',0);".format(33.5,time)) #.format(data[0])

   cursor.execute(insert1)
   cursor.execute(insert2)

   connection.commit()
   checker += 1


connection.close()
time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(time )
ser.close()

P.S:每秒1000个样本是我不使用数据库命令时获得的速率,包括它们,我仅每秒获得约250个样本。

感谢您的任何帮助或建议,谢谢。

我在物联网领域还很陌生。我正在设置一个带有Teensy的传感器,以读取其数据并通过串行通信传输到使用python的系统,我正在读取数据...
python arduino serial-port pyserial teensy
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.