Arduino DF1 Micrologix Sending Entire Bytes instead of one state at a time.

Hello,

I have a Micrologix 1200 PLC and a Mega hooked up and communicating via RS232 and DF1. I am using the DF1 library and can read a digital input state on the Mega and I have it sending to the Micrologix 1200 where it will toggle an output.

I need to send the state of 16 pins on the Mega and Instead of sending each pin state separately, I would really like to send 2 bytes (Entire ports A and C) PINA, PINC.

Below is my code for reading a single pin and outputting it to the Micrologix, but I would really appreciate any help with sending entire bytes to the Micrologix registers N7:0 to N7:15 respectively.

#include <DF1.h>

#define DF1destination 1
#define DF1baud 38400
#define DF1format SERIAL_8N1
#define DF1timeout 1000


enum
{ 
  DF1PACKET1,
  DF1TOTAL_NO_OF_PACKETS
};
DF1Packet DF1packets[DF1TOTAL_NO_OF_PACKETS];
DF1packetPointer DF1packet1 = &DF1packets[DF1PACKET1];
unsigned int DF1writeRegs[1];


void setup() {
  
DF1_construct(DF1packet1, DF1destination, DF1_WRITE_N7, 0, 1, DF1writeRegs);
DF1_configure(&Serial, DF1baud, DF1format, DF1timeout, DF1packets, DF1TOTAL_NO_OF_PACKETS);
pinMode(8, INPUT_PULLUP);
}

void loop() {
  DF1_update();
  int sw8 = digitalRead(8);   
  DF1writeRegs[0] = sw8;
   
}

Below is my code for reading a single pin

Since the smallest unit on the Arduino is a byte, reading one pin or 8 pins, results in exactly the same amount of data.

unsigned int DF1writeRegs[1];

An array of size 1 is pretty silly.

The code for writing all 8 bits at once will look exactly the same as the code for writing one bit, since you can't write a single bit.

Apologies for necro-ing an old thread, but I hope PaulS can clarify something for me. I know from his other plentiful threads that he knows about this stuff, and this thread should appear in his unreads.

My interest in this stems from this current thread, where the OP there talks of the “ab df1 arduino driver for plc” (and I’m only assuming she means the DF1.h library, but no matter if she does or not) and she laments its use of pins 0 and 1 by default.

But anyway, my question here concerns the use of &Serial here in the OP:

DF1_configure(&Serial, DF1baud, DF1format, DF1timeout, DF1packets, DF1TOTAL_NO_OF_PACKETS);

Am I right, firstly, that Serial there means Serial as we know it, as in Serial.begin()?

And then if so, that line presumably configures the DF1 interface to talk over pins 0 and 1, using the baud etc as defined in the other parameters, by linking (I’m sure there’s a real word for that) DF1 to Serial via that & “address of” operator.

And lastly then how would one use software serial? Is it a simple matter of:

#include <SoftwareSerial.h>

SoftwareSerial DF1_Serial(10, 11);

void setup()
{
..
DF1_configure(&DF1_Serial, DF1baud, DF1format, DF1timeout, DF1packets, DF1TOTAL_NO_OF_PACKETS);
..
}

Yes PaulS, before you say, it’s probably time I boned up on pointers and their related concepts :wink: but for now a quick and dirty answer to the above would be good of you.

Am I right, firstly, that Serial there means Serial as we know it, as in Serial.begin()?

Yes. The &Serial is just taking the address of the object.

And then if so, that line presumably configures the DF1 interface to talk over pins 0 and 1, using the baud etc as defined in the other parameters, by linking (I'm sure there's a real word for that) DF1 to Serial via that & "address of" operator.

That line tells the DF1 instance what object to use to communicate. It knows nothing about what the capabilities of the object are. You are, substantially, correct, though.

And lastly then how would one use software serial? Is it a simple matter of:

Yes.

Yes PaulS, before you say, it's probably time I boned up on pointers and their related concepts :wink: but for now a quick and dirty answer to the above would be good of you.

None of this involves pointers.

On the other hand, understanding pointers never hurt anybody. Although getting that understanding can hurt. 8)

PaulS:
None of this involves pointers.

I though & address of was was the "reverse path" of a pointer.

PaulS:
Although getting that understanding can hurt. 8)

That's pretty much why I haven't looked into them more than oooo about a dozen times over the years...

Thanks for the answers. I will make the point in the other thread that she should be able to use software serial like that, if it's the same library that is.

In continuation to the same topic posting a question here so that I can get answers from people who have already used same DF1 Arduino library. I’m right now testing arduino code with DF1 Simulator Software which works as a DF1 slave on PC. I have installed DF1 Simulator on PC and connected Arduino Uno to USB port through standard Uno USB cable. I tried writing and reading values from N7 -

  1. I am able to write values to DF1 Simulator, arduino is able to insert the values which can be seen added to the Simulator http://www.plcsimulator.org/downloads

  2. Writing values is working but reading values is not working, can someone suggest what could be the reason ?

Code Used to Read Values from N7:0:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);
#include <DF1.h>

#define DF1destination 1
#define DF1baud 19200
#define DF1format SERIAL_8N1
#define DF1timeout 1000

enum
{
DF1PACKET1,
DF1PACKET2,
DF1TOTAL_NO_OF_PACKETS
};

DF1Packet DF1packets[DF1TOTAL_NO_OF_PACKETS];
DF1packetPointer DF1packet1 = &DF1packets[DF1PACKET1];

unsigned int DF1readRegs[1];

void setup() {

lcd.begin();
lcd.backlight();

DF1_construct(DF1packet1, DF1destination, DF1_READ_N7, 0, 1, DF1readRegs);
DF1_configure(&Serial, DF1baud, DF1format, DF1timeout, DF1packets, DF1TOTAL_NO_OF_PACKETS);

}

void loop() {
DF1_update();
readValues();
}

void readValues() {

//Read N7:0
unsigned int N7_0 = DF1readRegs[0];
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("N7_0 = ");
lcd.println(N7_0);
delay(1000);

}

Using DF1 simulator with below settings:
Baud Rate: 19200
Parity: None
Data Bits: 8
Stop Bits: 1