Modbus slave/master 아누이드 프로그래밍
- 구구콘
- 73
- 0
Modbus slave code
#include <SimpleModbusSlave.h>
#define RXPin 0 // USB 시리얼일 경우 주석 처리할 것
#define TXPin 1 // USB 시리얼일 경우 주석 처리할 것
#define PTTPin 2 // USB 시리얼일 경우 주석 처리할 것
enum {
PARAM1,
PARAM2,
PARAM3, // add params as you want, here! ( 3개만 전송하는 경우의 예)
// leave this one
TOTAL_REGS_SIZE
// total number of registers for function 3 and 16 share the same register array
};
unsigned int registers[TOTAL_REGS_SIZE];
void setup() {
// Modbus init
modbus_configure(9600, 1, 2, TOTAL_REGS_SIZE,0);
}
void loop() {
modbus_update(registers);
}
Modbus master code
#include <SimpleModbusMaster.h>
#define baud 9600
#define timeout 1000
#define polling 250
#define retry_count 10
#define TxEnablePin 8 // Pay attention! Use 3 if you want…
enum {
READ_P,
// leave this one
TOTAL_NO_OF_PACKETS
// total number of registers for function 3 and 16 share the same register array
};
enum {
PARAM1,
PARAM2,
PARAM3, // add params as you want, here!
// leave this one
TOTAL_REGS_SIZE
// total number of registers for function 3 and 16 share the same register array
};
Packet packets[TOTAL_NO_OF_PACKETS];
packetPointer readPacket = &packets[READ_P];
unsigned int slavedata[TOTAL_REGS_SIZE];
void setup()
{
// read 3 registers starting at address 0
readPacket->id = 2;
readPacket->function = READ_HOLDING_REGISTERS;
readPacket->address = 0;
readPacket->no_of_registers = TOTAL_REGS_SIZE;
readPacket->register_array = slavedata;
modbus_configure(baud, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS);
}
void loop()
{
unsigned int connection_status = modbus_update(packets);
if (connection_status != TOTAL_NO_OF_PACKETS)
{
// You could re-enable the connection by:
packets[connection_status].connection = true;
}
}