rllib  1
rlmodbusclient.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rlmodbusclient.cpp - description
3  -------------------
4  begin : Wed Jan 07 2004
5  copyright : (C) 2004 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 #include "rlmodbusclient.h"
17 
18 rlModbusClient::rlModbusClient(const char *mbxname, const char *shmname, int shmsize)
19  :rlMailbox(mbxname), rlSharedMemory(shmname, shmsize)
20 {
21 }
22 
24 {
25 }
26 
27 int rlModbusClient::write(int slave, int function, const unsigned char *data, int datalen)
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 }
46 
47 int rlModbusClient::writeSingleCoil(int slave, int adr, int value)
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 }
56 
57 int rlModbusClient::writeMultipleCoils(int slave, int adr, const unsigned char *values, int num_coils)
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 }
74 
75 int rlModbusClient::writePresetSingleRegister(int slave, int adr, int value)
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 }
83 
84 int rlModbusClient::writePresetMultipleRegisters(int slave, int adr, const int *values, int num_values)
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 }
99 
100 int rlModbusClient::readBit(int offset, int number)
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 }
141 
142 int rlModbusClient::readByte(int offset, int number)
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 }
152 
153 int rlModbusClient::readShort(int offset, int number)
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)
#define BIT4
Definition: rldefine.h:78
#define BIT3
Definition: rldefine.h:77
int writePresetMultipleRegisters(int slave, int adr, const int *values, int num_values)
int writeMultipleCoils(int slave, int adr, const unsigned char *values, int num_coils)
#define BIT5
Definition: rldefine.h:79
#define BIT6
Definition: rldefine.h:80
int writeSingleCoil(int slave, int adr, int value)
#define BIT7
Definition: rldefine.h:81
virtual ~rlModbusClient()
int readByte(int offset, int number)
#define BIT1
Definition: rldefine.h:75
int readBit(int offset, int number)
#define BIT2
Definition: rldefine.h:76
int write(int slave, int function, const unsigned char *data, int len)
int readShort(int offset, int number)
#define BIT0
Definition: rldefine.h:74
int writePresetSingleRegister(int slave, int adr, int value)
int write(const void *buf, int len)
Definition: rlmailbox.cpp:149
rlModbusClient(const char *mbxname, const char *shmname, int shmsize)