1

So I am trying to get my arduino uno to talk to my arduino due but I'm having issues getting the data outputted on the due side. My arduino uno is the master while the due is the slave. The sensor is connected to the uno and I just require the arduino due to receive and spit out the data as it has a faster sampling rate than the arduino uno. I connected them along the serial (Tx and Rx) lines. And I'm using Serial3 on my arduino due to receive the transmitted data. Both arduinos are grounded together. But powered differently by usb cables connected to my machine. On the arduino uno end ( for brevity sake) the data is sent in binary with the $ sign as the starter bit. Here is the code for the receiving end ( the slave). I just can't seem to get the data out on the serial monitor.

    byte b = '$';
    unsigned char buffer[12];
    int count =0;

    void setup() {
    Serial.begin(115200);
    Serial3.begin(115200);
    delay(5);

    }

   void loop() {
   if(Serial.available())
   {
    if (b == Serial.available())
   {
    while(Serial.available())
    {
      buffer[count++] = Serial.read();
       if(count == 12)break;
      //Send the byte to serial port three
      Serial3.write(buffer, count);
    }
 }

 if (Serial3.available()){
  //get a byte from serial port 3
  if(b == Serial3.available()){
    buffer[count++] = Serial3.read();
    //Send the byte to the USB serial port
    Serial.write(buffer, count);
  }
 }
 Serial.println("lyca");
 }
  }
  • Did you cross TX and RX (i.e., connect TX to RX and RX to TX) or connect them straight (TX to TX and RX to RX)? – Majenko Jan 13 '16 at 19:04
1
if(Serial.available())
{
 if (b == Serial.available())
{
 while(Serial.available())
 {

I think you may be getting your knickers in a twist there.

... Is serial available? Yes. Is serial available? Still yes! But yes doesn't equal '$'!!!... WHAT?!

You really really need to rethink your understanding of serial communication.

Read this: http://hacking.majenko.co.uk/reading-serial-on-the-arduino

  • Thanks @Majenko ..I actually crossed it. Tx to Rx and Rx to Tx – dada Jan 13 '16 at 19:34
  • I agree with Majenko. Comparing '$' to Serial.available() makes no sense at all. You do it more than once. And why test if serial is available twice in direct succession? Three times, even, if you count the while(Serial.available()). – Nick Gammon Jan 15 '16 at 5:23
0

There seem to be a confusion on Serial & Serial3. You say that:

  • Serial is used to send back debug data to the PC
  • Serial3 is used to get data from the master

But this is not clear/coherent to your code. Let's try to see your code with proper indentation. I added comments.

void loop() {
  if(Serial.available()) { //isnt Serial your debug port? did you mean Serial3?
    if (b == Serial.available()) { //same. Serial3? also, read() instead of available() ?
      while(Serial.available()) { //same. Serial3?
        buffer[count++] = Serial.read(); //Serial3?
        if(count == 12) {
          break; //will this work for several frames? how can count ever go back to 0?
        }
        Serial3.write(buffer, count); //isnt Serial your debug output?
      }
    }
//your indentation didnt show it, but we're still guarded by the first 'if' here.
//Also I guess you meant Serial here?
    if (Serial3.available()) { 
//why check for a SoF? what do you do with data coming up fro the debug PC?
      if(b == Serial3.available()) {
//messing with data in your buffer, potentially outside of it? (count can be>11 here)
        buffer[count++] = Serial3.read();
        Serial.write(buffer, count);
      }
    }
    Serial.println("lyca");
  }
}

What about this (code below still has several flaws, but should perform better than yours)

void loop(void) {
  if(Serial3.available() > 0) { //available data from master?
    while(Serial3.available() > 0) { //if statement above for clarity, this while on its own would be enough
      inByte = Serial3.read();
      if(inByte  == SOF_BYTE) { //is this a start of frame?
        mybuff.ptr = 0; //let's say we dump everything in buff whenever we get a new SoF
        Serial.println("SoF received");     
      }
      else { //let's not keep SoF
        mybuff.append(inByte);
      }
    }
  }
  if(mybuff.size >= FULLFRAME_SIZE) { //we've got a full frame, output it
    Serial.print("Full frame received:");
    Serial.println(mybuff.string);
    mybuff.ptr = 0; //now that we've consumed the frame, kill it
  }
  else {
    Serial.print(".") // no frame, show progress/still alive
  }
  delay(refreshtime);
}
  • Hi @CQuinson, the problem would arise as a result of the fact that, the data sent to Serial from the slave, would have to be read back from serial on the master sketch, then sent to the specified serial port (Serial1, Serial2 ...etc.). The data on that port is then re-read back to Serial from the ports before it is written on the master side. Initially , I thought it was unnecessary and just a waste of my time, but when i tried it as suggested, no data was received, I had to go the long haul to get the data out. But maybe , I was missing something, I'll have a look. thanks for the feedback – dada Apr 10 '16 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.