【问题标题】:How to Get image from ComPort如何从 ComPort 获取图像
【发布时间】:2014-03-06 22:24:41
【问题描述】:

我在工作中遇到了一些问题.. 我已经在 Adafruit 教程 (learn.adafruit.com/ttl-serial-camera/overview) 的帮助下使用 Arduino UNO 成功地将 TTL 串行相机图像存储到 MicroSD 卡,但是当我通过 Zigbee 发射器传输这些图像时,在 comport (Zigbee接收者)我正在接收随机单词。我认为它的ASCII。 我想将从 comport 接收到的图像保存到我电脑的文件夹中。 可能吗? 我在一些使用java或python代码的论坛上看到过,但我不明白如何使用它? Read image data from COM7 port in Java

【问题讨论】:

    标签: java python jpeg


    【解决方案1】:

    我猜这就是你要找的东西:

    import serial
    ser = serial.Serial('/dev/tty.usbserial', 9600)
    image = ser.read()
    
    with open('/tmp/image', 'wb') as file:
        file.write(image)
    

    仅适用于 Python 3,在 Python 2 中您需要使用 io.open。如果您还没有安装 serial-modul,您可能需要先安装它。我不熟悉通过 com 端口发送图像所需的 Arduino-C 方言...

    【讨论】:

      【解决方案2】:

      Arduino IDESerial Monitor 使用Serial 类通过串行通信进行通信。港口。

      // Receive & send methods from the SerialMonitor class.
      private void send(String s) {
          ..
          serial.write(s);
      }
      
      public void message(final String s) {
          ..
          textArea.append(s);
      }
      

      我的建议是重用该 (Java) 代码,但由于 Serial 类是为纯文本通信设计的,因此您需要将图像字节编码为例如使用this library进行Base64编码并在PC上解码。

      如果传输速度很重要,并且有一个基于 Arduino 二进制的串行通信库,你应该使用它。

      更新

      您可以通过上述Serial 类从串行端口读取原始字节,如下所示:

      ...
      Serial port = ...;
      byte[] buffer = new byte[1024]; // 1KB buffer
      OutputStream imageOutput = new FileOutputStream(...);
      
      // Wait for the image.
      while (port.available() > 0) {
        int numBytes = port.readBytes(buffer);
        if (numBytes > 0) {
          imageOutput.write(buffer, numBytes);
        }
      }
      imageOutput.flush();
      imageOutput.close();
      ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多