0x2 0x31 0x33 0x31 0x35 0x30 and ending with 0xa I want to send serial 1
0x2 0x31 0x33 0x31 0x35 0x31 and ending with 0xa I want to send serial 2
You could try the following :
const char START_CHAR = '\x02';
const char END_CHAR = '\x0A';
const int BUF_SIZE = 32;
char message[BUF_SIZE];
void receive_message(HardwareSerial &serial)
{
int rx_index = 0;
char rx_char = '\0';
while (rx_char != START_CHAR)
{
while (!serial.available()) {}
rx_char = serial.read();
}
// skip STX
while (!serial.available()) {}
rx_char = serial.read();
// read until END_CHAR
while (rx_char != END_CHAR)
{
if (rx_char != END_CHAR)
{
if (rx_index < BUF_SIZE - 1)
{
message[rx_index] = rx_char;
rx_index++;
}
}
while (!serial.available()) {}
rx_char = serial.read();
}
message[rx_index] = '\0';
}
void send_message(HardwareSerial &tx_serial)
{
tx_serial.write (START_CHAR);
for (int index = 0; index < strlen(message); index++)
{
tx_serial.write (message[index]);
}
tx_serial.write (END_CHAR);
}
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
}
void loop()
{
// put your main code here, to run repeatedly:
receive_message(Serial);
if (strncmp(message, "13150", 5) == 0)
{
send_message(Serial1);
}
else if (strncmp(message, "13151", 5) == 0)
{
send_message(Serial2);
}
}
void setup() {
// initialize both serial ports:
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
// read from port 1, send to port 0:
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}
}
I could not receive the data coming in this way from the slave 1 Arduino. Is the code correct?
The master receives data from the PC, and send data to the slaves.
Slave 1 receives data for slave 1
Slave 2 receives data for slave 2
I don't see where the slaves send data to the master.
Btw: your pictures are too low res to be useful. All I can see is that there are 3 Megas and a bunch of wires.
slave 1 and slave 2 will only send to the LED screen of the receiving school
The code for the slaves is to call receive_message(), using whatever is the right Serial port.
Then display the received data.
There are examples right in the IDE.
Click on File, then Examples, then Communications.
There is a Multiserial example and a Passthrough example.
if (strncmp(message, "1", 1) == 0)
How can I send only the message without printing these 11 in your code. the code worked fine, thank you
Just change send_message() to start at the third character :
void send_message(HardwareSerial &tx_serial)
{
tx_serial.write (START_CHAR);
for (int index = 2; index < strlen(message); index++) // skip the first 2 characters
{
tx_serial.write (message[index]);
}
tx_serial.write (END_CHAR);
}
Why does Slave 2 leave spaces in teacher names? It's as if the line is skipping.
void setup() {
// initialize both serial ports:
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
// read from port 1, send to port 0:
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}
}
slave 1 and slave 2 have the same code loaded
const char START_CHAR = '\x02';
const char END_CHAR = '\x0A';
const int BUF_SIZE = 32;
char message[BUF_SIZE];
void receive_message(HardwareSerial &serial)
{
int rx_index = 0;
char rx_char = '\0';
while (rx_char != START_CHAR)
{
while (!serial.available()) {}
rx_char = serial.read();
}
// skip STX
while (!serial.available()) {}
rx_char = serial.read();
// read until END_CHAR
while (rx_char != END_CHAR)
{
if (rx_char != END_CHAR)
{
if (rx_index < BUF_SIZE - 1)
{
message[rx_index] = rx_char;
rx_index++;
}
}
while (!serial.available()) {}
rx_char = serial.read();
}
message[rx_index] = '\0';
}
void send_message(HardwareSerial &tx_serial)
{
tx_serial.write (START_CHAR);
for (int index = 2; index < strlen(message); index++)
{
tx_serial.write (message[index]);
}
tx_serial.write (END_CHAR);
}
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
}
void loop()
{
// put your main code here, to run repeatedly:
receive_message(Serial);
if (strncmp(message, "1", 1) == 0)
{
send_message(Serial1);
}
else if (strncmp(message, "2", 1) == 0)
{
send_message(Serial2);
}
}
This is the code you sent master. I changed the final version a little bit.
Turning it off and on again fixed it.




