Hey guys,
This is hard to explain but here goes!
I'm using this DF1 library Here to communicate between a Micrologix 1200 plc and a Mega. I also have 2 Mega's communicating with a Max485 and this is where the strange behavior happens.
I'm using the default serial port for the DF1 and then I'm using Serial2 for the Max485 communication between the two Mega's. In my code below you will see I have my Serial2.begin(9600) but this sketch will not work unless I begin Serial3. ?????
I have nothing hooked up to 3 and do not need it anywhere in the project, so why does this only work with Serial3.begin(9600) added to the sketch.
I have even tried modifying the code and wiring up to Serial1, but I had to begin Serial2 for that to work as well. Seems like I always have to .begin the next port in line to work.
I'm clueless here!!! Any hints would be awesome!
//======================= DF1 =============================
#include <DF1.h>
#define DF1destination 1
#define DF1baud 38400
#define DF1format SERIAL_8N1
#define DF1timeout 3000
// Define integer holders for the incomming Port bytes from the slaves
unsigned int cl_port1;
unsigned int cl_port2;
unsigned int cl_port3;
unsigned int el_port1;
unsigned int el_port2;
unsigned int el_port3;
unsigned int cab_port1;
unsigned int cab_port2;
unsigned int cab_port3;
enum
{
DF1PACKET1,
DF1TOTAL_NO_OF_PACKETS
};
DF1Packet DF1packets[DF1TOTAL_NO_OF_PACKETS];
DF1packetPointer DF1packet1 = &DF1packets[DF1PACKET1];
unsigned int DF1writeRegs[0];
//===================== END DF1 ==============================
#define Serial2TxControl 13 //RS485 Direction control
int incomingByte0;
int incomingByte1;
int CL_pin = 5; // C/L Status
int EL_pin = 6; // E/L Status
int CAB_pin = 7; // CAB Status
void setup() /****** SETUP******/
{
//======================= DF1 SETUP =============================
DF1_construct(DF1packet1, DF1destination, DF1_WRITE_N7, 70, 16, DF1writeRegs);
DF1_configure(&Serial, DF1baud, DF1format, DF1timeout, DF1packets, DF1TOTAL_NO_OF_PACKETS);
DF1writeRegs[0] = 0;
DF1writeRegs[1] = 0;
DF1writeRegs[2] = 0;
DF1writeRegs[3] = 0;
DF1writeRegs[4] = 0;
DF1writeRegs[5] = 0;
DF1writeRegs[6] = 0;
DF1writeRegs[7] = 0;
DF1writeRegs[8] = 0;
DF1writeRegs[9] = 0;
DF1writeRegs[10] = 0;
DF1writeRegs[11] = 0;
DF1writeRegs[12] = 0;
DF1writeRegs[13] = 0;
DF1writeRegs[14] = 0;
DF1_update();
//----------------------END DF1 SETUP --------------------------------------
//--- Status Pins-------- Added 9/20/17
pinMode(CL_pin, INPUT_PULLUP); // C/L Status
pinMode(EL_pin, INPUT_PULLUP); // E/L Status
pinMode(CAB_pin, INPUT_PULLUP); // Cab Status
pinMode(Serial2TxControl, OUTPUT);
Serial2.begin(9600); // set the data rate
Serial3.begin(9600); // set the data rate
//===============================SETUP@=======================================================
}//--(end setup )---
//-----------------------------------------------------------------------------------------------------------------------------
void loop()
{
// New feature for Status Check or Cable break to shut off outputs in case of break in cables and such
// do an if statement to see if pin 7 is low. If yes, then only run serial read for ports A & C. If no, then set PORTA = B00000000 (to shut off outputs) and rerun the loop until the status is good.
// Run another if statement to check if pins 5 $ 6 are low, this will let us know the back boxes are OK.
if (digitalRead(CL_pin) == HIGH && digitalRead(CAB_pin) == HIGH) // If there are no boxes connected to C/L or CAB then turn off all outputs
{
DF1writeRegs[0] = 0;
DF1writeRegs[1] = 0;
DF1writeRegs[2] = 0;
DF1writeRegs[3] = 0;
DF1writeRegs[4] = 0;
DF1_update();
//PORTC = B00000000;
}
else if (digitalRead(CL_pin) == LOW || digitalRead(CAB_pin) == LOW) // If there is a switch box connected to C/L or CAB controllers then run normal operations...
{
if (Serial2.available()>2) //Look for 3 bytes of data from other Arduino
{
incomingByte0 = Serial2.read(); // read a byte
cl_port1 = incomingByte0; // D22 - D29 - 1st byte of the incomming data
incomingByte0 = Serial2.read(); // read a byte
cl_port2 = incomingByte0; // D30 to D37 - 2nd byte of the incomming data
// **** Push the values to the PLC
DF1writeRegs[0] = cl_port1;
DF1writeRegs[1] = cl_port2;
DF1_update();
}
}
}//--(end main loop )---