问题
I'm trying to interface (1) LinkSprite JPEG Color Camera TTL Interface - Infrared and (2) Arduino Mega 2560 connected to my laptop. While I am able to print the HEX values of the images, it takes about 30 seconds to print 1 image to the monitor. I thought that it was because I was using SoftwareSerial, so I tried HardwareSerial, but still, 30 seconds per image. Shouldn't it be faster using HardwareSerial? Just wondering, do I need a special cable connecting the arduino to my laptop?
I tried different combinations of baud rates for Serial and Serial1. (Serial.begin(9600), Serial1.begin(38400)), (Serial.begin(38400), Serial1.begin(38400)) etc... This doesn't work when I set Serial1 anything higher than 38400. (It should be able to go higher..) Also, do I have to increase baud rate by a certain interval namely, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 250000?
#include <SoftwareSerial.h>
byte incomingbyte;
//Configure pin 2 and 3 as soft serial port
//SoftwareSerial Serial1 = SoftwareSerial(2, 3);
int a=0x0000, //Read Starting address
j=0,
k=0,
count=0;
uint8_t MH,ML;
boolean EndFlag=0;
void setup() {
Serial.begin(115200);
Serial1.begin(38400); //made changes in ChangeBaudRate()
ChangeBaudRate();[enter image description here][1]
SendResetCmd();
delay(3000);
}
void loop() {
SendTakePhotoCmd();
Serial.println("Start pic");
delay(100);
while(Serial1.available()>0) {
incomingbyte=Serial1.read();
}
byte b[32];
while(!EndFlag) {
j=0;
k=0;
count=0;
SendReadDataCmd();
delay(75); //try going up
while(Serial1.available()>0) {
incomingbyte=Serial1.read();
k++;
if((k>5)&&(j<32)&&(!EndFlag)) {
b[j]=incomingbyte;
if((b[j-1]==0xFF)&&(b[j]==0xD9))
EndFlag=1;
j++;
count++;
}
}
for(j=0;j<count;j++) {
if(b[j]<0x10)
Serial.print("0");
Serial.print(b[j], HEX);
}
Serial.println();
}
delay(3000);
StopTakePhotoCmd(); //stop this picture so another one can be taken
EndFlag = 0; //reset flag to allow another picture to be read
Serial.println("End of pic");
Serial.println();
while(1);
}
//Send Reset command
void SendResetCmd() {
Serial1.write((byte)0x56);
Serial1.write((byte)0x00);
Serial1.write((byte)0x26);
Serial1.write((byte)0x00);
}
//Send take picture command
void SendTakePhotoCmd() {
Serial1.write((byte)0x56);
Serial1.write((byte)0x00);
Serial1.write((byte)0x36);
Serial1.write((byte)0x01);
Serial1.write((byte)0x00);
a = 0x0000; //reset so that another picture can taken
}
void FrameSize() {
Serial1.write((byte)0x56);
Serial1.write((byte)0x00);
Serial1.write((byte)0x34);
Serial1.write((byte)0x01);
Serial1.write((byte)0x00);
}
//Read data
void SendReadDataCmd() {
MH=a/0x100;
ML=a%0x100;
Serial1.write((byte)0x56);
Serial1.write((byte)0x00);
Serial1.write((byte)0x32);
Serial1.write((byte)0x0c);
Serial1.write((byte)0x00);
Serial1.write((byte)0x0a);
Serial1.write((byte)0x00);
Serial1.write((byte)0x00);
Serial1.write((byte)MH);
Serial1.write((byte)ML);
Serial1.write((byte)0x00);
Serial1.write((byte)0x00);
Serial1.write((byte)0x00);
Serial1.write((byte)0x20);
Serial1.write((byte)0x00);
Serial1.write((byte)0x0a);
a+=0x20;
}
void StopTakePhotoCmd() {
Serial1.write((byte)0x56);
Serial1.write((byte)0x00);
Serial1.write((byte)0x36);
Serial1.write((byte)0x01);
Serial1.write((byte)0x03);
}
void ChangeBaudRate(){
Serial1.write((byte)0x56);
Serial1.write((byte)0x00);
Serial1.write((byte)0x24);
Serial1.write((byte)0x03);
Serial1.write((byte)0x01);
Serial1.write((byte)0x0D); // 115200 = 0x0D 0xA6
Serial1.write((byte)0xA6);
Serial1.end(); // Not really necessary
Serial1.begin( 115200 ); // change to match the camera's new baud rate
}
回答1:
Yes, the baud rate between the Arduino and the camera is the determining factor. The baud rate between the Arduino and the PC (i.e., the Serial Monitor window) is 9600, but you can change that to 115200 in the code with Serial.begin(115200);
and in the Serial Monitor window (pulldown in corner).
The default baud rate between the Arduino and the camera is 38400. While at that baud rate, you can send the "Change Baud Rate" command (section 9 on page 6/10 of the spec). That seems to be the purpose of this routine:
void ChangeBaudRate(){
Serial1.write((byte)0x56);
Serial1.write((byte)0x00);
Serial1.write((byte)0x24);
Serial1.write((byte)0x03);
Serial1.write((byte)0x01);
Serial1.write((byte)0x1c);
Serial1.write((byte)0x4c);
}
According to the spec, this sets the new baud rate to 57600. You could choose 115200 by changing the last two bytes to 0x0D and 0xA6. Then you have to set Serial1
to the new baud rate:
void ChangeBaudRate(){
Serial1.write((byte)0x56);
Serial1.write((byte)0x00);
Serial1.write((byte)0x24);
Serial1.write((byte)0x03);
Serial1.write((byte)0x01);
Serial1.write((byte)0x0D); // 115200 = 0x0D 0xA6
Serial1.write((byte)0xA6);
Serial1.end(); // Not really necessary
Serial1.begin( 115200 ); // change to match the camera's new baud rate
}
来源:https://stackoverflow.com/questions/34100518/why-am-i-getting-the-same-performance-from-hardware-serial-and-software-serial