Skip to content

Commit 399480d

Browse files
authored
Merge pull request #310 from LeeLeahy2/nmea-pc
Update the NMEA client support program
2 parents 3fd79a2 + fb57513 commit 399480d

File tree

1 file changed

+110
-52
lines changed

1 file changed

+110
-52
lines changed

Firmware/Tools/NMEA_Client.c

Lines changed: 110 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,62 +20,120 @@ main (
2020
)
2121
{
2222
int bytesRead;
23-
int nmeaSocket;
24-
struct sockaddr_in serverIpAddress;
23+
int bytesWritten;
24+
int displayHelp;
25+
struct sockaddr_in rtkServerIpAddress;
26+
int rtkSocket;
27+
int status;
28+
struct sockaddr_in vespucciServerIpAddress;
29+
int vespucciSocket;
2530

31+
do {
32+
displayHelp = 1;
33+
status = 0;
34+
if ((argc < 2) || (argc > 3))
35+
{
36+
status = -1;
37+
break;
38+
}
2639

27-
// Display the help text
28-
if (argc != 2)
29-
{
30-
printf ("%s serverIpAddress\n", argv[0]);
31-
return -1;
32-
}
33-
34-
// Initialize the server address
35-
memset (&serverIpAddress, '0', sizeof(serverIpAddress));
36-
serverIpAddress.sin_family = AF_INET;
37-
serverIpAddress.sin_addr.s_addr = htonl(INADDR_ANY);
38-
serverIpAddress.sin_port = htons(1958);
39-
if (inet_pton (AF_INET, argv[1], &serverIpAddress.sin_addr) <= 0)
40-
{
41-
perror ("ERROR - Invalid IPv4 address!\n");
42-
return -2;
43-
}
44-
45-
//Create the socket
46-
nmeaSocket = socket(AF_INET, SOCK_STREAM, 0);
47-
if (nmeaSocket < 0)
48-
{
49-
perror ("ERROR - Unable to create the client socket!\n");
50-
return -3;
51-
}
52-
53-
if (connect (nmeaSocket, (struct sockaddr *)&serverIpAddress, sizeof(serverIpAddress)) < 0)
54-
{
55-
perror("Error : Failed to connect to NMEA server!\n");
56-
return -4;
57-
}
58-
59-
// Read the NMEA data from the client
60-
while ((bytesRead = read (nmeaSocket, rxBuffer, sizeof(rxBuffer)-1)) > 0)
61-
{
62-
// Zero terminate the NMEA string
63-
rxBuffer[bytesRead] = 0;
64-
65-
// Output the NMEA buffer
66-
if (fputs ((char *)rxBuffer, stdout) == EOF)
40+
// Initialize the RTK server address
41+
memset (&rtkServerIpAddress, '0', sizeof(rtkServerIpAddress));
42+
rtkServerIpAddress.sin_family = AF_INET;
43+
rtkServerIpAddress.sin_addr.s_addr = htonl(INADDR_ANY);
44+
rtkServerIpAddress.sin_port = htons(1958);
45+
if (inet_pton (AF_INET, argv[1], &rtkServerIpAddress.sin_addr) <= 0)
46+
{
47+
perror ("ERROR - Invalid RTK server IPv4 address!\n");
48+
status = -2;
49+
break;
50+
}
51+
52+
if (argc == 3)
6753
{
68-
perror ("ERROR - Failed to write to stdout!\n");
69-
return -5;
54+
// Initialize the Vespucci server address
55+
memset (&vespucciServerIpAddress, '0', sizeof(vespucciServerIpAddress));
56+
vespucciServerIpAddress.sin_family = AF_INET;
57+
vespucciServerIpAddress.sin_addr.s_addr = htonl(INADDR_ANY);
58+
vespucciServerIpAddress.sin_port = htons(1958);
59+
if (inet_pton (AF_INET, argv[2], &vespucciServerIpAddress.sin_addr) <= 0)
60+
{
61+
perror ("ERROR - Invalid Vespucci IPv4 address!\n");
62+
status = -3;
63+
break;
64+
}
65+
displayHelp = 0;
66+
67+
//Create the vespucci socket
68+
vespucciSocket = socket(AF_INET, SOCK_STREAM, 0);
69+
if (vespucciSocket < 0)
70+
{
71+
perror ("ERROR - Unable to create the Vespucci client socket!\n");
72+
status = -4;
73+
break;
74+
}
75+
76+
if (connect (vespucciSocket, (struct sockaddr *)&vespucciServerIpAddress, sizeof(vespucciServerIpAddress)) < 0)
77+
{
78+
perror("Error : Failed to connect to Vespucci NMEA server!\n");
79+
status = -5;
80+
break;
81+
}
82+
}
83+
displayHelp = 0;
84+
85+
//Create the RTK socket
86+
rtkSocket = socket(AF_INET, SOCK_STREAM, 0);
87+
if (rtkSocket < 0)
88+
{
89+
perror ("ERROR - Unable to create the RTK client socket!\n");
90+
status = -6;
91+
break;
7092
}
71-
}
7293

73-
if (bytesRead < 0)
74-
{
75-
perror ("ERROR - Failed reading from NMEA server!\n");
76-
return -6;
77-
}
94+
if (connect (rtkSocket, (struct sockaddr *)&rtkServerIpAddress, sizeof(rtkServerIpAddress)) < 0)
95+
{
96+
perror("Error : Failed to connect to RTK NMEA server!\n");
97+
status = -7;
98+
break;
99+
}
100+
101+
// Read the NMEA data from the RTK server
102+
while ((bytesRead = read (rtkSocket, rxBuffer, sizeof(rxBuffer)-1)) > 0)
103+
{
104+
// Zero terminate the NMEA string
105+
rxBuffer[bytesRead] = 0;
106+
107+
// Output the NMEA buffer
108+
if (fputs ((char *)rxBuffer, stdout) == EOF)
109+
{
110+
perror ("ERROR - Failed to write to stdout!\n");
111+
status = -8;
112+
break;
113+
}
114+
115+
// Forward the NMEA data to the Vespucci server
116+
if (argc == 3)
117+
{
118+
if ((bytesWritten = write (vespucciSocket, rxBuffer, bytesRead)) != bytesRead)
119+
{
120+
perror ("ERROR - Failed to write to Vespucci socket");
121+
status = -9;
122+
break;
123+
}
124+
}
125+
}
126+
127+
if (bytesRead <= 0)
128+
{
129+
perror ("ERROR - Failed reading from NMEA server!\n");
130+
status = -10;
131+
}
132+
} while (0);
133+
134+
// Display the help text
135+
if (displayHelp)
136+
printf ("%s RtkServerIpAddress [VespucciServerIpAddress]\n", argv[0]);
78137

79-
// Successful execution
80-
return 0;
138+
return status;
81139
}

0 commit comments

Comments
 (0)