所以我们在将 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,这意味着串行读取缓冲区中没有任何内容。