title: Arduino communication with 3 RS485 devices
body: <p>This project i am doing for one student final thesis. Its so easy, but i just can not think how to make it. I have RPM counter and RS232 communication on one arduino. RPM counter is working good but communication isn't.
  I have 3 RS485 devices > RS485/RS232 converter > RS232/TTL > Arduino.
  To receive for ex. second device information i have to send "02READ". Example below, done with Hyperterminal.</p>
  
  <pre><code>02READ
  02ST,GS,    0.00,kg
  002READ
  02ST,GS,    0.00,kg
  002READ
  02ST,GS,    0.00,kg
  002READ
  02ST,GS,    0.00,kg
  0
  </code></pre>
  
  <p>It seems that devices are sending terminating 0, all message is <code>02ST,GS,    0.00,kg</code>. Its 19 bytes, right? What is the best solution to ask for 3 devices and then print them to log them on pc? I tried this one, not correct.</p>
  
  <pre><code>void READ03(){
        mySerial.println("03READ");
      while (mySerial.available()) { 
       delay(10); 
            if (mySerial.available() &gt;0) {
          char c = mySerial.read();
          readString += c;}
          }
          if (readString.length() == 19) {
        momentas3 = readString;
        readString="";
        }
  }
  </code></pre>
  
  <p>I have to go to loop and collect incoming data. But i dont figure out how to do it:( so sad...</p>
  
  <p>All code:</p>
  
  <pre><code>#include &lt;SoftwareSerial.h&gt;
  #include &lt;MegunoLink.h&gt;
  /*
  Baltas   +
  Raudonas -
  Pinout:
  ============= TCRT1 =============
  Juodas       -  GND
  Geltons      -  POSSITIVE
  Pilkas       -  SIGNAL
  ============= TCRT2 =============
  Rudas        -  GND
  Oranzinis    -  POSSITIVE
  Zydras       -  SIGNAL
  ============= TCRT3 =============
  Zalias       -  GND
  raudonas     -  POSSITIVE
  Baltas       -  SIGNAL
  */
  //Software serial for 485 communication : Tx = pin 9, Rx = pin 8.
  SoftwareSerial mySerial(9,8);
  TimePlot MyPlot;
  //CONSTANTS:
  //Pin number for Vishay Telefunken Opto-reflecting TCRT1000 sensors:
  Message MyCSVMessage("Stendas");
  #define TCRT1                12     
  #define TCRT2                11   
  #define TCRT3                10
  
  String readString;
  String momentas1;
  String momentas2;
  String momentas3;
  //Number of pulse changes per revolutuion:
  long PulsesPerRevolution1 = 78;
  long PulsesPerRevolution2 = 192;
  long PulsesPerRevolution3 = 82;
  long minute = 60;
  //VARIABLES:
  //Number of pulses counted:
  long           PulseCount1=0;       
  long           PulseCount2=0;       
  long           PulseCount3=0;       
  //Calculated rotations per minute:
  long        Rpm1       =0;       
  long        Rpm2       =0;       
  long        Rpm3       =0; 
  //Time saved to compare
  unsigned long TimeOld    =0;
  //Achieved signals saved:
  boolean       Status1    =0;       
  boolean       Status2    =0;      
  boolean       Status3    =0;       
  //Signals saved to compare:
  boolean       StatusOld1 =0;       
  boolean       StatusOld2 =0;       
  boolean       StatusOld3 =0;
  
  void setup() {
    //Begin serial communication with BAUD rate 9600bps:
    Serial.begin(9600);
    mySerial.begin(57600);
  
    //Set input pins for TCRT1000:
    pinMode(TCRT1, INPUT);           
    pinMode(TCRT2, INPUT);          
    pinMode(TCRT3, INPUT);          
  
    MyPlot.SetTitle("Greicio matavimas");
    MyPlot.SetXlabel("Laikas");
    MyPlot.SetYlabel("Greitis");
    MyPlot.SetSeriesProperties("Rpm", Plot::Magenta, Plot::Solid, 2, Plot::Square);
  }
  
  void loop() {
    //
  
    //Read and save TCRT1000 status:
    Status1=digitalRead(TCRT1);      
    Status2=digitalRead(TCRT2);      
    Status3=digitalRead(TCRT3);      
      //Compare current status with the previous one
      //If changed, then increment the counting:
    if (StatusOld1!=Status1){
      StatusOld1=Status1;
      PulseCount1++;
    }
    if (StatusOld2!=Status2){
      StatusOld2=Status2;
      PulseCount2++;
    }  
    if (StatusOld3!=Status3){
      StatusOld3=Status3;
      PulseCount3++;
    }  
    //Compare time if it exceeds 1s:
    if (millis()-TimeOld&gt;=1000){
     //Get data from RS485: 
     READ01();
     READ02();
     READ03();
     //Calculate RPM:
      Rpm1=PulseCount1*minute/PulsesPerRevolution1;
      Rpm2=PulseCount2*minute/PulsesPerRevolution2;
      Rpm3=PulseCount3*minute/PulsesPerRevolution3;
     //Print RPM
      MyCSVMessage.Begin();
      Serial.println(String(Rpm1) + "," + String(momentas1));
      Serial.println(String(Rpm2) + "," + String(momentas2));
      Serial.println(String(Rpm3) + "," + String(momentas3));
      MyCSVMessage.End();
      MyPlot.SendData("Rpm1", Rpm1);
      MyPlot.SendData("Rpm2", Rpm2);
      MyPlot.SendData("Rpm3", Rpm3);
  
     //reset the counting and time
      TimeOld=millis();
      PulseCount1=0;
      PulseCount2=0;
      PulseCount3=0;    
    }
  }
   //Get data from RS485:
  void READ01(){
      readString="";
       mySerial.println("01READ");
      while (mySerial.available()) { 
       delay(10); 
            if (mySerial.available() &gt;0) {
          char c = mySerial.read();
          readString += c;}
          }
          if (readString.length() == 19) {
        momentas1 = readString;
        readString="";
        }
  }
  void READ02(){
      readString="";
       mySerial.println("02READ");
      while (mySerial.available()) { 
       delay(10); 
            if (mySerial.available() &gt;0) {
          char c = mySerial.read();
          readString += c;}
          }
          if (readString.length() == 19) {
        momentas2 = readString;
        readString="";
        }
  }
  void READ03(){
      readString="";
       mySerial.println("03READ");
      while (mySerial.available()) { 
       delay(10); 
            if (mySerial.available() &gt;0) {
          char c = mySerial.read();
          readString += c;}
          }
          if (readString.length() == 19) {
        momentas3 = readString;
        readString="";
        }
  }
  </code></pre>
  
  <p><strong>Edited</strong> Void:</p>
  
  <pre><code>void READ03(){
    while (mySerial.available()){
      mySerial.read();
    }
      mySerial.println("03READ");
      momentas3="";
      delay(20);
  
      while (mySerial.available()) { 
        char c = mySerial.read();
        momentas3 += c;
       }
      // momentas3 = readString;
      //momentas3.setCharAt(momentas3.length() - 1, '\0');   
  }
  </code></pre>
  
  <p>It returns correct data, but in the end there is \0 or \n in the end of string</p>
  
  <pre><code>{MESSAGE:Stendas|DATA|
  0,01ST,GS,    0.03,kg
   0,02ST,GS,    0.00,kg
   0,03ST,GS,    0.00,kg
  </code></pre>
  
  <p>So i need to delete last char \0 or \n or whatever it is</p>