Good afternoon,
I am think about this idea.
I have a binary finex lenght word, for example
1010110010
I want to send this binary word to Arduino from the serial monitor.
Then I want to divide this word into
1 010 110 010
And finally assign these 4 parts into 4 int variables to have:
var_1 = 1;
var_2 = 2;
var_3 = 6;
var_4 = 2;
What do you think about feasibility of this code I need?
Many thanks!
Easy-peasy, is what I think about the feasibility of this code you need.
Idahowalker:
Easy-peasy, is what I think about the feasibility of this code you need.
Many thanks!
Do you have any hint for me?
I have tried to use Serial.readbytes() withou succeeds.
Many thanks!
Here is some receive serial code I use on an ESP32:
void fReceiveSerial_LIDAR( void * parameters )
{
bool BeginSentence = false;
char OneChar;
char *str;
str = (char *)ps_calloc(300, sizeof(char) ); // put str buffer into PSRAM
// log_i("Free PSRAM before String: %d", ESP.getFreePsram());
for ( ;; )
{
EventBits_t xbit = xEventGroupWaitBits (eg, evtReceiveSerial_LIDAR, pdTRUE, pdTRUE, portMAX_DELAY);
if ( LIDARSerial.available() >= 1 )
{
while ( LIDARSerial.available() )
{
OneChar = LIDARSerial.read();
if ( BeginSentence )
{
if ( OneChar == '>')
{
if ( xSemaphoreTake( sema_ParseLIDAR_ReceivedSerial, xSemaphoreTicksToWait10 ) == pdTRUE )
{
xQueueOverwrite( xQ_LIDAR_Display_INFO, ( void * ) &str );
xEventGroupSetBits( eg, evtParseLIDAR_ReceivedSerial );
//
}
BeginSentence = false;
break;
}
strncat( str, &OneChar, 1 );
}
else
{
if ( OneChar == '<' )
{
strcpy( str, ""); // clear string buffer
BeginSentence = true; // found beginning of sentence
}
}
} // while ( LIDARSerial.available() )
} //if ( LIDARSerial.available() >= 1 )
xSemaphoreGive( sema_ReceiveSerial_LIDAR );
// log_i( "fReceiveSerial_LIDAR " );
// log_i(uxTaskGetStackHighWaterMark( NULL ));
}
free(str);
vTaskDelete( NULL );
} //void fParseSerial( void * parameters )
Works great.
Here is a method which uses strncpy() to break the input character array into smaller pieces. The it uses strtol() to convert the smaller character arrays (in base 2)
to integers.
The method of input using .readBytes() may not be best for your application, and you may want to review Serial Input Basics tutorial.
char mySerialInput[11] = "";//size for 10 chars plus null terminator
char str_1[4] = "";//size for 3 chars plus null terminator
char str_2[4] = "";
char str_3[4] = "";
char str_4[4] = "";
byte var_1;
byte var_2;
byte var_3;
byte var_4;
void setup() {
Serial.begin(115200);
while(Serial.available()==0){};//wait for input
Serial.readBytes(mySerialInput,10);
Serial.println(mySerialInput);
//break input character array into smaller null terminated pieces
strncpy(str_1,&mySerialInput[0],1);
strncpy(str_2,&mySerialInput[1],3);
strncpy(str_3,&mySerialInput[4],3);
strncpy(str_4,&mySerialInput[7],3);
//convert character arrays, base 2, to integers
var_1 = strtol(str_1,NULL,2);
var_2 = strtol(str_2,NULL,2);
var_3 = strtol(str_3,NULL,2);
var_4 = strtol(str_4,NULL,2);
//print results
Serial.print("var_1 = ");
Serial.println(var_1);
Serial.print("var_2 = ");
Serial.println(var_2);
Serial.print("var_3 = ");
Serial.println(var_3);
Serial.print("var_4 = ");
Serial.println(var_4);
}
void loop() {}