Arduino Mega 2560 via RS232 to Omron PLC

Hello,

For my personal project (windmill : www.windmolensite.be) I'm trying to use an Arduino to read real-time data from the PLC controlling my windmill.
I figured out how the PLC 'hostlink' protocol works and did some simple tests using an RS232 serial cable and my laptop using a terminal program.

PC --> Serial Cable --> PLC

The terminal connection settings are 9600, 7 Data bits, Even Parity and 2 Stop bits.

When I send the command : @00TS123443* the PLC responds with the same string : @00TS123443* (This is for testing purpose)

No I would like to get the same thing working with my Arduino.

Arduino --> WaveShare RS232 shield --> Serial Cable --> PLC (The shield has it's RX and TX pins connected to the Arduino pins 16 (TX2) and 17 (RX2), VCC and GND to 3v3 and GND)

But in this setup, it isn't working.

The code, as you can see it basic :slight_smile:

/*
PLC
 */

int incomingByte = 0;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize Serial1
  Serial.begin(9600);  //For debugging, towards PC
  if (Serial) {
    Serial.println("Connected to Computer!");
  }
  
  Serial2.begin(9600,SERIAL_7E2); //SERIAL_7E2 = 0x2C, see HardwareSerial.h
  Serial.println("Waiting for Serial2...");
}

// the loop routine runs over and over again forever:
void loop() {
  Serial2.println("@00TS123443*");
  
  while (Serial2.available() > 0) {
    incomingByte = Serial2.read();
    // say what you got:
    Serial.print("I received: ");
    Serial.println(incomingByte, DEC);
   }
   delay(1500);  
}

If I connect the Arduino to a serial port on my PC, I can see the correct string being sent by the Arduino.

Arduino --> WaveShare RS232 shield --> Serial Cable --> Serial-to-USB adapter --> PC

Any ideas where to look?

Regards,

Andy

Your

Serial2.println("@00TS123443*");

The serial print line command adds carriage return/new line characters to the end of your message which might be causing problems. Try just using the serial print command as such:

Serial2.print("@00TS123443*");

Lefty

Lefty,

Thanks for the reply. I already tried all sorts of combinations with write & print/println, none seem to work :frowning: If I test it manually, I need to press after the test string @00TS123443* so println() sounds the correct method of sending the string?

I find it a bit odd, because a normal terminal session works. Could it be the rare serial settings like 9600, 7E2?

ps: The PLC doesn't respond if you don't enter a correct command. Maybe the string isn't beeing sent like I expect, but how to check this...
ps2: I also notice that the 'communication' light on the PLC doesn't lit when sending data with the arduino but when using the terminal session it does!

Regards,

Andy.

Have you tried this versions too?

Serial2.print("@00TS123443*\n");

or

Serial2.print("@00TS123443*\r");

Device like PLCs can be very picky in these aspects.

Yes, also tried these :frowning:

The communication led stays off. Like it doesn't get sent...

Andy.

The communication LEDs are connected only to the RX0/TX0 lines on the Arduino.

Do you have a link to that Waveshare shield? I cannot find that product to check it's electrical parameters.

I meant that the communication lights on the PLC don't flash when connected to the Arduino. They do when connected to the PC.

Here is the link to the documentation : http://www.wvshare.com/product/RS232-Board.htm

It contains an SIPEX SP3232E chip.

See attachement for details (File downloaded from URL above)

Andy.

RS232-Board.7z (1.79 MB)

To be a little picky: this is not a shield, it's just a RS232 adapter module. Shields are designed for Arduino and are stacked on it. That ensures that every connector goes to the right pin.

How have you connected the module to your Arduino? Describe every connection you made.

My guess is you have to cross the RX/TX lines.

I made the connections like this :

RS232 <------> Arduino
VCC 5V
GND GND
RXout RX2
TXin TX2
CTS (not connected)
RTS (not connected)

When I swap the TX/RX (wrong!) I randomly get a response which is exactly what I'm sending, so that's very strange, even without the PLC connected!?! Looks like garbage to me?

Andy.

My guess is, you have to swap the TX/RX lines behind the module, between the module and the PLC. Your module is wired to be a DCE, pin 2 of the sub-d connector is TX, while pin 3 is RX. A DTE (e.g. the PC) has it the other way around. So try a null modem cable between the module and the PLC.

Hey Pylon,

Thanks for the input. Took me a while to test again, but I've succeeded to get the Arduino to read out the PLC! It was all a matter of cabling (see attachement).

I'm able to read/set the configuration of the PLC.

/*
PLC
 */

int incomingByte = 0;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize Serial1
  Serial.begin(9600);  //For debugging, towards PC
  if (Serial) {
    Serial.println("Connected to Computer!");
  }
  
  Serial2.begin(9600,SERIAL_7E2); //SERIAL_7E2 = 0x2C, see HardwareSerial.h
  Serial.println("Waiting for Serial2...");
  Serial2.println("@00RD0100000354*");
}

// the loop routine runs over and over again forever:
void loop() {

  while (Serial2.available() > 0) {
    incomingByte = Serial2.read();
    Serial.print("I received: ");
    Serial.write(incomingByte);
    Serial.println(".");
   }
   delay(2000);  
}

If I run it, it gives me the expexted result :

Connected to Computer!
Waiting for Serial2...
I received: @.
I received: 0.
I received: 0.
I received: R.
I received: D.
I received: 0.
I received: 0.
I received: 9.
I received: 8.
I received: 7.
I received: 6.
I received: 1.
I received: 2.
I received: 3.
I received: 4.
I received: 8.
I received: 0.
I received: 0.
I received: 0.
I received: 5.
I received: A.
I received: *.
I received: 
.

Thanks a lot for the assistance!

Andy

RS232_Cable.JPG

did you download the MODBUS library to communicate with the PLC?

Hello, adjust the baudrate of your arduino to 10,000 and try again, arduino -> serial cable -> PLC