Strange output on Python with NRF24 output

Hi,

I'm back to ask (again) for help. I'm currently trying to send data (a string made from 0s ans 1s) wireless from an Arduino to another , with NRF24 modules. I manage to have it working well and get the result I expect. However, I need to do the same experiment without any control protocol. Yes I deliberately want to have a potentially disturbed message.

Please note that the message is built in Python, sent to the Tx via Python serial module, and that the same is done with Rx. The data I extract from the Rx is read thanks to a few Python lines (which work).

The RF24 library allows to disable CRC if we also disable auto-ack. After a few tries and lot of worries, I finally managed to get an output from the Rx with the auto-ack disabled. However, the output is quite strange, since it seems the string is no longer encoded in the same code I sent it.

The input is :

0110010010111000110101000

Which is also what my code outputs with auto-ack enabled.
Without, though, the output becomes :

\xcd\x18\x18\x98\x98\x18\x18\x98\x18\x18\x98\x18\x98\x98\x98\x18\x18\x18\x98\x98\x18\x98\x18\x98\x18\x18\xcd\x18\x18\x98\x98\x18\x18\x98\x18\x18\x98\x18\x98\x98\x98\x18\x18\x18\x98\x98\x18\x98\x18\x98\x18\x18\xcd\x18\x18\x98\x98\x18\x18\x98\x18\x18\x98\x18\x98\x98\x98\x18\x18\x18\x98\x98\x18\x98\x18\x98\x18\x18\xcd\x18\x18\x98\ ... and so on

I checked for an Ascii translation, but it doesn't seem to fit to what I expect.
What am I doing wrong ?
Here are the codes, to give you a better picture :

Tx :

#include <SPI.h>
#include <RF24.h>

#define pinCE   7             // On associe la broche "CE" du NRF24L01 à la sortie digitale D7 de l'arduino
#define pinCSN  8             // On associe la broche "CSN" du NRF24L01 à la sortie digitale D8 de l'arduino
#define tunnel  "PIPE1"       // On définit un "nom de tunnel" (5 caractères), pour pouvoir communiquer d'un NRF24 à l'autre

RF24 radio(pinCE, pinCSN);    // Instanciation du NRF24L01

const byte adresse[6] = tunnel;               // Mise au format "byte array" du nom du tunnel
                    // Message à transmettre à l'autre NRF24 (32 caractères maxi, avec cette librairie)

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup() {
    Serial.begin(9600);
    radio.begin();                      // Initialisation du module NRF24
    radio.openWritingPipe(adresse);     // Ouverture du tunnel en ÉCRITURE, avec le "nom" qu'on lui a donné
    radio.setPALevel(RF24_PA_MIN);      // Sélection d'un niveau "MINIMAL" pour communiquer (pas besoin d'une forte puissance, pour nos essais)
    radio.setCRCLength(0);
    radio.stopListening();// Arrêt de l'écoute du NRF24 (signifiant qu'on va émettre, et non recevoir, ici
    

}
char message[32] = "Mon message à envoyer !"; 


void loop() {
  recvWithEndMarker();
  if (newData) {
    showNewData();
  radio.write(receivedChars, strlen(receivedChars)+1);
  } 
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '>';
    char rc;
    
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}
void showNewData() {
    if (newData == true) {
        Serial.print(receivedChars);
        newData = false;
    }
}```

Rx : 

#include <SPI.h>
#include <RF24.h>

#define pinCE   7             // On associe la broche "CE" du NRF24L01 à la sortie digitale D7 de l'arduino
#define pinCSN  8             // On associe la broche "CSN" du NRF24L01 à la sortie digitale D8 de l'arduino
#define tunnel  "PIPE1"       // On définit le "nom de tunnel" (5 caractères) à travers lequel on va recevoir les données de l'émetteur

RF24 radio(pinCE, pinCSN);    // Instanciation du NRF24L01

const byte adresse[6] = tunnel;       // Mise au format "byte array" du nom du tunnel  
char message[32];
boolean newData = false;
const byte numChars = 16;
char receivedChars[numChars];
char add = '\0';

void setup() {
  // Initialisation du port série (pour afficher les infos reçues, sur le "Moniteur Série" de l'IDE Arduino)
  Serial.begin(9600);

  // Partie NRF24
  radio.begin();                      // Initialisation du module NRF24
  radio.openReadingPipe(0, adresse);  // Ouverture du tunnel en LECTURE, avec le "nom" qu'on lui a donné
  radio.setPALevel(RF24_PA_MIN);      // Sélection d'un niveau "MINIMAL" pour communiquer (pas besoin d'une forte puissance, pour nos essais)
  radio.setAutoAck(false);
  radio.setCRCLength(0);
  radio.startListening();    // Démarrage de l'écoute du NRF24 (signifiant qu'on va recevoir, et non émettre quoi que ce soit, ici)

}
void loop() {
    if (radio.available()) {        // On vérifie si un message est en attente de lecture   
      radio.read(&message, sizeof(message));// Si oui, on le charge dans la variable "message"
      newData = true;
      showNewData();
    }
}
void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = ">" ;
    char rc;
    
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

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

void showNewData() {
    if (newData == true) {
        Serial.write(message);
        newData = false;
    }

}

Python :

import serial
import CRC
import numpy as np
import time
a = '0110010010111000110'
CRC.Codage_Canal(a)
print(CRC.Codage_Canal(a))

ArduinoUno = serial.Serial(port="COM3", baudrate=9600, bytesize=8, timeout=2, stopbits=serial.STOPBITS_ONE)
ArduinoNano = serial.Serial(port="COM4", baudrate=9600, bytesize=8, timeout=2, stopbits=serial.STOPBITS_ONE)
time.sleep(2)
file = open('message.txt', 'r')
message = file.read() 
file.close()
print(message)

messagecode = CRC.Codage_Canal(message) + '>'
print(messagecode)
                                                                        #Le code fonctionne jusqu'ici
bits = []

Generateur = np.array([1, 0, 1, 1])

for i in range(len(messagecode)//8 + 1) :                               #On découpe en chaines de 8 bits car bytesize = 8 plus haut
        chaine = '' 
        chaine += messagecode[8*i:8*i+8]
        bits.append(chaine)
        print(bits)
w = 0
while w < 1 : 
        w += 1
        for i in range(len(bits)):                                     
                ArduinoUno.write(bytes(bits[i], encoding = 'ASCII'))            #On envoie à travers le moniteur chaque groupe de 8 bits  #write(bytes(bits[i]))          
                
        time.sleep(1)    
        recu = str(ArduinoNano.readline())[2:-1]                                   #On stocke tout ce qui est dans le buffer de la Nano
        print("reçu = ", recu)
###FONCTIONNE JUSQUE LA
        decode = CRC.Decodage_Canal(recu, Generateur)    #possible que le bug soit ici                           #decode est le reste dans la div euclidienne de la chaine reçue
        print(decode)
        ecrire = True                                                   #booléen
        
        compteur = 0
        while compteur < len(decode) :                                  #On vérifie que tous les coefficients du reste sont nuls
                if decode[compteur] == 0 :
                        compteur += 1
                else:
                        ecrire = False
                        break
        if ecrire :                                                     #Si le message est correct, on écrit le message reçu dans un fichier
                fichier = open('reception.txt', 'a')
                fichier.write(str(recu)[:-len(decode)] + '\n')                 #On enlève les bits de CRC à la fin du code
        else :
                fichier2 = open('erreurs.txt', 'a')                         #Sinon, on l'écrit dans un fichier d'erreurs
                fichier2.write(str(recu)[:-len(decode)] + '\n')        
        print('fin')

Thank you very much for any help

The simplest reason would be wrong expectations. :grinning:

It seems you have missed a crucial detail.

The bit stream you receive, has one part that is 9 bits long,
so you have to shift the whole rest of the packet.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.