因此我们无法将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表示串行读取缓冲区中没有任何内容。
答案 0 :(得分:0)
我将您的代码与制造商提供的example sketch进行了比较,发现您的初始化过程与原始过程不同。在正式的示例代码中,他们首先以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()函数,因为它在初始化过程中使用。