rllib  1
rlmodbus.h
Go to the documentation of this file.
1 /***************************************************************************
2  rlmodbus.h - description
3  -------------------
4  begin : Tue Mar 13 2003
5  copyright : (C) 2003 by R. Lehrig
6  email : lehrig@t-online.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
13  * published by the Free Software Foundation *
14  * *
15  ***************************************************************************/
16 #ifndef _RL_MODBUS_H_
17 #define _RL_MODBUS_H_
18 
19 #include "rldefine.h"
20 #include "rlsocket.h"
21 #include "rlserial.h"
22 
148 class rlModbus
149 {
150  public:
162  typedef union
163  {
164  unsigned char u_char[4]; // 4 * 8bit_data, unsigned
165  char s_char[4]; // 4 * 8bit_data, signed
166  unsigned short u_short[2]; // 2 * 16bit_data, signed
167  short s_short[2]; // 2 * 16bit_data, signed
168  unsigned int u_int; // 1 * 32bit_data, unsigned
169  int s_int; // 1 * 32bit_data, signed
170  float s_float; // 1 * 32bit_data, signed
171  }DATA;
172 
173  enum Modbus
174  {
180  };
181 
183  {
201  };
202 
203  rlModbus(long max_telegram_length = 1024, int mode = MODBUS_RTU, char end_delimitor = 0x0a);
204  virtual ~rlModbus();
205  int write (int slave, int function, const unsigned char *data, int len, int *transactionID = NULL);
206  int request (int slave, int function, int start_adr, int num_register);
207  int response(int *slave, int *function, unsigned char *data, int timeout=1000);
208  int readRequest(int *slave, int *function, unsigned char *data, int timeout=1000, int *transactionID = NULL);
209  void registerSocket(rlSocket *socket);
210  void registerSerial(rlSerial *serial);
211  int data2int(const unsigned char *data);
212  int int2data(int val, unsigned char *data);
213  int intsize();
215  int readCoilStatus (int slave, int start_adr, int number_of_coils, unsigned char *status, int timeout=1000);
216  int readInputStatus (int slave, int start_adr, int number_of_inputs, unsigned char *status, int timeout=1000);
220  int readHoldingRegisters (int slave, int start_adr, int number_of_registers, int *registers, int timeout=1000);
224  int readInputRegisters (int slave, int start_adr, int number_of_registers, int *registers, int timeout=1000);
225  int forceSingleCoil (int slave, int coil_adr, int value, int timeout=1000);
229  int presetSingleRegister (int slave, int register_adr, int value, int timeout=1000);
233  int forceMultipleCoils (int slave, int coil_adr, int number_of_coils, unsigned char *coils, int timeout=1000);
237  int presetMultipleRegisters (int slave, int start_adr, int number_of_registers, int *registers, int timeout=1000);
238 
239  private:
240  int buf2int_rtu(unsigned char *buf);
241  void int2buf_rtu(int i, unsigned char *buf);
242  int buf2int_ascii(unsigned char *buf);
243  void int2buf_ascii(int i, unsigned char *buf);
244  void insertLRC(int len);
245  void insertCRC(int len);
246  int LRCerror(int len);
247  int CRCerror(int len);
250  unsigned char *tel;
251  long maxtel;
252  int mode;
253  char delimitor;
254 };
255 
256 #endif
257 
int LRCerror(int len)
Definition: rlmodbus.cpp:645
unsigned int u_int
Definition: rlmodbus.h:168
void insertCRC(int len)
Definition: rlmodbus.cpp:730
int readInputStatus(int slave, int start_adr, int number_of_inputs, unsigned char *status, int timeout=1000)
Definition: rlmodbus.cpp:791
int mode
Definition: rlmodbus.h:252
void insertLRC(int len)
Definition: rlmodbus.cpp:627
int int2data(int val, unsigned char *data)
Definition: rlmodbus.cpp:42
int readCoilStatus(int slave, int start_adr, int number_of_coils, unsigned char *status, int timeout=1000)
Definition: rlmodbus.cpp:767
void registerSerial(rlSerial *serial)
Definition: rlmodbus.cpp:593
ModbusFunctionCodes
Definition: rlmodbus.h:182
rlSerial * tty
Definition: rlmodbus.h:249
void int2buf_ascii(int i, unsigned char *buf)
Definition: rlmodbus.cpp:622
float s_float
Definition: rlmodbus.h:170
long maxtel
Definition: rlmodbus.h:251
virtual ~rlModbus()
Definition: rlmodbus.cpp:32
int buf2int_ascii(unsigned char *buf)
Definition: rlmodbus.cpp:614
void registerSocket(rlSocket *socket)
Definition: rlmodbus.cpp:587
int autoreconnectSocket
Definition: rlmodbus.h:214
int write(int slave, int function, const unsigned char *data, int len, int *transactionID=NULL)
Definition: rlmodbus.cpp:54
unsigned char * tel
Definition: rlmodbus.h:250
rlSocket * s
Definition: rlmodbus.h:248
int buf2int_rtu(unsigned char *buf)
Definition: rlmodbus.cpp:599
int forceSingleCoil(int slave, int coil_adr, int value, int timeout=1000)
Definition: rlmodbus.cpp:873
int presetMultipleRegisters(int slave, int start_adr, int number_of_registers, int *registers, int timeout=1000)
Definition: rlmodbus.cpp:949
int intsize()
Definition: rlmodbus.cpp:49
rlModbus(long max_telegram_length=1024, int mode=MODBUS_RTU, char end_delimitor=0x0a)
Definition: rlmodbus.cpp:20
int readHoldingRegisters(int slave, int start_adr, int number_of_registers, int *registers, int timeout=1000)
Definition: rlmodbus.cpp:815
int forceMultipleCoils(int slave, int coil_adr, int number_of_coils, unsigned char *coils, int timeout=1000)
Definition: rlmodbus.cpp:922
int presetSingleRegister(int slave, int register_adr, int value, int timeout=1000)
Definition: rlmodbus.cpp:898
void int2buf_rtu(int i, unsigned char *buf)
Definition: rlmodbus.cpp:604
char delimitor
Definition: rlmodbus.h:253
int response(int *slave, int *function, unsigned char *data, int timeout=1000)
Definition: rlmodbus.cpp:132
int data2int(const unsigned char *data)
Definition: rlmodbus.cpp:37
int request(int slave, int function, int start_adr, int num_register)
Definition: rlmodbus.cpp:121
int readInputRegisters(int slave, int start_adr, int number_of_registers, int *registers, int timeout=1000)
Definition: rlmodbus.cpp:844
int readRequest(int *slave, int *function, unsigned char *data, int timeout=1000, int *transactionID=NULL)
Definition: rlmodbus.cpp:352
int CRCerror(int len)
Definition: rlmodbus.cpp:748