more gefran code
This commit is contained in:
30
gefran_gtf.c
30
gefran_gtf.c
@@ -1,11 +1,14 @@
|
||||
#include "modbus-master.h"
|
||||
#include "gefran_gtf.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#define FIRMWARE_VERSION 120
|
||||
|
||||
uint16_t gtf_firmware_version(uint8_t address){
|
||||
return gtf_read_register(address, GTF_FIRMWARE_VERSION);
|
||||
}
|
||||
|
||||
uint16_t gtf_read_register(uint8_t address, uint8_t reg){
|
||||
uint16_t val = 0;
|
||||
readReg(address, FIRMWARE_VERSION, 1);
|
||||
readReg(address, reg, 1);
|
||||
int8_t res = wait_receive(1, &val, 250);
|
||||
|
||||
if(!res)
|
||||
@@ -14,3 +17,24 @@ uint16_t gtf_firmware_version(uint8_t address){
|
||||
return res;
|
||||
}
|
||||
|
||||
int8_t gtf_write_register(uint8_t address, uint8_t reg, uint16_t value){
|
||||
writeReg(address, reg, value);
|
||||
return wait_write(100);
|
||||
}
|
||||
|
||||
int8_t gtf_read_coil(uint8_t address, uint8_t coil){
|
||||
uint16_t val=0;
|
||||
readCoil(address, coil);
|
||||
uint8_t res = wait_receive_coil(100);
|
||||
if(res)
|
||||
return -1;
|
||||
return val;
|
||||
}
|
||||
|
||||
int8_t gtf_write_coil(uint8_t address, uint8_t coil, uint8_t val){
|
||||
writeCoil(address, coil, val);
|
||||
uint8_t res = wait_write(100);
|
||||
if(res)
|
||||
return -1;
|
||||
return val;
|
||||
}
|
||||
|
||||
11
gefran_gtf.h
11
gefran_gtf.h
@@ -1,2 +1,13 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#define GTF_FIRMWARE_VERSION 122
|
||||
#define GTF_OPERATING_HOURS 161
|
||||
#define GTF_BAUDRATE 24
|
||||
#define GTF_SETPOINT 27
|
||||
#define GTF_POWER 106
|
||||
|
||||
uint16_t gtf_firmware_version(uint8_t address);
|
||||
uint16_t gtf_read_register(uint8_t address, uint8_t reg);
|
||||
int8_t gtf_write_register(uint8_t address, uint8_t reg, uint16_t value);
|
||||
int8_t gtf_read_coil(uint8_t address, uint8_t coil);
|
||||
int8_t gtf_write_coil(uint8_t address, uint8_t coil, uint8_t val);
|
||||
|
||||
15
main.c
15
main.c
@@ -168,6 +168,12 @@ int main()
|
||||
send_value(&mqtt_client, "/Filamentanlage/02_Wasserbecken/connected", 1);
|
||||
send_value(&mqtt_client, "/Filamentanlage/02_Wasserbecken/state/pumpe_warm", -1);
|
||||
|
||||
//gtf_write_coil(5, 0, 1);
|
||||
//gtf_write_coil(5, 1, 1);
|
||||
|
||||
//gtf_write_register(5, 54, 6);
|
||||
//gtf_write_register(5, 56, 100);
|
||||
|
||||
while(1)
|
||||
{
|
||||
wdt_reset(); // WDT reset at least every sec
|
||||
@@ -188,7 +194,14 @@ int main()
|
||||
if(millis() - timer_send_temps > 500){
|
||||
timer_send_temps = millis();
|
||||
send_temperatures();
|
||||
printf("gefran: 0x%02X\n", gtf_firmware_version(5));
|
||||
//printf("gefran firm: 0x%02X\n", gtf_firmware_version(5));
|
||||
//printf("gefran stat2: 0x%02X\n", gtf_read_register(5, 102));
|
||||
//printf("gefran stat3: 0x%02X\n", gtf_read_register(5, 117));
|
||||
////printf("gefran stell: 0x%02X\n", gtf_read_register(5, 56));
|
||||
////printf("gefran disp: 0x%02X\n", gtf_read_register(5, 0));
|
||||
//printf("gefran coil: 0x%02X\n", gtf_read_coil(5, 0));
|
||||
|
||||
//send_value(&mqtt_client, "/Filamentanlage/02_Wasserbecken/state/gtf5_power", gtf_read_register(5, GTF_POWER));
|
||||
|
||||
if(read_Input(IN_KLATWASSER_DRAN, LEVEL)){
|
||||
send_value(&mqtt_client, "/Filamentanlage/03_Wasserbecken/state/connected", 1);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <stdio.h>
|
||||
#include <util/delay.h>
|
||||
#include "modbus.h"
|
||||
|
||||
@@ -121,6 +122,44 @@ void writeCoil(uint8_t slaveid, uint16_t address, uint16_t value) {
|
||||
modbusSendMessage(5);
|
||||
}
|
||||
|
||||
void readCoil(uint8_t slaveid, uint16_t address) {
|
||||
_delay_ms(2);
|
||||
rxbuffer[0]=slaveid;
|
||||
modbusSetAddress(slaveid);
|
||||
rxbuffer[1]=0x01;
|
||||
rxbuffer[2]=(address>>8)&0xFF;
|
||||
rxbuffer[3]=address&0xFF;
|
||||
rxbuffer[4]=0x00;
|
||||
rxbuffer[5]=1;
|
||||
modbusSendMessage(5);
|
||||
}
|
||||
|
||||
int8_t wait_receive_coil(uint8_t timeout){
|
||||
|
||||
uint8_t breaker = timeout;
|
||||
while(!receiveOkay && breaker) { //wait for client response, time out after 1s
|
||||
breaker--;
|
||||
_delay_ms(1);
|
||||
if(breaker==0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(receiveOkay) { //if this fails, there was either no response or a crc error
|
||||
if(rxbuffer[1]&0x80) { //client responded with an error code
|
||||
//handle the error
|
||||
printf("modbus error\n");
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
printf("coil: %d\n", rxbuffer[3]);
|
||||
return (int8_t)rxbuffer[3];
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
ISR(TIMER2_COMPA_vect) { //this ISR is called 9765.625 times per second
|
||||
modbusTickTimer();
|
||||
}
|
||||
|
||||
@@ -9,3 +9,5 @@ void writeReg(uint8_t slaveid, uint16_t address, uint16_t value);
|
||||
void readInputReg(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 readCoil(uint8_t slaveid, uint16_t address);
|
||||
int8_t wait_receive_coil(uint8_t timeout);
|
||||
|
||||
Reference in New Issue
Block a user