Go Down

Topic: Serial Master/Slave problem using serialEvent (Read 656 times) previous topic - next topic

freeborn

Jun 04, 2013, 09:34 am Last Edit: Jun 04, 2013, 09:39 am by freeborn Reason: 1
I've made Master/Slave via RS485 using serialEvent.

Here is the code for Master :

Code: [Select]

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
String poll = "*00100011110001111;";
int i = 0;

void setup() {
 // initialize serial:
 Serial.begin(19200);
 // reserve 200 bytes for the inputString:
 inputString.reserve(200);  
 pinMode(2, OUTPUT);
}

void loop() {
 poll.setCharAt(1, 'a' + i++);
 if(i > 10) i = 0;
 digitalWrite(2, 1);
 delay(1);
 Serial.println(poll);
 waiting();
 digitalWrite(2, 0);
 delay(1);
 response();
 delay(1000);
}

void response() {
 if (stringComplete) {
   Serial.println(inputString);   // echo response from slave device
   // clear the string:
   inputString = "";
   stringComplete = false;
 }
}

void serialEvent() {
 while (Serial.available()) {
   // get the new byte:
   char inChar = (char)Serial.read();
   
   if(inChar == '-')
     inputString = "-";
   else
     inputString += inChar;
   // if the incoming character is a ";", set a flag
   // so the main loop can do something about it:
   if (inChar == ';') {
     stringComplete = true;
   }
 }
}


void waiting() {
 while (!(UCSR0A & (1 << UDRE0)))  // Wait for empty transmit buffer
   UCSR0A |= 1 << TXC0;  // mark transmission not complete
 while (!(UCSR0A & (1 << TXC0)));   // Wait for the transmission to complete
}



Here is the code for Slave :

Code: [Select]

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup() {
 // initialize serial:
 Serial.begin(19200);
 // reserve 200 bytes for the inputString:
 inputString.reserve(200);
 pinMode(3, OUTPUT);    // enable pin for rs485
 digitalWrite(3, 0);
 delay(1);
}

void loop() {
 if (stringComplete) {    
   digitalWrite(3, 1);    // enable send
   delay(1);
   Serial.print(inputString);
   waiting();
   digitalWrite(3, 0);    // enable receive
   delay(1);
   // clear the string:
   inputString = "";
   stringComplete = false;
 }
}

void serialEvent() {
 while (Serial.available()) {
   // get the new byte:
   char inChar = (char)Serial.read();
   
   if(inChar == '*')
     inputString = "-";
   else
     inputString += inChar;
   // if the incoming character is a ";", set a flag
   // so the main loop can do something about it:
   if (inChar == ';') {
     inputString = inputString.substring(0, 7) + "111222333444555666"
                   + inputString.substring(7);
     stringComplete = true;
   }
 }
}

void waiting() {    
 while (!(UCSR0A & (1 << UDRE0)))  // Wait for empty transmit buffer
   UCSR0A |= 1 << TXC0;  // mark transmission not complete
 while (!(UCSR0A & (1 << TXC0)));   // Wait for the transmission to complete
}


The message between Master and Slave is attached, Master start with "*" and Slave start with "-"

The problem is Master always get lack of response from Slave one step,
actually it is acceptable but I wonder if there is a way to make it better,
I knew that serialEvent only work after loop but do not have idea how to solve this.
Any idea is welcome.

Thanks in advance,


Nick Gammon

http://www.gammon.com.au/electronics

PaulS

Quote
Problem solved!

Useless information, unless you share the solution.

Go Up