-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserial.windows.c
114 lines (89 loc) · 2.17 KB
/
serial.windows.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
#include <windows.h>
#define AEM_INTERNAL
#include <aem/log.h>
#include "serial.h"
int aem_serial_open(struct aem_serial *s, const char *device, int baud)
{
aem_logf_ctx(AEM_LOG_NYI, "Untested on Windows");
ser->fd = CreateFile(device,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);
if (ser->fd == INVALID_HANDLE_VALUE) {
aem_logf_ctx(AEM_LOG_ERROR, "Failed to open tty %s", device);
return -1;
}
if (baud >= 0) {
DCB dcb;
FillMemory(&dcb, sizeof(dcb), 0);
dcb.DCBlength = sizeof(dcb);
// fix the baud later
if (!BuildCommDCB("115200,n,8,1", &dcb)) {
aem_logf_ctx(AEM_LOG_ERROR, "BuildCommDCB(def, &dcb) failed");
fprintf(stderr, "BuildCommDCB failed\n");
CloseHandle(ser->fd);
return 1;
}
dcb.BaudRate = baud;
if (!SetCommState(ser->fd, &dcb)) {
fprintf(stderr, "SetCommState failed\n");
CloseHandle(ser->fd);
return 1;
}
}
if (baud >= 0) {
aem_logf_ctx(AEM_LOG_NOTICE, "Opened tty %s baud %d", device, baud);
} else {
aem_logf_ctx(AEM_LOG_NOTICE, "Opened tty %s", device);
}
return 0;
}
int aem_serial_close(struct aem_serial *s)
{
aem_logf_ctx(AEM_LOG_NYI, "Untested on Windows");
if (ser->fd != INVALID_HANDLE_VALUE) {
aem_logf_ctx(AEM_LOG_NOTICE, "close fd");
CloseHandle(ser->fd);
ser->fd = INVALID_HANDLE_VALUE;
} else {
aem_logf_ctx(AEM_LOG_WARN, "Not open");
return 0;
}
}
int aem_serial_ok(struct aem_serial *s)
{
return ser->fd != INVALID_HANDLE_VALUE;
}
size_t aem_serial_write(struct aem_serial *s, struct aem_stringslice out)
{
aem_logf_ctx(AEM_LOG_NYI, "Untested on Windows");
DWORD n;
if (!WriteFile(s->fd, out.start, aem_stringslice_len(&out), &n, NULL)) {
return 0;
}
return n;
}
size_t aem_serial_read(struct aem_serial *s, struct aem_stringbuf *in)
{
aem_logf_ctx(AEM_LOG_NYI, "Untested on Windows");
DWORD n;
if (!ReadFile(s->fd, aem_stringbuf_end(in), aem_stringbuf_available(in), &n, NULL))
{
return 0;
}
in->n += n;
return n;
}
int aem_serial_getc(struct aem_serial *s)
{
aem_logf_ctx(AEM_LOG_NYI, "Untested on Windows");
unsigned char c;
DWORD n;
if (!ReadFile(s->fd, &c, 1, &n, NULL)) {
return -1;
}
return c;
}