====== ComfileHMI connection example =======
Let's take a look at how the connection between ComfileHMI and Modular FADUINO is made specifically through an example.
First, let's start from the source code that has nothing.
void setup()
{
}
void loop()
{
}
Please add the library in this state.And A global array (i.e., link array) is required for linking. Information can be exchanged between FADUINO and ComfileHMI through this link array.
To declare it as a global array, you must declare the array outside of setup or loop.
#include "CFMEGA.h"
#include "cfSimpleModbusRtu1.h"
CFNET cfnet;
uint16_t Lword[99]; // Link array storing 16-bit word values
uint8_t Lbit[99]; // Link array storing 1-bit values
void setup()
{
}
void loop()
{
}
The groundwork is done. Now let's start MODBUS. All we have to do is insert the startModbusServer function into setup.
#include "CFMEGA.h"
#include "cfSimpleModbusRtu1.h"
CFNET cfnet;
uint16_t Lword[99];
uint8_t Lbit[99];
void setup()
{
startModbusServer(1, Lword, Lbit); // uartCh, ModbusSlaveAdr, WordArea, BitArea
}
void loop()
{
}
This concludes the coding in Arduino IDE. Now let's move on to Comfile Studio.
===== First: Let's display numbers and increasing values =====
Display only one number widget in ComfileHMI and set the address to 2.
{{ :modularfaduino:4002.png?nolink |}}
(Explanation) Modbus' address system is described like a function code. The address 40002 means address 2 of function 3 (meaning word space).
And if you modify the source as follows, the value in the variable called incvalue will continue to increase, and the value will be stored in the link array Lword.
#include "CFMEGA.h"
#include "cfSimpleModbusRtu1.h"
CFNET cfnet;
uint16_t incvalue;
uint16_t Lword[99];
uint8_t Lbit[99];
void setup()
{
startModbusServer(1, Lword, Lbit); // uartCh, ModbusSlaveAdr, WordArea, BitArea
}
void loop() {
Lword[2] = incvalue++;
}
When you run it, a continuously increasing number will be displayed on the ComfileHMI screen.
#include "CFMEGA.h"
#include "cfSimpleModbusRtu.h"
CFNET cfnet;
uint16_t incvalue;
uint16_t Lword[99];
uint8_t Lbit[99];
void setup()
{
pinMode(13,1);
startModbusServer(2, 1, Lword, Lbit); // uartCh, ModbusSlaveAdr, WordArea, BitArea
}
u16 ltemp;
u8 tempv; void loop() {
Lword[2] = incvalue++;
if (Lbit[3] == 1) {
cfnet.digitalWrite(0,15,1); } // reads the button widget information and actually turns the port On/Off.
else {
cfnet.digitalWrite(0,15,0); }
}