Go Down

Topic: To slow response  (Read 271 times) previous topic - next topic

Woo_44

Hello,

I have a problem with too slow a response to the query by RS232.
The program works in the following way:

1. When receive commands via RS232 ENQ I sending response ACK, but it takes too much time and PLC reports an error.
2. If the PLC receives an ACK sends the data to me, important values are at positions 6 and 7, I add them between STX and ETX and ship via ethernet.

3. Any other case ends in response to NAK.

I use arduino UNO with Ethernet shield W5100 , RS232 is connected to the MAX3232

Code: [Select]

#include <SPI.h>
#include <Ethernet.h>

 byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 110);     
IPAddress server(192, 168, 1, 111);
 
EthernetClient client2;


void setup() {

  Serial.begin(9600);
  delay(2000);

  Ethernet.begin(mac, ip);
  // Open serial communications and wait for port to open:

  if (client2.connect(server, 20000)) {
  }
  else {
    while (1);
  }

}

void loop()
{

  byte inData[17]; // Allocate some space for the string
  int index = 0;

  while (Serial.available()>0) // Don't read unless
 
    if (index < 6)
    {
    Serial.flush();
      Serial.readBytes(inData, 17);
 
      switch (inData[0])
      {
        case 0x05:                         
          Serial.write(0x06);             
          index = 0;
          Serial.flush();

          break;

        case 0x45:                         

          client2.write(0x02);         
          client2.write(inData[6]);     
          client2.write(inData[7]);     
          client2.write(0x03);         
          index = 0;
          Serial.flush();               
          client2.flush();
          break;

        default:                   
          Serial.write(0x15);     
              index = 0;
              Serial.flush();

          break;
      }
   }
}


I know it's not the best way but maybe you can help me

Regards

Go Up