7 //***************************************************************************
8 /** @fn int serial_io_init( const char* port, const char* strsettings )
9 *****************************************************************************
10 * @b Description: initialise a serial port communication
11 *****************************************************************************
12 * @param port : port name
14 * @param strsettings : port settings
15 * example ; 'baud=115200 parity=N data=8 stop=1'
16 *****************************************************************************
17 * @return file descriptor
19 *****************************************************************************
21 int serial_io_init( const char* port, const char* strsettings )
28 g_snprintf( strport, sizeof( strport ), "\\\\.\\%s", port );
32 GENERIC_WRITE | GENERIC_READ,
39 if (hCom == INVALID_HANDLE_VALUE)
44 FORMAT_MESSAGE_ALLOCATE_BUFFER |
45 FORMAT_MESSAGE_FROM_SYSTEM |
46 FORMAT_MESSAGE_IGNORE_INSERTS,
49 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
54 dbg(1, "return (fd) : '-1' : serial_io_init error : '%s'\n", lpMsgBuf);
56 LocalFree( lpMsgBuf ); // Free the buffer.
60 ZeroMemory(&dcb, sizeof(DCB));
62 GetCommState(hCom, &dcb);
64 BuildCommDCB( strsettings, &dcb);
66 SetupComm(hCom, 4096, 4096);
68 SetCommState(hCom, &dcb);
70 memset(&sCT, 0, sizeof(sCT));
71 sCT.ReadTotalTimeoutConstant = 10;
73 SetCommTimeouts(hCom, &sCT);
75 dbg(1, "serial_io_init return (fd) : '%d'\n", (int)hCom);
80 //***************************************************************************
81 /** @fn int serial_io_read( int fd, char * buffer, int buffer_size )
82 *****************************************************************************
83 * @b Description: Read bytes on the serial port
84 *****************************************************************************
85 * @param fd : file descriptor
86 * @param buffer : buffer for data
87 * @param buffer_size : size in byte of the buffer
88 *****************************************************************************
89 * @return number of bytes read
90 *****************************************************************************
91 * @remarks buffer must be allocated by the caller
92 *****************************************************************************
94 int serial_io_read( int fd, char * buffer, int buffer_size )
97 dbg(1, "serial_io_read fd = %d buffer_size = %d\n", fd, buffer_size);
102 dbg(0, "serial_io_read return (dwBytesIn) : '0'\n");
107 ReadFile( (HANDLE)fd, buffer, buffer_size - 1, &dwBytesIn, NULL);
111 buffer[dwBytesIn] = 0;
120 dbg(1,"GPS < %s\n",buffer );
122 buffer[buffer_size - 1] = 0;
124 dbg(2, "serial_io_read return (dwBytesIn) : '%d'\n", dwBytesIn);
128 //***************************************************************************
129 /** @fn int serial_io_write(int fd, const char * buffer)
130 *****************************************************************************
131 * @b Description: Write bytes on the serial port
132 *****************************************************************************
133 * @param fd : file descriptor
134 * @param buffer : data buffer (null terminated)
135 *****************************************************************************
136 * @return number of bytes written
137 *****************************************************************************
139 int serial_io_write(int fd, const char * buffer)
141 DWORD dwBytesOut = 0;
142 dbg(1, "serial_io_write fd = %d buffer = '%s'\n",fd, buffer);
145 WriteFile((HANDLE)fd, buffer, strlen(buffer), &dwBytesOut, NULL);
150 //***************************************************************************
151 /** @fn void serial_io_shutdown(int fd )
152 *****************************************************************************
153 * @b Description: Terminate serial communication
154 *****************************************************************************
155 * @param fd : file descriptor
156 *****************************************************************************
158 void serial_io_shutdown(int fd )
160 dbg(1, "serial_io_shutdown fd = %d\n",fd);
164 CloseHandle((HANDLE)fd);