2 Arduino communications ?

Hi,
I'm trying to reconstruct HEX data from the Serial port with an arduino but i think i just don't get it, i'm completly lost.

So, I have an arduino sending HEX code formatted like this

FF735AFFFF6F879389000000000000\n

But everytime i try to read it, i get wrong Data or it get converted (I don't know).

Here's the codes I've tried to receive it :

byte Packet[21];

for (int i=0; i < 21; i++){
    Packet[i] = Wemos.read();
  }
  
  /*if (Wemos.available() > 0) {
    Packet = Wemos.readBytesUntil('\n',Packet, 21);
  }*/

I'm using software Serial since i'll need the Hardware one after i got the data to the right format.

Thanks, I tried searching for ways to read it, but i don't really understand all the notions.

Hi,
We need both complete codes to see where your problem can be.

If your code is too long to post in code tags, then have you shorter versions that exhibit thew same problem?

Thanks.. Tom.. :slight_smile:

here's the code for the reading Arduino :

#include "UnoJoy.h"
#include <SoftwareSerial.h>

#define PSB_SELECT      0x0001
#define PSB_L3          0x0002
#define PSB_R3          0x0004
#define PSB_START       0x0008
#define PSB_PAD_UP      0x0010
#define PSB_PAD_RIGHT   0x0020
#define PSB_PAD_DOWN    0x0040
#define PSB_PAD_LEFT    0x0080
#define PSB_L2          0x0100
#define PSB_R2          0x0200
#define PSB_L1          0x0400
#define PSB_R1          0x0800
#define PSB_GREEN       0x1000
#define PSB_RED         0x2000
#define PSB_BLUE        0x4000
#define PSB_PINK        0x8000
#define PSB_TRIANGLE    0x1000
#define PSB_CIRCLE      0x2000
#define PSB_CROSS       0x4000
#define PSB_SQUARE      0x8000

//These are analog buttons
#define PSAB_PAD_RIGHT    9
#define PSAB_PAD_UP      11
#define PSAB_PAD_DOWN    12
#define PSAB_PAD_LEFT    10
#define PSAB_L2          19
#define PSAB_R2          20
#define PSAB_L1          17
#define PSAB_R1          18
#define PSAB_GREEN       13
#define PSAB_RED         14
#define PSAB_BLUE        15
#define PSAB_PINK        16
#define PSAB_TRIANGLE    13
#define PSAB_CIRCLE      14
#define PSAB_CROSS       15
#define PSAB_SQUARE      16

//These are stick values
#define PSS_RX 5
#define PSS_RY 6
#define PSS_LX 7
#define PSS_LY 8

SoftwareSerial Wemos (5,6);

int error = 0;
byte type = 0;
byte vibrate = 0;
byte Packet[21];
uint16_t buttons = 0xFFFF;

unsigned char GamePadData[21] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

boolean Button(uint16_t button) {
  return ((~buttons & button) > 0);
}

byte GamePadAnalog(byte button) {
   return GamePadData[button];
}

void setup(){
  //setupUnoJoy();
  Serial.begin(74880);
  Wemos.begin(74880);
  delay(1000);
}

void loop(){
  for (int i=0; i < 21, i++){
    Packet[i] = Wemos.read();
  }
  }
  /*if (Wemos.available() > 0) {
    Packet = Wemos.readBytesUntil('\n',Packet, 21);
  }*/
  for (int i = 0; i < 21; i++) {
    GamePadData[i] = Packet[i];
  }
  
  for (int i = 0; i < 21; i++) {
    Serial.print(Packet[i], HEX);
  }
  for (int i = 0; i < 21; i++) {
    Serial.print(GamePadData[i], HEX);
  }
  Serial.println();
  buttons =  (uint16_t)(Packet[4] << 8) + Packet[3];
  Serial.print(buttons);
  Serial.println();

The serial.Print are here for debugging.

here the bit of code that sends the data :

if (lenght == 21) {

        memcpy(GamePadData, payload,  21);
        //Serial.print(GamePadData);
        for (int i = 0; i < 21; i++) {
          //if(GamePadData[i] < 0x10) {Serial.print("0");}
          Serial.print(GamePadData[i], HEX);
          //Serial.print("::");
        }
        Serial.println();
        buttons =  (uint16_t)(payload[4] << 8) + payload[3];
        analogWrite(D1, GamePadData[PSAB_BLUE]); //Set PWM value of D1 to reflect the pressure applied to the BLUE 'X' on the controller
        //hexdump(payload, lenght); //used for debug

      }

What Arduino board are you using for the receiver?

An Uno or Mega can't do SoftwareSerial at 74880 baud. Start with 9600 baud and if that works try increasing it. I don't think you will get beyond 38400 baud.

Have a look at the second example in Serial Input Basics

...R

And compare the example mentioned by @Robin2 with the way you are trying to read bytes from serial;

for (int i=0; i < 21; i++){
Packet = Wemos.read();

  • }*
    [/quote]

I have a Uno running the software serial.

I'll read thru that, thanks.

Ok, I now have all my data clearly received, Thanks a lot !

From the how to use this forum sticky post:

If you consider the issue solved, please edit your original post (the first one in the thread) and add "[solved]" to the subject line. That helps people know the issue is resolved. Also please post the solution, whether you worked it out yourself, or if someone else helped you. A note that "this was solved by doing X" is very helpful to other people with the same problem. Thanks!

yuukiflow:
Ok, I now have all my data clearly received, Thanks a lot !

Then say what the solution was !!!!!!!!!!!!!!!!!!!!!!!!

Might be helpful to others in the future and save the volunteers on these forums the work of dealing with similar queries ?

Allright,
I used the information they gave me. I've read his exemple and wrote this in my code:

void ReadSerial() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
 
    while (Wemos.available() > 0 && newData == false) {
        rc = Wemos.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                GamePadData[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                GamePadData[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

I've also set the sender to print the data like this