rllib  1
Public Member Functions | List of all members
rlModbusClient Class Reference

#include <rlmodbusclient.h>

Inheritance diagram for rlModbusClient:
Inheritance graph
[legend]
Collaboration diagram for rlModbusClient:
Collaboration graph
[legend]

Public Member Functions

 rlModbusClient (const char *mbxname, const char *shmname, int shmsize)
 
virtual ~rlModbusClient ()
 
int write (int slave, int function, const unsigned char *data, int len)
 
int writeSingleCoil (int slave, int adr, int value)
 
int writeMultipleCoils (int slave, int adr, const unsigned char *values, int num_coils)
 
int writePresetSingleRegister (int slave, int adr, int value)
 
int writePresetMultipleRegisters (int slave, int adr, const int *values, int num_values)
 
int readBit (int offset, int number)
 
int readByte (int offset, int number)
 
int readShort (int offset, int number)
 
- Public Member Functions inherited from rlMailbox
 rlMailbox (const char *name)
 
virtual ~rlMailbox ()
 
int write (const void *buf, int len)
 
int printf (const char *format,...)
 
int read (void *buf, int maxlen, int wait=WAIT)
 
int setReadBufferSize (int size)
 
const char * read (int wait=WAIT)
 
int write (const char *message)
 
void clear ()
 
- Public Member Functions inherited from rlSharedMemory
 rlSharedMemory (const char *name, unsigned long size, int rwmode=0600)
 
virtual ~rlSharedMemory ()
 
int deleteSharedMemory ()
 
int write (unsigned long offset, const void *buf, int len)
 
int read (unsigned long offset, void *buf, int len)
 
int readInt (unsigned long offset, int index)
 
int readShort (unsigned long offset, int index)
 
int readByte (unsigned long offset, int index)
 
float readFloat (unsigned long offset, int index)
 
int writeInt (unsigned long offset, int index, int val)
 
int writeShort (unsigned long offset, int index, int val)
 
int writeByte (unsigned long offset, int index, unsigned char val)
 
int writeFloat (unsigned long offset, int index, float val)
 
void * getUserAdr ()
 
int shmKey ()
 
int shmId ()
 
unsigned long size ()
 

Additional Inherited Members

- Public Types inherited from rlMailbox
enum  MailboxEnum {
  MAILBOX_ERROR = -1, MAILBOX_FULL = -2, WAIT = 1, NOWAIT = 0,
  MAX_MAILBOX = 256*256, OK = 2, COULD_NOT_CREATE_MAILBOX = 3, COULD_NOT_GET_KEY = 4,
  COULD_NOT_GET_CHAN_ID = 5
}
 
- Public Types inherited from rlSharedMemory
enum  SharedMemoryEnum {
  OK = 0, ERROR_FILE, ERROR_SHMGET, ERROR_SHMAT,
  ERROR_SHMCTL
}
 
- Public Attributes inherited from rlMailbox
int status
 
char * name
 
- Public Attributes inherited from rlSharedMemory
int status
 
char * name
 

Detailed Description

This class is for data aqusition over a modbus daemon as created by pvdevelop.
It communicates over a shared memory and a mailbox according to the pvbrowser principle.

Definition at line 28 of file rlmodbusclient.h.

Constructor & Destructor Documentation

◆ rlModbusClient()

rlModbusClient::rlModbusClient ( const char *  mbxname,
const char *  shmname,
int  shmsize 
)

Definition at line 18 of file rlmodbusclient.cpp.

19  :rlMailbox(mbxname), rlSharedMemory(shmname, shmsize)
20 {
21 }
rlSharedMemory(const char *name, unsigned long size, int rwmode=0600)
rlMailbox(const char *name)
Definition: rlmailbox.cpp:55

◆ ~rlModbusClient()

rlModbusClient::~rlModbusClient ( )
virtual

Definition at line 23 of file rlmodbusclient.cpp.

24 {
25 }

Member Function Documentation

◆ readBit()

int rlModbusClient::readBit ( int  offset,
int  number 
)

Definition at line 100 of file rlmodbusclient.cpp.

101 {
102  int ret;
103  unsigned char buf[128+1],b;
104 
105  if(number < 0) return -1;
106  if(number > 128) return -1;
107  rlSharedMemory::read(offset,buf,(number/8)+1);
108  b = buf[number/8];
109  switch(number%8)
110  {
111  case 0:
112  ret = b & BIT0;
113  break;
114  case 1:
115  ret = b & BIT1;
116  break;
117  case 2:
118  ret = b & BIT2;
119  break;
120  case 3:
121  ret = b & BIT3;
122  break;
123  case 4:
124  ret = b & BIT4;
125  break;
126  case 5:
127  ret = b & BIT5;
128  break;
129  case 6:
130  ret = b & BIT6;
131  break;
132  case 7:
133  ret = b & BIT7;
134  break;
135  default:
136  return -1;
137  }
138  if(ret > 0) return 1;
139  return 0;
140 }
int read(unsigned long offset, void *buf, int len)
#define BIT4
Definition: rldefine.h:78
#define BIT3
Definition: rldefine.h:77
#define BIT5
Definition: rldefine.h:79
#define BIT6
Definition: rldefine.h:80
#define BIT7
Definition: rldefine.h:81
#define BIT1
Definition: rldefine.h:75
#define BIT2
Definition: rldefine.h:76
#define BIT0
Definition: rldefine.h:74

◆ readByte()

int rlModbusClient::readByte ( int  offset,
int  number 
)

Definition at line 142 of file rlmodbusclient.cpp.

143 {
144  unsigned char buf[4],b;
145 
146  if(number < 0) return -1;
147  if(number > 128) return -1;
148  rlSharedMemory::read(offset+number,buf,1);
149  b = buf[0];
150  return b;
151 }
int read(unsigned long offset, void *buf, int len)

◆ readShort()

int rlModbusClient::readShort ( int  offset,
int  number 
)

Definition at line 153 of file rlmodbusclient.cpp.

154 {
155  unsigned char buf[4];
156  int s;
157 
158  if(number < 0) return -1;
159  if(number > 64) return -1;
160  rlSharedMemory::read(offset+(number*2),buf,2);
161  s = buf[0]*256 + buf[1];
162  return s;
163 }
int read(unsigned long offset, void *buf, int len)

◆ write()

int rlModbusClient::write ( int  slave,
int  function,
const unsigned char *  data,
int  len 
)

Definition at line 27 of file rlmodbusclient.cpp.

28 {
29  unsigned char buf[512];
30  int i;
31 
32  if(datalen < 0) return -1;
33  if(datalen > 256) return -1;
34  if(slave < 0) return -1;
35  if(slave > 255) return -1;
36  if(function < 0) return -1;
37  buf[0] = slave;
38  buf[1] = function;
39  for(i=0; i<datalen; i++)
40  {
41  buf[i+2] = data[i];
42  }
43  rlMailbox::write((void *) buf,datalen+2);
44  return 0;
45 }
int write(const void *buf, int len)
Definition: rlmailbox.cpp:149

◆ writeMultipleCoils()

int rlModbusClient::writeMultipleCoils ( int  slave,
int  adr,
const unsigned char *  values,
int  num_coils 
)

Definition at line 57 of file rlmodbusclient.cpp.

58 {
59  unsigned char data[512];
60  int i,byte_count;
61 
62  if(num_coils <= 0) return -1;
63  byte_count = ((num_coils-1)/8) + 1;
64  data[0] = adr/256; data[1] = adr & 0x0ff;
65  data[2] = num_coils/256; data[3] = num_coils & 0x0ff;
66  data[4] = byte_count;
67  for(i=0; i<byte_count; i++)
68  {
69  if((5+i) > (int) sizeof(data)) return -1;
70  data[5+i] = values[i];
71  }
72  return write(slave, rlModbus::ForceMultipleCoils, data, 5+byte_count);
73 }
int write(int slave, int function, const unsigned char *data, int len)

◆ writePresetMultipleRegisters()

int rlModbusClient::writePresetMultipleRegisters ( int  slave,
int  adr,
const int *  values,
int  num_values 
)

Definition at line 84 of file rlmodbusclient.cpp.

85 {
86  unsigned char data[512];
87  int i;
88 
89  data[0] = adr/256; data[1] = adr & 0x0ff;
90  data[2] = num_values/256; data[3] = num_values & 0x0ff;
91  data[4] = num_values*2; // byte count
92  for(i=0; i<num_values; i++)
93  {
94  if((5+i*2) > (int) sizeof(data)) return -1;
95  data[5+i*2] = values[i]/256; data[6+i*2] = values[i] & 0x0ff;
96  }
97  return write(slave, rlModbus::PresetMultipleRegs, data, 5+2*num_values);
98 }
int write(int slave, int function, const unsigned char *data, int len)

◆ writePresetSingleRegister()

int rlModbusClient::writePresetSingleRegister ( int  slave,
int  adr,
int  value 
)

Definition at line 75 of file rlmodbusclient.cpp.

76 {
77  unsigned char data[8];
78 
79  data[0] = adr/256; data[1] = adr & 0x0ff;
80  data[2] = value/256; data[3] = value & 0x0ff;
81  return write(slave, rlModbus::PresetSingleRegister, data, 4);
82 }
int write(int slave, int function, const unsigned char *data, int len)

◆ writeSingleCoil()

int rlModbusClient::writeSingleCoil ( int  slave,
int  adr,
int  value 
)

Definition at line 47 of file rlmodbusclient.cpp.

48 {
49  unsigned char data[8];
50 
51  data[0] = adr/256; data[1] = adr & 0x0ff;
52  data[2] = 0; data[3] = 0;
53  if(value != 0) data[2] = 0x0ff;
54  return write(slave, rlModbus::ForceSingleCoil, data, 4);
55 }
int write(int slave, int function, const unsigned char *data, int len)

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