【问题标题】:Image transmission and reception over usb port using python使用python通过usb端口传输和接收图像
【发布时间】:2015-05-01 06:39:39
【问题描述】:

我对 python 完全陌生。我正在使用 python 3.4.3,我一直在尝试将数据传输到 USB 到 LED 驱动电路。我想调制到达 USB 端口的数据。我的系统中安装了一个 USB 转串口转换器。任何人都可以建议相同的程序代码。我试过的程序是

import serial
ser=serial.Serial("/dev/ttyUSB0", 115200)
ser.open()
ser.isOpen()

print ('Enter your commands below.\r\nInsert "exit" to leave the application.')

我有 64 位操作系统的 MS Windows 8.1。

我已经使用从https://pypi.python.org/pypi/pyserial.The下载的pyserial-2.7.win32_py3k.exe安装了串口模块,错误信息是

Traceback (most recent call last):
File "C:/Users/shamsu/Desktop/ss.py", line 10, in <module>
bytesize=serial.SEVENBITS
File "C:\Python34\lib\site-packages\serial\serialwin32.py", line 38, in __init__
SerialBase.__init__(self, *args, **kwargs)
File "C:\Python34\lib\site-packages\serial\serialutil.py", line 282, in __init__
self.open()
File "C:\Python34\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 '/dev/ttyUSB1': FileNotFoundError(2, 'The system cannot find the path specified.', None, 3)

我是否安装了 32 位的串行模块?我的系统使用的是 64 位 windows 8.1 操作系统。

我不知道这个程序是否适合我的应用程序。谁能帮帮我?

【问题讨论】:

  • 错误说它无法打开串口/dev/ttyUSB1,因为它没有找到。 usb/tty1 是 linux 串口而不是 windows。在 Windows 上,您需要转到设备管理器并查看您的 USB 转串口适配器正在使用的 com 端口。例如 ser = serial.Serial('COM1', 9600)
  • 感谢您的回复。现在您能告诉我如何向 USB 端口发送和接收图像吗?
  • usb -serial 不是 USB 的序列号。打开图像,将其转换为数组或您需要的任何格式,然后通过串行发送。如何取决于您在 LED 驱动端需要什么。二进制位、十六进制、整数???
  • 我认为对我来说最好使用二进制位。我不知道其他方案是否有更好的性能。请问如何将图像转换为数组?
  • 你试过什么?使用 opencv(cv2) 或 pil 很容易转换为数组或以数组形式访问图像。发布您的代码,以便人们可以帮助/建议您。你在led端使用什么?一个arduino还是?

标签: python


【解决方案1】:

在windows上改为

ser = serial.Serial('COM1', 9600) or ser = serial.Serial(port='COM4')

在一个类中使用可以这样做

self.ser=serial.Serial(port='\\.\COM1', baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=1)

要打开图像,可以使用 PIL 或 opencv 例如How do I convert a numpy array to (and display) an image?

也是基本的图像处理教程http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_core/py_basic_ops/py_basic_ops.html

使用 opencv2 和 numpy

import cv2
import numpy as np
image = cv2.imread("image.png") # or full path to image

print(image.size)
print(image.shape)    
print image[0,0]
ser.write(image[0,0])

Do the serial writing in a loop and iterate across the image. Or first convert it to a black and white image then send it.

关于使用 pyserial 发送文件 Using Pyserial to send a file?

如何使用 pyserial 读取数据 How to read data from pyserial incrementally?

这应该足以让你开始

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 2014-07-11
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    相关资源
    最近更新 更多