u sure u want the full codes?
it's really really long....
actually im testing the ofstream thing as i'm actually doing motion tracking and i need to capture some measurements into a file.
so i'm juz testing to see whether the file i/o works or not.
this is the main method
#include <fstream.h>
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "birdnet.h"
#include "sockport.h"
#ifdef _WIN32
#include <windows.h>
HANDLE hStdout, hStdin;
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
#else
#endif
#define TRUE 1 /* true value */
#define FALSE 0 /* false value */
#define TCP_PORT 6000 // moitonstar tcp port
#define UDP_PORT 5000 // motionstar udp port
/* table to convert format to data format byte */
/* required in setup */
short rtbl[16] = {0x00,0x31,0x32,0x93,0x64,0xc5,0,0x47,0x78,0,0,0,0,0,-1,-1};
char *formattxt[16] = { "No Data",
"Position",
"Angles",
"Matrix",
"Position/Angle",
"Position/Matrix",
"No Data",
"Quaternion",
"Position/Quaternion",
"No Data",
"No Data",
"No Data",
"No Data",
"No Data",
"No Data",
"No Data"};
extern int errno;
/* server socket address */
struct sockaddr_in client,server,broadcast;
int TCPflag = TRUE; /* TCP or UDP */
int birdVerbose; // message verbosity level
short sequence = 0;
float posScale; /* scaling factor for positon data */
/* = inches returned from birdnet / 32767; */
#define angScale 180./32767.
/* save stack space by making global */
struct BN_PACKET
{
struct BN_HEADER header;
unsigned char data[2048]; /* undifferentiated packet data */
};
struct BN_PACKET response;
char inbuf[80];
int broadcastFlag = FALSE;
int get_status_fbb(int cmdSocket,int addr,struct BN_BIRD_STATUS **bird);
int set_status_fbb(int cmdSocket,int addr,struct BN_BIRD_STATUS *bird);
int run_continuous(int cmdSocket,int dataSocket);
int send_wakeup(int cmdSocket);
int send_shutdown(int cmdSocket);
int single_shot(int cmdSocket,int dataSocket);
int get_status_all(int cmdSocket,struct BN_SYSTEM_STATUS **sys);
int set_status_all(int cmdSocket,struct BN_SYSTEM_STATUS *sys);
int tcpSocket_open(char *ipaddr);
int udpSocket_open(char *ipaddr);
int broadcastSocket_open(char *ipaddr);
int parse_data(struct BN_PACKET *pkt);
int send_command(int cmdSocket,int cmd,int xtype);
int send_status(int cmdSocket,int xtype,char *data,int size);
int get_response(int cmdSocket,int rsp);
void parse_position(char *data);
void parse_angles(char *data);
void parse_matrix(char *data);
void parse_quaternion(char *data);
void parse_serial(char *data, int n);
void print_header(struct BN_HEADER *header);
void print_help(void);
int chkkbhit(void);
void clear_screen(void);
void set_cursorxy(int x, int y);
int main(int argc, char *argv[])
{
int birdFormat = BN_POSANG;
int birdsRequired = BN_MAX_ADDR;
float birdRate = 86.1;
int singleShots = 0;
int reportRate = 1;
int i;
char ip_address[16] = "192.9.200.51";
char broadcast_address[16] = "192.9.200.255";
char buf[10];
int nDevices;
struct BN_SYSTEM_STATUS *sys;
struct BN_BIRD_STATUS *bird;
char *cptr;
int cmdSocket,broadcastSocket;
int receiveOnly = FALSE;
int on = 1;
#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 0 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
/* Tell the user that we couldn't find a usable */
/* WinSock DLL. */
return(-1);
}
/* Confirm that the WinSock DLL supports 2.0.*/
/* Note that if the DLL supports versions greater */
/* than 2.0 in addition to 2.0, it will still return */
/* 2.0 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 0 )
{
/* Tell the user that we couldn't find a usable */
/* WinSock DLL. */
WSACleanup( );
return(-1);
}
/* The WinSock DLL is acceptable. Proceed. */
hStdin = GetStdHandle(STD_INPUT_HANDLE);
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdin == INVALID_HANDLE_VALUE || hStdout == INVALID_HANDLE_VALUE)
printf("GetStdHandle");
/* Save the current text colors. */
if (! GetConsoleScreenBufferInfo(hStdout, &csbiInfo))
printf("GetConsoleScreenBufferInfo");
#else
#endif
if ((cptr = getenv("BN_IPADDR")) != NULL)
strcpy(ip_address,cptr);
if ((cptr = getenv("BN_PROTOCOL")) != NULL)
if (strcmp(cptr,"UDP") == 0)
TCPflag = FALSE;
if ((cptr = getenv("BN_VERBOSE")) != NULL)
birdVerbose = atoi(cptr);
if ((cptr = getenv("BN_RATE")) != NULL)
birdRate = atof(cptr);
if ((cptr = getenv("BN_DATA")) != NULL)
birdFormat = atoi(cptr);
/* process command line switches */
for(i=1; i < argc ; i++)
{
cptr = *++argv;
if (*cptr++ != '-')
{
printf("Bad Command Line Option - %c\n",*--cptr);
print_help();
exit(-1);
}
switch (*cptr++)
{
case 'h':
return(0);
case 'i':
strcpy(ip_address,cptr);
break;
case 'p':
if (strcmp(cptr,"UDP") == 0)
TCPflag = FALSE;
break;
case 'B':
// Broadcast Receive only
receiveOnly = TRUE;
// need to make assumption about scaling
posScale = 144./32767.;
case 'b':
broadcastFlag = TRUE;
TCPflag = FALSE; //if broadcast=>UDP
strcpy(broadcast_address,cptr);
break;
case 'v':
birdVerbose = atoi(cptr);
break;
case 'm':
birdRate = atof(cptr);
break;
case 'r':
reportRate = atoi(cptr);
break;
case 'f':
birdFormat = atoi(cptr);
break;
case 'n':
birdsRequired = atoi(cptr);
break;
case 's':
singleShots = atoi(cptr);
if(singleShots<0) singleShots = 0;
printf("\nSINGLE SHOTS REQUESTED = %d", singleShots);
break;
default:
printf("Bad Command Line Option - %c\n",*--cptr);
print_help();
exit(-1);
break;
}
}
// bounds check user input
if(birdRate>144) birdRate=144;
if(birdRate<20) birdRate=20;
if (reportRate > 255) reportRate = 255;
if (reportRate < 1) reportRate = 1;
if (birdFormat < 0) birdFormat = 0;
if (birdFormat > 15) birdFormat = 15;
// Print out program settings
/*************************************/
/*************My Codes****************/
std::ofstream testFile("example.txt", ios:app);
testFile<<"hello";
testFile.close();
printf("IP ADDRESS of SERVER = %s\n", ip_address);
if (TCPflag)
printf("TCPIP Protocol\n");
else
printf("UDP Protocol\n");
printf("BIRD RATE = %f\n", birdRate);
printf("REPORT RATE = %d\n", reportRate);
printf("BIRD FORMAT = %s\n", formattxt[birdFormat]);
printf("NUMBER OF BIRDS = %d\n\n", birdsRequired);
if (TCPflag)
cmdSocket = tcpSocket_open(ip_address);
else
cmdSocket = udpSocket_open(ip_address);
// cmdSocket = socket_open(ip_address);
if (!cmdSocket)
// if (!socket_open(ip_address,broadcast_address))
{
print_help(); // try to be useful
return(-1);
}
if (broadcastFlag)
{
printf("BROADCAST IP = %s\n",broadcast_address);
broadcastSocket = broadcastSocket_open(broadcast_address);
if (!broadcastSocket)
// if (!socket_open(ip_address,broadcast_address))
{
print_help(); // try to be useful
return(-1);
}
else
{
i = setsockopt(broadcastSocket, SOL_SOCKET, SO_BROADCAST, (void *)&on, sizeof(on));
if(i<0)
{
perror("Broadcast");
exit(-1);
}
}
}
// if receive only then just loop getting and parsing data
if (receiveOnly)
{
while(TRUE)
{
if (!get_response(broadcastSocket,DATA_PACKET))
return(FALSE);
parse_data(&response);
}
}
/* send the wake up call */
if (!send_wakeup(cmdSocket))
return(-1);
/* get the system status */
if (!get_status_all(cmdSocket,&sys))
return(-1);
/* set measurement rate requested */
sprintf(buf,"%06.0f",birdRate * 1000.);
strncpy(sys->rate,buf,6);
printf("Chassis# = %d, \n", sys->chassisID);
printf("Number of devices in this chassis = %d, \n", sys->chassisDevices);
printf("First flock address in this chassis = %d, \n", sys->firstAddress);
printf("SERVER Software Version = %d.%d\n",sys->softRev[0],sys->softRev[1]);
printf("\n\n");
nDevices = sys->chassisDevices;
/* send the system setup */
if (!set_status_all(cmdSocket,sys))
return(-1);
/* for 1 to n flock boards */
/* get the individual bird status */
/* modify the appropriate contents */
/* send the individual bird status packet back */
for(i = 2; i <= nDevices; i++)
{
/* get the status of an individual bird */
get_status_fbb(cmdSocket,i,&bird);
/* change the data format to something new for all birds */
if(i <= birdsRequired+1)
bird->header.dataFormat = rtbl[birdFormat];
else
bird->header.dataFormat = 0; /* no data */
/* set new report rate for all birds */
bird->header.reportRate = reportRate;
bird->header.setup |= BN_FLOCK_APPENDBUT;
posScale = (float)ntohs(bird->header.scaling) / 32767.;
/* set the status of an individual bird */
if (!set_status_fbb(cmdSocket,i,bird))
return(-1);
}
/* get data from birds */
if(singleShots==0)
{
if (broadcastFlag)
run_continuous(cmdSocket,broadcastSocket);
else
run_continuous(cmdSocket,cmdSocket);
}
else
{
for(i = 0; i < singleShots; i++)
if (broadcastFlag)
single_shot(cmdSocket,broadcastSocket);
else
single_shot(cmdSocket,cmdSocket);
}
send_shutdown(cmdSocket);
#ifdef WIN32
WSACleanup( );
#endif
return(0);
}
Last edited by Garth Farley : 20-Feb-2004 at 04:31.
Reason: Added C++ syntax highlighting
|