-
Notifications
You must be signed in to change notification settings - Fork 1
/
PCode.c
executable file
·284 lines (267 loc) · 8.5 KB
/
PCode.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "PCode.h"
#include "Globals.h"
#define PCODE_MAXLEN 100
#define PCODE_ONESTRLEN 75
static int PCodeThread = 0;
int InitPCode(PCodeStructPtr PCodePtr)
{
PCodePtr->PCodePhase = 1;
PCodePtr->PCodeDone = FALSE;
PCodePtr->PCodeReqReadSize = 1;
PCodePtr->SerialPort4PCode.reqReadSize = 1;
return 1;
}
void *ReadPCodeDataFromPort(void *arg)
{
PCodeStruct *PCodePtr=NULL;
int serialPortIndx = 0;
PCodePtr = (PCodeStruct*)arg;
while(!PCodePtr->PCodeDone){
if(PCode_AcquireData(PCodePtr)==0)
continue;
// copy data into circle buffer when we get the whole PCode packet
if(PCodePtr->SerialPort4PCode.doneRead)
{
ParsingPCode(PCodePtr);
}
}
printf("End of ReadPCodefromPort() in Pthread.c\n");
return NULL;
}
// Not use this function any more, write data into circular buffer when we get a valid string in aquire routine
void WritePCodeDataIntoBuffer(char *str, PCodeStructPtr PCodePtr)
{
int indx;
char timeStr[25];
struct timeval timev, *timevPtr;
struct timezone timez, *timezPtr;
char ctime = 'T';
timevPtr = &timev;
timezPtr = &timez;
gettimeofday(timevPtr, timezPtr);
itoa(timevPtr->tv_sec,timeStr);
// find the current index
indx = PCodePtr->SerialPort4PCode.Write2BufferIndx%MAX_CIRBUFF;
snprintf(PCodePtr->SerialPort4PCode.SerialDataCirBuff[indx].DataStr,
sizeof(PCodePtr->SerialPort4PCode.SerialDataCirBuff[indx].DataStr),
"%c%s%s",ctime,timeStr,str);
PCodePtr->SerialPort4PCode.Write2BufferIndx++;
PCodePtr->SerialPort4PCode.SerialDataCirBuff[indx].ParseDone = FALSE;
}
//$GPGGA,030542.761,3506.1963,N,12902.3561,E,3,08,0.9,034.8,M,-025.4,M,,*4A\r\n = 75
// time lat lon
//$GPVTG,281.4,T,288.3 ,M,000.0 ,N,000.0,K*40\r\n = 43
// cogT magnetic speed(knots) sog
//$GPZDA,030544.00,14,01,2000,00,00,*4A\r\n = 39
enum PCodeType{GPGGAstr,GPVTGstr,GPZDAstr};
int ParsingPCode(PCodeStructPtr PCodePtr)
{
char *strSep = "\0", *dataSep = ",";
char *brks, *brkd;
char *dataStr, *pcodeData;
char emptyStr[PCODE_MAXLEN] = "\0";
char tempStr[PCODE_MAXLEN] = "\0";
int i=0, j,k;
enum PCodeType pcodestr;
float val, mlat, lat, mlon, lon;
int dlat, dlon;
float temp = 0.0;
int indx;
int val1 = 0;
int rate = 0;
indx = PCodePtr->ParseIndx % MAX_CIRBUFF;
if ((!PCodePtr->SerialPort4PCode.SerialDataCirBuff[indx].ParseDone) && (PCodePtr->ParseIndx < PCodePtr->SerialPort4PCode.Write2BufferIndx))
{
PCodePtr->SerialPort4PCode.SerialDataCirBuff[indx].ParseDone = TRUE;
strcpy(PCodePtr->ParsingStr,PCodePtr->SerialPort4PCode.SerialDataCirBuff[indx].DataStr);
PCodePtr->ParseIndx++;
for (pcodeData = strtok_r(PCodePtr->ParsingStr, dataSep, &brkd); pcodeData;pcodeData=strtok_r(NULL,dataSep,&brkd))
{
if (i==0)
{
if (!strcmp(pcodeData,"$GPGGA"))
{
pcodestr = GPGGAstr;
printf("%s",PCodePtr->SerialPort4PCode.SerialDataCirBuff[indx].DataStr);
}
if (!strcmp(pcodeData,"$GPVTG"))
{
pcodestr = GPVTGstr;
printf("%s",PCodePtr->SerialPort4PCode.SerialDataCirBuff[indx].DataStr);
}
if (!strcmp(pcodeData,"$GPZDA"))
pcodestr = GPZDAstr;
i++;
continue;
}
if (pcodestr==GPGGAstr)
{
switch(i)
{
case 1: // time
PCodePtr->PCodeData.time = atof(pcodeData);
i++;
break;
// the format of PCode:
// the first two digits is degrees and the rest is minutes
// ex: lat: ,3242.395,N, => degrees = 32, minutes = 42.395
// lon: ,11714.1730,W,=> degrees = 117, minutes = 14.173
case 2: //lat
for (j=0;j<2;j++)
tempStr[j] = pcodeData[j];
dlat = atoi(tempStr);
for (j=2,k=0;j<strlen(pcodeData);j++,k++)
tempStr[k] = pcodeData[j];
tempStr[k] = '\0';
mlat = atof(tempStr);
lat = dlat + (mlat/60);
i++;
break;
case 3: //lat direction
if (pcodeData[0] == 'S')
lat *= -1;
PCodePtr->PCodeData.lat = lat;
i++;
break;
case 4: //lon
for (j=0;j<2;j++)
tempStr[j] = pcodeData[j];
dlon = atoi(tempStr);
for (j=2,k=0;j<strlen(pcodeData);j++,k++)
tempStr[k] = pcodeData[j];
tempStr[k] = '\0';
mlon = atof(tempStr);
lon = dlon + (mlon/60);
i++;
break;
case 5: //lon direction
if (pcodeData[0] == 'E')
lon *= -1;
PCodePtr->PCodeData.lon = lon;
i++;
break;
case 6:
PCodePtr->PCodeData.flag = (long)(pcodeData - 0x30);
i++;
break;
}
if (i==7) break; // ignore the rest
}
//$GPVTG,281.4,T,288.3 ,M,000.0 ,N,000.0,K*40\r\n = 43
// cogT magnetic speed(knots) sog
if (pcodestr==GPVTGstr)
{
switch(i)
{
case 1: // cogT
PCodePtr->PCodeData.cogT = atof(pcodeData);
temp = PCodePtr->PCodeData.cogT/180.0*pi;
PCodePtr->PCodeData.cogT_cos = cos(temp);
PCodePtr->PCodeData.cogT_sin = sin(temp);
i += 6; // skip T, magnetic, M, speed, N
break;
case 7: // sog
PCodePtr->PCodeData.sog = atof(pcodeData)*100000.0/3600.0;
i++;
break;
}
if (i>7) break;
}
if (pcodestr==GPZDAstr) // not use this data for now.
{
// printf("GPZDA Data = %s\n",pcodeData);
}
}
}
}
int SetOptionSerialPort4PCode(PCodeStructPtr PCodePtr)
{
cfsetspeed(&PCodePtr->SerialPort4PCode.spOptions, PCodePtr->SerialPort4PCode.speed); // use GG24 replace PCode - MNB 7/27/06
}
// This routine get one byte at a time
Boolean PCode_AcquireData(PCodeStructPtr PCodePtr)
{
ssize_t numBytes = 0; // Number of bytes read or written
int indx;
struct timeval *timevPtr;
struct timezone timez, *timezPtr;
struct tm time_str;
char* buffPtr; // Current char in buffer
char tstr[1024] = "\0";
int local = 1;
char timeStr[125] = "\0";
buffPtr = tstr;
PCodePtr->SerialPort4PCode.reqReadSize = 1;
switch (PCodePtr->PCodePhase)
{
case 1: //looking for a $ sign
// read one by one byte
numBytes = read(PCodePtr->SerialPort4PCode.spd, buffPtr, PCodePtr->SerialPort4PCode.reqReadSize);
PCodePtr->SerialPort4PCode.doneRead = 0;
if (numBytes == -1)
{
printf("Error reading from serial port of PCode at phase 0 - %s(%d).\n",
strerror(errno), errno);
return false;
}
if (numBytes>0) // get some thing
{
if (*buffPtr == '$') // find a sync -> save this char and go to case 1 to get the next char
{
PCodePtr->charsIn = 0;
PCodePtr->PCodePhase = 2;
// save this char in PCode string
PCodePtr->CommingStr[PCodePtr->charsIn] = *buffPtr;
PCodePtr->charsIn++;
}
}
break;
case 2: // read the next bytes after $ sign until get the line feed
numBytes = read(PCodePtr->SerialPort4PCode.spd, buffPtr, PCodePtr->SerialPort4PCode.reqReadSize);
if (numBytes == -1)
{
printf("Error reading from serial port of PCode at phase 1 - %s(%d).\n",
strerror(errno), errno);
return false;
}
if (numBytes > 0)
{
// put this char into PCode string
PCodePtr->CommingStr[PCodePtr->charsIn] = *buffPtr;
PCodePtr->charsIn++;
// set VMIN to 1byte for serial port
if(PCodePtr->charsIn >= PCODE_MAXLEN)
{
PCodePtr->charsIn = 0;
PCodePtr->PCodePhase = 1;
PCodePtr->SerialPort4PCode.doneRead = 0;
}
else
{
// if get the whole packet (the end with the linefeed, store it in cirbuff
if (*buffPtr == 0xa) // if get a linefeed (0xa)
{
PCodePtr->CommingStr[PCodePtr->charsIn-1] = '\n'; // terminate PCode string
PCodePtr->CommingStr[PCodePtr->charsIn] = '\0'; // JMK 16 Apr 05: terminate PCode string with CR/LF
// Write data into the circular buffer
indx = PCodePtr->SerialPort4PCode.Write2BufferIndx%MAX_CIRBUFF;
strncpy(PCodePtr->SerialPort4PCode.SerialDataCirBuff[indx].DataStr,PCodePtr->CommingStr,PCodePtr->charsIn);
// Get the current time, get offset time and calculate the number of seconds since Jan 1, 2005 to now and save it.
timevPtr = &PCodePtr->SerialPort4PCode.SerialDataCirBuff[indx].UnixTime;
timezPtr = &timez;
gettimeofday(timevPtr, timezPtr);
PCodePtr->SerialPort4PCode.SerialDataCirBuff[indx].timeInHundredsecs = Get_Time_In_Hundred_Secs(timeStr, local);
// Increase the write count
PCodePtr->SerialPort4PCode.Write2BufferIndx++;
PCodePtr->SerialPort4PCode.SerialDataCirBuff[indx].ParseDone = FALSE;
PCodePtr->SerialPort4PCode.PackCnt ++; // increase the package PCode count
PCodePtr->charsIn = 0;
PCodePtr->PCodePhase = 1;
PCodePtr->SerialPort4PCode.doneRead = 1;
}
}
}
break;
}
return TRUE;
}