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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
#include <iostream>
#include <string>
#include <cstdlib>
#include <windows.h>
#include <cmath>
using namespace std;
HANDLE hSerial;
int initSerialPort()
{
int ok = 0;
hSerial = CreateFile("COM5",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if (hSerial == INVALID_HANDLE_VALUE)
{
if(GetLastError() == ERROR_FILE_NOT_FOUND)
{
cout << "COM5 does not exist" << endl;
}
else
{
cout << "Unknown error during opening of COM5\nAllready in use?" << endl;
}
}
else
{
cout << "COM5 opened successfully" << endl;
DCB dcbParameters;
dcbParameters.DCBlength=sizeof(dcbParameters);
if (!GetCommState(hSerial, &dcbParameters))
{
cout << "Error getting serial port state" << endl;
}
else
{
dcbParameters.BaudRate=CBR_9600;
dcbParameters.ByteSize=8;
dcbParameters.StopBits=ONESTOPBIT;
dcbParameters.Parity=EVENPARITY;
if(!SetCommState(hSerial, &dcbParameters))
{
cout << "Error setting config parameters" << endl;
}
else
{
cout << "config parameters configured correctly" << endl;
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = 0;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 0;
if(!SetCommTimeouts(hSerial, &timeouts)){
cout << "Error setting timing configuration" << endl;
}
else
{
cout << "Timing parameters set\nCOM5 ready to be used"<< endl;
ok = 1;
}
}
}
}
return ok;
}
std::string getNewPacket()
{
std::string code;
char recvBuffer[40];
DWORD dwBytesRead;
static unsigned char totalBuffer[2048];
static int nextBytePosition = 0;
while(1)
{
if(!ReadFile(hSerial, recvBuffer, sizeof(recvBuffer), &dwBytesRead, NULL))
{
cout << "Error during reading" << endl;
break;
}
if (dwBytesRead == 0)
{
break;
}
else
{
memcpy(&totalBuffer[nextBytePosition], recvBuffer, dwBytesRead);
nextBytePosition += dwBytesRead;
}
}
int stop = 0;
int state = 0;
for(int i = 0 ; (i < nextBytePosition) && (stop == 0) ; i++)
{
switch(state)
{
case 0:
if(totalBuffer[i] == 0x10)
{
state = 1;
}
break;
case 1:
if(totalBuffer[i] == 0x10)
{
i++;
stop = 1;
for(int j = 0 ; j <= i ; j++)
{
char temp[5];
sprintf(temp, "%.2x", totalBuffer[j]);
code.append(temp);
}
memmove(&totalBuffer[0], &totalBuffer[i + 1], (i + 1));
nextBytePosition -= (i + 1);
}
break;
}
}
return code;
}
int main()
{
cout << "Initialize the serial port" << endl;
if(initSerialPort() == 1)
{
do
{
system ("CLS");
std::string code;
code = getNewPacket();
if(code != "")
{
std::string id_strnormal = "42";
std::string start_str = code.substr(0, 2);
std::string id_str = code.substr(2, 2);
if (start_str != "10")
{
//clear buffer
}
if (id_str == id_strnormal)
{
cout << "New data from GPS:" << code << endl;
}
}
}while (1);
CloseHandle(hSerial);
}
system ("pause");
return 0;
}
|