简体   繁体   English

Python 串行通信 Arduino Led 控制问题

[英]Python Serial Communication Arduino Led Controlling Problems

I am trying to control a total of 6 LEDS with python.我正在尝试用 python 控制总共 6 个 LED。 I was using pyserial to send arduino some data but I have encountered several problems with it.我正在使用 pyserial 向 arduino 发送一些数据,但我遇到了几个问题。

The first problem I have encountered was:我遇到的第一个问题是:

According to the code I have written on arduino, the LEDS should blink for 1 second for several amount of times a specific data received.根据我在 arduino 上编写的代码,LEDS 应闪烁 1 秒,多次收到特定数据。 (This is later explained below.) However, the LEDS stay on the number of seconds they should blink. (稍后将在下面解释。)但是,LED 会停留在它们应该闪烁的秒数上。 Meaning if the LEDS are supposed to blink for 10 times.这意味着 LED 是否应该闪烁 10 次。 The LEDS stay on for 10 seconds and turn off. LED 灯保持亮起 10 秒钟然后熄灭。

The second problem was:第二个问题是:

The if conditions I have put in the code was not in order.我在代码中放置的 if 条件不正确。 As you can see in the arduino code, the if conditions are in order.正如您在 arduino 代码中看到的,if 条件是有序的。 However, this is what happens when I run the code.但是,这就是我运行代码时发生的情况。

The first LED lights up for 10 seconds, the second one lights up for 10 seconds as well.第一个 LED 亮起 10 秒,第二个 LED 也亮起 10 秒。 And later on the fifth one lights up.稍后,第五个亮起。

To explain a little more of the code:再解释一下代码:

I am storing lists inside a list inside of python.我将列表存储在 python 内部的列表中。 There is a for loop that sends each list with a delay of 1 second.有一个 for 循环以 1 秒的延迟发送每个列表。 The list has 6 elements.( This is for later experimentation.) However, in this work only the first two of elements of each list matter.该列表有 6 个元素。(这是为了以后的实验。)但是,在这项工作中,只有每个列表的前两个元素很重要。

In order to negate the auto reset on arduino, I put 10 microfarad capacitor between the ground and reset.为了取消 arduino 上的自动重置,我在接地和重置之间放置了 10 微法电容器。 After that I run the python code to send the data.之后我运行python代码来发送数据。

I think I have explained the situation with details, however I am open to suggestions and will answer questions on the comments.我想我已经详细解释了情况,但是我愿意接受建议并将回答有关评论的问题。

The Python Code: Python代码:

import time

import serial

 
incomingByte2=[[1,20,200,300,400,500],[2,30,24,63,200],[3,5,400,500,100,200],[4,10,1,1,1,1],[5,10,1,1,1,1],[6,10,1,1,1,1]]

uzunluk= len(incomingByte2)


def close():

#    arduino=serial.Serial("COM4",9600)

   

    arduino = serial.Serial(

            port='COM3',\

            baudrate=115200,\

            parity=serial.PARITY_NONE,\

            stopbits=serial.STOPBITS_ONE,\

            bytesize=serial.EIGHTBITS,\

            timeout=0)


    print("connected to: " + arduino.portstr)
   
    for i in range(0,uzunluk):

        arduino.write(str.encode(str(incomingByte2[i])))
    
        time.sleep(1)

The Arduino Code: Arduino代码:

int ledPins[] = {2,3,4,5,6,7,8,9};
int incomingdata[6];
int ilkled,ikinciled,ucunculed,dordunculed,besinciled,altinciled;
void setup() {
  // put your setup code here, to run once:
int index;
Serial.begin(115200);
for(index = 0; index <= 7; index++)
{
pinMode(ledPins[index],OUTPUT);
}
}
void loop() {
  // put your main code here, to run repeatedly:
if(Serial.available()){
    for (int a=0; a < 6; a++) { 
      incomingdata[a] = Serial.parseInt();
      delay(100);
      ilkled=incomingdata[0];
      ikinciled=incomingdata[1];
      ucunculed=incomingdata[2];
      dordunculed=incomingdata[3];
      besinciled=incomingdata[4];
      altinciled=incomingdata[5];
      
}
}

if (ilkled==1){
  for (int x=0;x<ikinciled;x++){
  digitalWrite(ledPins[0],HIGH);
  delay(1000);
  digitalWrite(ledPins[0],LOW);
  }
}
if (ilkled==2){
  for (int x=0;x<ikinciled;x++){
  digitalWrite(ledPins[1],HIGH);
  delay(1000);
  digitalWrite(ledPins[1],LOW);
}
}
if (ilkled==3){
  for (int x=0;x<ikinciled;x++){
  digitalWrite(ledPins[2],HIGH);
  delay(1000);
  digitalWrite(ledPins[2],LOW);
}
}
if (ilkled==4){
  for (int x=0;x<ikinciled;x++){
  digitalWrite(ledPins[3],HIGH);
  delay(1000);
  digitalWrite(ledPins[3],LOW);
}
}
if (ilkled==5){
  for (int x=0;x<ikinciled;x++){
  digitalWrite(ledPins[4],HIGH);
  delay(1000);
  digitalWrite(ledPins[4],LOW);
}
}
if (ilkled==6){
  for (int x=0;x<ikinciled;x++){
  digitalWrite(ledPins[5],HIGH);
  delay(1000);
  digitalWrite(ledPins[5],LOW);
}
}
}

I think your read loop is broken.我认为您的阅读循环已损坏。 It should close after delay(100), no?它应该在延迟(100)后关闭,不是吗?

for (int a=0; a < 6; a++) { 
  incomingdata[a] = Serial.parseInt();
  delay(100);
}

Personally I would not string encode the data in Python.我个人不会用 Python 对数据进行字符串编码。 Send it as raw bytes, then read it as raw bytes into your int array.将其作为原始字节发送,然后将其作为原始字节读入您的 int 数组。

Serial.readBytes( incomingData, 6 ); // assumes 8 bit ints.

That would do away with the loop completely.这将完全消除循环。

Your LED staying on instead of flashing because you missed the line I added below.您的 LED 保持亮起而不是闪烁,因为您错过了我在下面添加的行。

for (int x=0;x<ikinciled;x++){
  digitalWrite(ledPins[5],HIGH);
  delay(1000);
  digitalWrite(ledPins[5],LOW);
  delay(1000); // <<<< Hold the LOW time

} }

Otherwise it will be set LOW for only a few microseconds.否则它将被设置为低电平仅几微秒。

You may also run into synchronous issues with your Serial read versus how much time you spend in "delay()" during the LED flashing.您还可能会遇到串行读取的同步问题,以及 LED 闪烁期间在“delay()”上花费的时间。 Your python looks like it's sleeping only for 1 second, but your code responding to that will take many seconds as it delays in delay().您的 python 看起来只休眠了 1 秒,但是您的代码响应它需要很多秒,因为它在 delay() 中延迟。

The Serial buffer will overflow and data will be lost/overwritten and when you call your next "parseInt" or "readBytes" there is no guarantee where the next bit of data in the buffer starts.串行缓冲区将溢出,数据将丢失/覆盖,当您调用下一个“parseInt”或“readBytes”时,无法保证缓冲区中的下一位数据从哪里开始。 Highly likely not at the next block of 6 ints.很可能不在下一个 6 个整数块。

You could send the data less often or send it based on how long the flashing will take.您可以减少发送数据的频率,也可以根据闪烁所需的时间发送数据。 Alternatively you could implement an interrupt system to flash the LEDs... and the solutions get more complex from there up.或者,您可以实现一个中断系统来使 LED 闪烁……并且解决方案从那里开始变得更加复杂。

Welcome to the world of low level communications protocols.欢迎来到低级通信协议的世界。

PS, get rid of these PS,摆脱这些

if (ilkled==6){

Just use it directly.直接用就行了。

digitalWrite(ledPins[ilkled-1],HIGH);

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

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