【问题标题】:Serial Communication ending between Python (OpenCV) and ArduinoPython(OpenCV)和Arduino之间的串行通信结束
【发布时间】:2017-05-08 19:48:52
【问题描述】:

我尝试检查我的 OpenCV 代码是否正在与 Arduino 通信。

OpenCV 代码:

import numpy as np
import cv2
import serial
import time
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
while 1:
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x, y, w, h) in faces:
       cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
       detect=x
       print(detect)

       cv2.imshow('img', img)
       k = cv2.waitKey(30) & 0xff

       if 0 < detect < 100:
        ser = serial.Serial("COM1", 19200, timeout=5)
        time.sleep(2)
        ser.write("\x35")
        print "RECIEVED BACK:", repr(ser.read(5000))

       if k == 27:
           break

Arduino 代码:

int incomingByte = 0;   // for incoming serial data

void setup() {
        Serial.begin(19200);    
}

void loop() {

        // send data only when you receive data:
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();

                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte, DEC);
        }
}

我得到以下“检测”值:

'301
71
RECIEVED BACK: 'I received: 53\r\n'
299
301
301
302
302
301
303
300
306
72'

在detect=71,一个信号被发送到Arduino,它返回一个值,之后它工作了一点,然后所有的通信都中断了,我得到了以下错误:

Traceback (most recent call last):
  File "C:/Users/khan1/Desktop/python 
  project/tennis_ball_vid/tennis_vid.py", line 40, in <module>
    ser = serial.Serial('COM1', 19200,timeout=5)
  File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 38, in 
 __init__
    SerialBase.__init__(self, *args, **kwargs)
  File "C:\Python27\lib\site-packages\serial\serialutil.py", line 282, in 
 __init__
    self.open()
  File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 66, in 
  open
    raise SerialException("could not open port %r: %r" % (self.portstr, 
    ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM1': 
WindowsError(5, 'Access is denied.')

Process finished with exit code 1

这是我的原帖: Serial comunication between opencv (python) and arduino

如何保持沟通畅通?

【问题讨论】:

  • 请修正缩进。你在哪里向 Arduino 发送信号,它在哪里返回一个值? serial.Serial('COM1', 19200,timeout=5) 行创建了一个端口对象,但不发送或接收任何内容。
  • 你一直打开序列,但从不关闭它。为什么不一开始就打开呢。
  • 我已经编辑了我的帖子。很抱歉沟通不畅。 @PaulCornelius
  • 似乎将 ser 端口移出 while 循环起到了作用!谢谢@gre_gor

标签: python opencv arduino serial-port


【解决方案1】:

评论:我不明白你的问题。能解释一下吗

您的loop 应如下所示,例如:

ser = serial.Serial("COM1", 19200, timeout=5)
time.sleep(2)
while True:
    ret, img = cap.read()
    # ... img processing

    for (x, y, w, h) in faces:
        # ... faces processing

       if 0 < detect < 100:
            print('ser.is_open=%s' % ser.is_open() )
            ser.write("\x35")
            print("RECIEVED BACK:", repr(ser.read(5000)) )

问题:如何保持沟通畅通?

将这行代码移到while ...循环之外

ser = serial.Serial('COM1', 19200,timeout=5)
time.sleep(6)
print(ser)

【讨论】:

  • 端口保持打开状态,直到您调用ser.close()。是什么让print(ser.is_open)if 0&lt;detect&lt;100 内?
  • 我不明白你的问题。你能解释一下吗?
  • 为什么我需要(ser.is_open)?我是新手。
  • @sayem48:你不需要它。它仅适用于 tracing 您的代码。看来我们有重叠的沟通。现在可以用了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-03
相关资源
最近更新 更多