ソースを参照

feat(serial): add timeout option when opening port

pull/113/head
allefr 3年前
コミット
0d3214b382
2個のファイルの変更35行の追加15行の削除
  1. +34
    -15
      wiringPi/wiringSerial.c
  2. +1
    -0
      wiringPi/wiringSerial.h

+ 34
- 15
wiringPi/wiringSerial.c ファイルの表示

@@ -35,13 +35,17 @@
#include "wiringSerial.h"

/*
* serialOpen:
* serialOpenWithTimeout:
* Open and initialise the serial port, setting all the right
* port parameters - or as many as are required - hopefully!
*
* need to specify timeout in tenth of seconds as that is the minimum resolution
* max 255 (25.5 seconds)
* use timeout = 0 for non-blocking polling
*********************************************************************************
*/

int serialOpen (const char *device, const int baud)
int serialOpenWithTimeout (const char *device, const int baud, int timeout)
{
struct termios options ;
speed_t myBaud ;
@@ -89,24 +93,26 @@ int serialOpen (const char *device, const int baud)

fcntl (fd, F_SETFL, O_RDWR) ;

// Get and modify current options:
// Get and modify current options:

tcgetattr (fd, &options) ;

cfmakeraw (&options) ;
cfsetispeed (&options, myBaud) ;
cfsetospeed (&options, myBaud) ;
cfmakeraw (&options) ;
cfsetispeed (&options, myBaud) ;
cfsetospeed (&options, myBaud) ;

options.c_cflag |= (CLOCAL | CREAD) ;
options.c_cflag &= ~PARENB ;
options.c_cflag &= ~CSTOPB ;
options.c_cflag &= ~CSIZE ;
options.c_cflag |= CS8 ;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
options.c_oflag &= ~OPOST ;
options.c_cflag |= (CLOCAL | CREAD) ;
options.c_cflag &= ~PARENB ;
options.c_cflag &= ~CSTOPB ;
options.c_cflag &= ~CSIZE ;
options.c_cflag |= CS8 ;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
options.c_oflag &= ~OPOST ;

options.c_cc [VMIN] = 0 ;
options.c_cc [VTIME] = 100 ; // Ten seconds (100 deciseconds)
if (timeout < 0) timeout = 0;
if (timeout > 255) timeout = 255;
options.c_cc [VMIN] = 0 ;
options.c_cc [VTIME] = (cc_t)timeout ; // tenth of seconds

tcsetattr (fd, TCSANOW, &options) ;

@@ -124,6 +130,19 @@ int serialOpen (const char *device, const int baud)


/*
* serialOpen:
* Open and initialise the serial port, setting all the right
* port parameters - or as many as are required - hopefully!
* using default timeout of 10 seconds
*********************************************************************************
*/

int serialOpen (const char *device, const int baud) {
return serialOpenWithTimeout(device, baud, 100);
}


/*
* serialFlush:
* Flush the serial buffers (both tx & rx)
*********************************************************************************


+ 1
- 0
wiringPi/wiringSerial.h ファイルの表示

@@ -24,6 +24,7 @@
extern "C" {
#endif

extern int serialOpenWithTimeout (const char *device, const int baud, int timeout) ;
extern int serialOpen (const char *device, const int baud) ;
extern void serialClose (const int fd) ;
extern void serialFlush (const int fd) ;


読み込み中…
キャンセル
保存