Lydia - Printhead
Loading...
Searching...
No Matches
Mudbus Class Reference

#include <Mudbus.h>

Public Member Functions

 Mudbus ()
 
void Run ()
 

Public Attributes

bool Active
 
bool C [MB_N_C]
 
unsigned long PreviousActivityTime
 
int R [MB_N_R]
 
int Reads
 
int Runs
 
int Writes
 

Private Member Functions

void SetFC (int fc)
 

Private Attributes

uint8_t ByteArray [260]
 
int FC
 

Detailed Description

Definition at line 37 of file Mudbus.h.

Constructor & Destructor Documentation

◆ Mudbus()

Mudbus::Mudbus ( )

Definition at line 28 of file Mudbus.cpp.

29{
30}

Member Function Documentation

◆ Run()

void Mudbus::Run ( )

Definition at line 32 of file Mudbus.cpp.

33{
34 Runs = 1 + Runs * (Runs < 999);
35
36 //****************** Read from socket ****************
37 // For Arduino 0022
38 // Client client = MbServer.available();
39 // For Arduino 1.0
40 EthernetClient client = MbServer.available();
41 if(client.available())
42 {
43 Reads = 1 + Reads * (Reads < 999);
44 int i = 0;
45 while(client.available())
46 {
47 ByteArray[i] = client.read();
48 i++;
49 }
50 SetFC(ByteArray[7]); //Byte 7 of request is FC
51 if(!Active)
52 {
53 Active = true;
55 #ifdef MbDebug
56 Serial.println("Mb active");
57 #endif
58 }
59 }
60 if(millis() > (PreviousActivityTime + 60000))
61 {
62 if(Active)
63 {
64 Active = false;
65 #ifdef MbDebug
66 Serial.println("Mb not active");
67 #endif
68 }
69 }
70
72
73 //****************** Read Coils **********************
74 if(FC == MB_FC_READ_COILS)
75 {
81 #ifdef MbDebug
82 Serial.print(" MB_FC_READ_COILS S=");
83 Serial.print(Start);
84 Serial.print(" L=");
85 Serial.println(CoilDataLength);
86 #endif
87 ByteArray[5] = ByteDataLength + 3; //Number of bytes after this one.
88 ByteArray[8] = ByteDataLength; //Number of bytes after this one (or number of bytes of data).
89 for(int i = 0; i < ByteDataLength ; i++)
90 {
91 for(int j = 0; j < 8; j++)
92 {
93 bitWrite(ByteArray[9 + i], j, C[Start + i * 8 + j]);
94 }
95 }
98 Writes = 1 + Writes * (Writes < 999);
99 FC = MB_FC_NONE;
100 }
101
102 //****************** Read Registers ******************
104 {
105 Start = word(ByteArray[8],ByteArray[9]);
108 #ifdef MbDebug
109 Serial.print(" MB_FC_READ_REGISTERS S=");
110 Serial.print(Start);
111 Serial.print(" L=");
112 Serial.println(WordDataLength);
113 #endif
114 ByteArray[5] = ByteDataLength + 3; //Number of bytes after this one.
115 ByteArray[8] = ByteDataLength; //Number of bytes after this one (or number of bytes of data).
116 for(int i = 0; i < WordDataLength; i++)
117 {
118 ByteArray[ 9 + i * 2] = highByte(R[Start + i]);
119 ByteArray[10 + i * 2] = lowByte(R[Start + i]);
120 }
123 Writes = 1 + Writes * (Writes < 999);
124 FC = MB_FC_NONE;
125 }
126
127 //****************** Write Coil **********************
128 if(FC == MB_FC_WRITE_COIL)
129 {
130 Start = word(ByteArray[8],ByteArray[9]);
131 C[Start] = word(ByteArray[10],ByteArray[11]) > 0;
132 #ifdef MbDebug
133 Serial.print(" MB_FC_WRITE_COIL C");
134 Serial.print(Start);
135 Serial.print("=");
136 Serial.println(C[Start]);
137 #endif
138 ByteArray[5] = 2; //Number of bytes after this one.
139 MessageLength = 8;
141 Writes = 1 + Writes * (Writes < 999);
142 FC = MB_FC_NONE;
143 }
144
145 //****************** Write Register ******************
147 {
148 Start = word(ByteArray[8],ByteArray[9]);
149 R[Start] = word(ByteArray[10],ByteArray[11]);
150 #ifdef MbDebug
151 Serial.print(" MB_FC_WRITE_REGISTER R");
152 Serial.print(Start);
153 Serial.print("=");
154 Serial.println(R[Start]);
155 #endif
156 ByteArray[5] = 6; //Number of bytes after this one.
157 MessageLength = 12;
159 Writes = 1 + Writes * (Writes < 999);
160 FC = MB_FC_NONE;
161 }
162
163
164 //****************** Write Multiple Coils **********************
165 //Function codes 15 & 16 by Martin Pettersson http://siamect.com
167 {
168 Start = word(ByteArray[8],ByteArray[9]);
173 #ifdef MbDebug
174 Serial.print(" MB_FC_WRITE_MULTIPLE_COILS S=");
175 Serial.print(Start);
176 Serial.print(" L=");
177 Serial.println(CoilDataLength);
178 #endif
179 ByteArray[5] = ByteDataLength + 5; //Number of bytes after this one.
180 for(int i = 0; i < ByteDataLength ; i++)
181 {
182 for(int j = 0; j < 8; j++)
183 {
184 C[Start + i * 8 + j] = bitRead( ByteArray[13 + i], j);
185 }
186 }
187 MessageLength = 12;
189 Writes = 1 + Writes * (Writes < 999);
190 FC = MB_FC_NONE;
191 }
192
193
194 //****************** Write Multiple Registers ******************
195 //Function codes 15 & 16 by Martin Pettersson http://siamect.com
197 {
198 Start = word(ByteArray[8],ByteArray[9]);
201 #ifdef MbDebug
202 Serial.print(" MB_FC_READ_REGISTERS S=");
203 Serial.print(Start);
204 Serial.print(" L=");
205 Serial.println(WordDataLength);
206 #endif
207 ByteArray[5] = ByteDataLength + 3; //Number of bytes after this one.
208 for(int i = 0; i < WordDataLength; i++)
209 {
210 R[Start + i] = word(ByteArray[ 13 + i * 2],ByteArray[14 + i * 2]);
211 }
212 MessageLength = 12;
214 Writes = 1 + Writes * (Writes < 999);
215 FC = MB_FC_NONE;
216 }
217
218 #ifdef MbDebug
219 Serial.print("Mb runs: ");
220 Serial.print(Runs);
221 Serial.print(" reads: ");
222 Serial.print(Reads);
223 Serial.print(" writes: ");
224 Serial.print(Writes);
225 Serial.println();
226 #endif
227}
EthernetServer MbServer(MB_PORT)
bool Active
Definition Mudbus.h:44
int FC
Definition Mudbus.h:49
int Reads
Definition Mudbus.h:46
unsigned long PreviousActivityTime
Definition Mudbus.h:45
int Writes
Definition Mudbus.h:46
int R[MB_N_R]
Definition Mudbus.h:42
bool C[MB_N_C]
Definition Mudbus.h:43
void SetFC(int fc)
Definition Mudbus.cpp:230
int Runs
Definition Mudbus.h:46
uint8_t ByteArray[260]
Definition Mudbus.h:48
Definition xtimer.h:21

◆ SetFC()

void Mudbus::SetFC ( int fc)
private

Definition at line 230 of file Mudbus.cpp.

231{
232 if(fc == 1) FC = MB_FC_READ_COILS;
233 if(fc == 3) FC = MB_FC_READ_REGISTERS;
234 if(fc == 5) FC = MB_FC_WRITE_COIL;
235 if(fc == 6) FC = MB_FC_WRITE_REGISTER;
236 if(fc == 15) FC = MB_FC_WRITE_MULTIPLE_COILS;
238}

Member Data Documentation

◆ Active

bool Mudbus::Active

Definition at line 44 of file Mudbus.h.

◆ ByteArray

uint8_t Mudbus::ByteArray[260]
private

Definition at line 48 of file Mudbus.h.

◆ C

bool Mudbus::C[MB_N_C]

Definition at line 43 of file Mudbus.h.

◆ FC

int Mudbus::FC
private

Definition at line 49 of file Mudbus.h.

◆ PreviousActivityTime

unsigned long Mudbus::PreviousActivityTime

Definition at line 45 of file Mudbus.h.

◆ R

int Mudbus::R[MB_N_R]

Definition at line 42 of file Mudbus.h.

◆ Reads

int Mudbus::Reads

Definition at line 46 of file Mudbus.h.

◆ Runs

int Mudbus::Runs

Definition at line 46 of file Mudbus.h.

◆ Writes

int Mudbus::Writes

Definition at line 46 of file Mudbus.h.


The documentation for this class was generated from the following files: