通过串行端口将数据从python发送到Arduino [英] Sending data from python to Arduino throught serial port

查看:117
本文介绍了通过串行端口将数据从python发送到Arduino的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果要在串行端口上读取字符"s",我试图使Arduino触发继电器.字符"s"由python根据从屏幕读取的图像发送.

I'm trying to make Arduino trigger a relay if the char "s" is read on serial port. That char "s" is sent by python based on an image read from screen.

我的问题是arduino似乎无法从串行端口读取,因为它从不执行if条件.我的猜测是两者之间存在某种死锁(这就是为什么我将ardu.close()放入foo函数中的原因)

My problem is that arduino seems not able to read from serial port as it never performs the if condition. My guess is that there is some kind of deadlock between the two (that's why I put ardu.close() in the function foo)

这是我的arduino代码:

This is my arduino code:

char serial;
#define RELAY1  7                        
void setup()

{    


Serial.begin(9600);
  pinMode(RELAY1, OUTPUT);       

}

  void loop()

{


if(Serial.available() > 0){
    serial = Serial.read();
    //Serial.print(serial);
    if(serial=='s'){
      digitalWrite(RELAY1,0);           
   Serial.println("Light ON");
   delay(2000);                                      

   digitalWrite(RELAY1,1);          
   Serial.println("Light OFF");
   delay(2000);
      }
      } 
}

这是我的python代码:

This is my python code:

import time
import serial
#from serial import serial
import cv2
import mss
import numpy
import pytesseract

def foo():
    print("sent")
    ardu= serial.Serial('COM6',9600, timeout=.1)
    time.sleep(1)
    ardu.write('s'.encode())
    time.sleep(1)
    ardu.close()


foo()

推荐答案

要从 Windows 机器与Arduino板通信,您必须安装 PySerial .请参阅此处的在计算机上安装PySerial的说明: PySerial网站

To communicate with the Arduino board from a Windows machine, you have to install PySerial. See the instructions here for installing PySerial on your machine: PySerial website

并且,请确保已为您的板安装了正确的串行驱动程序.这应该与您的电路板软件一起安装.但是,如果您需要手动进行操作,以下两个链接可能会有所帮助,Sparkfun驱动程序说明Arduino驱动程序说明

And, make sure that you have installed the correct serial driver for your board. This should be installed with your board software. But, in case you need to do it manually, here are two links that may be helpful, Sparkfun driver instructions and Arduino driver instructions

然后,确保使用正确的com端口.运行arduino IDE,将程序上载到arduino,然后在工具"菜单下(在IDE中),设置com端口并运行串行监视器.然后,在串行监视器中输入"s",并确认您看到灯亮,灯灭的消息.

Then, make sure that you are using the correct com port. Run your arduino IDE, upload your program to the arduino, and then under the Tool menu (in the IDE), set the com port and run the serial monitor. Then, in the serial monitor, enter an 's' and verify that you see the light on, light off messages.

这是您的arduino和python代码,被剥去最少的指令集来演示您的示例,以及一条println()语句(在arduino代码中)以十六进制形式回显接收到的字符.该调试语句将帮助您在开发代码时整理换行等.

Here are your arduino and python codes, stripped to the minimum set of instructions to demonstrate your example, plus a println() statement (in the arduino code) to echo the received characters in hex. That debugging statement will help you sort out line feeds and so forth as you develop your code.

更改继电器的插针号和端口的设备名称后,此处列出的代码可在我的主板和Linux机器上使用.将close()注释掉只是为了向您展示它在没有该行的情况下可以工作.

The codes as listed here, work on my board and Linux machine after changing the pin number for the relay, and the device name for the port. The close() is commented-out only to show you that it works without that line.

在arduino上:

#include <stdlib.h>

char serial;
#define RELAY1  7                       
void setup()
{    
  Serial.begin(9600);
  pinMode(RELAY1, OUTPUT);       
}

void loop()
{
  if(Serial.available() > 0)
  {
      serial = Serial.read();
      Serial.println( serial, HEX);
      if (serial=='s')
      {
        digitalWrite(RELAY1,0);           
        Serial.println("Light ON");
        delay(2000);                                      
        digitalWrite(RELAY1,1);          
        Serial.println("Light OFF");
        delay(2000);
      }
   } 
}

python代码:

import time
import serial

def foo():
    print("sent")
    ardu= serial.Serial('/dev/ttyACM0',9600, timeout=.1)
    time.sleep(1)
    ardu.write('s'.encode())
    time.sleep(1)
    #ardu.close()


foo()

这篇关于通过串行端口将数据从python发送到Arduino的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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