-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.cpp
92 lines (80 loc) · 1.87 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
// serial.cpp : 定义 DLL 应用程序的导出函数。
//
#include "stdafx.h"
#include "serial.h"
#include "SerialPort.h"
/*
#include <vector>
using namespace std;
vector<CSerialPort> vPorts;
*/
/*
// 这是导出变量的一个示例
SERIAL_API int nserial=0;
// 这是导出函数的一个示例。
SERIAL_API int fnserial(void)
{
return 42;
}
// 这是已导出类的构造函数。
// 有关类定义的信息,请参阅 serial.h
CSerial::CSerial()
{
return;
}
*/
LPVOID WINAPI OpenComm(
LPCSTR lpszPortNum,
DWORD dwBaudRate,
BYTE byParity,
BYTE byStopBits,
BYTE byByteSize)
{
CSerialPort *pSerial = new CSerialPort();
if (pSerial->Open(lpszPortNum, dwBaudRate, byParity, byStopBits, byByteSize)){
return pSerial;
} else {
delete pSerial;
return INVALID_HANDLE_VALUE;
}
}
void WINAPI CloseComm(LPVOID lpComm)
{
if (lpComm != INVALID_HANDLE_VALUE){
//CSerialPort *pSerial = (CSerialPort*)lpComm;
CSerialPort *pSerial = static_cast<CSerialPort *>(lpComm);
if (NULL != pSerial) {
pSerial->Close();
delete pSerial;
}
}
}
DWORD WINAPI WriteComm(LPVOID lpComm, LPCSTR lpData, DWORD dwLen)
{
if (lpComm != INVALID_HANDLE_VALUE){
//CSerialPort *pSerial = (CSerialPort*)lpComm;
CSerialPort *pSerial = static_cast<CSerialPort *>(lpComm);
DWORD dwResult = pSerial->WriteData(lpData, dwLen);
if (dwResult > 0 && dwResult < (DWORD)-1){
pSerial->Flush();
pSerial->Clear();
}
return dwResult;
}
return (0);
}
DWORD WINAPI ReadComm(LPVOID lpComm, LPSTR lpDest, DWORD dwLen, DWORD dwMaxWait)
{
if (lpComm != INVALID_HANDLE_VALUE){
//CSerialPort *pSerial = (CSerialPort*)lpComm;
char *buffer = new char[dwLen];
CSerialPort *pSerial = static_cast<CSerialPort *>(lpComm);
DWORD dwResult = pSerial->ReadData(buffer, dwLen, dwMaxWait);
if (dwResult > 0 && dwResult <= dwLen){
//strncpy(lpDest, buffer, dwResult);
strncpy_s(lpDest, dwLen, buffer, dwResult);
}
return dwResult;
}
return (0);
}