2. The Aduino Code is as below,
#include <SoftwareSerial.h>
#include "Timer.h"
//#include <Stdint.h>
// software serial #1: TX = digital pin 10, RX = digital pin 11
SoftwareSerial portOne(10,11);
String plcString = "";
String inputPLC = "";
boolean plcComplete =false;
boolean flag_save_plc =false;
uint8_t count_words;
String words[2] ="";
int LEDpin3 = 3;
int LEDpin4 = 4;
int LEDpin5 = 5;
int LEDpin6 = 6;
int LEDpin7 = 7;
const int PBpin2 = 2; // the number of the pushbutton pin
int PBpin2State = 0; // variable for reading the pushbutton status
int PBpin2StatePrev = 0;
int AI0Pin = A0; // select the input pin for the potentiometer
int AI0Value = 0; // variable to store the value coming from the sensor
Timer t; // Timer
String resultBuf[6];
void setup()
{
inputPLC.reserve(32);
plcString.reserve(128);
words[0].reserve(32);
words[1].reserve(32);
count_words =0;
pinMode(LEDpin3, OUTPUT); // initialize the LED pin as an output:
pinMode(LEDpin4, OUTPUT); // initialize the LED pin as an output:
pinMode(LEDpin5, OUTPUT); // initialize the LED pin as an output:
pinMode(LEDpin6, OUTPUT); // initialize the LED pin as an output:
pinMode(LEDpin7, OUTPUT); // initialize the LED pin as an output:
digitalWrite(LEDpin3, LOW);
digitalWrite(LEDpin4, LOW);
digitalWrite(LEDpin5, LOW);
digitalWrite(LEDpin6, LOW);
digitalWrite(LEDpin7, LOW);
pinMode(PBpin2, INPUT); // initialize the pushbutton pin as an input:
PBpin2State = digitalRead(PBpin2); // read the initial state
// t.every(1000, takeReading); // Timer .... msec for reading data and sending to Serial
t.every(1000, takeReading); // Timer .... msec for reading data and sending to Serial
t.oscillate(LEDpin7, 1000, LOW); // Pin, time mSec, initial state
Serial.begin(9600);
portOne.begin(9600);
// portOne.begin(19200);
// portOne.begin(38400);
}
void loop() {
t.update(); // Timer
// read the value from the sensor:
AI0Value = analogRead(AI0Pin);
PBpin2StatePrev = digitalRead(PBpin2); // read input value and store it in val
if (PBpin2StatePrev != PBpin2State) { // the button state has changed!
if (PBpin2StatePrev != LOW) { // check if the button is pressed
digitalWrite(LEDpin6, HIGH); // turn LED on
// AI0Value = analogRead(AI0Pin);
// portOne.println(AI0Value);
// portOne.println("ABCDEF");
SendData();
}
if (PBpin2StatePrev != HIGH) { // check if the button is not pressed
digitalWrite(LEDpin6, LOW); // turn LED off
}
}
PBpin2State = PBpin2StatePrev; // save the new state in our variable
// Read data from PLC
if (portOne.available()) {
char inByte = (char)portOne.read();
// portOne.write(inByte);
// portOne.print(inByte);
if (inByte == 0x03) { //ETX
flag_save_plc =false;
plcComplete =true;
digitalWrite(LEDpin3, LOW);
Serial.print(">");
Serial.println(plcString);
// portOne.print(plcString);
// portOne.print(inByte);
}
else if (inByte == 0x02) { //STX
flag_save_plc =true;
inputPLC ="";
digitalWrite(LEDpin3, HIGH);
// portOne.print(inByte);
}
if (flag_save_plc == true) { //Data body
if (inByte != 0x02) {
plcString += inByte;
// portOne.print(inByte);
// portOne.print(plcString);
}
}
}
if (plcComplete) {
digitalWrite(LEDpin4, HIGH);
for(int i=0; i<plcString.length(); i++) {
if(plcString.charAt(i) != ' ') {
words[count_words] += plcString.charAt(i);
if (words[0].equals("D10012345")) {
digitalWrite(LEDpin5, HIGH);
} else {
digitalWrite(LEDpin5, LOW);
}
} else {
count_words++;
}
} //End for
// clear variable
count_words = 0;
words[0] = "";
words[1] = "";
plcString = "";
plcComplete = false;
} // End plcComplete
else
{
digitalWrite(LEDpin4, LOW);
}
// delay(10);
} //End loop
// Timer task
void takeReading()
{
SendData();
} //End takeReading
void SendData()
{
// read the value from the sensor:
// AI0Value = analogRead(AI0Pin);
String StrBuf[10];
// String StrAI0 = String(StrDataIn);
String StrAI0 = String(AI0Value);
//String Length & Trim
// portOne.print("StrAI0 = ");
// portOne.println(StrAI0);
StrAI0.trim();
// portOne.print("StrAI0_Length = ");
int StrAI0_Length = StrAI0.length();
// portOne.println(StrAI0_Length);
// portOne.println(StrDataIn);
if (StrAI0_Length == 5 ) {
StrBuf[0] = "+";
StrBuf[1] = String(StrAI0[0]);
StrBuf[2] = String(StrAI0[1]);
StrBuf[3] = String(StrAI0[2]);
StrBuf[4] = String(StrAI0[3]);
StrBuf[5] = String(StrAI0[4]);
} else if (StrAI0_Length == 4 ) {
StrBuf[0] = "+";
StrBuf[1] = "0";
StrBuf[2] = String(StrAI0[0]);
StrBuf[3] = String(StrAI0[1]);
StrBuf[4] = String(StrAI0[2]);
StrBuf[5] = String(StrAI0[3]);
} else if (StrAI0_Length == 3 ) {
StrBuf[0] = "+";
StrBuf[1] = "0";
StrBuf[2] = "0";
StrBuf[3] = String(StrAI0[0]);
StrBuf[4] = String(StrAI0[1]);
StrBuf[5] = String(StrAI0[2]);
} else if (StrAI0_Length == 2 ) {
StrBuf[0] = "+";
StrBuf[1] = "0";
StrBuf[2] = "0";
StrBuf[3] = "0";
StrBuf[4] = String(StrAI0[0]);
StrBuf[5] = String(StrAI0[1]);
} else if (StrAI0_Length == 1 ) {
StrBuf[0] = "+";
StrBuf[1] = "0";
StrBuf[2] = "0";
StrBuf[3] = "0";
StrBuf[4] = "0";
StrBuf[5] = String(StrAI0[0]);
} else {
} //End if StrAI0_Length
StrBuf[6] = "D";
StrBuf[7] = "1";
StrBuf[8] = "0";
StrBuf[9] = "0";
//Tx data => PLC
portOne.print(0x02); //STX - D0
portOne.print(StrBuf[0]); //Sign - D1
portOne.print(StrBuf[1]); //x1 - D2
portOne.print(StrBuf[2]); //x10 - D3
portOne.print(StrBuf[3]); //x100 - D4
portOne.print(StrBuf[4]); //x1000 - D5
portOne.print(StrBuf[5]); //x10000 - D6
portOne.print(StrBuf[6]); //D - D7
portOne.print(StrBuf[7]); //1 - D8
portOne.print(StrBuf[8]); //0 - D9
portOne.print(StrBuf[9]); //0 - D10
portOne.print(0x03); //ETX - D11
//Serial Mornitor on P0, P1, and on Console
Serial.print(0x02);
Serial.print(StrBuf[0]);
Serial.print(StrBuf[1]);
Serial.print(StrBuf[2]);
Serial.print(StrBuf[3]);
Serial.print(StrBuf[4]);
Serial.print(StrBuf[5]);
Serial.print(StrBuf[6]);
Serial.print(StrBuf[7]);
Serial.print(StrBuf[8]);
Serial.print(StrBuf[9]);
Serial.print(0x03);
Serial.println();
} //End SendData
To be continues ...