Arduino Python串行通信错误

詹姆斯·B

我已经有几个星期了,这是一个复杂的错误,我不知道如何解决。我在第一个模拟引脚上插入了一个热敏电阻阵列,它们在Python脚本上返回温度,该脚本在串行端口上与Arduino通信。这是Arduino代码:

float R1 = 2000;
float c1 = 8.7956830817e-4, c2 = 2.52439152444e-04, c3 = 1.94859260345973e-7;

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
}

int getVoltage(int i) {
  return analogRead(i);
}

float getTemp(int i) {
  int V = getVoltage(i);
  float R2 = R1 * (1023.0 / (float)V - 1.0); \\minus 1 for 1 index based
  float logR2 = log(R2);
  float T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
  return T - 273.15;
}

String getTempString(int i) {
  float temp = getTemp(i);
  String result;
  if(temp > 99.99){
    return String(temp, 2);
  } else {
    return String(temp, 3);
  }
}

void loop() {
  if (Serial.available() > 0) {
    digitalWrite(13, HIGH);
    // read the incoming byte:
    String input = Serial.readStringUntil('\n');
    digitalWrite(13, LOW);
    //send the temperature
    char* response = new char[6];
    int channelNumber = String(input[0]).toInt() - 1;//-1 to make it index based 1
    getTempString(channelNumber).toCharArray(response, 6);
    //delay(20);
    Serial.write(response, 6);
    Serial.write('\n');
  }
}

这是Python代码:

from serial import Serial
import time

ser = Serial('COM5', 9600, timeout=1)

def readTempChannel(i):
    global ser
    
    ser.write(i+b'\n')
    raw = str.rstrip(ser.readline())
    try:
        return float(raw)
    except Exception:
        ser.close()
        ser = Serial('COM5', 9600, timeout=1)
        return 5.0[![enter image description here][1]][1]

if __name__ == "__main__":
    while 1:
        channel1 = readTempChannel('1')
        channel2 = readTempChannel('2')
        channel3 = readTempChannel('3')
        channel4 = readTempChannel('4')
        print('%.2f, %.2f, %.2f, %.2f' % (channel1, channel2, channel3, channel4))

问题是我在前10秒钟获得了值,但是之后我得到了空字符串或来自Arduino的不是数字的随机字符。

我尝试关闭并重新打开串行端口,并且可以工作(有时),但是这会增加流的延迟,并且我需要为应用程序高速通信而没有任何延迟。我在此帖子中添加了一个屏幕截图,显示了PuTTY终端上的错误(Python代码在Beaglebone上运行,它是Python 2.7)。

因此,如果有任何人可以帮助我解决此错误,我将非常感激。

在此处输入图片说明

麦克斯韦编辑

在没有所有物理硬件的情况下,这是一个很难解决的问题,但是我可以与您分享我在arduino中使用pyserial的经验:

在python方面,我没有打扰新行:

ser.write(b'{}'.format(command)) 
## writes the command as bytes to the arduino

我还使用以下行暂停python程序,直到它收到响应-这对于流控制非常重要

while(ser.inWaiting()==0):pass ## waits until there is data

在arduino方面,我使用以下命令读取串行文件并执行命令:

void loop() {
  switch(Serial.read()){
    case '0': ## when command (in python) = 0
              do_command_1()
              break;
    case '1': ## when command (in python) = 1
              do_command_2()
              break;
    default: break; # default - do nothing if I read garbage
  }
}

尝试将上述内容集成到代码中,然后再找我-就像我说的那样,没有硬件就很难解决硬件问题,而我这里的代码是很久以前的一个项目

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Java的Arduino串行通信

来自分类Dev

Arduino - 处理串行通信

来自分类Dev

arduino至raspberrypi串行通信

来自分类Dev

树莓派-Arduino串行通信

来自分类Dev

Arduino-Unity串行通信

来自分类Dev

将串行通信(Ubuntu,Python)发送到Arduino

来自分类Dev

串行python到arduino

来自分类Dev

Arduino与处理之间的串行通信问题

来自分类Dev

Arduino MKR Wifi 1010与Arduino Mega的串行通信

来自分类Dev

python至arduino串行读写

来自分类Dev

Arduino和Python之间的串行通信,使用十六进制值的问题

来自分类Dev

Arduino串行端口提供错误数据?

来自分类Dev

Arduino串行接收错误数据

来自分类Dev

Python线程和Arduino通信

来自分类Dev

串行通信-Node.js-Arduino板无人应答

来自分类Dev

Raspberry Pi和Arduino之间的串行通信不稳定

来自分类Dev

MATLAB未找到用于与Arduino通信的串行端口

来自分类Dev

帮助了解Arduino Mega上的串行通信(SPI)

来自分类Dev

Arduino和EPOS之间的串行通信:CRC计算问题

来自分类Dev

C# - Arduino 串行通信在读取数据时冻结(?)

来自分类Dev

Arduino 和 Raspberry pi3 之间的串行通信

来自分类Dev

使用8086汇编语言与Arduino串行通信

来自分类Dev

两个 Arduino 板之间的串行通信

来自分类Dev

在python中读取arduino串行连接

来自分类Dev

Arduino-python读取串行布局

来自分类Dev

python串行到arduino速度测试

来自分类Dev

用于 Arduino 的 Python 串行和 CSV

来自分类Dev

Arduino串行命令

来自分类Dev

Arduino VirtualBox串行端口