一个串行通信 - 多阅读和Python脚本和Arduino的写之间 [英] One serial communication - multiple read and write between Python script and Arduino

查看:245
本文介绍了一个串行通信 - 多阅读和Python脚本和Arduino的写之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个Python守护进程和一个Arduino之间的串行通信。
起初,Python的守护程序设置将持续守护程序的整个生命周期的串行连接。通过这种连接,我想将数据发送到Arduino并在的ACK每一个Python的守护程序接收命令时接收回的数据变量。

问题是,虽然在第一时间沟通顺利,没有什么是通过串口发送之后。如果我做每一个它的工作要求一个新的连接,但它使程序很慢,我想避免。
修改真正的问题是,当发送一个正确的字符串到arduio evrything进展顺利,但是当我发出一个错误的串口模块,它绝不会再次reconize CORRCT字符串(问题在Arduino的code

Python的code:

 导入序列
进口时间
进口SYS
从插座进口*
进口螺纹
进口螺纹高清处理器(clientsock,地址):
    而1:
        #arduino.flush()
        数据= clientsock.recv(BUFSIZ)
        如果没有数据:
            打破
        打印数据
        打印数据
        #time.sleep(3)
        arduino.write(数据)
        #time.sleep(3)
        ACK = arduino.readline(1)
        arduino.flush()
        clientsock.send(ACK +\\ n)
    clientsock.close()如果__name __ =='__ main__:
    HOST =0.0.0.0
    PORT = 21567
    BUFSIZ = 1024
    ADDR =(主机,端口)
    Arduino的= serial.Serial('的/ dev / ttyACM0,9600,超时= 6)
    serversock =插座(AF_INET,SOCK_STREAM)
    serversock.bind(ADDR)
    serversock.listen(2)    而1:
        打印等待连接......
        clientsock,ADDR = serversock.accept()
        打印......从连接:'地址
        thread.start_new_thread(处理器,(clientsock,地址))

Arduino的code:

  INT relayPinCH1 = 7; //销德COMMANDE杜继电器1
  焦炭INDATA [20]; //分配一些空间字符串
  焦炭INCHAR = -1; //在何处存放字符读
  字节索引= 0; //索引数组;在哪里存储的字符无效设置()
{
  pinMode(relayPinCH1,OUTPUT);
  Serial.begin(9600);
}字符比较(字符*本){
    而(Serial.available()0)//不要读,除非
                                   //还有你知道有数据
    {
        如果(指数所述19)//比数组的大小少了一个
        {
            INCHAR = Serial.read(); //读取字符
            INDATA [指数] = INCHAR; //它存储
            指数++; //增量在哪里旁边写
            INDATA [指数] ='\\ 0'; //空终止字符串
        }
    }
    Serial.flush();
    如果(STRCMP(INDATA,这)== 0){
        的for(int i = 0;我所述19;我++){
            INDATA [I] = 0;
        }
        索引= 0;
        返回(0);
    }
    其他{
        返回(1);
    }
}无效循环()
{
  //Serial.println(\"Hello皮);
    如果(比(L11 \\ n)== 0)
    {
      Serial.flush();
      digitalWrite(relayPinCH1,HIGH);
      Serial.println(Y);
    }    如果(比(L10 \\ n)== 0)
    {
      Serial.flush();
      digitalWrite(relayPinCH1,LOW);
      Serial.println(N);
    }
  延迟(1000);
}


解决方案

在您的Arduino code,你的逻辑是一种时髦的 - 所以,我不知道,但你清除指数为0,然后再开始再次循环?它看起来像一次指数= 19,它可能会或可能不会被重置为0取决于后逻辑。如果输入比较()第二次和索引> = 19,那么你将永远不会再次读取串口。

I would like to set up a serial communication between a Python daemon and an Arduino. At first, the Python daemon sets up a serial connection that will last for the whole lifetime of the daemon. Through this connection, I would like to send data to the Arduino and receive back data in the acks variable every time the Python daemon receives commands.

The problem is that while the first time the communication goes well, nothing is sent through serial afterwards. If I make the a new connection for every request it works, but it makes the program very slow, which I'd like to avoid. edit: the real issue is when send a correct string to the arduio evrything goes well but when i send a wrong one the serial port block and it will never reconize corrct strings again( the problem is in the arduino code)

Python code:

import serial
import time
import sys
from socket import *
import threading
import thread

def handler(clientsock,addr):
    while 1:
        #arduino.flush()
        data = clientsock.recv(BUFSIZ)
        if not data:
            break
        print data
        print data
        #time.sleep(3)
        arduino.write(data)
        #time.sleep(3)
        ack = arduino.readline(1)
        arduino.flush()
        clientsock.send(ack+"\n")
    clientsock.close()

if __name__=='__main__':
    HOST = '0.0.0.0'
    PORT = 21567
    BUFSIZ = 1024
    ADDR = (HOST, PORT)
    arduino = serial.Serial('/dev/ttyACM0',9600,timeout=6)
    serversock = socket(AF_INET, SOCK_STREAM)
    serversock.bind(ADDR)
    serversock.listen(2)

    while 1:
        print 'waiting for connection...'
        clientsock, addr = serversock.accept()
        print '...connected from:', addr
        thread.start_new_thread(handler, (clientsock, addr))

Arduino code:

      int relayPinCH1 = 7; // pin de commande du relais 1
  char inData[20]; // Allocate some space for the string
  char inChar=-1; // Where to store the character read
  byte index = 0; // Index into array; where to store the character

void setup()
{
  pinMode(relayPinCH1, OUTPUT);
  Serial.begin(9600);
}



char Comp(char* This) {
    while (Serial.available() > 0) // Don't read unless
                                   // there you know there is data
    {
        if(index < 19) // One less than the size of the array
        {
            inChar = Serial.read(); // Read a character
            inData[index] = inChar; // Store it
            index++; // Increment where to write next
            inData[index] = '\0'; // Null terminate the string
        }
    }
    Serial.flush();
    if (strcmp(inData,This)  == 0) {
        for (int i=0;i<19;i++) {
            inData[i]=0;
        }
        index=0;
        return(0);
    }
    else {
        return(1);
    }
}



void loop()
{
  //Serial.println("Hello Pi");
    if (Comp("l11\n")==0)
    {
      Serial.flush();
      digitalWrite(relayPinCH1, HIGH);
      Serial.println("y");
    }

    if (Comp("l10\n")==0)
    {
      Serial.flush();
      digitalWrite(relayPinCH1, LOW);
      Serial.println("n");
    } 
  delay(1000);
}

解决方案

In your Arduino code, your logic is kind of funky - so, I'm not sure, but are you clearing index to 0 before you start the loop again? It looks like once index == 19, it may or may not get reset to 0 depending upon later logic. If you enter Comp() a second time and index >= 19 then you'll never read the serial port again.

这篇关于一个串行通信 - 多阅读和Python脚本和Arduino的写之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆