-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
brickpool
committed
Dec 23, 2020
1 parent
e9d185c
commit 75f32e8
Showing
9 changed files
with
1,888 additions
and
1,741 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,124 @@ | ||
#include <CustomSoftwareSerial.h> | ||
#include "LogoPG.h" | ||
|
||
const byte rxPin = 2; | ||
const byte txPin = 3; | ||
byte buf[1]; | ||
|
||
// set up the SoftwareSerial object | ||
CustomSoftwareSerial LogoSerial(rxPin, txPin); | ||
// set up the LogoClient object | ||
LogoClient LOGO(&LogoSerial); | ||
|
||
void setup() { | ||
// Init Monitor interface | ||
Serial.begin(9600); | ||
while (!Serial) { | ||
; // wait for serial port to connect. Needed for native USB port only | ||
} | ||
|
||
// Start the SoftwareSerial Library | ||
LogoSerial.begin(9600, CSERIAL_8E1); | ||
// Setup Time, 1s. | ||
delay(1000); | ||
Serial.println(""); | ||
Serial.println("Cable connected"); | ||
if (LogoSerial.isListening()) | ||
Serial.println("Softserial is listening !"); | ||
} | ||
|
||
void loop() | ||
{ | ||
int Result, Status; | ||
|
||
// Connection | ||
while (!LOGO.Connected) | ||
{ | ||
if (!Connect()) | ||
delay(2000); | ||
} | ||
|
||
Result = LOGO.GetPlcStatus(&Status); | ||
if (Result == 0) | ||
{ | ||
if (Status == LogoCpuStatusRun) | ||
{ | ||
Serial.print("FETCH DATA ... "); | ||
Result = LOGO.ReadArea(LogoAreaDB, 1, VM_Q01_08, sizeof(buf), buf); | ||
if (Result == 0) | ||
{ | ||
Serial.print("OK: "); | ||
Serial.println(buf[0], HEX); | ||
} | ||
else | ||
CheckError(Result); | ||
} | ||
else | ||
{ | ||
Serial.println("STARTING THE PROG"); | ||
LOGO.PlcStart(); | ||
} | ||
} | ||
else | ||
CheckError(Result); | ||
|
||
delay(2000); | ||
} | ||
|
||
bool Connect() | ||
{ | ||
int Result = LOGO.Connect(); | ||
Serial.println("Try to connect with LOGO"); | ||
if (Result == 0) | ||
{ | ||
Serial.println("Connected!"); | ||
Serial.print("PDU Length = "); | ||
Serial.println(LOGO.GetPDULength()); | ||
} | ||
else | ||
{ | ||
Serial.println("Connection error!"); | ||
} | ||
return Result == 0; | ||
} | ||
|
||
void CheckError(int ErrNo) | ||
{ | ||
Serial.print("Error No. 0x"); | ||
Serial.println(ErrNo, HEX); | ||
|
||
// Checks if it's a LOGO Error => we need to disconnect | ||
if (ErrNo & 0x01FF) | ||
{ | ||
Serial.println("LOGO ERROR, disconnecting."); | ||
LOGO.Disconnect(); | ||
} | ||
} | ||
#ifdef ARDUINO_AVR_UNO | ||
#if ARDUINO != 10805 | ||
#error Arduino 1.8.5 required. | ||
#endif | ||
#include <CustomSoftwareSerial.h> | ||
#endif | ||
#include <ArduinoLog.h> | ||
#include "LogoPG.h" | ||
|
||
byte buf[2]; | ||
|
||
// set up the LogoSerial object | ||
#ifdef CustomSoftwareSerial_h | ||
#if defined(SERIAL_8E1) | ||
#undef SERIAL_8E1 | ||
#endif | ||
#define SERIAL_8E1 CSERIAL_8E1 | ||
#define rxPin 2 | ||
#define txPin 3 | ||
CustomSoftwareSerial LogoSerial(rxPin, txPin); | ||
#else | ||
// Serial1: | ||
// rxPin 18 | ||
// txPin 19 | ||
#define LogoSerial Serial1 | ||
#endif | ||
|
||
// set up the LogoClient object | ||
LogoClient LOGO(&LogoSerial); | ||
|
||
void setup() { | ||
// Init Monitor interface | ||
Serial.begin(9600); | ||
while (!Serial) { | ||
; // wait for serial port to connect. Needed for native USB port only | ||
} | ||
|
||
// Start the SoftwareSerial Library | ||
LogoSerial.begin(9600, SERIAL_8E1); | ||
// Setup Time, 1s. | ||
delay(1000); | ||
Serial.println(""); | ||
Serial.println("Cable connected"); | ||
#ifdef CustomSoftwareSerial_h | ||
if (LogoSerial.isListening()) | ||
#else | ||
if (LogoSerial) | ||
#endif | ||
Serial.println("LogoSerial is ready."); | ||
|
||
// LOG_LEVEL_SILENT, LOG_LEVEL_FATAL, LOG_LEVEL_ERROR, LOG_LEVEL_WARNING, LOG_LEVEL_NOTICE, LOG_LEVEL_TRACE, LOG_LEVEL_VERBOSE | ||
// Note: if you want to fully remove all logging code, uncomment #define DISABLE_LOGGING in Logging.h | ||
// this will significantly reduce your project size | ||
Log.begin(LOG_LEVEL_ERROR, &Serial); | ||
} | ||
|
||
void loop() | ||
{ | ||
int Result, Status; | ||
|
||
// Connection | ||
while (!LOGO.Connected) | ||
{ | ||
if (!Connect()) | ||
delay(2000); | ||
} | ||
|
||
Result = LOGO.GetPlcStatus(&Status); | ||
if (Result == 0) | ||
{ | ||
if (Status == LogoCpuStatusRun) | ||
{ | ||
Serial.print("FETCH DATA ... "); | ||
// read first analog input | ||
Result = LOGO.ReadArea(LogoAreaDB, 1, AI_0BA7, sizeof(buf), buf); | ||
if (Result == 0) | ||
{ | ||
Serial.print("OK: "); | ||
Serial.println(LH.WordAt(buf, 0)); | ||
} | ||
else | ||
CheckError(Result); | ||
} | ||
else | ||
{ | ||
Serial.println("STARTING THE PROG"); | ||
LOGO.PlcStart(); | ||
} | ||
} | ||
else | ||
CheckError(Result); | ||
|
||
delay(2000); | ||
} | ||
|
||
bool Connect() | ||
{ | ||
int Result = LOGO.Connect(); | ||
Serial.println("Try to connect with LOGO"); | ||
if (Result == 0) | ||
{ | ||
Serial.println("Connected!"); | ||
Serial.print("PDU Length = "); | ||
Serial.println(LOGO.GetPDULength()); | ||
} | ||
else | ||
{ | ||
Serial.println("Connection error!"); | ||
} | ||
return Result == 0; | ||
} | ||
|
||
void CheckError(int ErrNo) | ||
{ | ||
Serial.print("Error No. 0x"); | ||
Serial.println(ErrNo, HEX); | ||
|
||
// Checks if it's a LOGO Error => we need to disconnect | ||
if (ErrNo & 0x01FF) | ||
{ | ||
Serial.println("LOGO ERROR, disconnecting."); | ||
LOGO.Disconnect(); | ||
} | ||
} |
Oops, something went wrong.