Hi, I hope someone can help. I am writing a sketch as part of a larger project using the RN-42XV with the UNO. As part of the setup I want to test the serial connection to the RS-42 serial connection to the UNO.
I have it working but needed to make changes to reduce the memory usage so I started to use functions to reduce the Flash RAM usage and the Flash memory print function "F(print)" to reduce the SRAM usage.
This worked well up to the point where I have a function ignoring an if statement. The if statement looks for the CR and NL characters from a mySerial write to a variable rnBuffer, if they are there then the function should just loop until not further mySerial input is seen.
I've copied the function below and the full code after
void getRN() {
byte buffIndex = 0;
byte rnBuffer;
while (!mySerial.available()) {
}
while (mySerial.available()) {
rnBuffer = mySerial.read();
if (rnBuffer != 13 || rnBuffer != 10) {
dataBuffer[buffIndex] = rnBuffer;
buffIndex ++;
}
}
}
I have tried using else statement, and removing the if statement completely and still get the same results.
There are come other strange things going on too. Originally I was assigning the Char (string) variable dataBuffer = "abcdefghijk" to test menset (never used it before). When I assign these characters to the variable I the mySerial stops printing the "---" (Triple hyphen) at the end of the setup to exit the CMD mode and I get random "--" (Double Hyphen) prints on my Serial monitor.
Maybe something to do with memory ?? I don't know I've spent all of tonight trying to figure this out. Please help
Ow one lat thing..... please ignore some of the excess variables and the myANStoBIN function call results it has not worked since I added the functions, which I needed to change some o the variable names for, just not got round to it fixing yet, its a work in progress! but welcome any comments or info on anything else.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); //Rx, Tx
char rnCMD[3], rnBaudRate[5], rnName[12], rnPinCode[5], rnHID[5];
uint16_t rnHID_bin;
byte rnHID_bin1, rnHID_bin2, rnHID_bin3, rnHID_bin4, rnHID_mode, rnHID_cnt;
byte buffIndex = 0;
byte exitSetup = 0;
char dataBuffer[12];
void setup() {
Serial.begin(57600);
mySerial.begin(57600);
Serial.println(F("======================================================================"));
Serial.println(F("= Serial Communication Test ="));
Serial.println(F("======================================================================"));
Serial.print(F("Connecting to RN-42XV"));
delay (250);
Serial.print(F("."));
delay (250);
Serial.print(F("."));
delay (250);
Serial.print(F("."));
delay (250);
Serial.print(F("."));
delay (250);
Serial.print(F("."));
delay (250);
Serial.print(F("."));
delay (250);
Serial.println(F("."));
delay (250);
//Enter CMD mode and test connection
mySerial.print(F("$$"));
getRN();
if (dataBuffer[0] == 'C' & dataBuffer[1] == 'M' & dataBuffer[2] == 'D') {
Serial.println (F("Connection: Pass"));
}
else {
Serial.println(F("Connection: Failed, please check"));
Serial.println(F(" - Wiring"));
Serial.println(F(" - Baud rate"));
Serial.println(F(" - Power supply"));
Serial.println(F(" - Reset Power to RN-42"));
mySerial.println(F("---"));
return;
}
Serial.println(F("RN-42XV Settings:"));
//Retrieve name of the device
mySerial.println("GN");
getRN();
Serial.print (F("\tDevice name : "));
Serial.println (dataBuffer);
memset(dataBuffer, 0, sizeof(dataBuffer));
//Retrieve Baudrate of the device
mySerial.println(F("GU"));
getRN();
Serial.print (F("\tBaud Rate : "));
Serial.println (dataBuffer);
memset(dataBuffer, 0, sizeof(dataBuffer));
//Retrieve Pincode of the device
mySerial.println(F("GP"));
getRN();
Serial.print (F("\tPin Code : "));
Serial.println (dataBuffer);
memset(dataBuffer, 0, sizeof(dataBuffer));
//Retrieve BIN HID code of the device
mySerial.println(F("GH"));
getRN();
rnHID_bin = myANStoBIN(dataBuffer[0], dataBuffer[1], dataBuffer[2], dataBuffer[3]);
Serial.print (F("\tHID Binary : "));
for (int x = 15 ; x > -1 ; x--) {
Serial.print(bitRead(rnHID_bin, x));
}
Serial.print (F("\n"));
//Bit masking for HID Binary
Serial.print (F("\tHID Settings : "));
if (bitRead(rnHID_bin, 9) == 1) {
Serial.println (F("HID mode active if GPIO11 is high "));
}
if (bitRead(rnHID_bin, 8) == 1) {
Serial.println (F("\t\t\tIOS virtual keyboard active on power up "));
}
if (bitRead(rnHID_bin, 3) == 1) {
Serial.println (F("\t\t\tOutput reports over UART active "));
}
Serial.print (F("\t\t\tHID Mode (if active) : "));
for (int j = 7; j > 3; j --) {
byte mode = bitRead(rnHID_bin, j);
rnHID_mode = rnHID_mode << (4 - j) | mode;
}
if (rnHID_mode == 0) {
Serial.println (F("Keyboard"));
}
else if (rnHID_mode == 1) {
Serial.println (F("Game pad"));
}
else if (rnHID_mode == 2) {
Serial.println (F("Mouse"));
}
else if (rnHID_mode == 3) {
Serial.println (F("Combo"));
}
else if (rnHID_mode == 3) {
Serial.println (F("Joystick"));
}
else {
Serial.println (F("Unrecognised value"));
}
Serial.print (F("\t\t\tNumber of connected devices allowed : "));
for (int j = 2; j > -1 ; j --) {
byte mode = bitRead(rnHID_bin, j);
rnHID_cnt = rnHID_cnt << (4 - j) | mode;
}
Serial.println ((int)rnHID_cnt);
mySerial.println("---");
//Exit Setup
Serial.println(F("\nPress enter to continue!"));
while (exitSetup != 13) {
exitSetup = Serial.read();
}
}
void loop() {
if (buffIndex < 1) {
Serial.println(F("End setup"));
mySerial.println("---");
buffIndex = ++ buffIndex;
}
}
//FUNCTIONS
void getRN() {
byte buffIndex = 0;
byte rnBuffer;
while (!mySerial.available()) {
}
while (mySerial.available()) {
rnBuffer = mySerial.read();
if (rnBuffer != 13 || rnBuffer != 10) {
dataBuffer[buffIndex] = rnBuffer;
buffIndex ++;
}
}
}
uint16_t myANStoBIN(char w, char x, char y, char z) {
uint16_t result;
int word1[4] = {w, x, y, z};
for (int i = 0; i > 3; i ++) {
switch (word1[i]) {
case 48: word1[i] = 0; break;
case 49: word1[i] = 1; break;
case 50: word1[i] = 2; break;
case 51: word1[i] = 3; break;
case 52: word1[i] = 4; break;
case 53: word1[i] = 5; break;
case 54: word1[i] = 6; break;
case 55: word1[i] = 7; break;
case 56: word1[i] = 8; break;
case 57: word1[i] = 9; break;
case 65: word1[i] = 10; break;
case 66: word1[i] = 11; break;
case 67: word1[i] = 12; break;
case 68: word1[i] = 13; break;
case 69: word1[i] = 14; break;
case 70: word1[i] = 15; break;
default: word1[i] = 16; break;
}
result = result << (i * 4) | word1[i];
}
return result;
}