5

所以我们在将 Arduino Uno 连接到 LinkSprite 相机时遇到了问题,我们一直在使用 LinkSprite 示例代码,并添加了一些打印语句

#include < SoftwareSerial.h >

  /* Linksprite */

byte incomingbyte;
SoftwareSerial mySerial(4, 5); //Configure pin 4 and 5 as soft serial port
long a = 0x0000, j = 0, k = 0, count = 0; //Read Starting address 
uint8_t MH, ML;
boolean EndFlag = 0;

void SendResetCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();

void setup() {
  Serial.begin(38400);
  mySerial.begin(38400);
  Serial.print("Serial began\n");
}

void loop() {
  SendResetCmd();
  Serial.print("Reset Command Sent\n");
  Serial.print(mySerial.available());
  Serial.print("\n");
  while (mySerial.available() > 0) {
    incomingbyte = mySerial.read();
    Serial.print(incomingbyte, HEX);
  } //After reset, wait 2-3 second to send take picture command

  Serial.print("Delay Ended\n");
  SendTakePhotoCmd();
  Serial.print("Take Photo Command Sent\n");
  while (mySerial.available() > 0) {
    Serial.print("Checking Available Bytes\n");
    incomingbyte = mySerial.read();

  }
  byte a[32];
  Serial.print("Byte array intialized\n");
  while (!EndFlag) {
    Serial.print("Entering While loop\n");
    j = 0;
    k = 0;
    count = 0;
    SendReadDataCmd();
    Serial.print("Read Command Sent\n");
    delay(250);
    Serial.print("Delay Ended\n");
    Serial.print(mySerial.available());
    Serial.print("\n");
    while (mySerial.available() > 0) {
      incomingbyte = mySerial.read();
      Serial.print("Incoming Byte Read\n");
      k++;
      if ((k > 5) && (j < 32) && (!EndFlag)) {
        Serial.print("Byte Added to Array\n");
        a[j] = incomingbyte;
        if ((a[j - 1] == 0xFF) && (a[j] == 0xD9)) { //Check if the picture is over
          Serial.print("End Flag");
          EndFlag = 1;
        }
        j++;
        count++;
      }
    }

    for (j = 0; j < count; j++) {
      if (a[j] < 0x10)
        Serial.print("0");
      Serial.print("Picture Printing\n");
      Serial.print(a[j], HEX);
      Serial.print(" ");
    } //Send jpeg picture over the serial port

    Serial.println();
    delay(10000);
  }
  Serial.print("Picture Complete");
  while (1);
}

//Send Reset command 
void SendResetCmd() {
  mySerial.write(0x56);
  mySerial.write(byte(0x00));
  mySerial.write(0x26);
  mySerial.write(byte(0x00));
}

//void SetImageSizeCmd() 
//{ 
//mySerial.write(0x56); 
//mySerial.write(byte(0x00)); 
//mySerial.write(0x31); 
//mySerial.write(0x05); 
//mySerial.write(0x04); 
//mySerial.write(0x01); 
//mySerial.write(byte(0x00)); 
//mySerial.write(0x19); 
//mySerial.write(0x22); 
//} 

//void SetBaudRateCmd() 
//{ 
//mySerial.write(0x56); 
//mySerial.write(byte(0x00)); 
//mySerial.write(0x24); 
//mySerial.write(0x03); 
//mySerial.write(0x01); 
//mySerial.write(0xAE); 
//mySerial.write(0xC8); 
//
//} 

//Send take picture command 
void SendTakePhotoCmd() {
  mySerial.write(0x56);
  mySerial.write(byte(0x00));
  mySerial.write(0x36);
  mySerial.write(0x01);
  mySerial.write(byte(0x00));
}

//Read data 
void SendReadDataCmd() {

  MH = a / 0x100;
  ML = a % 0x100;
  mySerial.write(0x56);
  mySerial.write(byte(0x00));
  mySerial.write(0x32);
  mySerial.write(0x0c);
  mySerial.write(byte(0x00));
  mySerial.write(0x0a);
  mySerial.write(byte(0x00));
  mySerial.write(byte(0x00));
  mySerial.write(MH);
  mySerial.write(ML);
  mySerial.write(byte(0x00));
  mySerial.write(byte(0x00));
  mySerial.write(byte(0x00));
  mySerial.write(0x20);
  mySerial.write(byte(0x00));
  mySerial.write(0x0a);
  a += 0x20; //address increases 32£¬set according to buffer size 

}

void StopTakePhotoCmd() {
  mySerial.write(0x56);
  mySerial.write(byte(0x00));
  mySerial.write(0x36);
  mySerial.write(0x01);
  mySerial.write(0x03);
}

`
该代码基本上向相机发送一个拍照命令,然后读取相机发送的 HEX 值并将这些值保存在一个数组中。我们知道相机可以正常工作,因为我们在另一台设备上对其进行了测试。

最初的问题是,我们从相机获得的值不正确。while 循环中的 EndFlag 永远不会切换,因为指示 JPEG 值(FF 和 D9)结束的 HEX 值永远不会被读取,因此它永远不会从 while 循环中中断。现在,在相机断开连接之前,终端不会打印任何内容,然后所有值似乎都会刷新到屏幕上,并且命令 mySerial.available() 返回 0,这意味着串行读取缓冲区中没有任何内容。

4

1 回答 1

0

我将您的代码与制造商提供的示例草图进行了比较,发现您的初始化过程与原始程序不同。在官方示例代码中,他们首先以 115200 串行波特率连接到相机,告诉相机将其波特率更改为 38400,然后他们再次以不同的速度连接到相机 - 38400 波特。

在您的代码中,我看到您一开始就以 38400 波特率连接。但是,相机以不同的速度运行,这解释了为什么您会得到垃圾而不是有效数据。我建议您执行正确的初始化过程,如示例中所述:

void setup()
{
    Serial.begin(38400);

    mySerial.begin(115200);
    delay(100);
    SendResetCmd();
    delay(2000);
    SetBaudRateCmd(0x2A);
    delay(500);
    mySerial.begin(38400);
    delay(100);

    Serial.println("initialization done.");
}

此外,取消注释 SetBaudRateCmd() 函数,因为它在初始化过程中使用。

于 2018-08-25T22:43:14.310 回答