int i3dmgx3_openPort(int portNum, int baudrate, int size, int parity, int stopbits, int inputBuff, int outputBuff) { int errcheck; int porth; errcheck = openPort(portNum, inputBuff, outputBuff); if (errcheck<0) { return errcheck; } porth = errcheck; /* no error, so this is the port number. */ /* set communications parameters */ errcheck = setCommParameters(porth, baudrate, size, parity, stopbits); if (errcheck!=I3DMGX3_COMM_OK) { return errcheck; } /* set timeouts */ errcheck = setCommTimeouts(porth, 50, 50); /* Read Write */ if (errcheck!=I3DMGX3_COMM_OK) { return errcheck; } return porth; }
/*--------------------------------------------------------------------*/ int main(int argc, char **argv) { s32 zvert=0; BOOL endloopy = FALSE; s16 portNum; s16 deviceNum = 0; s16 i; s16 Ccount=0; u16 value=0; s16 id_flag = 0; s16 errorCode; s16 tryPortNum = 1; unsigned char Record[79]; //record returned from device read where max size is 79 C2Accel_AngRecord Accel_AngRecord; printf("\n 3DM-GX3 Read Acceleration and Angular Rate\n"); /*-------- If user specifies a port, then use it */ if (argc > 1) { tryPortNum = atoi(argv[1]); if (tryPortNum < 2 || tryPortNum > 256) { printf(" usage: i3dmgx3 <portNumber>\n"); printf(" valid ports are 2..256\n"); exit(1); } /*-------- open a port, map a device */ portNum = i3dmgx3_openPort(tryPortNum, 115200, 8, 0, 1, 1024, 1024); if (portNum<0) { printf(" port open failed.\n"); printf(" Comm error %d, %s: ", portNum, explainError(portNum)); goto Exit; } }else{ portNum=OnGetSerialPorts(); if(portNum<0) goto Exit; } printf("\n Using COM Port #%d \n", portNum); /*-------- Set Comm Timeout values */ errorCode = setCommTimeouts(portNum, 50, 50); /* Read & Write timeout values */ if (errorCode!=I3DMGX3_COMM_OK) { printf(" setCommTimeouts failed on port:%d with errorcode:%d\n",portNum,errorCode); goto Exit; } /*-------- Disclose the byte order of host */ if( TestByteOrder() !=BIG_ENDIAN) printf(" (Local Host is in Little Endian format)\n"); else printf(" (Local Host is in Big Endian format)\n"); printf("\n"); /*-------- 0xC2 Accel and Ang rate Output --- Accel x y z and Ang x y z */ printf("\n 0xC2 Accel and Ang Output \n"); errorCode = i3dmgx3_AccelAndAngRate(portNum, &Record[0]); if (errorCode < 0){ printf(" Error Accel and AngRate - : %s\n", explainError(errorCode)); endloopy =TRUE; }else{ for (i=0; i<3; i++) { Accel_AngRecord.Accel[i] = FloatFromBytes(&Record[1 + i*4]); // extract float from byte array Accel_AngRecord.AngRt[i] = FloatFromBytes(&Record[13 + i*4]); // extract float from byte array } printf("\n\tAccel X\t\tAccel Y\t\tAccel Z\n"); printf(" \t%f\t%f\t%f\n", Accel_AngRecord.Accel[0], Accel_AngRecord.Accel[1], Accel_AngRecord.Accel[2]); printf("\n\t Ang X\t\t Ang Y\t\t Ang Z\n"); printf(" \t%f\t%f\t%f\n", Accel_AngRecord.AngRt[0], Accel_AngRecord.AngRt[1], Accel_AngRecord.AngRt[2]); Accel_AngRecord.timer = convert2ulong(&Record[25]); printf("\n Time Stamp: %u\n", Accel_AngRecord.timer); } Exit: /*-------- close device */ if (portNum >= 0) i3dmgx3_closeDevice(portNum); /*-------- wait for user to respond before exiting */ printf("\nHit return to exit...\n"); while (getchar() == EOF); return(0); }