rllib  1
Public Types | Public Member Functions | Public Attributes | Private Attributes | List of all members
rl3964R Class Reference

#include <rl3964r.h>

Collaboration diagram for rl3964R:
Collaboration graph
[legend]

Public Types

enum  priorityEnum { highPriority = 0, lowPriority }
 

Public Member Functions

 rl3964R (int _priority=highPriority)
 
virtual ~rl3964R ()
 
int open (const char *devicename, int _baudrate=B9600)
 
int close ()
 
int setReadCallback (void(*_readCallback)(const unsigned char *buf, int len))
 
int write (const unsigned char *buf, int len)
 
int send ()
 
int receive ()
 
int dprintf (const char *format,...)
 

Public Attributes

rlThread receiver
 
rlSerial tty
 
int state
 
int priority
 
int run
 
int debug
 

Private Attributes

void(* readCallback )(const unsigned char *buf, int len)
 
unsigned char tel_send [512]
 
unsigned char tel_receive [512]
 
int tel_send_length
 
int tel_receive_length
 
int isOpen
 
int send_result
 

Detailed Description

This class implements the siemens 3964R dust protocol.
The read messages must be handled within the callback routine.

Definition at line 27 of file rl3964r.h.

Member Enumeration Documentation

◆ priorityEnum

Enumerator
highPriority 
lowPriority 

Definition at line 30 of file rl3964r.h.

Constructor & Destructor Documentation

◆ rl3964R()

rl3964R::rl3964R ( int  _priority = highPriority)

Definition at line 124 of file rl3964r.cpp.

125 {
126  priority = _priority;
127  readCallback = NULL;
128  debug = 0;
129  isOpen = 0;
130  state = IDLE;
131  send_result = 0;
133 }
Definition: rl3964r.cpp:32
int send_result
Definition: rl3964r.h:59
int isOpen
Definition: rl3964r.h:58
int state
Definition: rl3964r.h:47
int tel_receive_length
Definition: rl3964r.h:57
int priority
Definition: rl3964r.h:48
int debug
Definition: rl3964r.h:50
int tel_send_length
Definition: rl3964r.h:56
void(* readCallback)(const unsigned char *buf, int len)
Definition: rl3964r.h:53

◆ ~rl3964R()

rl3964R::~rl3964R ( )
virtual

Definition at line 135 of file rl3964r.cpp.

136 {
137  close();
138 }
int close()
Definition: rl3964r.cpp:159

Member Function Documentation

◆ close()

int rl3964R::close ( )

Definition at line 159 of file rl3964r.cpp.

160 {
161  if(isOpen == 1)
162  {
163  isOpen = 0;
164  run = 0;
165  dprintf("close(): cancel\n");
166  receiver.cancel();
167  dprintf("close(): closeDevice\n");
168  tty.closeDevice();
169  dprintf("close(): return\n");
170  return 0;
171  }
172  isOpen = 0;
173  return -1;
174 }
int closeDevice()
Definition: rlserial.cpp:763
int cancel()
Definition: rlthread.cpp:78
rlSerial tty
Definition: rl3964r.h:46
rlThread receiver
Definition: rl3964r.h:45
int dprintf(const char *format,...)
Definition: rl3964r.cpp:310
int run
Definition: rl3964r.h:49
int isOpen
Definition: rl3964r.h:58

◆ dprintf()

int rl3964R::dprintf ( const char *  format,
  ... 
)

Definition at line 310 of file rl3964r.cpp.

311 {
312  char message[rl_PRINTF_LENGTH]; // should be big enough
313  int ret;
314 
315  if(debug != 1) return 0;
316  va_list ap;
317  va_start(ap,format);
318  ret = rlvsnprintf(message, rl_PRINTF_LENGTH - 1, format, ap);
319  va_end(ap);
320  printf("%s",message);
321  return ret;
322 }
#define rl_PRINTF_LENGTH
Definition: rldefine.h:71
int rlvsnprintf(char *text, int len, const char *format, va_list ap)
Definition: rlcutil.cpp:197
int debug
Definition: rl3964r.h:50

◆ open()

int rl3964R::open ( const char *  devicename,
int  _baudrate = B9600 
)

Definition at line 140 of file rl3964r.cpp.

141 {
142  int ret;
143 
144  if(isOpen == 0)
145  {
146  //int openDevice(const char *devicename, int speed=B9600, int block=1, int rtscts=1, int bits=8, int stopbits=1, int parity=NONE);
147  ret = tty.openDevice(devicename,_baudrate,1,0,8,1,rlSerial::EVEN);
148  if(ret >= 0)
149  {
150  isOpen = 1;
151  run = 1;
153  }
154  return ret;
155  }
156  return -1;
157 }
rlSerial tty
Definition: rl3964r.h:46
int create(void *(*func)(void *), void *argument)
Definition: rlthread.cpp:35
rlThread receiver
Definition: rl3964r.h:45
int openDevice(const char *devicename, int speed=B9600, int block=1, int rtscts=1, int bits=8, int stopbits=1, int parity=rlSerial::NONE)
Definition: rlserial.cpp:100
int run
Definition: rl3964r.h:49
int isOpen
Definition: rl3964r.h:58
static void * receiverThread(void *arg)
Definition: rl3964r.cpp:41

◆ receive()

int rl3964R::receive ( )

Definition at line 248 of file rl3964r.cpp.

249 {
250  int i,c,c2,bcc,received_bcc,ret;
251 
252  dprintf("receive()\n");
253  //bcc = STX;
254  bcc = 0;
255  i = received_bcc = 0;
256  tel_receive[i++] = c = STX;
257  while(c > 0 && i < (int) sizeof(tel_receive))
258  {
259  ret = tty.select(1000);
260  if(ret == 1) c = tty.readChar();
261  else
262  {
263  dprintf("receive(): partner was sending nothing\n");
264  return -1;
265  }
266  dprintf(" %x\n",c);
267  switch(c)
268  {
269  case -1:
270  case -2:
271  return -1;
272  case DLE:
273  bcc = bcc ^ c;
274  c2 = tty.readChar();
275  dprintf(" %x\n",c2);
276  bcc = bcc ^ c2;
277  if(c2 < 0) return -1;
278  tel_receive[i++] = c2;
279  if(c2 == ETX)
280  {
281  c2 = tty.readChar();
282  dprintf(" %x\n",c2);
283  if(c2 < 0) return -1;
284  tel_receive[i++] = c2; // bcc
285  received_bcc = c2;
286  dprintf(" bcc=%d received_bcc=%d\n",bcc,received_bcc);
287  c = -1;
288  }
289  break;
290  default:
291  bcc = bcc ^ c;
292  tel_receive[i++] = c;
293  break;
294  }
295  }
296  tel_receive_length = i;
297 
298  if(bcc == received_bcc)
299  {
300  tty.writeChar(DLE);
301  dprintf(" success\n");
303  return tel_receive_length-3;
304  }
305  tty.writeChar(NAK);
306  dprintf(" failure\n");
307  return -1;
308 }
#define ETX
Definition: rl3964r.cpp:22
rlSerial tty
Definition: rl3964r.h:46
#define STX
Definition: rl3964r.cpp:21
int writeChar(unsigned char uchar)
Definition: rlserial.cpp:454
#define DLE
Definition: rl3964r.cpp:26
int select(int timeout=500)
Definition: rlserial.cpp:719
int dprintf(const char *format,...)
Definition: rl3964r.cpp:310
int readChar()
Definition: rlserial.cpp:405
#define NAK
Definition: rl3964r.cpp:27
int tel_receive_length
Definition: rl3964r.h:57
unsigned char tel_receive[512]
Definition: rl3964r.h:55
void(* readCallback)(const unsigned char *buf, int len)
Definition: rl3964r.h:53

◆ send()

int rl3964R::send ( )

Definition at line 201 of file rl3964r.cpp.

202 {
203  int i,bcc,c,ret;
204 
205  dprintf("send()");
206  //bcc = STX;
207  bcc = 0;
208  for(i=0; i<tel_send_length; i++)
209  {
210  switch(tel_send[i])
211  {
212  case DLE:
213  tty.writeChar(DLE);
214  tty.writeChar(DLE);
215  bcc = bcc ^ DLE;
216  bcc = bcc ^ DLE;
217  break;
218  default:
219  tty.writeChar(tel_send[i]);
220  bcc = bcc ^ tel_send[i];
221  break;
222  }
223  }
224  tty.writeChar(DLE);
225  bcc = bcc ^ DLE;
226  tty.writeChar(ETX);
227  bcc = bcc ^ ETX;
228  tty.writeChar(bcc);
229  ret = tty.select(1000);
230  if(ret == 1) c = tty.readChar();
231  else
232  {
233  send_result = -1;
234  dprintf("send(): partner was sending nothing\n");
235  return -1;
236  }
237  if(c == DLE)
238  {
239  send_result = 0;
240  dprintf(" success\n");
241  return tel_send_length;
242  }
243  send_result = -1;
244  dprintf(" failure\n");
245  return -1;
246 }
#define ETX
Definition: rl3964r.cpp:22
rlSerial tty
Definition: rl3964r.h:46
int writeChar(unsigned char uchar)
Definition: rlserial.cpp:454
#define DLE
Definition: rl3964r.cpp:26
int select(int timeout=500)
Definition: rlserial.cpp:719
int send_result
Definition: rl3964r.h:59
int dprintf(const char *format,...)
Definition: rl3964r.cpp:310
unsigned char tel_send[512]
Definition: rl3964r.h:54
int readChar()
Definition: rlserial.cpp:405
int tel_send_length
Definition: rl3964r.h:56

◆ setReadCallback()

int rl3964R::setReadCallback ( void(*)(const unsigned char *buf, int len)  _readCallback)

Definition at line 176 of file rl3964r.cpp.

177 {
178  readCallback = _readCallback;
179  return 0;
180 }
void(* readCallback)(const unsigned char *buf, int len)
Definition: rl3964r.h:53

◆ write()

int rl3964R::write ( const unsigned char *  buf,
int  len 
)

Definition at line 182 of file rl3964r.cpp.

183 {
184  dprintf("write() len=%d\n",len);
185  if(len >= (int) sizeof(tel_send)) return -1;
186  receiver.lock();
187  tel_send_length = len;
188  memcpy(tel_send,buf,len);
190  dprintf("write() STX\n");
191  tty.writeChar(STX);
192  dprintf("write() unlock\n");
193  receiver.unlock();
194  dprintf("write() waitSemaphore\n");
196  dprintf("write() return len=%d\n",len);
197  if(send_result < 0) return -1;
198  return len;
199 }
int waitSemaphore()
Definition: rlthread.cpp:57
int unlock()
Definition: rlthread.cpp:52
rlSerial tty
Definition: rl3964r.h:46
#define STX
Definition: rl3964r.cpp:21
rlThread receiver
Definition: rl3964r.h:45
int writeChar(unsigned char uchar)
Definition: rlserial.cpp:454
int send_result
Definition: rl3964r.h:59
int dprintf(const char *format,...)
Definition: rl3964r.cpp:310
unsigned char tel_send[512]
Definition: rl3964r.h:54
int state
Definition: rl3964r.h:47
int tel_send_length
Definition: rl3964r.h:56
int lock()
Definition: rlthread.cpp:47

Member Data Documentation

◆ debug

int rl3964R::debug

Definition at line 50 of file rl3964r.h.

◆ isOpen

int rl3964R::isOpen
private

Definition at line 58 of file rl3964r.h.

◆ priority

int rl3964R::priority

Definition at line 48 of file rl3964r.h.

◆ readCallback

void(* rl3964R::readCallback) (const unsigned char *buf, int len)
private

Definition at line 53 of file rl3964r.h.

◆ receiver

rlThread rl3964R::receiver

Definition at line 45 of file rl3964r.h.

◆ run

int rl3964R::run

Definition at line 49 of file rl3964r.h.

◆ send_result

int rl3964R::send_result
private

Definition at line 59 of file rl3964r.h.

◆ state

int rl3964R::state

Definition at line 47 of file rl3964r.h.

◆ tel_receive

unsigned char rl3964R::tel_receive[512]
private

Definition at line 55 of file rl3964r.h.

◆ tel_receive_length

int rl3964R::tel_receive_length
private

Definition at line 57 of file rl3964r.h.

◆ tel_send

unsigned char rl3964R::tel_send[512]
private

Definition at line 54 of file rl3964r.h.

◆ tel_send_length

int rl3964R::tel_send_length
private

Definition at line 56 of file rl3964r.h.

◆ tty

rlSerial rl3964R::tty

Definition at line 46 of file rl3964r.h.


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