I am (supposed to be) programming an Omron PLC to communicate with a particle counter. The particle counter accepts ASCII commands over RS-232 serial with the following parameters (see attached): 4800bps, 7 data bits, 2 stop bits, even parity.
I don’t have access to the particle counter right now so I am programming an Arduino Uno to act as a particle counter simulator.
As a test, I am sending (2) 16-bit words (4 bytes) from the PLC (see attachment):
D100: 01100010 01100001 [b a]
D101: 01100011 01100100 [c d]
The PLC is configured to send least significant byte first no start code, and for an end code: CR,LF
I am trying to get the Arduino to receive these bytes and recognize them as ASCII, and print out in the serial monitor so I can confirm on the laptop that it is receiving the proper commands from the PLC.
I was not able to make this work (serial @ 7,E,2) using the normal softwareSerial library so I am using ledongthuc’s CustomSoftwareSerial and I am receiving data from the PLC but it doesn’t represent the bits I am sending. I was only getting garbledy-gook out of it.
I’m expecting “abdc” on the serial monitor but I’m getting “=;7rJV|”
#include <CustomSoftwareSerial.h> //import the custom software serial library
CustomSoftwareSerial* customSerial; //define serial port name
String myText;
boolean done = 0;
void setup() {
Serial.begin(19200); //baud rate between Arduino and PC
customSerial = new CustomSoftwareSerial(3, 2); // define rx & tx pins.
customSerial->begin(4800, CSERIAL_7E2); //start software serial port with 4800baud, 7 bits, even parity, 2 stop bits.
}
void loop() {
// this code prints out each character from the software serial port in binary. paste into void loop()
while(customSerial->available() > 0){
done = 0;
delay(1); //small delay to allow input buffer to fill
char c = customSerial->read(); //gets one byte from serial buffer
if (c == '\r') {
break; //breaks out of capture loop to print readstring
}
myText += c; //makes the string readString
}
if (done == 0){
for(int i=0; i<myText.length(); i++){
char myChar = myText.charAt(i);
Serial.print(myChar);
Serial.println("");
}
}
done = 1;
}
If I view the binary by making this change…
Serial.print(myChar, BIN);
…I get this:
111101
111011
110111
1110010
1001010
1010110
1111100
When I was expecting this:
00001010 LF
00001101 CR
01100011 c [D101 most significant byte]
01100100 d [D101 least significant byte]
01100010 b [D100 most significant byte]
01100001 a [D100 least significant byte]
I think it must be something to do with the 7 bits versus 8, but I don’t know what to do. Any help would be greatly appreciated!