Hi, I was playing around a bit on my macbook, and made this app so I can get outgauge data to display on it in a fancy way
So I decided to make a simple application first in C and BSD sockets. I can receive the UDP packets fine, and can clearly see the car's name when the app receives a packet, but the rest is messed up.
As you can see I changed the default outgauge struct a bit, because there are no byte and word data types in C or objective-C, so I just put chars there with the right sizes.
Here is the code:
It would be great if someone can help me make this app work. By the way, I know this isn't the preferred style of coding, but it was just something simple.
Check the attachment to see the weird output sometimes. You can see it gets the type of car right, but everything else is just messed up.
So I decided to make a simple application first in C and BSD sockets. I can receive the UDP packets fine, and can clearly see the car's name when the app receives a packet, but the rest is messed up.
As you can see I changed the default outgauge struct a bit, because there are no byte and word data types in C or objective-C, so I just put chars there with the right sizes.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define OG_SHIFTLIGHT 1
#define OG_FULLBEAM 2
#define OG_HANDBRAKE 4
#define OG_PITSPEED 8
#define OG_TC 16
#define OG_HEADLIGHTS 32
#define OG_SIGNAL_L 64
#define OG_SIGNAL_R 128
#define OG_REDLINE 256
#define OG_OILWARN 512
#define OG_1 1024
#define OG_2 2048
#define OG_3 4096
#define OG_4 8192
#define OG_KM 16384
#define OG_BAR 32768
#define MYPORT 55555 // the port users will be connecting to
#define MAXBUFLEN 96
typedef struct {
unsigned Time;
char Car[4];
char Flags[2];
char Gear;
char SpareB;
float Speed;
float RPM;
float Turbo; // BAR
float EngTemp; // C
float Fuel; // 0 to 1
float OilPress; // BAR
float Spare1;
float Spare2;
float Spare3;
float Throttle; // 0 to 1
float Brake; // 0 to 1
float Clutch; // 0 to 1
char Display1[16]; // Usually Fuel
char Display2[16]; // Usually Settings
} OutGauge;
int main(void)
{
int sockfd;
OutGauge new;
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector's address information
socklen_t addr_len;
int numbytes;
memset(&(new),'\0',96); // zero out the struct
if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
addr_len = sizeof(struct sockaddr);
if ((numbytes=recvfrom(sockfd, &new, MAXBUFLEN-1 , 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}
printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr));
printf("packet is %d bytes long\n\n", numbytes);
printf("Time = %u\n", new.Time);
printf("Car = %s\n", &new.Car);
printf("Speed = %f\n", new.Speed);
printf("RPM = %f\n", new.RPM);
close(sockfd);
return 0;
}
It would be great if someone can help me make this app work. By the way, I know this isn't the preferred style of coding, but it was just something simple.
Check the attachment to see the weird output sometimes. You can see it gets the type of car right, but everything else is just messed up.