Hello. In the information I sent only to Serial 1 and Serial 2, in the section of my code
//for (int index = 0; index < strlen(message); index++)]// I want to change the value of int index=0 to
int index=2. For the message sent to the other Serial 3s, the value of index=0 should remain 0. I couldn't do it.
My Arduino board is Mega.
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(){
Serial.begin(9600);
Serial3.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
}
void loop(){
receive_message(Serial);
if (strncmp(message, "1", 1) == 0)
{
send_message(Serial1);
}
else if (strncmp(message, "2", 1) == 0)
{
send_message(Serial2);
}
else if (strncmp(message, "3", 1) == 0)
{
send_message(Serial3);
}
else if (strncmp(message, "4", 1) == 0)
{
send_message(Serial3);
}
else if (strncmp(message, "5", 1) == 0)
{
send_message(Serial3);
}
}
you should really indent your code, it's not very readable...
An idea would be to change your function to
void send_message(HardwareSerial &tx_serial, size_t startPos = 0)
{
tx_serial.write (START_CHAR);
for (size_t index = startPos0; index < strlen(message); index++) {
tx_serial.write (message[index]);
}
tx_serial.write (END_CHAR);
}
when you want to send starting with index at 0, you just do as you were doing
send_message(Serial2);
and the position will take the default value 0
but if you want to start at 2, then you can pass that as a second parameter
send_message(Serial3, 2);
another way of doing it would be to check in the function which Serial is the target or to pass the message to the function and you'd point at the first character or third.
Thanks. I'll try it. It's a bit complicated. I hope I can handle it.
how is it complicated ?
basically your loop is probably best written like this
void loop() {
receive_message(Serial);
switch (message[0]) {
case '1': send_message(Serial1); break;
case '2': send_message(Serial2); break;
case '3': send_message(Serial3); break;
case '4': send_message(Serial3); break;
case '5': send_message(Serial3); break;
default: break; // do nothing oytherwise
}
}
with the new function the full message (from index 0) would be sent.
if you write it like that
void loop() {
receive_message(Serial);
switch (message[0]) {
case '1': send_message(Serial1); break;
case '2': send_message(Serial2); break;
case '3': send_message(Serial3); break;
case '4': send_message(Serial3, 2); break; // send message's content starting at index 2
case '5': send_message(Serial3, 2); break; // send message's content starting at index 2
default: break; // do nothing oytherwise
}
}
then the last 2 cases would send the message's content starting at index 2
is that soooo difficult ?
Compilation error: 'startPos0' was not declared in this scope
gave error
Compilation error: too many arguments to function 'void send_message(HardwareSerial&)'
I typed the code here and the copy paste kept your 0 and I wrote
size_t index = startPos0 instead of size_t index = startPos
Can't you figure out simple things like this?
void send_message(HardwareSerial &tx_serial, size_t startPos = 0)
{
tx_serial.write (START_CHAR);
for (size_t index = startPos; index < strlen(message); index++) {
tx_serial.write (message[index]);
}
tx_serial.write (END_CHAR);
}
and of course you need to replace the function in your code...
I'm sorry.
Ramadan, fasting, we're a little distracted. I missed it.
I missed it.
It’s totally okay! Ramadan is about spiritual growth, connection, and intention, not about perfection.
And we all feel distracted or miss moments. Don't worry about it.
It worked better as you said. You really know your job well. God bless you.
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, size_t startPos = 0)
{
tx_serial.write (START_CHAR);
for (size_t index = startPos; 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); //pcden gelen veriler
Serial3.begin(9600); //slave-2 ye giden veriler.
Serial1.begin(2400); //led panele giden-Sıra nosu ve Binici ismi
Serial2.begin(2400); //led panele giden-At ismi
}
void loop() {
receive_message(Serial);
switch (message[0]) {
case '1': send_message(Serial1, 2); break;
case '2': send_message(Serial2, 2); break;
case '3': send_message(Serial3); break;
case '4': send_message(Serial3); break; // send message's content starting at index 2
case '5': send_message(Serial3); break; // send message's content starting at index 2
default: break; // do nothing oytherwise
}
}
if you write it like that, it does not send starting at index 2. You need to pass the index as the second parameter
case '4': send_message(Serial3, 2); break; // send message's content starting at index 2
case '5': send_message(Serial3, 2); break; // send message's content starting at index 2
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.