Pages: [1]   Go Down
Author Topic: Serial Master/Slave problem using serialEvent  (Read 411 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've made Master/Slave via RS485 using serialEvent.

Here is the code for Master :

Code:
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:
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,


* serialEvent.png (23.12 KB, 411x361 - viewed 9 times.)
« Last Edit: June 04, 2013, 02:39:45 am by freeborn » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Problem solved!

Thanks
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46042
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Problem solved!
Useless information, unless you share the solution.
Logged

Pages: [1]   Go Up
Jump to: