rllib  1
rlmodbus.h
Go to the documentation of this file.
00001 /***************************************************************************
00002                           rlmodbus.h  -  description
00003                              -------------------
00004     begin                : Tue Mar 13 2003
00005     copyright            : (C) 2003 by Rainer Lehrig
00006     email                : lehrig@t-online.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This library is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as        *
00013  *   published by the Free Software Foundation                             *
00014  *                                                                         *
00015  ***************************************************************************/
00016 #ifndef _RL_MODBUS_H_
00017 #define _RL_MODBUS_H_
00018 
00019 #include "rldefine.h"
00020 #include "rlsocket.h"
00021 #include "rlserial.h"
00022 
00028 class rlModbus
00029 {
00030   public:
00031     enum Modbus
00032     {
00033       MODBUS_CHECKSUM_ERROR = -2,
00034       MODBUS_ERROR          = -1,
00035       MODBUS_SUCCESS        = 0,
00036       MODBUS_RTU            = 1,
00037       MODBUS_ASCII          = 2
00038     };
00039 
00040     enum ModbusFunctionCodes
00041     {
00042       ReadCoilStatus         = 1,
00043       ReadInputStatus        = 2,
00044       ReadHoldingRegisters   = 3,
00045       ReadInputRegisters     = 4,
00046       ForceSingleCoil        = 5,
00047       PresetSingleRegister   = 6,
00048       ReadExceptionStatus    = 7,
00049       FetchCommEventCtr      = 11,
00050       FetchCommEventLog      = 12,
00051       ForceMultipleCoils     = 15,
00052       PresetMultipleRegs     = 16,
00053       ReportSlaveID          = 17,
00054       ReadGeneralReference   = 20,
00055       WriteGeneralReference  = 21,
00056       MaskWrite4XRegisters   = 22,
00057       ReadWrite4XRegisters   = 23,
00058       ReadFifoQueue          = 24
00059     };
00060 
00061     rlModbus(long max_telegram_length = 1024, int mode = MODBUS_RTU, char end_delimitor = 0x0a);
00062     virtual ~rlModbus();
00063     int  write   (int  slave, int  function, const unsigned char *data, int len, int *transactionID = NULL);
00064     int  request (int  slave, int  function, int start_adr, int num_register);
00065     int  response(int *slave, int *function, unsigned char *data, int timeout=1000);
00066     int  readRequest(int *slave, int *function, unsigned char *data, int timeout=1000, int *transactionID = NULL);
00067     void registerSocket(rlSocket *socket);
00068     void registerSerial(rlSerial *serial);
00069     int  data2int(const unsigned char *data);
00070     int  int2data(int val, unsigned char *data);
00071     int  intsize();
00072     int  autoreconnectSocket;
00073 
00074   private:
00075     int  buf2int_rtu(unsigned char *buf);
00076     void int2buf_rtu(int i, unsigned char *buf);
00077     int  buf2int_ascii(unsigned char *buf);
00078     void int2buf_ascii(int i, unsigned char *buf);
00079     void insertLRC(int len);
00080     void insertCRC(int len);
00081     int  LRCerror(int len);
00082     int  CRCerror(int len);
00083     rlSocket *s;
00084     rlSerial *tty;
00085     unsigned char *tel;
00086     long     maxtel;
00087     int      mode;
00088     char     delimitor;
00089 };
00090 
00091 #endif
00092