Hello all, here are the two codes I'm using to get two boards to communicate. But it seems that they can't as I'm not getting any packets received. Both boards are powered by the computer through the USB they are connected to.
Here is the transmitter code:
//This program demonstrates a reliable transport protocol
int txData = 0; //This variable holds the data to be TX'd
int rxData = 0; //This variable holds the RX'd data
int msgID = 0; //Msg ID number
int txCounter = 0; //Records the number of msg TX attempts
int delayLength = 100; //Length of time in which ACK should be received
int on = 72; //H = On
int off = 76; //L = Off
const int green =11 ; //Pin which shows successful message TX
const int yellow =12 ; //Pin which shows message re-TX
const int red = 13; //Pin which shows message TX failure after 4 attempts
int AckCount = 0; //ACK recvd counter
int nackCount = 0; //NACK recvd counter
int msgCounter = 0; //No of messages TX'd
int loopCount = 0;
boolean msgFlag = true; //true = ON, false = OFF
int maxMsg = 20;
float per, failRate; //Packet Error Rate
int failedPkt = 0; //Discarded packet counter
int txAttempts = 0;
void setup() {
Serial.begin(9600);
pinMode(green, OUTPUT); //Set pin 11 as an o/p pin
pinMode(yellow, OUTPUT); //Set pin 12 as an o/p pin
pinMode(red, OUTPUT); //Set pin 12 as an o/p pin
txData = on; //Send "H"
loopCount = maxMsg;
}
void loop(){
while (loopCount > 0) {
msgFlag = true; //Send "H"
txMsg(); //TX an "ON" message
msgFlag = false; //Send "L"
txMsg(); //TX an "OFF" message
}
//Print all counter values
Serial.println("****************Finished********** ");
Serial.print("No of messages transmitted: ");
Serial.println(msgID,DEC);
Serial.print("No of ACK's received: ");
Serial.println(AckCount,DEC);
Serial.print("No of NACK's received: ");
Serial.println(nackCount,DEC);
Serial.print("Number of transmission attempts: ");
Serial.println(txAttempts,DEC);
Serial.print("Packet Error Rate (%): ");
per = ((float) nackCount/ (float)txAttempts)*100;
Serial.println(per);
Serial.print("Msg Delivery Success Rate (%): ");
failRate = (((float) msgID - (float) failedPkt)/ (float) msgID)*100;
Serial.println(failRate);
Serial.print('\n');
//Reset all the variables
loopCount = maxMsg;
AckCount = 0;
txCounter = 0;
nackCount = 0;
per = 0;
msgID = 0;
failedPkt = 0;
failRate = 0;
txAttempts = 0;
Serial.println("Waiting for 10 seconds before restarting....... ");
delay (10000); //Wait 20 seconds
}
//Message transmission function
void txMsg(){
if (msgFlag)
{
txData = on; //TX "H"
}
else
{
txData = off; //TX "L"
}
Serial.println(txData,BYTE);
loopCount--; //Increment MSG counter
txAttempts++;
ackHandler(); //Call reliable delivery function ie ACK handler
}
void ackHandler(){
delay(delayLength); //Wait 120ms for ACK
if ((Serial.available() > 0) ) //Any data received on the serial port?
{
rxData = Serial.read();
if (rxData == 49) //ie if an ACK is received 49 == 1 in ASCII
{
digitalWrite(green, HIGH);
delay(1000); //Turn on green LED for 1 sec
digitalWrite(green, LOW);
Serial.println("ACK received");
msgID++; //Increment MSG ID
AckCount++; //Increment ACK counter
txCounter= 0; //reset re-transmit counter
rxData = 0; //Reset receive buffer contents to eliminate false positves
}
else
{
nackCount++; //Msg received is not an ACK, increment the NACK counter
txCounter++;
digitalWrite(yellow, HIGH);
delay(1000); //Turn on red LED for 1 sec
digitalWrite(yellow, LOW);
rxData = 0;
if (txCounter > 4)
{
Serial.println("Exceeded maximum number of Re-transmission attempts");
failedPkt++;
txCounter= 0;
digitalWrite(red, HIGH);
delay(1000); //Turn on red LED for 1 sec
digitalWrite(red, LOW);
}
else
{
Serial.print("Re-transmit attempts: ");
Serial.println(txCounter, DEC);
txMsg(); //Re-transmit data
}
}
}
else
{
txCounter++;
nackCount++; //Increment NACK count
digitalWrite(yellow, HIGH);
delay(1000); //Turn on yellow LED for 1 sec
digitalWrite(yellow, LOW);
rxData = 0;
if (txCounter > 4)
{
Serial.println("Exceeded maximum number of Re-transmission attempts");
failedPkt++;
txCounter= 0;
digitalWrite(red, HIGH);
delay(1000); //Turn on red LED for 1 sec
digitalWrite(red, LOW);
}
else
{
Serial.print("Re-transmit attempts: ");
Serial.println(txCounter, DEC);
txMsg(); //Re-transmit data
}
}
}
And here is the receiver:
const int ledPin = 13; // the pin that the LED is attached to
const int ledOffPin = 12; // the pin that the LED is attached to
int incomingByte; // a variable to read incoming serial data into
int ACK = 1;
void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(ledOffPin, OUTPUT);
}
void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
// if it's a capital H (ASCII 72), turn on the LED:
if (incomingByte == 'H') {
digitalWrite(ledPin, HIGH);
Serial.print(ACK,BIN); //(ACK, DEC) implies that 101 => '101', (ACK, BYTE) implies that 101 => 'e' and then ACK = e
delay(200); //Should use Serial.print instead of Serial.println bcos the latter adds /r and /n to the sent msg
digitalWrite(ledPin, LOW);
}
// if it's an L (ASCII 76) turn off the LED:
if (incomingByte == 'L') {
digitalWrite(ledOffPin, HIGH);
Serial.print(ACK,BIN);
delay(200);
digitalWrite(ledOffPin, LOW);
}
}
}