add set and read functions over modbus
This commit is contained in:
99
main.c
99
main.c
@@ -18,6 +18,7 @@
|
|||||||
#include "mqtt.h"
|
#include "mqtt.h"
|
||||||
#include "modbus.h"
|
#include "modbus.h"
|
||||||
#include "modbus-master.h"
|
#include "modbus-master.h"
|
||||||
|
#include "modbus_io_slave.h"
|
||||||
|
|
||||||
#include "util/delay.h"
|
#include "util/delay.h"
|
||||||
|
|
||||||
@@ -135,71 +136,6 @@ void send_values(void){
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void modbus_set_Output(uint8_t slave_adr, uint8_t nr, uint8_t state) {
|
|
||||||
switch (state) {
|
|
||||||
case BLINK:
|
|
||||||
//ioHelperSetBit(outStatesBlinking, nr, ON);
|
|
||||||
writeCoil(slave_adr,100+nr,1);
|
|
||||||
wait_write(10);
|
|
||||||
break;
|
|
||||||
//case TOGGLE:
|
|
||||||
// ioHelperSetBit(outStatesBlinking, nr, OFF);
|
|
||||||
// if (ioHelperReadBit(outStates, nr)) {
|
|
||||||
// ioHelperSetBit(outStates, nr, OFF);
|
|
||||||
// } else {
|
|
||||||
// ioHelperSetBit(outStates, nr, ON);
|
|
||||||
// }
|
|
||||||
// break;
|
|
||||||
case ON:
|
|
||||||
//ioHelperSetBit(outStates, nr, ON);
|
|
||||||
//ioHelperSetBit(outStatesBlinking, nr, OFF);
|
|
||||||
writeCoil(slave_adr,nr,1);
|
|
||||||
wait_write(10);
|
|
||||||
break;
|
|
||||||
case OFF:
|
|
||||||
writeCoil(slave_adr,nr,0);
|
|
||||||
wait_write(10);
|
|
||||||
//ioHelperSetBit(outStates, nr, OFF);
|
|
||||||
//ioHelperSetBit(outStatesBlinking, nr, OFF);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t modbus_read_Input(uint8_t nr, uint8_t type) {
|
|
||||||
uint8_t state = 0;
|
|
||||||
switch (type) {
|
|
||||||
case LEVEL:
|
|
||||||
state = ioHelperReadBit(inStates, nr);
|
|
||||||
break;
|
|
||||||
case EDGE:
|
|
||||||
state = ioHelperReadBit(inStatesBothEdges, nr);
|
|
||||||
ioHelperSetBit(inStatesBothEdges, nr, 0);
|
|
||||||
break;
|
|
||||||
case RISING:
|
|
||||||
state = ioHelperReadBit(inStatesRisingEdge, nr);
|
|
||||||
ioHelperSetBit(inStatesRisingEdge, nr, 0);
|
|
||||||
break;
|
|
||||||
case FALLING:
|
|
||||||
state = ioHelperReadBit(inStatesFallingEdge, nr);
|
|
||||||
ioHelperSetBit(inStatesFallingEdge, nr, 0);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
//reset edges after being read once
|
|
||||||
// if (state) {
|
|
||||||
// if (type == EDGE)
|
|
||||||
// ioHelperSetBit(inStatesBothEdges, nr, OFF);
|
|
||||||
// if (type == RISING)
|
|
||||||
// ioHelperSetBit(inStatesRisingEdge, nr, OFF);
|
|
||||||
// if (type == FALLING)
|
|
||||||
// ioHelperSetBit(inStatesFallingEdge, nr, OFF);
|
|
||||||
// }
|
|
||||||
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
// INIT MCU
|
// INIT MCU
|
||||||
avr_init();
|
avr_init();
|
||||||
@@ -260,6 +196,7 @@ int main(){
|
|||||||
uint32_t timer_blink_outs = millis();
|
uint32_t timer_blink_outs = millis();
|
||||||
uint32_t timer_send_uptime = millis();
|
uint32_t timer_send_uptime = millis();
|
||||||
uint32_t timer_send_info = millis();
|
uint32_t timer_send_info = millis();
|
||||||
|
uint32_t timer_read_slave = millis();
|
||||||
|
|
||||||
//printf("anlage: %x\n\r", read_Input(IN_ANLAGE_EIN_INV, LEVEL));
|
//printf("anlage: %x\n\r", read_Input(IN_ANLAGE_EIN_INV, LEVEL));
|
||||||
|
|
||||||
@@ -282,20 +219,34 @@ int main(){
|
|||||||
|
|
||||||
do_kraftsensor();
|
do_kraftsensor();
|
||||||
|
|
||||||
|
#if PLC_MQTT_ENABLED
|
||||||
char msg[64];
|
char msg[64];
|
||||||
sprintf(msg, "%d", kraftsensor_value);
|
sprintf(msg, "%d", kraftsensor_value);
|
||||||
mqtt_pub(&mqtt_client, "/Filamentanlage/01_Extruder/kraft", msg, strlen(msg));
|
mqtt_pub(&mqtt_client, "/Filamentanlage/01_Extruder/kraft", msg, strlen(msg));
|
||||||
|
#endif
|
||||||
|
|
||||||
//tgl ^= 0x01;
|
modbus_io_set_Output(4, 0, TOGGLE);
|
||||||
//modbus_set_Output(4, 0, OFF);
|
}
|
||||||
//modbus_set_Output(4, 1, ON);
|
|
||||||
//modbus_set_Output(4, 2, BLINK);
|
|
||||||
//modbus_set_Output(4, 3, tgl);
|
|
||||||
|
|
||||||
//readInputReg(4, 0, 1);
|
if(millis() - timer_read_slave > 100){
|
||||||
//uint16_t inp;
|
timer_read_slave = millis();
|
||||||
//wait_receive(1, &inp, 100);
|
//readInputState(4, 200, 32);
|
||||||
//printf("read inputs: 0x%X\n", inp);
|
modbus_io_read();
|
||||||
|
|
||||||
|
if(modbus_io_read_Input(16, RISING))
|
||||||
|
modbus_io_set_Output(4, 3, ON);
|
||||||
|
if(modbus_io_read_Input(17, RISING))
|
||||||
|
modbus_io_set_Output(4, 3, OFF);
|
||||||
|
|
||||||
|
|
||||||
|
//printf("read inputs: 0x%02X 0x%02X 0x%02X 0x%02X\n", inp[3], inp[2], inp[1], inp[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(read_Input(BTN_HEIZEN_AN, RISING)){
|
||||||
|
modbus_io_set_Output(4, MOD_OUT_HEIZEN_LED, 1);
|
||||||
|
}
|
||||||
|
if(read_Input(BTN_HEIZEN_AUS, RISING)){
|
||||||
|
modbus_io_set_Output(4, MOD_OUT_HEIZEN_LED, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if PLC_MQTT_ENABLED
|
#if PLC_MQTT_ENABLED
|
||||||
|
|||||||
@@ -97,6 +97,18 @@ void readInputReg(uint8_t slaveid, uint16_t address, uint16_t amount) {
|
|||||||
modbusSendMessage(5);
|
modbusSendMessage(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void readInputState(uint8_t slaveid, uint16_t address, uint16_t amount) {
|
||||||
|
_delay_ms(2);
|
||||||
|
rxbuffer[0]=slaveid;
|
||||||
|
modbusSetAddress(slaveid);
|
||||||
|
rxbuffer[1]=0x02;
|
||||||
|
rxbuffer[2]=(address>>8)&0xFF;
|
||||||
|
rxbuffer[3]=address&0xFF;
|
||||||
|
rxbuffer[4]=0;
|
||||||
|
rxbuffer[5]=amount;
|
||||||
|
modbusSendMessage(5);
|
||||||
|
}
|
||||||
|
|
||||||
void writeCoil(uint8_t slaveid, uint16_t address, uint16_t value) {
|
void writeCoil(uint8_t slaveid, uint16_t address, uint16_t value) {
|
||||||
_delay_ms(2);
|
_delay_ms(2);
|
||||||
rxbuffer[0]=slaveid;
|
rxbuffer[0]=slaveid;
|
||||||
|
|||||||
@@ -7,5 +7,5 @@ int8_t wait_write(uint8_t timeout);
|
|||||||
void readReg(uint8_t slaveid, uint16_t address, uint8_t amount);
|
void readReg(uint8_t slaveid, uint16_t address, uint8_t amount);
|
||||||
void writeReg(uint8_t slaveid, uint16_t address, uint16_t value);
|
void writeReg(uint8_t slaveid, uint16_t address, uint16_t value);
|
||||||
void readInputReg(uint8_t slaveid, uint16_t address, uint16_t amount);
|
void readInputReg(uint8_t slaveid, uint16_t address, uint16_t amount);
|
||||||
void readInputStatus(uint8_t slaveid, uint16_t address, uint16_t amount);
|
void readInputState(uint8_t slaveid, uint16_t address, uint16_t amount);
|
||||||
void writeCoil(uint8_t slaveid, uint16_t address, uint16_t value);
|
void writeCoil(uint8_t slaveid, uint16_t address, uint16_t value);
|
||||||
|
|||||||
67
modbus_io_slave.c
Normal file
67
modbus_io_slave.c
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
#include "avrIOhelper/io-helper.h"
|
||||||
|
#include "modbus-master.h"
|
||||||
|
|
||||||
|
#define IO_SLAVE_ADDR 0x04
|
||||||
|
|
||||||
|
volatile uint8_t modbus_io_inStates[4];
|
||||||
|
volatile uint8_t modbus_io_inStatesBothEdges[4];
|
||||||
|
volatile uint8_t modbus_io_inStatesRisingEdge[4];
|
||||||
|
volatile uint8_t modbus_io_inStatesFallingEdge[4];
|
||||||
|
|
||||||
|
void modbus_io_read(){
|
||||||
|
readInputReg(IO_SLAVE_ADDR, 0, 2);
|
||||||
|
wait_receive(2, (void*)modbus_io_inStates, 100);
|
||||||
|
|
||||||
|
readInputReg(IO_SLAVE_ADDR, 100, 2);
|
||||||
|
wait_receive(2, (void*)modbus_io_inStatesBothEdges, 100);
|
||||||
|
|
||||||
|
readInputReg(IO_SLAVE_ADDR, 200, 2);
|
||||||
|
wait_receive(2, (void*)modbus_io_inStatesFallingEdge, 100);
|
||||||
|
|
||||||
|
readInputReg(IO_SLAVE_ADDR, 300, 2);
|
||||||
|
wait_receive(2, (void*)modbus_io_inStatesRisingEdge, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
void modbus_io_set_Output(uint8_t slave_adr, uint8_t nr, uint8_t state) {
|
||||||
|
switch (state) {
|
||||||
|
case BLINK:
|
||||||
|
writeCoil(slave_adr,100+nr,1);
|
||||||
|
wait_write(10);
|
||||||
|
break;
|
||||||
|
//case TOGGLE:
|
||||||
|
// ioHelperSetBit(outStatesBlinking, nr, OFF);
|
||||||
|
// if (ioHelperReadBit(outStates, nr)) {
|
||||||
|
// ioHelperSetBit(outStates, nr, OFF);
|
||||||
|
// } else {
|
||||||
|
// ioHelperSetBit(outStates, nr, ON);
|
||||||
|
// }
|
||||||
|
// break;
|
||||||
|
case ON:
|
||||||
|
writeCoil(slave_adr,nr,1);
|
||||||
|
wait_write(10);
|
||||||
|
break;
|
||||||
|
case OFF:
|
||||||
|
writeCoil(slave_adr,nr,0);
|
||||||
|
wait_write(10);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t modbus_io_read_Input(uint8_t nr, uint8_t type) {
|
||||||
|
uint8_t state = 0;
|
||||||
|
switch (type) {
|
||||||
|
case LEVEL:
|
||||||
|
state = ioHelperReadBit(modbus_io_inStates, nr);
|
||||||
|
break;
|
||||||
|
case EDGE:
|
||||||
|
state = ioHelperReadBit(modbus_io_inStatesBothEdges, nr);
|
||||||
|
break;
|
||||||
|
case RISING:
|
||||||
|
state = ioHelperReadBit(modbus_io_inStatesFallingEdge, nr);
|
||||||
|
break;
|
||||||
|
case FALLING:
|
||||||
|
state = ioHelperReadBit(modbus_io_inStatesRisingEdge, nr);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
10
modbus_io_slave.h
Normal file
10
modbus_io_slave.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef _MODBUS_IO_SLAVE_H_
|
||||||
|
#define _MODBUS_IO_SLAVE_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
void modbus_io_read(void);
|
||||||
|
void modbus_io_set_Output(uint8_t slave_adr, uint8_t nr, uint8_t state);
|
||||||
|
uint8_t modbus_io_read_Input(uint8_t nr, uint8_t type);
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user