Hi,
I have a Arduino Mega 2560, a rs232 to ttl converter and a device that has a RS232 plug and i want to chat with it through Arduino.
When sending #S it is suppose to answer STAND-BY but nothing happens.
To check if my source is well written i've made several tests : (PC -> Hyperterminal)
Arduino to PC with the RS232 serial converter It Worked
Device to PC It worked as well
and the part when i try to get Arduino and the device to communicate just fail.
Serial is USB Serial Monitor to arduino and Serial1 is the device.
I put some "Serial.print()" here and there to check where my program is read and the function serialEvent1() is never triggered.
In the serial monitor, i've put those settings : NL & CR / 9600bd
char byte = 0;
String str = "";
boolean flag_str = false;
char byte1 = 0;
String str1 = "";
boolean flag_str1 = false;
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Serial.flush();
Serial1.flush();
}
void loop()
{
if (flag_str)
{
int nb = Serial1.print(str);
Serial.println(nb); // It displays the right length (4)
Serial.println(str);
flag_str = false;
str = "";
}
if (flag_str1)
{
Serial.println("String received from the device");
Serial.print(str1);
flag_str1 = false;
str1 = "";
}
}
void serialEvent()
{
Serial.println("serialEvent triggered");
delay(40);
while ( Serial.available() > 0 && flag_str == false)
{
byte = Serial.read();
str += byte;
if (byte == '\n')
flag_str = true;
}
}
void serialEvent1()
{
Serial.println("serialEvent1 triggered");
delay(40);
while ( Serial1.available() > 0 && flag_str1 == false)
{
byte1 = Serial1.read();
str1 += byte1;
if (byte1 == '\n')
flag_str1 = true;
}
}
So what's wrong?
If anyone has some ideas or suggestions, please let me know
Thanks for reading