-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRWf.h
More file actions
94 lines (70 loc) · 2.45 KB
/
RWf.h
File metadata and controls
94 lines (70 loc) · 2.45 KB
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
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <math.h>
#define READ_TIMEOUT 500 // milliseconds
/*------------------------------------ Delay Function --------------------------------------------------*/
void delay(int wait_input)
{
int milli_seconds = 1 * wait_input;
clock_t start_time = clock();
while (clock() < start_time + milli_seconds);
}
/*------------------------------------ Read Function --------------------------------------------------*/
char* readcom(HANDLE rhComm, BOOL rStatus, char* dest)
{
DWORD received;
DWORD dwEventMask;
char buffer[4096];
int i = 0;
rStatus = SetCommMask(rhComm, EV_RXCHAR);
if (!rStatus) {
return "Error while applying CommMask\n";
exit(0);
}
WaitCommEvent(rhComm, &dwEventMask, NULL);
while (i < 1024) {
rStatus = ReadFile(rhComm, &buffer[i], 1, &received, NULL);
if (!rStatus) {
return "Error while Reading\n";
exit(0);
}
if (buffer[i] == '\n') {
buffer[i] = 0;
break;
}
++i;
}
buffer[i] = 0;
strcpy(dest, buffer);
return TRUE;
}
/*----------------------------- Writing an Integer to the Serial Port----------------------------------------*/
int writecomint(int input, HANDLE whComm, BOOL wStatus)
{
char lpBuffer[8] = "\0\0\0\0\0\0\0\0"; // lpBuffer should be char or byte array, otherwise write wil fail
DWORD dNoOFBytestoWrite; // No of bytes to write into the port
DWORD dNoOfBytesWritten = 0; // No of bytes written to the port
sprintf(lpBuffer, "%d", input);
dNoOFBytestoWrite = (int)(log10(input) + 1); // Calculating the no of bytes to write into the port
wStatus = WriteFile(whComm, // Handle to the Serialport
lpBuffer, // Data to be written to the port
dNoOFBytestoWrite, // No of bytes to write into the port
&dNoOfBytesWritten, // No of bytes written to the port
NULL);
return 0;
}
/*----------------------------- Writing a String to the Serial Port----------------------------------------*/
int writecomchar(char* inputS, HANDLE whComm, BOOL wStatus)
{
DWORD dNoOFBytestoWrite;
DWORD dNoOfBytesWritten = 0;
dNoOFBytestoWrite = strlen(inputS);
wStatus = WriteFile(whComm,
inputS,
dNoOFBytestoWrite,
&dNoOfBytesWritten,
NULL);
return 0;
}