Pages: 1 [2]   Go Down
Author Topic: Trouble receiving Serial data correctly from two serial ports  (Read 1047 times)
0 Members and 1 Guest are viewing this topic.
Los Angeles
Offline Offline
Full Member
***
Karma: 1
Posts: 148
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

RX CODE:
Code:
// VR GLOVE RX SKETCH
// Based on Nick Gammons code www.gammon.com.au/forum/?id=11425

typedef enum { 
  NONE, GOT_A, GOT_B, GOT_C, GOT_D, GOT_E, GOT_F, GOT_AA, GOT_BB, GOT_CC, GOT_DD, GOT_EE, GOT_FF}
states;
states stateA = NONE;
states stateB = NONE;
unsigned int currentValueA;
unsigned int currentValueB;
int currentA;
int currentB;
int currentC;
int currentD;
int currentE;
int currentF;
int currentAA;
int currentBB;
int currentCC;
int currentDD;
int currentEE;
int currentFF;

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

void loop()
{
  if (Serial2.available ())
    processIncomingByteA (Serial2.read ());
  if (Serial3.available ())
    processIncomingByteB (Serial3.read ());

  //DEBUG
  Serial.print("  currentA=");
  Serial.print(currentA);
  /*
Serial.print("  currentB=");Serial.print(currentB);
   Serial.print("  currentC=");Serial.print(currentC);
   Serial.print("  currentD=");Serial.print(currentD);
   Serial.print("  currentE=");Serial.print(currentE);
   Serial.print("  currentF=");Serial.print(currentF);
   Serial.print("  currentAA=");Serial.print(currentAA);
   Serial.print("  currentBB=");Serial.print(currentBB);
   Serial.print("  currentCC=");Serial.print(currentCC);
   Serial.print("  currentDD=");Serial.print(currentDD);
   Serial.print("  currentEE=");Serial.print(currentEE);
   Serial.print("  currentFF=");Serial.print(currentFF);
   */
  Serial.println();
  //delay(10);
}


void processA (const unsigned int valueA)
{
  currentA=valueA;
}

void processB (const unsigned int valueA)
{
  currentB=valueA;
}

void processC (const unsigned int valueA)
{
  currentC=valueA;
}

void processD (const unsigned int valueA)
{
  currentD=valueA;
}

void processE (const unsigned int valueA)
{
  currentE=valueA;
}

void processF (const unsigned int valueA)
{
  currentF=valueA;
}

void processAA (const unsigned int valueB)
{
  currentAA=valueB;
}

void processBB (const unsigned int valueB)
{
  currentBB=valueB;
}

void processCC (const unsigned int valueB)
{
  currentCC=valueB;
}

void processDD (const unsigned int valueB)
{
  currentDD=valueB;
}

void processEE (const unsigned int valueB)
{
  currentEE=valueB;
}

void processFF (const unsigned int valueB)
{
  currentFF=valueB;
}

void handlePreviousStateA ()
{
  switch (stateA)
  {
  case GOT_A:
    processA (currentValueA);
    break;
  case GOT_B:
    processB (currentValueA);
    break;
  case GOT_C:
    processC (currentValueA);
    break;
  case GOT_D:
    processD (currentValueA);
    break;
  case GOT_E:
    processE (currentValueA);
    break;
  case GOT_F:
    processF (currentValueA);
    break;

  }  // end of switch 

  currentValueA = 0;
}  // end of handlePreviousState


void handlePreviousStateB ()
{
  switch (stateB)
  {
  case GOT_AA:
    processAA (currentValueB);
    break;
  case GOT_BB:
    processBB (currentValueB);
    break;
  case GOT_CC:
    processCC (currentValueB);
    break;
  case GOT_DD:
    processDD (currentValueB);
    break;
  case GOT_EE:
    processEE (currentValueB);
    break;
  case GOT_FF:
    processFF (currentValueB);
    break;

  }  // end of switch 

  currentValueB = 0;
}  // end of handlePreviousState



void processIncomingByteA (const byte c)
{
  if (isdigit (c))
  {
    currentValueA *= 10;
    currentValueA += c - '0';
  }  // end of digit
  else
  {

    // The end of the number signals a state change
    handlePreviousStateA ();

    // set the new state, if we recognize it
    switch (c)
    {
    case 'A':
      stateA = GOT_A;
      break;
    case 'B':
      stateA = GOT_B;
      break;
    case 'C':
      stateA = GOT_C;
      break;
    case 'D':
      stateA = GOT_D;
      break;
    case 'E':
      stateA = GOT_E;
      break;
    case 'F':
      stateA = GOT_F;
      break;

    default:
      stateA = NONE;
      break;
    }  // end of switch on incoming byte
  } // end of not digit 

} // end of processIncomingByte



void processIncomingByteB (const byte c)
{
  if (isdigit (c))
  {
    currentValueB *= 10;
    currentValueB += c - '0';
  }  // end of digit
  else
  {

    // The end of the number signals a state change
    handlePreviousStateB ();

    // set the new state, if we recognize it
    switch (c)
    {
    case 'A':
      stateB = GOT_AA;
      break;
    case 'B':
      stateB = GOT_BB;
      break;
    case 'C':
      stateB = GOT_CC;
      break;
    case 'D':
      stateB = GOT_DD;
      break;
    case 'E':
      stateB = GOT_EE;
      break;
    case 'F':
      stateB = GOT_FF;
      break;

    default:
      stateB = NONE;
      break;
    }  // end of switch on incoming byte
  } // end of not digit 

} // end of processIncomingByte





Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50888
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't see anything on the receiver side that prints A= anything. Posting some REAL output would be good.

If the delay() on the sender end is needed, use it,
Logged

Los Angeles
Offline Offline
Full Member
***
Karma: 1
Posts: 148
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ill post data tomorrow when Im back at work.
One thing that throws me off with serial communication is that even when I have the code working well and displaying good data on the screen (data in through Serial 2 and 3 and data out to Serial monitor via Serial0)
when I'm done troubleshooting and comment out the Serial 0 port lines... suddenly the data is not the same...
But I cant see why...  just know because it makes the device stop responding to the data coming in.

Is there a better way to troubleshoot serial data so as to not affect the functionality of the code????
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19350
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Correctly written code should be reasonably resistant to debugging prints (if you don't overdo them, and thus block the processor).

Here are some other tips for debugging:

http://www.gammon.com.au/forum/?id=11329
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Pages: 1 [2]   Go Up
Jump to: