What issues are you getting? I recommend you run a project on just one Bela looping back Rx and Tx, using one thread for sending and one for receiving. That program should successfully transmit data between the two threads. Once that's working, run the same program on both boards, wiring them together as you detailed.
If the connection between the boards doesn't seem to work, log in via ssh on both boards at once and run screen /dev/ttyS4
on each. You should be able to type in one terminal and read it on the other one and vice versa.
Minor comments on the code below, which shouldn't be the reason why things haven't worked so far.
This will write outside the buffer memory if bytesRead == bufferSize
.
int bytesRead = gSerial.read(buffer, bufferSize, 300);
...
// Null-terminate the received data to safely print it as a string
buffer[bytesRead] = '\0';
printf("Received: %s", buffer);
I recommend using this instead:
int bytesRead = gSerial.read(buffer, bufferSize - 1, 300);
(i.e.: limit to bufferSize - 1 bytes, so buffer[bytesRead]
is guaranteed to be inside the buffer).
If it's just for printing, you could do this instead to avoid having to null-terminate the string:
printf("Received: %.*s\n", bytesRead, buffer);