-
Notifications
You must be signed in to change notification settings - Fork 1
/
win_com.c
142 lines (122 loc) · 2.62 KB
/
win_com.c
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
#include <windows.h>
#include <stdio.h>
int com_open(int __com_num)
{
char com[32];
COMMTIMEOUTS timeout;;
HANDLE hf;
sprintf(com, "COM%d", __com_num);
hf = CreateFile(com,
GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if(hf == INVALID_HANDLE_VALUE){
return (int)-1;
}
SetupComm(hf,1024 * 10,1024 * 10);
GetCommTimeouts((HANDLE)hf,&timeout);
timeout.ReadIntervalTimeout = MAXDWORD;
timeout.ReadTotalTimeoutMultiplier = MAXDWORD;
timeout.ReadTotalTimeoutConstant = 1000 * 1000;
timeout.WriteTotalTimeoutMultiplier = MAXDWORD;
timeout.WriteTotalTimeoutConstant = 1000 * 1000;
SetCommTimeouts(hf,&timeout);
PurgeComm((HANDLE)hf, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
return (int)hf;
}
int com_set_deadline(int __hf, int __deadline)
{
COMMTIMEOUTS timeout;
int ret;
ret = GetCommTimeouts((HANDLE)__hf,&timeout);
if(!ret){
return -1;
}
timeout.ReadTotalTimeoutConstant = __deadline * 1000;
timeout.WriteTotalTimeoutConstant = __deadline * 1000;
ret = SetCommTimeouts((HANDLE)__hf,&timeout);
if(!ret){
return -1;
}
return 0;
}
int com_set_read_deadline(int __hf, int __deadline)
{
COMMTIMEOUTS timeout;
int ret;
ret = GetCommTimeouts((HANDLE)__hf,&timeout);
if(!ret){
return -1;
}
timeout.ReadTotalTimeoutConstant = __deadline * 1000;
ret = SetCommTimeouts((HANDLE)__hf,&timeout);
if(!ret){
return -1;
}
return 0;
}
int com_set_write_deadline(int __hf, int __deadline)
{
COMMTIMEOUTS timeout;;
int ret;
ret = GetCommTimeouts((HANDLE)__hf,&timeout);
if(!ret){
return -1;
}
timeout.WriteTotalTimeoutConstant = __deadline * 1000;
ret = SetCommTimeouts((HANDLE)__hf,&timeout);
if(!ret){
return -1;
}
return 0;
}
int com_set(int __hf, DCB *__dcb)
{
DCB dcb;
int ret;
ret = GetCommState((HANDLE)__hf,&dcb);
if(!ret){
return -1;
}
dcb.BaudRate = __dcb->BaudRate;
dcb.ByteSize = __dcb->ByteSize;
dcb.Parity = __dcb->Parity;
dcb.StopBits = __dcb->StopBits;
ret = SetCommState((HANDLE)__hf, &dcb);
if(!ret){
return -1;
}
return 0;
}
int com_read(int __hf, void *__b, int __l)
{
BOOL ret;
size_t rl = 0;
int addr = *(int *)__b;
char *p = (char *)addr;
DWORD err;
ret = ReadFile((HANDLE)__hf, p, __l,(PDWORD)&rl,NULL);
if(ret == 0){
return -1;
}
return rl;
}
int com_write(int __hf, const void *__wb, int __wl)
{
size_t wl;
BOOL ret;
int addr = *(int *)__wb;
char *p = (char *)addr;
ret = WriteFile((HANDLE)__hf, p, __wl,(PDWORD)&wl,NULL);
if(ret == 0){
return -1;
}
return wl;
}
int com_close(int __hf)
{
int ret;
ret = CloseHandle((HANDLE)__hf);
if(ret == 0){
return -1;
}
return 0;
}