30 baudRateConst = B9600;
33 baudRateConst = B19200;
37 baudRateConst = B38400;
42 baudRateConst = B57600;
47 baudRateConst = B115200;
52 baudRateConst = B230400;
57 baudRateConst = B460800;
62 baudRateConst = B921600;
66 baudRateConst = baudRate;
86 struct termios options;
107 pSerialHandle = (
int*)malloc(
sizeof(
int));
112 (*pSerialHandle) = open(deviceName, O_RDWR | O_NOCTTY | O_NDELAY);
117 if ((*pSerialHandle) != -1)
122 if (fcntl((*pSerialHandle), F_SETFL, O_NONBLOCK) != -1)
127 if (tcgetattr((*pSerialHandle), &options) != -1)
132 options.c_cflag |= (CLOCAL | CREAD);
133 options.c_cflag &= ~(PARENB|CSTOPB|CSIZE);
134 options.c_cflag |= CS8;
135 options.c_cflag &= ~CRTSCTS;
140 options.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
145 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG );
146 options.c_oflag &= ~OPOST;
151 options.c_cc[VMIN] = 0;
152 options.c_cc[VTIME] = 1;
157 if ( (cfsetispeed(&options, baudRateConst) != -1) && (cfsetospeed(&options, baudRateConst) != -1) )
162 if (tcsetattr((*pSerialHandle), TCSANOW, &options) != -1)
167 pHandle->
handle = (
void*)pSerialHandle;
179 fprintf(stderr,
"sbgInterfaceSerialCreate: tcsetattr fails.\n");
184 fprintf(stderr,
"sbgInterfaceSerialCreate: Unable to set speed.\n");
189 fprintf(stderr,
"sbgInterfaceSerialCreate: tcgetattr fails.\n");
194 fprintf(stderr,
"sbgInterfaceSerialCreate: fcntl fails\n");
199 fprintf(stderr,
"sbgInterfaceSerialCreate: Unable to open the com port: %s\n", deviceName);
240 pSerialHandle = (
int*)pHandle->
handle;
245 close((*pSerialHandle));
274 hSerialHandle = *((
int*)pHandle->
handle);
279 if (tcflush(hSerialHandle, TCIOFLUSH) == 0)
303 struct termios options;
314 hSerialHandle = *((
int*)pHandle->
handle);
324 if (tcgetattr(hSerialHandle, &options) != -1)
329 if ( (cfsetispeed(&options, baudRateConst) == -1) || (cfsetospeed(&options, baudRateConst) == -1) )
331 fprintf(stderr,
"sbgInterfaceSerialChangeBaudrate: Unable to set speed.\n");
338 if (tcsetattr(hSerialHandle, TCSADRAIN, &options) != -1)
344 fprintf(stderr,
"sbgInterfaceSerialChangeBaudrate: tcsetattr fails.\n");
350 fprintf(stderr,
"sbgInterfaceSerialChangeBaudrate: tcgetattr fails.\n");
373 size_t numBytesLeftToWrite = bytesToWrite;
375 ssize_t numBytesWritten;
381 if ( (pHandle) && (pBuffer) )
386 hSerialHandle = *((
int*)pHandle->
handle);
391 while (numBytesLeftToWrite > 0)
396 numBytesWritten = write(hSerialHandle, pCurrentBuffer, numBytesLeftToWrite);
401 if (numBytesWritten == -1)
406 fprintf(stderr,
"sbgDeviceWrite: Unable to write to our device: %s\n", strerror(errno));
413 numBytesLeftToWrite -= (size_t)numBytesWritten;
414 pCurrentBuffer += (size_t)numBytesWritten;
437 ssize_t numBytesRead;
442 if ( (pHandle) && (pBuffer) && (pReadBytes) )
447 hSerialHandle = *((
int*)pHandle->
handle);
452 numBytesRead = read(hSerialHandle, pBuffer, bytesToRead);
457 if (numBytesRead > 0)
472 *pReadBytes = (size_t)numBytesRead;
SbgErrorCode sbgInterfaceSerialChangeBaudrate(SbgInterface *pHandle, uint32 baudRate)
SbgErrorCode sbgInterfaceSerialFlush(SbgInterface *pHandle)
SbgErrorCode sbgInterfaceSerialDestroy(SbgInterface *pHandle)
uint32 sbgInterfaceSerialGetBaudRateConst(uint32 baudRate)
SbgInterfaceHandle handle
SbgErrorCode sbgInterfaceSerialRead(SbgInterface *pHandle, void *pBuffer, size_t *pReadBytes, size_t bytesToRead)
SbgInterfaceReadFunc pReadFunc
This file implements a serial interface.
SbgErrorCode sbgInterfaceSerialWrite(SbgInterface *pHandle, const void *pBuffer, size_t bytesToWrite)
SbgErrorCode sbgInterfaceSerialCreate(SbgInterface *pHandle, const char *deviceName, uint32 baudRate)
SbgInterfaceWriteFunc pWriteFunc
enum _SbgErrorCode SbgErrorCode