为什么我从硬件串行和软件串行获得相同的性能? [英] Why am I getting the same performance from Hardware Serial and Software Serial?

查看:32
本文介绍了为什么我从硬件串行和软件串行获得相同的性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 (1) LinkSprite JPEG 彩色相机 TTL 接口 - 红外线和 (2) Arduino Mega 2560 连接到我的笔记本电脑.虽然我能够打印图像的十六进制值,但将 1 个图像打印到显示器需要大约 30 秒.我认为这是因为我使用的是 SoftwareSerial,所以我尝试了 HardwareSerial,但每个图像仍然是 30 秒.使用 HardwareSerial 不应该更快吗?只是想知道,我需要一根特殊的电缆将 arduino 连接到我的笔记本电脑吗?

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?

我尝试了 Serial 和 Serial1 的不同波特率组合.(Serial.begin(9600), Serial1.begin(38400)), (Serial.begin(38400), Serial1.begin(38400)) 等等......当我将 Serial1 设置为高于 38400 的任何值时,这不起作用.(应该可以更高..) 另外,我是否必须将波特率提高一定的间隔,即 9600、19200、38400、57600、74880、115200、230400、250000?

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
}

推荐答案

是的,Arduino 和相机之间的波特率是决定性因素.Arduino 和 PC(即串行监视器窗口)之间的波特率是 9600,但您可以在代码中使用 Serial.begin(115200); 和串行监视器将其更改为 115200窗口(角落里的下拉菜单).

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).

Arduino 和相机之间的默认波特率为 38400.在该波特率下,您可以发送更改波特率"命令(spec).这似乎是这个例程的目的:

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);   
}

根据规范,这将新的波特率设置为 57600.您可以通过将最后两个字节更改为 0x0D 和 0xA6 来选择 115200.然后您必须将Serial1设置为新的波特率:

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
}

这篇关于为什么我从硬件串行和软件串行获得相同的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆