Hello! I have a project in which I have to use UART connection between Arduinos. What i have on 1 Arduino, is 4 sensors which values are stored in an array, and a value of 0 or 1 stored in respective array if the sensor is activated or not.
I'm trying to send the 0 or 1 for each sensor at the same time to the other Arduino. I went about it by doing Serial.write(array,size). the "array" inside is byte var[4];. The com port for writing shows correctly which is activated e.g 0100 --> Sensor 2 is activated. I send the variable in 0x30 for 0 and 0x31 for 1.
The issue is, the receiving side does get the 1011, but then loads of random numbers for a while, until the 0100 shows up again. I can't figure out why. The way i went about reading the value: Serial.readBytes(data,4) Where data is also a Byte data[4]; and then serial.print(data[0]-48)... etc until [3].
Are you sending a sentence or just the data without a sentence structure?
Say your sentence looked like this "<#,1,0,1,1,0>", with a "<" that represents the beginning of the dat stream and the ">" as the end.
If your receiver looks for a < before it starts recording data and a > to end its data recording, your receiver can synchronize its data receptions with the sender.
void fReceiveSerial_LIDAR( void * parameters )
{
bool BeginSentence = false;
char OneChar;
char *str;
str = (char *)ps_calloc(300, sizeof(char) ); // put str buffer into PSRAM
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 )
The example code shows the principle I have been using from Uno's to ESP32's to receive serial data; ignore the ESP32 code.
Can you please post the send and receive code which gives you the output you posted.
An excellent tutorial is Robin2's Serial Input Basics.
If you want to beta test my code and schematics for sending between two Arduinos, (UNO, Mega2650, ESP8266, ESP8266-01, ESP32, PC(python) ), drop a MP with your email and the two boards you are using.
The code sends text lines, auto connects and re-connects, works with half-duplex software like the UNO/Mega Software Serial, includes a checksum and is very tolerant of processing delays on either side.
Does the ESP32 code directly use the ESP32 I2C API or does the code use the Arduino Core API for ESP32 I2C communications?
Here is a draft of the software and board circuits I have so far
https://www.forward.com.au/pfod/ArduinoProgramming/SoftwareSolutions/ComsPair.html
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.