Hi Everyone,
Well I have jumped into the deep end as I have not played with Arduino before and I am after a little advice on implementing hardware serial on an Arduino Mega 2560 (mainly the receiving). What I would like to do is implement all the hardware serial ports on the device and connect it to various pieces of other equipment implementing different bauds to send commands and monitor what is actually going on.
The first serial port Serial 0 is connected to the programmer as well as a LCD running 9600 baud for debug purposes
Serial 1 - 4800 baud
Serial 2 - 2400 baud
Serial 3 - 4800 baud
Basically all I need to do is wait for a Test command on serial 0 (T), once received then send out various sequences to each of the other Serial ports and check for the correct response to come back and either pass or fail. What I have implemented so far, sends out the data and only receives some of the data back. I am pretty sure that I am hung up in loops and missing the responses that should be be received back. I have looked at the various serial examples and not sure if I have implemented the receive correctly.
Here is the code I have got so far, if someone can point in the right direction that would be fantastic. I am not sure what size the serial buffers on the arduino. I will be re-reading the manual to get a bit more of an idea what is going on.
//Intialise Variables Needed
int greenled = 2;
int redled =3;
int i = 0;
byte tempByte;
bool SendTestSeq = false;
bool TestFAIL = false;
const int buttonPin = 13; // the number of the pushbutton pin
byte buf[6];
int buttonState = 0; // variable for reading the pushbutton status
byte TXBuff[16];
byte RXBuff[16];
byte RXBuff1[16];
int RX1Index;
byte RXBuff2[16]; // for incoming serial data
int RX2Index;
byte RXBuff3[16];
int RX3Index;
void serialEvent()
{
//Check Commands from LCD Screen +
char command = Serial.read();
Serial.write(command);
switch(command)
{
case 'T' : SendTestSequence(); //Send out Test Sequence
}
}
void serialEvent1()
{
RXBuff1[RX1Index] = Serial1.read();
RX1Index++;
}
void serialEvent2()
{
RXBuff2[RX2Index] = Serial2.read();
RX2Index++;
}
void serialEvent3()
{
RXBuff3[RX3Index] = Serial3.read();
RX3Index++;
}
void setup()
{
//Initialise Pins that are used
pinMode(greenled, OUTPUT);
pinMode(redled,OUTPUT);
// InitialiSe the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
// initialise Serial ports
Serial.begin(9600);
Serial1.begin(4800);
Serial2.begin(2400); //2400
Serial3.begin(4800);
RX1Index = 0;
RX2Index = 0;
RX3Index = 0;
}
void loop()
{
//FLASH LED to indicate working
digitalWrite(greenled, HIGH); // turn the GreenLED on (HIGH is the voltage level)
digitalWrite(redled,LOW);
delay(50);
digitalWrite(greenled, LOW); // turn the GreenLED on (HIGH is the voltage level)
digitalWrite(redled,HIGH);
delay(50);
//*********************************
//Check Serial Responses
//*********************************
if(RX1Index == 3)
{
//
// Serial.write("\n");
//Recieve Modem Data
TXBuff[0] = 0x64;
TXBuff[1] = 0x02;
TXBuff[2] = 0x66;
//Serial.write("\n");
if (array_cmp(TXBuff, RXBuff1, 2, 2) == true)
{
// do this if they are equal
Serial.write("Pass \n");
buf[0] = 0x50; //P
buf[1] = 0x31; //1
Serial.write(buf,2);
}
else
{
// do this if they are different
//Serial.write("Array Different\n");
buf[0] = 0x46;
buf[1] = 0x31;
Serial.write(buf,2);
}
//Reset Index
RX1Index = 0;
}
if(RX2Index == 6)
{
//
Serial.write("rx2\n");
//Recieve Modem Data
TXBuff[0] = 0xAA; //0x54 T
TXBuff[1] = 0x7B; //0x45 E
TXBuff[2] = 0x01; //0x53 S
TXBuff[3] = 0x03; //0x54 T
TXBuff[4] = 0x50; //0x45 E
TXBuff[5] = 0x54; //0x52 R
//Serial.write("\n");
if (array_cmp(TXBuff, RXBuff2, 5, 5) == true)
{
// do this if they are equal
//Serial.write("Array Same\n");
buf[0] = 0x50; //P
buf[1] = 0x32; //1
Serial.write(buf,2);
}
else
{
// do this if they are different
//Serial.write("Array Different\n");
buf[0] = 0x46;
buf[1] = 0x32;
Serial.write(buf,2);
}
//Reset Index
RX2Index = 0;
}
if(RX3Index == 8)
{
//
//Serial.write("\n");
//Recieve Modem Data
TXBuff[0] = 0x4E;
TXBuff[1] = 0x32;
TXBuff[2] = 0xAA;
TXBuff[3] = 0x7B;
TXBuff[4] = 0x01;
TXBuff[5] = 0x03;
TXBuff[6] = 0x50;
TXBuff[7] = 0x54;
//Serial.write("\n");
if (array_cmp(TXBuff, RXBuff3, 7, 7) == true)
{
// do this if they are equal
// Serial.write("Array Same\n");
buf[0] = 0x50; //P
buf[1] = 0x33; //1
Serial.write(buf,2);
}
else
{
// do this if they are different
//Serial.write("Array Different\n");
buf[0] = 0x46;
buf[1] = 0x33;
Serial.write(buf,2);
}
//Reset Index
RX3Index = 0;
}
}
boolean array_cmp(byte *a, byte *b, int len_a, int len_b){
int n;
// if their lengths are different, return false
if (len_a != len_b) return false;
// test each element to be the same. if not, return false
for (n=0;n<len_a;n++)
if (a[n]!=b[n])
return false;
//ok, if we have not returned yet, they are equal :)
return true;
}
void SendTestSequence()
{
//***********************
//Send out Test sequence
//***********************
//Channel Change
Serial.write("C");
TXBuff[0] = 0x64;
TXBuff[1] = 0x02;
TXBuff[2] = 0x66;
//Write Channel Change
//Serial.write("WriteCh\n");
//
Serial3.write(TXBuff, 3);
//Wait
delay(1000);
//Send Modem Data
TXBuff[0] = 0xAA;
TXBuff[1] = 0x7B;
TXBuff[2] = 0x01;
TXBuff[3] = 0x03;
TXBuff[4] = 0x80;
TXBuff[5] = 0x84;
//Write Send Modem Data
//
//Serial.write("WriteModem\n");
Serial.write("M");
Serial3.write(TXBuff, 6);
//Wait
delay(1000);
//Send ID Request
TXBuff[0] = 0x3F; // Send ?
TXBuff[1] = 0x13;
TXBuff[2] = 0x10;
//Write Send ID Request
// Display.putstr("Sending ID Request\n");
// Serial.write("WriteID\n");
Serial.write("I");
Serial3.write(TXBuff, 3);
//Wait
delay(1000);
//Recieve Modem Data
TXBuff[0] = 0xAA;
TXBuff[1] = 0x7B;
TXBuff[2] = 0x01;
TXBuff[3] = 0x03;
TXBuff[4] = 0x50;
TXBuff[5] = 0x54;
//
// Serial.write("WriteRXMod\n"); //
Serial.write("R");
Serial2.write(TXBuff, 6);
//Wait
delay(1000);
}
Thanks for the support, stuck noob.