OpenCores
URL https://opencores.org/ocsvn/gpib_controller/gpib_controller/trunk

Subversion Repositories gpib_controller

[/] [gpib_controller/] [trunk/] [prototype_1/] [PC_software/] [gpib_src/] [RegAccess_windows.c] - Blame information for rev 3

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 Andrewski
/*
2
 * RegAccess.c
3
 *
4
 *  Created on: 2012-01-28
5
 *      Author: Administrator
6
 */
7
 
8
#include <windows.h>
9
 
10
#include "GpibRegAccess.h"
11
 
12
 
13
bool GpibRegAccess_init(struct GpibRegAccess *ra)
14
{
15
        const char *portName = "COM3";
16
 
17
        ra->isBurstMode = false;
18
 
19
        ra->portHandle = (HandleType)CreateFile(
20
                portName,
21
                GENERIC_READ | GENERIC_WRITE,
22
                0,
23
                NULL,
24
                OPEN_EXISTING,
25
                FILE_ATTRIBUTE_NORMAL,
26
                NULL
27
        );
28
 
29
        if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
30
        {
31
                COMMTIMEOUTS timeouts;
32
 
33
                timeouts.ReadIntervalTimeout = 5000;
34
                timeouts.ReadTotalTimeoutMultiplier = 2;
35
                timeouts.ReadTotalTimeoutConstant = 5000;
36
                timeouts.WriteTotalTimeoutMultiplier = 1;
37
                timeouts.WriteTotalTimeoutConstant= 5000;
38
 
39
                SetCommTimeouts(
40
                        (HANDLE)ra->portHandle,
41
                        &timeouts
42
                );
43
 
44
                DCB commSetting;
45
 
46
                memset(&commSetting, 0, sizeof(DCB));
47
 
48
                commSetting.DCBlength = sizeof(DCB);
49
                commSetting.BaudRate = 921600;
50
                commSetting.fBinary = TRUE;
51
                commSetting.fParity = FALSE;
52
                commSetting.fOutxCtsFlow = FALSE;
53
                commSetting.fOutxDsrFlow = FALSE;
54
                commSetting.fDtrControl = DTR_CONTROL_DISABLE;
55
                commSetting.fDsrSensitivity = FALSE;
56
                commSetting.fTXContinueOnXoff = FALSE;
57
                commSetting.fOutX = FALSE;
58
                commSetting.fInX = FALSE;
59
                commSetting.fErrorChar = FALSE;
60
                commSetting.fNull = FALSE;
61
                commSetting.fRtsControl = RTS_CONTROL_DISABLE;
62
                commSetting.fAbortOnError = TRUE;
63
                commSetting.ByteSize = 8;
64
                commSetting.Parity = NOPARITY;
65
                commSetting.StopBits = ONESTOPBIT;
66
 
67
                SetCommState(
68
                        (HANDLE)ra->portHandle,
69
                        &commSetting
70
                );
71
        }
72
 
73
        return INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle;
74
}
75
 
76
void GpibRegAccess_release(struct GpibRegAccess *ra)
77
{
78
        if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
79
        {
80
                CloseHandle((HANDLE)ra->portHandle);
81
        }
82
}
83
 
84
bool GpibRegAccess_readReg(struct GpibRegAccess *ra, SizeType addr, RegType *value)
85
{
86
        if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
87
        {
88
                unsigned char realAddr = (unsigned char)addr | 0x80;
89
                DWORD bytesWrittenRead;
90
                bool result;
91
 
92
                result = WriteFile(
93
                                (HANDLE)ra->portHandle,
94
                        &realAddr,
95
                        1,
96
                        &bytesWrittenRead,
97
                        NULL
98
                );
99
 
100
                if(!result || bytesWrittenRead != 1)
101
                {
102
                        return false;
103
                }
104
 
105
                result = ReadFile(
106
                                (HANDLE)ra->portHandle,
107
                        value,
108
                        2,
109
                        &bytesWrittenRead,
110
                        NULL
111
                );
112
 
113
                if(!result || bytesWrittenRead != 2)
114
                {
115
                        return false;
116
                }
117
                else
118
                {
119
                        return true;
120
                }
121
        }
122
        else
123
        {
124
                return false;
125
        }
126
}
127
 
128
bool GpibRegAccess_repeatedlyRead(struct GpibRegAccess *ra, SizeType addr,
129
                char *buf, SizeType bufLen)
130
{
131
        if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
132
        {
133
                if(!ra->isBurstMode)
134
                {
135
                        ra->isBurstMode = true;
136
                }
137
 
138
                unsigned char realAddr = (unsigned char)addr | 0xc0;
139
                DWORD bytesWrittenRead;
140
                char addrBuf[3];
141
                bool result;
142
 
143
                addrBuf[0] = realAddr;
144
                addrBuf[1] = bufLen & 0xFF;
145
                addrBuf[2] = (bufLen >> 8) & 0xFF;
146
 
147
                result = WriteFile(
148
                        (HANDLE)ra->portHandle,
149
                        addrBuf,
150
                        3,
151
                        &bytesWrittenRead,
152
                        NULL
153
                );
154
 
155
                if(!result || bytesWrittenRead != 3)
156
                {
157
                        return false;
158
                }
159
 
160
                bytesWrittenRead = 0;
161
 
162
                result = ReadFile(
163
                        (HANDLE)ra->portHandle,
164
                        buf,
165
                        bufLen,
166
                        &bytesWrittenRead,
167
                        NULL
168
                );
169
 
170
                if(!result || bytesWrittenRead != bufLen)
171
                {
172
                        return false;
173
                }
174
 
175
                return true;
176
        }
177
        else
178
        {
179
                return false;
180
        }
181
}
182
 
183
bool GpibRegAccess_writeReg(struct GpibRegAccess *ra, SizeType addr, RegType value)
184
{
185
        if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
186
        {
187
                unsigned char realAddr = (unsigned char)addr & 0x7F;
188
                DWORD writeBuf;
189
                DWORD bytesWrittenRead;
190
                bool result;
191
 
192
                writeBuf = realAddr;
193
                writeBuf |= value << 8;
194
 
195
                result = WriteFile(
196
                                (HANDLE)ra->portHandle,
197
                        &writeBuf,
198
                        3,
199
                        &bytesWrittenRead,
200
                        NULL
201
                );
202
 
203
                if(!result || bytesWrittenRead != 3)
204
                {
205
                        return false;
206
                }
207
                else
208
                {
209
                        return true;
210
                }
211
        }
212
        else
213
        {
214
                return false;
215
        }
216
}
217
 
218
bool GpibRegAccess_repeatedlyWrite(struct GpibRegAccess *ra, SizeType addr,
219
                char *buf, SizeType bufLen)
220
{
221
        if(INVALID_HANDLE_VALUE != (HANDLE)ra->portHandle)
222
        {
223
                unsigned char realAddr = (unsigned char)addr | 0x40;
224
                DWORD bytesWrittenRead;
225
                char addrBuf[3];
226
                bool result;
227
 
228
                addrBuf[0] = realAddr;
229
                addrBuf[1] = bufLen & 0xFF;
230
                addrBuf[2] = (bufLen >> 8) & 0xFF;
231
 
232
                result = WriteFile(
233
                        (HANDLE)ra->portHandle,
234
                        addrBuf,
235
                        3,
236
                        &bytesWrittenRead,
237
                        NULL
238
                );
239
 
240
                if(!result || bytesWrittenRead != 3)
241
                {
242
                        return false;
243
                }
244
 
245
                result = WriteFile(
246
                        (HANDLE)ra->portHandle,
247
                        buf,
248
                        bufLen,
249
                        &bytesWrittenRead,
250
                        NULL
251
                );
252
 
253
                if(!result || bytesWrittenRead != bufLen)
254
                {
255
                        return false;
256
                }
257
 
258
                return true;
259
        }
260
        else
261
        {
262
                return false;
263
        }
264
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.