Go Down

Topic: What is the Arduino Uno Serial buffer size? (Read 20411 times) previous topic - next topic

Blaylock1988

It wouldn't do me much good to calculate the data on the fly if the checksum doesn't match though, right? That would mean one or all of the data is faulty and everything I just calculated would need to be disregarded.

Blaylock1988

#31
Apr 01, 2012, 03:04 am Last Edit: Apr 01, 2012, 03:20 am by Blaylock1988 Reason: 1
I put the code that sends the request into a a function and it seems to send the initial request from the setup, then it reads it on the first iteration, and then sends the next request, but it does not read the second stream of data. I think this has something to do with the mySerial.available() function. Hopefully you guys might be able to spot the problem with my code:

Code: [Select]
/* 
This program is used for an Arduino to request and retrieve
data from an 07 Buell XB9SX Lightning ECM DDFI-2,
EEPROM Version: BUEIB

Currently the Arduino successfully sends a request code and
receives all 107 bytes of Real-time data, and displays
on the TPS on the LCD screen and all data via serial monitor.
The program is supposed to be running in real time, but only
shows the very first iteration. The problem may have something
to do with the mySerial.avalable() function.

Created by Michael Blaylock
*/

// include the library code:
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>

//new serial pins for ECM, 0 and 1 caused interferance from PC
#define rxPin 2
#define txPin 3

// set up a new serial port
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
byte inArray[9]; //request code for real-time data
byte outArray[107]; //real-time data series from ECM
byte long Value;
int IterationNumber = 0;
int ByteNumber;

void setup(){
  lcd.begin(16, 2);  // set up the LCD's number of columns and rows:
  mySerial.begin(9600);  // baud rate for the ECM is 9600
  Serial.begin(9600);
  ByteNumber = 0;
  inArray[0]=0x01; //SOH
  inArray[1]=0x00; //Emittend
  inArray[2]=0x42; //Recipient
  inArray[3]=0x02; //Data Size
  inArray[4]=0xFF; //EOH
  inArray[5]=0x02; //SOT
  inArray[6]=0x43; //Data 1 //0x56 = Get version, 0x43 = Get runttime data
  inArray[7]=0x03; //EOT
  inArray[8]=0xFD; //Checksum

  lcd.setCursor(0,0);
  //Stream request data series to ECM as HEX
  for (int i = 0; i<9; i+=1){
    mySerial.write(inArray[i]);
    lcd.print(inArray[i],HEX); //print sent HEX code
  }
  //mySerial.write(0x01 0x00 0x43 0x02 0xFF 0x02 0x43 0x03 0xFD);
}

//Send data request to ECM
void requestData() {
  for (int i = 0; i<9; i+=1){
    mySerial.write(inArray[i]);
    //lcd.setCursor(0,0);
    //lcd.print("printing"+i);
  }
  Serial.print("Data Request Sent");
  delay(100);
}

//display the array in serial monitor one time
void displayData(){
  //if(IterationNumber < 3){
    for (int j = 0; j<107; j+=1){
      Serial.println(outArray[j],HEX);
    }
    //IterationNumber ++;
    Serial.println("Data Printed");
  //}
}

//Display the Throttle Position Sensor
void TPS(){
  unsigned Value = outArray[91] << 8 | outArray[90];
  lcd.setCursor(0,1);
  lcd.print("TPS: ");
  lcd.print(Value);
}

void loop() {
  // fill buffer
  if (mySerial.available () > 0)
    outArray[ByteNumber ++] = mySerial.read();
  if (ByteNumber > 106){// process it
    Serial.println("Data Stream Read");
    ByteNumber = 0;  // ready for next time
    displayData();
    lcd.setCursor(1,1);
    TPS();
    requestData();
  }  // end if full
}  // end of loop


This is the response I get:

Code: [Select]
Data Stream Read
1
42
0
64
FF
2
6
7B
5A
0
0
0
0
0
0
0
0
BB
20
BB
20
38
36
38
36
33
0
F
4F
4
65
2
98
2
0
0
EE
2
1F
6
E8
3
0
0
E8
3
E8
3
E8
3
E8
3
30
4
30
4
0
80
0
0
0
0
0
0
0
FC
0
0
0
0
0
0
0
0
0
0
10
0
0
FF
FF
FF
FE
0
0
5
0
0
0
0
A1
0
1A
1
F0
BC
0
0
0
0
0
0
0
0
0
3
F5
Data Printed
Data Request Sent


My desire is for it to repeat this response over and over with newer run-time data, but like I mentioned, it isn't doing this like I thought it would.

I have not added any data verification yet with the header or checksum. So far the data coming in has been good and very consistent without skipping a beat, so I am going to focus on core functionality before I add in that feature.

EDIT: I just thought of something, would clearing the mySerial buffer before requesting more data help?

wildbill

Does it still work (once) if you call requestData from setup rather than replicating the send loop there?

Blaylock1988

Yeah, you can see by the data it works the first time. I call the same bit of code from the function for a second and it seems to send the request but no data appears to be read and I don't know why.

wildbill

I mean does it work if you call the function rather than replicating what it does, because they are not quite the same - the LCD print in setup introduces a (small) delay. I was wondering whether that difference is important.

Blaylock1988

#35
Apr 01, 2012, 10:05 pm Last Edit: Apr 02, 2012, 01:32 am by Blaylock1988 Reason: 1
GOOD NEWS!!!!

I got it working! It displays the information on my LCD screen quickly and in real-time. I will post a video of it in a few hours. The problem I was having was the location of the display() function. It was interfering with the data coming in and non of it would get read. I just removed it because I won't need since the arduino will be a stand alone on the bike with no serial monitor connected.

Thank you so much to everyone for the help, there is still a lot left I have to do. The main part of my project can start moving forward now with calculating torque, horsepower, and creating a shift indicator. I am excited!

Thanks again, and video coming soon!

Code: [Select]
/*  
This program is used for an Arduino to request and retrieve
data from an 07 Buell XB9SX Lightning ECM DDFI-2,
EEPROM Version: BUEIB

Currently the Arduino successfully sends a request code and
receives all 107 bytes of Real-time data, and displays
on the TPS on the LCD screen and all data via serial monitor.

Created by Michael Blaylock
*/

// include the library code:
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>

//new serial pins for ECM, 0 and 1 caused interferance from PC
#define rxPin 2
#define txPin 3

// set up a new serial port
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
byte inArray[9]; //request code for real-time data
byte outArray[107]; //real-time data series from ECM
byte long TPS;
byte long ETemp;
int IterationNumber = 0;
int ByteNumber;
int Processing = 0;

void setup(){
 lcd.begin(16, 2);  // set up the LCD's number of columns and rows:
 mySerial.begin(9600);  // baud rate for the ECM is 9600
 Serial.begin(9600);
 ByteNumber = 0;
 inArray[0]=0x01; //SOH
 inArray[1]=0x00; //Emittend
 inArray[2]=0x42; //Recipient
 inArray[3]=0x02; //Data Size
 inArray[4]=0xFF; //EOH
 inArray[5]=0x02; //SOT
 inArray[6]=0x43; //Data 1 //0x56 = Get version, 0x43 = Get runttime data
 inArray[7]=0x03; //EOT
 inArray[8]=0xFD; //Checksum

 //mySerial.write(0x01 0x00 0x43 0x02 0xFF 0x02 0x43 0x03 0xFD);
 requestData();
}

//Send data request to ECM
void requestData() {
 //lcd.setCursor(0,0);
 for (int i = 0; i<9; i+=1)
   mySerial.write(inArray[i]);
 //Do not place code here
}

//display the array in serial monitor
void displayData(){
 for (int j = 0; j<107; j+=1){
   Serial.println(outArray[j],HEX);
 }
 Serial.println("Data Printed");
 Processing = 0;
}

//Display the Throttle Position Sensor
void callTPS(){
 unsigned TPS = outArray[91] << 8 | outArray[90];
 lcd.setCursor(0,0); //right then down
 lcd.print("TPS: ");
 lcd.print(TPS);
}

void callETemp(){
 unsigned ETemp = (outArray[31] << 8 | outArray[30]) * 0.18 -40;
 lcd.setCursor(0,1); //right then down
 lcd.print("ETemp: ");
 lcd.print(ETemp);
 lcd.print("degF");
}

void loop() {
 if (mySerial.available () > 0) {
   outArray[ByteNumber ++] = mySerial.read();
 }
 if (ByteNumber > 106){// process it
   Serial.println("Data Stream Read");
   //Processing = 0;
   ByteNumber = 0;  // ready for next time
   //displayData();
   lcd.setCursor(1,1);
   callTPS();
   callETemp();
   requestData();  //restart data
   //Do not place code here
 }  // end if full
}  // end of loop


http://www.youtube.com/watch?v=uztKs8W0mi4

sathopper


GOOD NEWS!!!!
I got it working!


Hi Michael,

Thanks to your program I managed to receive serial PELCO commands. These commands are used to control
CCTV camera systems.  :)

http://forum.arduino.cc/index.php?PHPSESSID=bhv3n6da4s1f2a8d0b3ip2er66&topic=155693.0

Go Up