RS232Device.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 SCHUNK GmbH & Co. KG
3  * Copyright (c) 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include "RS232Device.h"
19 #if defined (_WIN32)
20 #endif
21 #if defined(__LINUX__)
22  #include <fcntl.h>
23  #include <termios.h>
24  #include <unistd.h>
25 #endif
26 #if defined (__QNX__)
27  #include <fcntl.h>
28  #include <termios.h>
29 #endif
30 
31 #define RSID_SENDDAT 0x04
32 #define RSID_RECVDAT 0x08
33 #define STX 0x02
34 #define ETX 0x03
35 #define DLE 0x10
36 
37 // ========================================================================== ;
38 // ;
39 // ---- private auxiliary functions ----------------------------------------- ;
40 // ;
41 // ========================================================================== ;
42 
43 // ========================================================================== ;
44 // ;
45 // ---- protected auxiliary functions --------------------------------------- ;
46 // ;
47 // ========================================================================== ;
48 
49 int CRS232Device::getDeviceError(int iErrorState)
50 {
51  m_iErrorState = 0;
52  return iErrorState;
53 }
54 
56 {
57  m_iErrorState = 0;
58  return m_iErrorState;
59 }
60 
61 int CRS232Device::setMessageId(unsigned long uiMessageId)
62 {
63  m_iErrorState = 0;
64  return m_iErrorState;
65 }
66 
68 {
69  m_iErrorState = 0;
70  return m_iErrorState;
71 }
72 
73 int CRS232Device::reinit(unsigned char ucBaudRateId)
74 {
75  m_iErrorState = 0;
76  return m_iErrorState;
77 }
78 
80 {
81  unsigned char aucMessageBuffer[22] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
82  unsigned char aucReadBuffer[22] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
83  unsigned char ucChar;
84  unsigned char aucDecodeBuffer[11] = {0,0,0,0,0,0,0,0,0,0,0};
85  unsigned char ucMessageLength = 0;
86  unsigned int i = 0;
87  unsigned int k = 0;
88  unsigned int uiMessageBufferIndex = 0;
89  unsigned int uiReadBufferIndex = 0;
90  double fTime = 0;
91  bool bExit = false;
92  bool bMessageComplete = false;
93  bool bDecodeError = false;
94  int iRetVal;
95  m_iErrorState = 0;
96 #if defined(_WIN32)
97  DWORD iErrorCode = 0;
98  DWORD iReadLength = 0;
99  m_clTimer.start();
100 
101  do
102  {
103  iRetVal = ReadFile( m_hDevice, &ucChar, 1, &iReadLength, NULL );
104  if (iRetVal == 0)
105  {
106  void* lpMsgBuf;
107  FormatMessage(
108  FORMAT_MESSAGE_ALLOCATE_BUFFER |
109  FORMAT_MESSAGE_FROM_SYSTEM |
110  FORMAT_MESSAGE_IGNORE_INSERTS,
111  NULL,
112  GetLastError(),
113  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
114  (LPTSTR) &lpMsgBuf,
115  0,
116  NULL
117  );
118  debug(1,"COMERROR:%s\n",lpMsgBuf);
119  warning( "Receive Error. ReadFile failed.\n" );
120  ClearCommError( m_hDevice, &iErrorCode, NULL );
122  return m_iErrorState;
123  }
124  if( iReadLength == 1 )
125  {
126  aucReadBuffer[uiReadBufferIndex++] = ucChar;
127  if( ucChar == ETX )
128  bExit = true;
129  }
130  m_clTimer.stop();
131  fTime = m_clTimer.executionTime() * 1000;
132  if( fTime > m_uiTimeOut + 10 * m_iModuleCount)
133  {
134  bExit = true;
135  warning( "Receive Error. Timeout %f. %d bytes received.\n", fTime, uiReadBufferIndex);
137  return m_iErrorState;
138  }
139  } while( !bExit );
140 
141 #endif
142 
143 #if defined(__QNX__)
144  struct timeval clTimeout;
145  fd_set fdReadSet;
146  clTimeout.tv_sec=0; // seconds to wait
147  clTimeout.tv_usec=m_uiTimeOut * 1000;
148  m_clTimer.start();
149  ssize_t bytesRead = 0;
150  do
151  {
152  FD_ZERO( &fdReadSet );
153  FD_SET( m_hDevice, &fdReadSet );
154 
155  iRetVal=select( FD_SETSIZE, &fdReadSet, NULL, NULL, &clTimeout );
156  if(iRetVal < 0)
157  {
158  warning("Receive Error in select");
159  return ERRID_IO_READERROR;
160  }
161  else if (iRetVal>0 && FD_ISSET(m_hDevice,&fdReadSet))
162  {
163  bytesRead = read( m_hDevice, &ucChar, 1 );
164  if( bytesRead < 0 )
165  { warning( "Receive Error. Read error.\n" );
167  return m_iErrorState;
168  }
169  else if( bytesRead == 1 )
170  {
171  aucReadBuffer[uiReadBufferIndex++] = ucChar;
172  if( ucChar == ETX )
173  bExit = true;
174  }
175  else if( bytesRead == 0 )
176  {
177  warning( "Receive Error. Timeout\n");
179  return m_iErrorState;
180  }
181  }
182  m_clTimer.stop();
183  fTime = m_clTimer.executionTime() * 1000;
184  if( fTime > m_uiTimeOut * m_iModuleCount)
185  { bExit = true;
186  warning( "Receive Error. Timeout. %d bytes received.\n", uiReadBufferIndex );
188  return m_iErrorState;
189  }
190  } while( !bExit );
191 
192 #endif
193 
194 #if defined(__LINUX__)
195  ssize_t bytesRead = 0;
196  struct timeval clTimeout;
197  fd_set fdReadSet;
198  clTimeout.tv_sec=0; // seconds to wait
199  clTimeout.tv_usec=m_uiTimeOut * 1000;
200  m_clTimer.start();
201 
202  do
203  {
204  FD_ZERO( &fdReadSet );
205  FD_SET( m_hDevice, &fdReadSet );
206 
207  iRetVal=select( FD_SETSIZE, &fdReadSet, NULL, NULL, &clTimeout );
208  if(iRetVal < 0)
209  {
210  warning("Receive Error in select");
211  return ERRID_IO_READERROR;
212  }
213  else if (iRetVal>0 && FD_ISSET(m_hDevice,&fdReadSet))
214  {
215  bytesRead = read( m_hDevice, &ucChar, 1 );
216  if( bytesRead < 0 )
217  { warning( "Receive Error. Read error.\n" );
219  return m_iErrorState;
220  }
221  else if( bytesRead == 1 )
222  {
223  aucReadBuffer[uiReadBufferIndex++] = ucChar;
224  if( ucChar == ETX )
225  bExit = true;
226  }
227  else if( bytesRead == 0 )
228  {
229  warning( "Receive Error. Timeout\n");
231  return m_iErrorState;
232  }
233  }
234  m_clTimer.stop();
235  fTime = m_clTimer.executionTime() * 1000;
236  if( fTime > m_uiTimeOut * m_iModuleCount)
237  { bExit = true;
238  warning( "Receive Error. Timeout. %d bytes received.\n", uiReadBufferIndex );
240  return m_iErrorState;
241  }
242  } while( !bExit );
243 
244 #endif
245 
246  for( i = 0; i < uiReadBufferIndex; i++ )
247  {
248  if( aucReadBuffer[i] == STX )
249  {
250  uiMessageBufferIndex = 0;
251  bMessageComplete = false;
252  }
253  if( aucReadBuffer[i] == ETX )
254  bMessageComplete = true;
255  if( uiMessageBufferIndex > 22 )
256  {
257  uiMessageBufferIndex = 0;
258  bMessageComplete = false;
259  warning( "More than 22 bytes!" );
260  }
261  aucMessageBuffer[uiMessageBufferIndex] = aucReadBuffer[i];
262  uiMessageBufferIndex++;
263  }
264 
265  if( bMessageComplete )
266  { // save net m_aucMessageData in aucDecodeBuffer[]
267  if( (aucMessageBuffer[0] == STX) && (aucMessageBuffer[uiMessageBufferIndex-1] == ETX) )
268  { // Message frame begins with STX and ending with ETX
269  for( i = 1; i < uiMessageBufferIndex-1; i++ )
270  {
271  if( aucMessageBuffer[i] == DLE )
272  {
273  i++;
274  aucDecodeBuffer[k] = aucMessageBuffer[i] - 0x80;
275  }
276  else if( aucMessageBuffer[i] == ETX || aucMessageBuffer[i] == STX )
277  {
278  bDecodeError = true;
279  break;
280  }
281  else
282  aucDecodeBuffer[k] = aucMessageBuffer[i];
283  k++;
284  }
285  }
286  else
287  { // Anfang kein STX / Ende kein ETX
288  warning( "Receive Error: STX/ETX framing incorrect.\n" );
290  return m_iErrorState;
291  }
292 
293  // Calculate net Len of Message
294  ucMessageLength = aucDecodeBuffer[1] & 0x0F;
295  if( aucDecodeBuffer[0] & RSID_RECVDAT )
296  { // Message comes from Module
297  if( !bDecodeError )
298  {
299  if( ucMessageLength == k-3 )
300  {
301  rclProtocolMessage.m_ucMessageLength = ucMessageLength;
302  rclProtocolMessage.m_iModuleId = (((aucDecodeBuffer[0]&0x03)<<3)+((aucDecodeBuffer[1]&0xE0)>>5));
303  for( i = 0; i < ucMessageLength; i++ )
304  rclProtocolMessage.m_aucMessageData[i] = aucDecodeBuffer[i+2];
305  rclProtocolMessage.m_uiMessageId = MSGID_ACK + rclProtocolMessage.m_iModuleId;
306  m_iErrorState = 0;
307  return m_iErrorState;
308  }
309  warning( "Receive Error: Length incorrect received %d instead of %d\n", k-3, ucMessageLength );
311  return m_iErrorState;
312  }
313  // Decoder-Fehler: mittendrin STX oder ETX
314  warning( "Receive Error: STX/ETX inside message.\n" );
316  return m_iErrorState;
317  }
318  // Message kommt nicht vom Modul (TelId != TELID_RECVDAT)
319  warning( "Receive Error: MessageId incorrect.\n" );
321  return m_iErrorState;
322  }
323  // Kein ETX empfangen...
324  warning( "Receive Error: ETX not received.\n" );
326  return m_iErrorState;
327 }
328 
330 {
331  int i = 0;
332  unsigned int uiWriteBufferIndex = 0;
333  unsigned long uiWriteLength = 0;
334  unsigned char aucWriteBuffer[24];
335  unsigned short uiSum = 0;
336  unsigned char aucEncodeBuffer[11] = {0,0,0,0,0,0,0,0,0,0,0};
337  int iRetVal;
338  CRS232Message clRS232Message;
339 
340  m_iErrorState = 0;
341 
342  clRS232Message.m_aucMessageId[0] = (unsigned char)rclProtocolMessage.m_iModuleId >> 3;
343  clRS232Message.m_aucMessageId[0] |= RSID_SENDDAT;
344  clRS232Message.m_aucMessageId[1] = (unsigned char)rclProtocolMessage.m_iModuleId << 5;
345  clRS232Message.m_aucMessageId[1] |= (unsigned char)rclProtocolMessage.m_ucMessageLength;
346  clRS232Message.m_ucMessageLength = (unsigned char)rclProtocolMessage.m_ucMessageLength;
347  memcpy(clRS232Message.m_aucMessageData, rclProtocolMessage.m_aucMessageData, rclProtocolMessage.m_ucMessageLength);
348 
349  aucEncodeBuffer[0] = clRS232Message.m_aucMessageId[0];
350  aucEncodeBuffer[1] = clRS232Message.m_aucMessageId[1];
351  for( i = 0; i < clRS232Message.m_ucMessageLength; i++ )
352  aucEncodeBuffer[i+2] = clRS232Message.m_aucMessageData[i];
353  for( i = 0; i < clRS232Message.m_ucMessageLength+2; i++ )
354  uiSum += aucEncodeBuffer[i];
355  aucEncodeBuffer[clRS232Message.m_ucMessageLength+2] = uiSum + (uiSum>>8);
356 
357  aucWriteBuffer[0] = STX;
358  uiWriteBufferIndex = 1;
359 
360  for( i = 0; i < clRS232Message.m_ucMessageLength+3; i++ )
361  {
362  if( aucEncodeBuffer[i] == DLE || aucEncodeBuffer[i] == STX || aucEncodeBuffer[i] == ETX )
363  {
364  aucWriteBuffer[uiWriteBufferIndex] = DLE;
365  uiWriteBufferIndex++;
366  aucWriteBuffer[uiWriteBufferIndex] = aucEncodeBuffer[i] + 0x80;
367  }
368  else
369  aucWriteBuffer[uiWriteBufferIndex] = aucEncodeBuffer[i];
370  uiWriteBufferIndex++;
371  }
372 
373  aucWriteBuffer[uiWriteBufferIndex] = ETX;
374 
375 #if defined(_WIN32)
376  DWORD iErrorCode;
377 
378  PurgeComm( m_hDevice, PURGE_TXABORT );
379  PurgeComm( m_hDevice, PURGE_RXABORT );
380  PurgeComm( m_hDevice, PURGE_TXCLEAR );
381  PurgeComm( m_hDevice, PURGE_RXCLEAR );
382 
383  iRetVal = WriteFile( m_hDevice, aucWriteBuffer, uiWriteBufferIndex+1, &uiWriteLength, NULL );
384  if (iRetVal == 0)
385  {
386  // Transmission error
387  void* lpMsgBuf;
388  FormatMessage(
389  FORMAT_MESSAGE_ALLOCATE_BUFFER |
390  FORMAT_MESSAGE_FROM_SYSTEM |
391  FORMAT_MESSAGE_IGNORE_INSERTS,
392  NULL,
393  GetLastError(),
394  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
395  (LPTSTR) &lpMsgBuf,
396  0,
397  NULL
398  );
399  debug(1,"COMERROR:%s\n",lpMsgBuf);
400  warning( "Transmission Error. Sent %ld bytes instead of &ld: \n", uiWriteLength, uiWriteBufferIndex+1 );
401  ClearCommError( m_hDevice, &iErrorCode, NULL );
403  return m_iErrorState;
404  }
405 
406  if( uiWriteLength != uiWriteBufferIndex+1 )
407  { warning( "Transmission Error. Sent %ld bytes instead of &ld: \n", uiWriteLength, uiWriteBufferIndex+1 );
409  return m_iErrorState;
410  }
411 #endif
412 
413 #if defined(__QNX__)
414  tcflush( m_hDevice, TCIOFLUSH );
415 
416  uiWriteLength = write(m_hDevice, aucWriteBuffer, uiWriteBufferIndex+1);
417  if( uiWriteLength != uiWriteBufferIndex+1 )
418  { warning( "Transmission Error %d. Sent %ld bytes instead of %ld.\n", errno, uiWriteLength, uiWriteBufferIndex+1 );
420  return m_iErrorState;
421  }
422  tcdrain( m_hDevice );
423 #endif
424 
425 #if defined(__LINUX__)
426  tcflush( m_hDevice, TCIOFLUSH );
427 
428  uiWriteLength = write(m_hDevice, aucWriteBuffer, uiWriteBufferIndex+1);
429  if( uiWriteLength != uiWriteBufferIndex+1 )
430  { warning( "Transmission Error. Sent %ld bytes instead of %ld.\n", uiWriteLength, uiWriteBufferIndex+1 );
432  return m_iErrorState;
433  }
434  tcdrain( m_hDevice );
435 #endif
436 
437  return m_iErrorState;
438 }
439 
440 // ========================================================================== ;
441 // ;
442 // ---- constructors / destructor ------------------------------------------- ;
443 // ;
444 // ========================================================================== ;
445 
447 {
448  initMessage("CRS232Device", g_iDebugLevel, g_bDebug, g_bDebugFile);
449 }
450 
452 {
453  error(-1, "Sorry constructor is not implemented");
454 }
455 
457 {
458  exit();
459 }
460 
461 // ========================================================================== ;
462 // ;
463 // ---- operators ----------------------------------------------------------- ;
464 // ;
465 // ========================================================================== ;
466 
468 {
469  error(-1, "Sorry operator= is not implemented");
470  return *this;
471 }
472 
473 // ========================================================================== ;
474 // ;
475 // ---- query functions ----------------------------------------------------- ;
476 // ;
477 // ========================================================================== ;
478 
479 // ========================================================================== ;
480 // ;
481 // ---- modify functions ---------------------------------------------------- ;
482 // ;
483 // ========================================================================== ;
484 
485 void CRS232Device::setTimeOut(unsigned long uiTimeOut)
486 {
487  m_uiTimeOut= uiTimeOut;
488 }
489 
490 // ========================================================================== ;
491 // ;
492 // ---- I/O functions ------------------------------------------------------- ;
493 // ;
494 // ========================================================================== ;
495 
496 // ========================================================================== ;
497 // ;
498 // ---- exec functions ------------------------------------------------------ ;
499 // ;
500 // ========================================================================== ;
501 
503 {
504  return init(m_acInitString);
505 }
506 
507 int CRS232Device::init(const char* acInitString)
508 {
509  InitializeCriticalSection(&m_csDevice);
510  char* pcToken;
511  char acString[128];
512  char acDevice[128];
513  if(m_bInitFlag)
514  {
515  warning("device already initialized");
517  return m_iErrorState;
518  }
519  m_iDeviceId = -1;
520  m_iErrorState = 0;
521  strncpy(m_acInitString,acInitString,128);
522  strncpy(acString,acInitString,128);
523 
524  pcToken = strtok( acString, ":" );
525  if( !pcToken )
527  return m_iErrorState;
528  }
529  if( strcmp( pcToken, "RS232" ) != 0 )
531  return m_iErrorState;
532  }
533 
534  pcToken = strtok( NULL, "," );
535  if( !pcToken )
537  return m_iErrorState;
538  }
539  m_iDeviceId = atoi(pcToken);
540 
541  pcToken = strtok( NULL, "," );
542  if( !pcToken )
544  return m_iErrorState;
545  }
546  m_iBaudRate = atoi(pcToken);
547 
548 #if defined(_WIN32)
549  COMMTIMEOUTS commtimeouts;
550  DCB dcb;
551  sprintf( acDevice, "COM%d", m_iDeviceId );
552  m_hDevice = CreateFile( acDevice, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );
553  if( m_hDevice == INVALID_HANDLE_VALUE )
554  { warning( "Could not initialize %s\n", acDevice );
556  return m_iErrorState;
557  }
558 
559  if( !GetCommState( m_hDevice, &dcb ) )
560  { warning( "GetCommState-Error %d\n", GetLastError() );
562  return m_iErrorState;
563  }
564 
565  switch( m_iBaudRate )
566  {
567  case 1200:
568  dcb.BaudRate = CBR_1200;
569  break;
570  case 2400:
571  dcb.BaudRate = CBR_2400;
572  break;
573  case 4800:
574  dcb.BaudRate = CBR_4800;
575  break;
576  case 9600:
577  dcb.BaudRate = CBR_9600;
578  break;
579  case 19200:
580  dcb.BaudRate = CBR_19200;
581  break;
582  case 38400:
583  dcb.BaudRate = CBR_38400;
584  break;
585  case 57600:
586  dcb.BaudRate = CBR_57600;
587  break;
588  case 115200:
589  dcb.BaudRate = CBR_115200;
590  break;
591  default:
592  dcb.BaudRate = CBR_9600;
593  break;
594  }
595 
596  dcb.ByteSize = 8;
597  dcb.StopBits = ONESTOPBIT;
598  dcb.Parity = NOPARITY;
599  dcb.fRtsControl = RTS_CONTROL_DISABLE; //RTS_CONTROL_TOGGLE; //RTS_CONTROL_HANDSHAKE; //RTS_CONTROL_ENABLE;
600  dcb.fDtrControl = DTR_CONTROL_HANDSHAKE; //DTR_CONTROL_HANDSHAKE; //DTR_CONTROL_ENABLE;
601 
602  if( !SetCommState( m_hDevice, &dcb) )
603  { warning( "SetCommState-Error %d\n", GetLastError() );
605  return m_iErrorState;
606  }
607  commtimeouts.ReadIntervalTimeout = 0;
608  commtimeouts.ReadTotalTimeoutMultiplier = 0;
609  commtimeouts.ReadTotalTimeoutConstant = m_uiTimeOut;
610  commtimeouts.WriteTotalTimeoutMultiplier = 0;
611  commtimeouts.WriteTotalTimeoutConstant = m_uiTimeOut;
612  SetCommTimeouts(m_hDevice,&commtimeouts);
613 
614 #endif
615 
616 #if defined(__QNX__)
617  sprintf(acDevice,"/dev/ser%d",m_iDeviceId);
618  m_hDevice = open(acDevice, O_RDWR|O_NONBLOCK );
619  if( m_hDevice <=0 )
620  { warning( "Could not initialize %s\n", acDevice );
622  return m_iErrorState;
623  }
624 
625  struct termios clPortOptions;
626  tcgetattr(m_hDevice, &clPortOptions); //get current port-options
627 
628  switch( m_iBaudRate )
629  {
630  case 1200:
631  cfsetispeed(&clPortOptions, B1200);
632  cfsetospeed(&clPortOptions, B1200);
633  break;
634  case 2400:
635  cfsetispeed(&clPortOptions, B2400);
636  cfsetospeed(&clPortOptions, B2400);
637  break;
638  case 4800:
639  cfsetispeed(&clPortOptions, B4800);
640  cfsetospeed(&clPortOptions, B4800);
641  break;
642  case 9600:
643  cfsetispeed(&clPortOptions, B9600);
644  cfsetospeed(&clPortOptions, B9600);
645  break;
646  case 19200:
647  cfsetispeed(&clPortOptions, B19200);
648  cfsetospeed(&clPortOptions, B19200);
649  break;
650  case 38400:
651  cfsetispeed(&clPortOptions, B38400);
652  cfsetospeed(&clPortOptions, B38400);
653  break;
654  case 57600:
655  cfsetispeed(&clPortOptions, B57600);
656  cfsetospeed(&clPortOptions, B57600);
657  break;
658  case 115200:
659  cfsetispeed(&clPortOptions, B115200);
660  cfsetospeed(&clPortOptions, B115200);
661  break;
662  default:
663  cfsetispeed(&clPortOptions, B9600);
664  cfsetospeed(&clPortOptions, B9600);
665  break;
666  }
667 
668  clPortOptions.c_cflag |= CREAD|CS8;
669  clPortOptions.c_lflag = 0;
670  if( tcsetattr(m_hDevice, TCSANOW, &clPortOptions) != 0 )
671  { warning( "open: Could not set attributes\n" );
673  return m_iErrorState;
674  }
675 
676 #endif
677 
678 #if defined(__LINUX__)
679  sprintf(acDevice,"/dev/ttyS%d",m_iDeviceId-1);
680  m_hDevice = open( acDevice, O_RDWR ); //open Port
681  if( m_hDevice == -1 )
682  { warning( "open: Could not initialize %s\n", acDevice );
684  return m_iErrorState;
685  }
686 
687  struct termios clPortOptions;
688  tcgetattr(m_hDevice, &clPortOptions); //get current port-options
689 
690  switch( m_iBaudRate )
691  {
692  case 1200:
693  cfsetispeed(&clPortOptions, B1200);
694  cfsetospeed(&clPortOptions, B1200);
695  break;
696  case 2400:
697  cfsetispeed(&clPortOptions, B2400);
698  cfsetospeed(&clPortOptions, B2400);
699  break;
700  case 4800:
701  cfsetispeed(&clPortOptions, B4800);
702  cfsetospeed(&clPortOptions, B4800);
703  break;
704  case 9600:
705  cfsetispeed(&clPortOptions, B9600);
706  cfsetospeed(&clPortOptions, B9600);
707  break;
708  case 19200:
709  cfsetispeed(&clPortOptions, B19200);
710  cfsetospeed(&clPortOptions, B19200);
711  break;
712  case 38400:
713  cfsetispeed(&clPortOptions, B38400);
714  cfsetospeed(&clPortOptions, B38400);
715  break;
716  case 57600:
717  cfsetispeed(&clPortOptions, B57600);
718  cfsetospeed(&clPortOptions, B57600);
719  break;
720  case 115200:
721  cfsetispeed(&clPortOptions, B115200);
722  cfsetospeed(&clPortOptions, B115200);
723  break;
724  default:
725  cfsetispeed(&clPortOptions, B9600);
726  cfsetospeed(&clPortOptions, B9600);
727  break;
728  }
729 
730  clPortOptions.c_iflag = 0;
731  clPortOptions.c_oflag = 0;
732  clPortOptions.c_cflag |= CLOCAL|CREAD|CS8|CSIZE;
733  clPortOptions.c_lflag = 0;
734  if( tcsetattr(m_hDevice, TCSANOW, &clPortOptions) != 0 )
735  { warning( "open: Could not set attributes\n" );
737  return m_iErrorState;
738  }
739 #endif
740 
742  if(m_iErrorState != 0)
743  return m_iErrorState;
744 
745  if(m_iErrorState == 0)
746  m_bInitFlag = true;
748  return m_iErrorState;
749 }
750 
752 {
753  m_iErrorState = 0;
754  if(!m_bInitFlag)
755  {
756  warning("device not initialized");
758  return m_iErrorState;
759  }
760  EnterCriticalSection(&m_csDevice);
761 #if defined(_WIN32)
762  if( !CloseHandle( m_hDevice ) )
763  {
764  warning( "Error closing Device.\n" );
766  return m_iErrorState;
767  }
768 #endif
769 
770 #if defined(__QNX__)
771  if( close(m_hDevice) < 0 )
772  { warning( "Error closing Device.\n" );
774  return m_iErrorState;
775  }
776 #endif
777 
778 #if defined(__LINUX__)
779  if( close(m_hDevice) < 0 )
780  { warning( "Error closing Device.\n" );
782  return m_iErrorState;
783  }
784 #endif
785 
786  m_bInitFlag = false;
787  LeaveCriticalSection(&m_csDevice);
788  DeleteCriticalSection(&m_csDevice);
789  return m_iErrorState;
790 }
#define ERRID_DEV_BADINITSTRING
Definition: m5apiw32.h:209
int setBaudRate()
Definition: RS232Device.cpp:55
int writeDevice(CProtocolMessage &rclProtocolMessage)
unsigned long m_uiTimeOut
Definition: RS232Device.h:63
#define ERRID_IO_READERROR
Definition: IOErrors.h:80
unsigned char m_aucMessageData[8]
#define MSGID_ACK
int updateModuleIdMap()
Definition: Device.cpp:3779
int initMessage(const char *pcClassName, int iDebuglevel=0, bool bDebug=true, bool bDebugFile=false)
Definition: Message.cpp:90
unsigned char m_aucMessageId[2]
CRITICAL_SECTION m_csDevice
void stop()
Definition: StopWatch.cpp:236
int readDevice(CProtocolMessage &rclProtocolMessage)
Definition: RS232Device.cpp:79
#define ERRID_DEV_ISINITIALIZED
Definition: m5apiw32.h:223
void error(const int iErrorCode, const char *pcErrorMessage,...) const
Definition: Message.cpp:204
#define ERRID_DEV_WRITEERROR
Definition: m5apiw32.h:212
CStopWatch m_clTimer
Definition: RS232Device.h:64
int reinit(unsigned char ucBaudRateId)
Definition: RS232Device.cpp:73
#define DLE
Definition: RS232Device.cpp:35
CRS232Device()
default constructor
#define ERRID_DEV_NOTINITIALIZED
Definition: m5apiw32.h:211
#define ERRID_DEV_READTIMEOUT
Definition: m5apiw32.h:215
double executionTime()
Definition: StopWatch.cpp:166
unsigned char m_ucMessageLength
#define ETX
Definition: RS232Device.cpp:34
int g_iDebugLevel
Definition: Message.cpp:20
int getDeviceError(int iErrorState)
Definition: RS232Device.cpp:49
unsigned long m_uiMessageId
int setMessageId(unsigned long uiMessageId)
Definition: RS232Device.cpp:61
void start()
Definition: StopWatch.cpp:211
void setTimeOut(unsigned long uiTimeOut)
void warning(const char *pcWarningMessage,...) const
Definition: Message.cpp:257
bool g_bDebug
Definition: Message.cpp:22
#define ERRID_DEV_INITERROR
Definition: m5apiw32.h:210
#define STX
Definition: RS232Device.cpp:33
bool g_bDebugFile
Definition: Message.cpp:21
bool m_bInitFlag
Definition: Device.h:40
virtual ~CRS232Device()
destructor
#define ERRID_DEV_EXITERROR
Definition: m5apiw32.h:219
int clearReadQueue()
Definition: RS232Device.cpp:67
void debug(const int iDebugLevel, const char *pcDebugMessage,...) const
Definition: Message.cpp:332
unsigned char m_ucMessageLength
int m_iModuleCount
Definition: Device.h:45
CRS232Device & operator=(const CRS232Device &rclRS232Device)
#define RSID_RECVDAT
Definition: RS232Device.cpp:32
#define RSID_SENDDAT
Definition: RS232Device.cpp:31
int m_iErrorState
Definition: Device.h:50
unsigned long m_uiBaudRate
Definition: RS232Device.h:62
#define ERRID_DEV_READERROR
Definition: m5apiw32.h:213
unsigned char m_aucMessageData[8]
char m_acInitString[128]
Definition: Device.h:42
int m_iBaudRate
Definition: Device.h:44


schunk_libm5api
Author(s): Florian Weisshardt
autogenerated on Mon Nov 25 2019 03:48:19