git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk@3060 ffa7fe5e-494d...
authorGauthier60 <Gauthier60@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Sat, 20 Mar 2010 11:10:09 +0000 (11:10 +0000)
committerGauthier60 <Gauthier60@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Sat, 20 Mar 2010 11:10:09 +0000 (11:10 +0000)
navit/navit/support/win32/serial_io.c [new file with mode: 0644]

diff --git a/navit/navit/support/win32/serial_io.c b/navit/navit/support/win32/serial_io.c
new file mode 100644 (file)
index 0000000..fef6d38
--- /dev/null
@@ -0,0 +1,167 @@
+#include <stdio.h>
+#include <windows.h>
+#include "serial_io.h"
+#include "debug.h"
+
+//***************************************************************************\r
+/** @fn int serial_io_init( const char* port, const char* strsettings )\r
+*****************************************************************************\r
+* @b Description: initialise a serial port communication\r
+*****************************************************************************\r
+* @param      port : port name\r
+*                 example 'COM7'\r
+* @param      strsettings : port settings\r
+*                 example ; 'baud=115200 parity=N data=8 stop=1'\r
+*****************************************************************************\r
+* @return     file descriptor\r
+*             -1 if error\r
+*****************************************************************************\r
+**/\r
+int serial_io_init( const char* port, const char* strsettings )
+{
+    HANDLE hCom = NULL;
+\r
+        char strport[16];
+        snprintf( strport, sizeof( strport ), "\\\\.\\%s", port );
+
+        hCom = CreateFile(
+                        strport,
+                        GENERIC_WRITE | GENERIC_READ,
+                        0,
+                        0,
+                        OPEN_EXISTING,
+                        0,
+                        NULL);
+
+        if (hCom == INVALID_HANDLE_VALUE)
+        {
+                LPVOID lpMsgBuf;
+
+                FormatMessage(
+                        FORMAT_MESSAGE_ALLOCATE_BUFFER |
+                        FORMAT_MESSAGE_FROM_SYSTEM |
+                        FORMAT_MESSAGE_IGNORE_INSERTS,
+                        NULL,
+                        GetLastError(),
+                        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
+                        (LPTSTR) &lpMsgBuf,
+                        0,
+                        NULL
+                );
+                dbg(1, "return (fd) : '-1' : serial_io_init error : '%s'\n", lpMsgBuf);\r
+
+                LocalFree( lpMsgBuf );       // Free the buffer.
+                return -1;
+        }
+
+        DCB dcb;
+
+        ZeroMemory(&dcb, sizeof(DCB));
+
+        GetCommState(hCom, &dcb);
+
+        BuildCommDCB( strsettings, &dcb);
+
+        SetupComm(hCom, 4096, 4096);
+
+        SetCommState(hCom, &dcb);
+
+        COMMTIMEOUTS sCT;
+
+        memset(&sCT, 0, sizeof(sCT));
+        sCT.ReadTotalTimeoutConstant = 10;
+
+        SetCommTimeouts(hCom, &sCT);
+
+        dbg(1, "serial_io_init return (fd) : '%d'\n", (int)hCom);\r
+\r
+   return (int)hCom;
+}
+
+//***************************************************************************\r
+/** @fn int serial_io_read( int fd, char * buffer, int buffer_size )\r
+*****************************************************************************\r
+* @b Description: Read bytes on the serial port\r
+*****************************************************************************\r
+* @param      fd : file descriptor\r
+* @param      buffer : buffer for data\r
+* @param      buffer_size : size in byte of the buffer\r
+*****************************************************************************\r
+* @return     number of bytes read\r
+*****************************************************************************\r
+* @remarks buffer must be allocated by the caller\r
+*****************************************************************************\r
+**/\r
+int serial_io_read( int fd, char * buffer, int buffer_size )
+{
+        dbg(1, "serial_io_read fd = %d buffer_size = %d\n", fd, buffer_size);\r
+\r
+        DWORD dwBytesIn = 0;
+
+        if (fd <= 0)
+        {
+               dbg(0, "serial_io_read return (dwBytesIn) : '0'\n");\r
+               *buffer = 0;
+                return 0;
+        }
+
+        ReadFile( (HANDLE)fd, buffer, buffer_size - 1, &dwBytesIn, NULL);
+
+        if (dwBytesIn >= 0)
+        {
+                buffer[dwBytesIn] = 0;
+        }
+        else\r
+        {
+            dwBytesIn = 0;\r
+                       buffer[0] = 0;
+        }
+        if (dwBytesIn > 0)\r
+        {\r
+            dbg(1,"GPS < %s\n",buffer );\r
+        }\r
+        buffer[buffer_size - 1] = 0;
+
+        dbg(2, "serial_io_read return (dwBytesIn) : '%d'\n", dwBytesIn);\r
+        return dwBytesIn;
+}
+
+//***************************************************************************\r
+/** @fn int serial_io_write(int fd, const char * buffer)\r
+*****************************************************************************\r
+* @b Description: Write bytes on the serial port\r
+*****************************************************************************\r
+* @param      fd : file descriptor\r
+* @param      buffer : data buffer (null terminated)\r
+*****************************************************************************\r
+* @return     number of bytes written\r
+*****************************************************************************\r
+**/\r
+int serial_io_write(int fd, const char * buffer)
+{
+        dbg(1, "serial_io_write fd = %d buffer = '%s'\n",fd, buffer);\r
+\r
+        DWORD dwBytesOut = 0;
+
+        WriteFile((HANDLE)fd, buffer, strlen(buffer), &dwBytesOut, NULL);
+
+        return dwBytesOut;
+}
+
+//***************************************************************************\r
+/** @fn void serial_io_shutdown(int fd )\r
+*****************************************************************************\r
+* @b Description: Terminate serial communication\r
+*****************************************************************************\r
+* @param      fd : file descriptor\r
+*****************************************************************************\r
+**/\r
+void serial_io_shutdown(int fd )
+{
+       dbg(1, "serial_io_shutdown fd = %d\n",fd);\r
+\r
+        if (fd > 0)
+        {
+                CloseHandle((HANDLE)fd);
+        }
+}