Go Down

Topic: Problem using multiple serial ports on due (Read 875 times) previous topic - next topic

fxmech

Oct 14, 2013, 09:08 pm Last Edit: Oct 14, 2013, 09:10 pm by fxmech Reason: 1
Hi, I am using the Due for the first time and am having a problem with achieving dual serial communication

I basically have two sensor units each has a NANO receiving analog data  from 5 sensors each and sending it out via serial (tx pin)

At the receiving end I have a single DUE receiving data from sensor unit A on serial port #2 and data from Sensor unit B on serial port #3
I am also using the main Serial port on the DUE to serial print to the Serial monitor so I can see what is happening.

When I read the 5 analog  sensor values (0-1024) they are processed by the arduino Nano (map, constrain, etc) then the final values are sent out the serial port with a letter prefixing the value (sensor 1 gets Serial.print ('A'); Serial.println(value);, sensor two gets a 'B', etc)

When the data is received by the DUE it comes in on two seperate ports.


Here is the TX sketch:

Code: [Select]
//limits for sensor range
int digit1UL=330;
int digit1LL=330;
int digit2UL=330;
int digit2LL=330;
int digit3UL=330;
int digit3LL=330;
int digit4UL=330;
int digit4LL=330;
int digit5UL=330;
int digit5LL=330;

int del=(100); // delay if needed to keep serial from overflowing
int aux; //aux switch value (on/off)


void setup(){
 Serial.begin(9600);
 pinMode(3, INPUT); //Lower Limit
 pinMode(4, INPUT);// Upper limit
 pinMode(6, INPUT);// Aux switch
}

void loop(){
 // read all sensors
 int buttonA=digitalRead(3);// Upper limit
 int buttonB=digitalRead(4);//Lower limit
 int buttonC=digitalRead(6);//Aux switch
 int thumbSensorReading1 = analogRead(A0);
 int indexSensorReading2 = analogRead(A1);
 int middleSensorReading3 = analogRead(A2);
 int ringSensorReading4 = analogRead(A3);
 int pinkySensorReading5 = analogRead(A4);

 //calibrate unit
 if (buttonA==HIGH && buttonB==LOW){ //Button A pressed = high limit set
   digit1UL=thumbSensorReading1;
   digit2UL=indexSensorReading2;
   digit3UL=middleSensorReading3;
   digit4UL=ringSensorReading4;
   digit5UL=pinkySensorReading5;
 }

 if (buttonB==HIGH && buttonA==LOW){ //Button B pressed = low limit set
   digit1LL=thumbSensorReading1;
   digit2LL=indexSensorReading2;
   digit3LL=middleSensorReading3;
   digit4LL=ringSensorReading4;
   digit5LL=pinkySensorReading5;
 }

 if (buttonC==HIGH ) //Button C pressed = Aux ON
   aux=1024;
 else aux=0;


 //modify sensor values based on calibration parameters
 int adjustedReading1=map(thumbSensorReading1,digit1LL,digit1UL,1024,0);
 adjustedReading1=constrain(adjustedReading1,0,1024);
 int adjustedReading2=map(indexSensorReading2,digit2LL,digit2UL,1024,0);
 adjustedReading2=constrain(adjustedReading2,0,1024);
 int adjustedReading3=map(middleSensorReading3,digit3LL,digit3UL,1024,0);
 adjustedReading3=constrain(adjustedReading3,0,1024);
 int adjustedReading4=map(ringSensorReading4,digit4LL,digit4UL,1024,0);
 adjustedReading4=constrain(adjustedReading4,0,1024);
 int adjustedReading5=map(pinkySensorReading5,digit5LL,digit5UL,1024,0);
 adjustedReading5=constrain(adjustedReading5,0,1024);


 //OUTPUT DATA TO SERIAL PORT
 Serial.print('A'); //Header character to let receiver know what sensor the value received is associated with
 Serial.println(adjustedReading1); // send actual sensor value
 delay(del); //  here in case a delay is needed
 Serial.print('B');
 Serial.println(adjustedReading2);
 delay(del);
 Serial.print('C');
 Serial.println(adjustedReading3);
 delay(del);
 Serial.print('D');
 Serial.println(adjustedReading4);
 delay(del);
 Serial.print('E');
 Serial.println(adjustedReading5);
 delay(del);
 Serial.print('F'); // this is the auxillary switch (on or off)
 Serial.println(aux);
 delay(del);
}










Here is my receiving sketch so far (doesn't do anything yet except receive data and put it on the screen):


Code: [Select]

char code2;  //variable to store lettter for sensor unit A
char code3;  //variable to store lettter for Sensor unit B
int value2;  //variable to store integer value for sensor unit A
int value3;  //variable to store integer value for Sensor unit B
int del=0;  //delay value if needed


void setup() {
 Serial.begin(9600);  //initialize serial for displaying data to screen
 Serial2.begin(9600); //initialize serial for receiving Sensor unit A data
 Serial3.begin(9600); //initialize serial for receiving Sensor unit B data
}

void loop()
{
readSerial2(); //read serial port function Sensor unit A
readSerial3(); //read serial port function Sensor unit B

 if (code2=='A')   {
   Serial.print("2value A: ");
   Serial.println(value2);
 }
 if (code2=='B')   {
   Serial.print("2value B: ");
   Serial.println(value2);
 }    
 if (code2=='C')   {
   Serial.print("2value C: ");
   Serial.println(value2);
 }
 if (code2=='D')   {
   Serial.print("2value D: ");
   Serial.println(value2);
 }
 if (code2=='E')   {
   Serial.print("2value E: ");
   Serial.println(value2);
 }
 if (code2=='F')   {
   Serial.print("2value F: ");
   Serial.println(value2);
 }


 if (code3=='A')   {
   Serial.print("3value A: ");
   Serial.println(value3);
 }
 if (code3=='B')   {
   Serial.print("3value B: ");
   Serial.println(value3);
 }      
 if (code3=='C')   {
   Serial.print("3value C: ");
   Serial.println(value3);
 }
 if (code3=='D')   {
   Serial.print("3value D: ");
   Serial.println(value3);
 }
 if (code3=='E')   {
   Serial.print("3value E: ");
   Serial.println(value3);
 }
 if (code3=='F')   {
   Serial.print("3value F: ");
   Serial.println(value3);
 }

 delay(del); //delay if needed

}

void readSerial2(){  //read serial port function for Sensor unit A data IN
 while (Serial2.available() > 0) { //wait for something to come through serial port
   int a = Serial2.peek(); //peek into data to see if its a letter or number
   if (a<48 || a>57) code2=Serial2.read(); //if its a value other than 0-9 read it and load it into text variable
  else value2= Serial2.parseInt(); //if not then it must be a number so parse it and save it to integer variable
 }
}

void readSerial3(){  //read serial port function for Sensor unit B data IN
 while (Serial3.available() > 0) { //wait for something to come through serial port
   int a = Serial3.peek(); //peek into data to see if its a letter or number
   if (a<48 || a>57) code3=Serial3.read(); //if its a value other than 0-9 read it and load it into text variable
  else value3= Serial3.parseInt(); //if not then it must be a number so parse it and save it to integer variable
 }
}



the results have  Sensor A showing sensor F values so its like its reading the number first and not the letter so it assigns the value to the incorrect letter.

Data stream looks like this:
2value A: 0
3value E: 1024
2value B: 1023
3value F: 1024
2value C: 1024
3value A: 0
2value D: 1024
3value B: 1024
2value E: 1024
3value C: 1024
2value F: 1023
3value D: 1024
2value A: 0


2value F: should be the 0 value not 2 valueA...
Is there a better way to read data via two serial ports simultaneously?
Also any ideas why my data is shifted from the identifier character?


Would this be better to ask in the networking section instead???

Paul Stoffregen

Have you tried using Serial.begin() on due with a baud rate of 38400 or faster?  Or anything more than double the 2 incoming baud rates, so Due is always capable of retransmitting the data...

TheKitty

Break it down into two problems.  Put a nano on the desk with serial and see what data it passes to your console.  Once that is correct, reconnect serial to the Due to see if the Due gets the same values you got with the nano alone.

Go Up