LightHub 4.6.0
Open-source smart home controller firmware
Loading...
Searching...
No Matches
ipmodbus.h
Go to the documentation of this file.
1
11#ifdef IPMODBUS
12#pragma once
13#include <CircularBuffer.h> // CircularBuffer https://github.com/rlogiacco/CircularBuffer
14#include <Arduino.h>
15#include <Ethernet.h>
16#include <EthernetUdp.h>
17//#include <utility/w5100.h>
18#include <HardwareSerial.h>
19#include "options.h"
20#include "microtimer.h"
21//#include "streamlog.h"
22//extern Streamlog debugSerial;
23#include "main.h"
24extern void preTransmission();
25extern void postTransmission();
26extern short modbusBusy;
27
28
29#ifndef SERIAL_TX_BUFFER_SIZE
30#define SERIAL_TX_BUFFER_SIZE SERIAL_BUFFER_SIZE
31#endif
32
33const bool enableRtuOverTcp = false;
34const int serialRetry = 5;
35const int serialTimeout = 800;
36const int tcpPort = 502;
37const int udpPort = 502;
38
43typedef struct {
44 byte tid[2]; // MBAP Transaction ID
45 byte uid; // MBAP Unit ID (address)
46 byte PDUlen; // lenght of PDU (func + data) stored in queuePDUs
47 IPAddress remIP; // remote IP for UDP client (UDP response is sent back to remote IP)
48 unsigned int remPort; // remote port for UDP client (UDP response is sent back to remote port)
49 byte clientNum; // TCP client who sent the request, UDP_REQUEST (0xFF) designates UDP client
50} header;
51
52
53
54const byte reqQueueCount = 15; // max number of TCP or UDP requests stored in queue
55const int reqQueueSize = 256; // total length of TCP or UDP requests stored in queue (in bytes)
56const byte maxSlaves = 247; // max number of Modbus slaves (Modbus supports up to 247 slaves, the rest is for reserved addresses)
57const int modbusSize = 256; // size of a MODBUS RTU frame (determines size of serialInBuffer and tcpInBuffer)
58
59#define mySerial modbusSerial // define serial port for RS485 interface, for Arduino Mega choose from Serial1, Serial2 or Serial3
60
61// #define DEBUG // Main Serial (USB) is used for printing some debug info, not for Modbus RTU. At the moment, only web server related debug messages are printed.
62//#define debugSerial Serial
63
64#ifdef MAX_SOCK_NUM //if the macro MAX_SOCK_NUM is defined
65// #undef MAX_SOCK_NUM //un-define it
66// #define MAX_SOCK_NUM 8 //redefine it with the new value
67#endif
68
69/****** ETHERNET AND SERIAL ******/
70
71#ifdef UDP_TX_PACKET_MAX_SIZE //if the macro MAX_SOCK_NUM is defined
72#undef UDP_TX_PACKET_MAX_SIZE //un-define it
73#define UDP_TX_PACKET_MAX_SIZE modbusSize //redefine it with the new value
74#endif
75
76
77
78
79//#ifdef DEBUG
80#define dbg(x...) debugSerial.print(x);
81#define dbgln(x...) debugSerial.println(x);
82//#else /* DEBUG */
83//#define dbg(x...) ;
84//#define dbgln(x...) ;
85//#endif /* DEBUG */
86
87#define UDP_REQUEST 0xFF // We store these codes in "header.clientNum" in order to differentiate
88#define SCAN_REQUEST 0xFE // between TCP requests (their clientNum is nevew higher than 0x07), UDP requests and scan requests (triggered by scan button)
89
90
91
93enum state : byte
94{
95 MBIDLE, SENDING, DELAY, WAITING
96};
97
98
99
104void ipmodbusLoop();
108void setupIpmodbus();
116byte checkRequest(byte buffer[], unsigned int bufferSize);
121void calculateCRC(byte b);
127bool getSlaveResponding(const uint8_t index);
134bool checkCRC(byte buf[], int len);
135#endif
short modbusBusy
Definition item.cpp:78
#define b
Definition item.cpp:2411
void preTransmission()
Marks that an Ethernet transmission is about to happen; disables time-sensitive drivers as needed.
Definition main.cpp:2136
void postTransmission()
Marks that an Ethernet transmission has finished; re-enables drivers and handles lost-time compensati...
Definition main.cpp:2145
Main controller interface of LightHub.