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

查看:123
本文介绍了通过串口将数据从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,然后在 Tool 菜单下(在 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天全站免登陆