summaryrefslogtreecommitdiff
path: root/navit/support/win32/serial_io.c
blob: 56966bcf796f8cdd4a07e2da3d2f60fd929ef2b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <windows.h>
#include <glib.h>
#include "serial_io.h"
#include "debug.h"

//***************************************************************************
/** @fn int serial_io_init( const char* port, const char* strsettings )
*****************************************************************************
* @b Description: initialise a serial port communication
*****************************************************************************
* @param      port : port name
*                 example 'COM7'
* @param      strsettings : port settings
*                 example ; 'baud=115200 parity=N data=8 stop=1'
*****************************************************************************
* @return     file descriptor
*             -1 if error
*****************************************************************************
**/
int serial_io_init( const char* port, const char* strsettings )
{
    HANDLE hCom = NULL;
    DCB dcb;
    COMMTIMEOUTS sCT;

        char strport[16];
        g_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(lvl_warning, "return (fd) : '-1' : serial_io_init error : '%s'", lpMsgBuf);

                LocalFree( lpMsgBuf );       // Free the buffer.
                return -1;
        }

        ZeroMemory(&dcb, sizeof(DCB));

        GetCommState(hCom, &dcb);

        BuildCommDCB( strsettings, &dcb);

        SetupComm(hCom, 4096, 4096);

        SetCommState(hCom, &dcb);

        memset(&sCT, 0, sizeof(sCT));
        sCT.ReadTotalTimeoutConstant = 10;

        SetCommTimeouts(hCom, &sCT);

        dbg(lvl_debug, "serial_io_init return (fd) : '%d'", (int)hCom);

   return (int)hCom;
}

//***************************************************************************
/** @fn int serial_io_read( int fd, char * buffer, int buffer_size )
*****************************************************************************
* @b Description: Read bytes on the serial port
*****************************************************************************
* @param      fd : file descriptor
* @param      buffer : buffer for data
* @param      buffer_size : size in byte of the buffer
*****************************************************************************
* @return     number of bytes read
*****************************************************************************
* @remarks buffer must be allocated by the caller
*****************************************************************************
**/
int serial_io_read( int fd, char * buffer, int buffer_size )
{
        DWORD dwBytesIn = 0;
        dbg(lvl_debug, "serial_io_read fd = %d buffer_size = %d", fd, buffer_size);


        if (fd <= 0)
        {
               dbg(lvl_debug, "serial_io_read return (dwBytesIn) : '0'");
               *buffer = 0;
                return 0;
        }

        ReadFile( (HANDLE)fd, buffer, buffer_size - 1, &dwBytesIn, NULL);

        if (dwBytesIn >= 0)
        {
                buffer[dwBytesIn] = 0;
        }
        else
        {
            dwBytesIn = 0;
			buffer[0] = 0;
        }
        if (dwBytesIn > 0)
        {
            dbg(lvl_debug,"GPS < %s",buffer );
        }
        buffer[buffer_size - 1] = 0;

        dbg(lvl_info, "serial_io_read return (dwBytesIn) : '%d'", dwBytesIn);
        return dwBytesIn;
}

//***************************************************************************
/** @fn int serial_io_write(int fd, const char * buffer)
*****************************************************************************
* @b Description: Write bytes on the serial port
*****************************************************************************
* @param      fd : file descriptor
* @param      buffer : data buffer (null terminated)
*****************************************************************************
* @return     number of bytes written
*****************************************************************************
**/
int serial_io_write(int fd, const char * buffer)
{
        DWORD dwBytesOut = 0;
        dbg(lvl_debug, "serial_io_write fd = %d buffer = '%s'",fd, buffer);


        WriteFile((HANDLE)fd, buffer, strlen(buffer), &dwBytesOut, NULL);

        return dwBytesOut;
}

//***************************************************************************
/** @fn void serial_io_shutdown(int fd )
*****************************************************************************
* @b Description: Terminate serial communication
*****************************************************************************
* @param      fd : file descriptor
*****************************************************************************
**/
void serial_io_shutdown(int fd )
{
       dbg(lvl_debug, "serial_io_shutdown fd = %d",fd);

        if (fd > 0)
        {
                CloseHandle((HANDLE)fd);
        }
}