Converting values within a char array to bytes for radiolink

Hi,

A serial input is taken from a python script on a PC, where the values of the ASCII string are 4x 0-255 with colon as end of line. The Pro Micro then takes a String from serial.read, I hear it is better to use char arrays so converts to this. To best optimise radio link it is wise to send minimum amount of data, in my case ASCII is not required as all values are supplied in the range of 0 - 255 the range of a byte.

Therefore I need to split the char array, I have tried and failed miserably with following thoughts.

  1. Slice into 4 ints then convert ints, just using byte = integer value of XXX
  2. Direct interperation of char values [0 to 2],[3 to 5]... to bytes

Simply put: How best to take char array (123123123123) of 4x 0-255 values into bytes to be transmitted over the RF24 library.

Code atm:

serialIN = Serial.readStringUntil(':'); //This is blocking 
serialIN = "123123123123:"; //For testing XXXYYYZZZTTT

Which then is converted to char array:

serialIN.toCharArray(txOUT,13); //Char array - 12(+1) bytes

Which once coverted needs to be sent using:

ack_rec = radio.write(SEND_BYTES_HERE, sizeof(SEND_BYTES_HERE)); //Sends PPPRRRYYYTTT

I've also seen structs while trying to search for an answer for this, I'm assuming that 4x byte values will be better placed in some sort of byte array?
Many thanks! (And apologies for what seems like a simple question)

atoi()

all values are supplied in the range of 0 - 255

So why is the Python script sending ASCII values and not bytes ?

Can you not just use Serial.readBytesUntil() to read the input and avoid any subsequent conversion ?

UKHeliBob:
So why is the Python script sending ASCII values and not bytes ?

Can you not just use Serial.readBytesUntil() to read the input and avoid any subsequent conversion ?

I suppose that it was due to the fact USB serial connection doesn't require the same level of scrutiny of data optimisation (/me being lazy!). Without straying away from Arduino I suppose that could be achieved using bytearray() to send. With readBytesUntil() how would you implement char search for end of line if just feeding 4x byte values? Or would length alone work?

gcjr:
atoi()

I had a go using atoi, seemed easy enough to get char = 123 into int 123 then byte of said int. Issue was pointing the atoi function to chars 0-2, 3-5, 6-8, 9-11, and to take 3 values within each as an int.

code needs to copy the substrings into a separate char string that can be processed by atoi()

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