-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerial.cpp
207 lines (146 loc) · 4.08 KB
/
Serial.cpp
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "stdafx.h"
#include "Serial.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#endif
CSerial::CSerial()
{
InitializeCriticalSectionAndSpinCount( &m_cs , 3000 );
}
CSerial::~CSerial()
{
ClosePort();
DeleteCriticalSection( &m_cs );
}
BOOL CSerial::OpenPort( int Port , DWORD dwBaud , BYTE byData , BYTE byStop , BYTE byParity , DWORD& LastError )
{
CSPtr CS( m_cs );
m_osRead.Offset = 0;
m_osRead.OffsetHigh = 0;
m_osWrite.Offset = 0;
m_osWrite.OffsetHigh = 0;
if ( !(m_osRead.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL)) )
{
LastError = GetLastError();
ClosePort();
return FALSE;
}
if (! (m_osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
{
LastError = GetLastError();
ClosePort();
return FALSE;
}
if ( m_hComm != INVALID_HANDLE_VALUE ) ClosePort();
CString PortName = STR( _T("\\\\.\\COM%d") , Port );
m_hComm = CreateFile( PortName , GENERIC_READ | GENERIC_WRITE , 0 , NULL ,
OPEN_EXISTING , FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED , NULL);
if ( m_hComm == INVALID_HANDLE_VALUE )
{
LastError = GetLastError();
ClosePort();
return FALSE;
}
DCB dcb = { 0 , };
dcb.DCBlength = sizeof(DCB);
if( !GetCommState( m_hComm , &dcb ) )
{
LastError = GetLastError();
ClosePort();
return FALSE;
}
dcb.BaudRate = dwBaud;
dcb.ByteSize = byData;
dcb.Parity = byParity;
dcb.StopBits = byStop;
if( !SetCommState( m_hComm , &dcb ) )
{
LastError = GetLastError();
ClosePort();
return FALSE;
}
if( !SetupComm( m_hComm , MAX_BUFFER_SIZE , MAX_BUFFER_SIZE ) ) return FALSE;
COMMTIMEOUTS CommTimeOuts = { 0 , };
CommTimeOuts.ReadIntervalTimeout = 100;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.ReadTotalTimeoutConstant = 1000;
CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
CommTimeOuts.WriteTotalTimeoutConstant = 1000;
if( !SetCommTimeouts( m_hComm , &CommTimeOuts ) )
{
LastError = GetLastError();
ClosePort();
return FALSE;
}
m_ComPort = Port;
LastError = ERROR_SUCCESS;
return TRUE;
}
void CSerial::ClosePort()
{
CSPtr CS( m_cs );
m_ComPort = INVALID_PORT_NUM;
if ( m_hComm != INVALID_HANDLE_VALUE )
{
SetCommMask( m_hComm, 0);
PurgeComm( m_hComm, PURGE_TXABORT | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_RXCLEAR);
CLOSE_FILE( m_hComm );
}
CLOSE_HANDLE( m_osRead.hEvent );
CLOSE_HANDLE( m_osWrite.hEvent );
}
DWORD CSerial::Send( const BYTE* pBuff , DWORD Size )
{
if( m_ComPort == INVALID_PORT_NUM ) return 0;
CSPtr CS( m_cs );
DWORD dwWritten = 0;
if( WriteFile( m_hComm, pBuff, Size, &dwWritten, &m_osWrite) )
{
Log::Write( _T("[Serial] Send : %s") , Str::ByteToHex( pBuff , Size) );
return dwWritten;
}
if ( GetLastError() != ERROR_IO_PENDING ) return 0;
while ( !GetOverlappedResult( m_hComm, &m_osWrite, &dwWritten, TRUE ) )
{
DWORD dw = GetLastError();
Sleep( 10 );
if ( dw != ERROR_IO_INCOMPLETE ) break;
}
COMSTAT comstat = { 0 , };
DWORD dwErrorFlags = 0;
ClearCommError( m_hComm, &dwErrorFlags, &comstat);
return dwWritten;
}
DWORD CSerial::Read(BYTE *pBuff, DWORD nToRead)
{
if( m_ComPort == INVALID_PORT_NUM ) return 0;
CSPtr CS( m_cs );
COMSTAT comstat = { 0 , };
DWORD dwErrorFlags = 0;
if ( !ClearCommError( m_hComm , &dwErrorFlags , &comstat ) ) return 0;
if ( dwErrorFlags != 0 ) return 0;
DWORD dwRead = 0;
if ( ReadFile( m_hComm, pBuff, nToRead, &dwRead, &m_osRead) ) return dwRead;
if ( GetLastError() != ERROR_IO_PENDING ) return 0;
while ( !GetOverlappedResult( m_hComm, &m_osRead, &dwRead, TRUE) )
{
DWORD dw = GetLastError();
Sleep( 10 );
if ( dw != ERROR_IO_INCOMPLETE ) break;
}
ClearCommError( m_hComm, &dwErrorFlags, &comstat);
return dwRead;
}
DWORD CSerial::SendAndRead( const BYTE* pSend , DWORD SendSize , BYTE* ResBuffer , DWORD ResBufferSize , DWORD WaitTick )
{
CSPtr CS( m_cs );
if ( Send( pSend , SendSize ) <= 0 ) return 0;
Sleep( WaitTick );
BYTE Buffer[MAX_BUFFER_SIZE] = { 0 , };
int ReadSize = Read( Buffer , MAX_BUFFER_SIZE );
if ( ReadSize <= 0 ) return 0;
memcpy( ResBuffer , Buffer , ReadSize );
return ReadSize;
}