Hello. I have problem with reading indicators bits. Other values are OK. I am total noob in C++ so i edited some codes from forum. Everything is ok but i still cant read informations from Spare3. std::cout << bits[X] << std::endl; is always 0
#include <stdlib.h>
#include <iostream>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <winsock.h>
#include <bitset>
#define HTONL(n) (((((unsigned long)(n) & 0xFF)) << 24) | \
((((unsigned long)(n) & 0xFF00)) << 8) | \
((((unsigned long)(n) & 0xFF0000)) >> 8) | \
((((unsigned long)(n) & 0xFF000000)) >> 24))
#define NTOHL(n) (((((unsigned long)(n) & 0xFF)) << 24) | \
((((unsigned long)(n) & 0xFF00)) << 8) | \
((((unsigned long)(n) & 0xFF0000)) >> 8) | \
((((unsigned long)(n) & 0xFF000000)) >> 24))
#define MYPORT 55321 // the port users will be connecting to
using namespace std;
typedef struct
{
unsigned int Time; // time in milliseconds (to check order)
char Car [4]; // Car name
unsigned short Flags; // OG_FLAGS (see below)
unsigned char Gear; // Reverse:0, Neutral:1, First:2...
float Speed; // M/S
float SpareB;
float RPM; // RPM
float Turbo; // BAR
float EngTemp; // C
float Fuel; // 0 to 1
float OilPress; // BAR
float Spare1;
unsigned Spare2;
unsigned 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
int ID;
} __attribute__ ((packed)) OutGaugePacket;
float floatSwap (float value)
{
char raw_speed[4];
raw_speed[0]= ((char*)&value)[ 3 ];
raw_speed[1]= ((char*)&value)[ 2 ];
raw_speed[2]= ((char*)&value)[ 1 ];
raw_speed[3]= ((char*)&value)[ 0 ];
float speed = reinterpret_cast<float&>(raw_speed);
return speed;
}
int main(void)
{
int sockfd; // let's make a file descriptor
OutGaugePacket og_packet; // let's create an packet
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector's address information
int addr_len;
int numbytes;
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA data;
memset(&(og_packet),'\0',96); // zero out the struct
cout << "a" << endl;
if (WSAStartup(wVersionRequested, &data) != 0)
{
perror("first");
exit(1);
}
cout << "a" << endl;
//let's create the socket
if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
cout << "a" << endl;
// setting up our contact information here
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
cout << "a" << endl;
//binding our socket to a port and address
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
addr_len = sizeof(struct sockaddr);
for (;;){
// receiving the goodies
if ((numbytes=recvfrom(sockfd, (char *)&og_packet, sizeof(OutGaugePacket) , 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}
cout << "Time" << NTOHL(og_packet.Time) << endl;
cout << "Car" << og_packet.Car << endl;
cout << "Gear" << og_packet.Gear-1 << endl;
cout << "SpareB" << og_packet.SpareB << endl;
cout << "Speed" << floatSwap(og_packet.Speed)*3.6 << endl;
cout << "RPM" << floatSwap(og_packet.RPM) << endl;
cout << "Turbo" << floatSwap(og_packet.Turbo) << endl;
cout << "EngTemp" << floatSwap(og_packet.EngTemp) << endl;
cout << "Fuel" << floatSwap(og_packet.Fuel) << endl;
cout << "OilPress" << floatSwap(og_packet.OilPress) << endl;
cout << "Spare1" << floatSwap(og_packet.Spare1) << endl;
cout << "Spare2" << floatSwap(og_packet.Spare2) << endl;
cout << "Spare3" << floatSwap(og_packet.Spare3) << endl;
std::bitset<sizeof(int)> bits(og_packet.Spare3);
std::cout << bits[0] << std::endl;
std::cout << bits[1] << std::endl;
std::cout << bits[2] << std::endl;
std::cout << bits[3] << std::endl;
std::cout << bits[4] << std::endl;
std::cout << bits[5] << std::endl;
std::cout << bits[6] << std::endl;
std::cout << bits[7] << std::endl;
std::cout << bits[8] << std::endl;
cout << "Throttle" << (floatSwap(og_packet.Throttle) * 100) << endl;
cout << "Brake" << (floatSwap(og_packet.Brake)* 100) << endl;
cout << "Clutch" << (floatSwap(og_packet.Clutch)* 100) << endl;
cout << "Display1" << og_packet.Display1 << endl;
cout << "Display2" << og_packet.Display2 << endl;
cout << "ID" << NTOHL(og_packet.ID) << endl;
}
closesocket(sockfd);
WSACleanup();
return 0;
}