繁体   English   中英

如何通过串行端口接收JPEG图像

[英]How to receive a JPEG image over serial port

因此,我尝试使用Xbee系列1从覆盆子pi向我的Mac无线发送jpeg图像(4Kb)。我在树莓派上有一个图像,可以将其读成二进制格式。 我已经使用这种二进制格式将其保存到另一个图像文件中,并正确地创建了图像的副本。 这告诉我,我正在读它。 所以我试图通过串行端口(由xbee传输)将数据发送到我的Mac。 旁注,Xbee只能传输我认为每个数据包80个字节的数据。 我不知道这会影响我正在做的事情。

我的问题是,我不知道如何读取数据并将其妥善存储到jpeg文件本身。 我发现的大多数Read()函数都要求你输入一个长度来读取,我不知道如何判断它只是一个串行流进来的时间。

这是我发送jpeg的代码。

#include "xSerial.hpp"
#include <iostream>
#include <cstdlib>
using namespace std;

int copy_file( const char* srcfilename, const char* dstfilename );


int main(){

copy_file("tylerUseThisImage.jpeg", "copyImage.jpeg");

return 0;
}

int copy_file( const char* srcfilename, const char* dstfilename )
  {
  long  len;
  char* buf = NULL;
  FILE* fp  = NULL;

  // Open the source file
  fp = fopen( srcfilename, "rb" );
  if (!fp) return 0;

  // Get its length (in bytes)
  if (fseek( fp, 0, SEEK_END ) != 0)  // This should typically succeed
    {                                 // (beware the 2Gb limitation, though)
    fclose( fp );
    return 0;
    }

  len = ftell( fp );
  std::cout << len;
  rewind( fp );
  // Get a buffer big enough to hold it entirely
  buf = (char*)malloc( len );
  if (!buf)
    {
    fclose( fp );
    return 0;
    }

  // Read the entire file into the buffer
  if (!fread( buf, len, 1, fp ))
    {
    free( buf );
    fclose( fp );
    return 0;
    }

  fclose( fp );

  // Open the destination file
  fp = fopen( dstfilename, "wb" );
  if (!fp)
    {
    free( buf );
    return 0;
    }
  // this is where I send data in but over serial port. 
  //serialWrite() is just the standard write() being used
  int fd;
  fd = xserialOpen("/dev/ttyUSB0", 9600);
  serialWrite(fd, buf, len);

   //This is where the file gets copied to another file as a test
  // Write the entire buffer to file
  if (!fwrite( buf, len, 1, fp ))
    {
    free( buf );
    fclose( fp );
    return 0;
    }

  // All done -- return success
  fclose( fp );
  free( buf );
  return 1;
  }

在接收端,我知道我需要打开串口来读取和使用某种read()但我不知道是怎么做的。 使用串行库,它具有一些功能来检查串行数据是否可用并返回可读取的字符数。

有关可读字符数的一个问题是,该数字会随着串行流的过来而增长,还是会立即告诉读取数据的整个长度?

但最后,我知道在打开串口后,我需要将数据读入缓冲区,然后将该缓冲区写入文件,但我没有运气。 这是我到目前为止所尝试的。

// Loop, getting and printing characters
char temp;
bool readComplete = false;
int bytesRead = 0;

fp = fopen("copyImage11.jpeg", "rwb");

  for (;;)
  {
        if(xserialDataAvail(fd) > 0)
        {
        bytesRead = serialRead(fd, buf, len);
        readComplete = true;
        }
        if (readComplete)
        {
         if (!fwrite(buf, bytesRead, 1, fp))
         {
         free(buf);
         fclose(fp);
         return 0;
         }

         fclose(fp);
         free(buf);
         return 1;
        }
}

我没有得到我的代码错误,它只是没有正确创建jpeg文件。 也许我没有正确传输它,或者我可能没有正确地读/写文件。 任何帮助,将不胜感激。 谢谢大家摇滚!

如果您要定义自己的协议,那么您需要首先发送长度的方法。

我建议通过发送短块的ascii文本来测试你的代码,以确认你的i / o。 一旦工作,你可以使用ascii来设置转移; 即发送长度,并让你的接收器准备好预期的阻止。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM