コード例 #1
0
/*----------------------------------------------------------------------
 * i3dmgx3_getEEPROMValue	0xE5
 *
 * parameters   portNum    : the number of the sensor device (1..n)
 *              address      : the EEPROM address location
 *              readFlag     : specifies the number of reads required.
 *                             and identifies data type long or float.
 *              value        : the value to get at the address specified
 *
 *
 * returns:     errorCode : I3DMGX3_OK if succeeded, otherwise returns
 *                          an error code.
 *--------------------------------------------------------------------*/
int i3dmgx3_getEEPROMValue(int portNum, char address, int readFlag, int *readval) {
	int status = 0;
	long bytesRead = 0;
	unsigned char Bresponse[5]  = {0};
	unsigned char BFresponse[5]  = {0};
	unsigned char ConvertBuff[4] = {0};
	int responseLength = 5;
	unsigned long nvalL=0;
	unsigned short nvalA=0, nvalB=0;
	float nvalF=0.0;
	unsigned char zoutbuff[4];
	unsigned short wChecksum = 0;
    unsigned short wCalculatedCheckSum = 0;

	zoutbuff[0] = 0xE5;     // EEPROM command identifier
	zoutbuff[1] = 0x00;     // Required identifier
	zoutbuff[2] = 0xFC;     // Required identifier
	zoutbuff[3] = address;  // EEPROM address location

	status = sendBuffData(portNum, &zoutbuff[0], 4);
	if (DEBUG) printf(" eeprom read status is %d\n", status);
    if (status == I3DMGX3_COMM_OK) { 
		status = receiveData(portNum, &Bresponse[0], 5);
        if (status == I3DMGX3_COMM_OK) {
            status = I3DMGX3_OK;
			  wChecksum = convert2ushort(&Bresponse[responseLength-2]);
	          wCalculatedCheckSum = i3dmgx3_Checksum(&Bresponse[0], responseLength-2); //calculate the checkusm, 29 = 31-2 don't include the checksum bytes
			if(wChecksum != wCalculatedCheckSum)
				return status = I3DMGX3_CHECKSUM_ERROR;
			
            nvalA = convert2ushort(&Bresponse[1]);
			
			if (readFlag > 0) {
				ConvertBuff[3] = Bresponse[2];
				ConvertBuff[2] = Bresponse[1];
				zoutbuff[3] = address + 2;

				status = sendBuffData(portNum, &zoutbuff[0], 4);
				if (status != I3DMGX3_COMM_OK)
				   return status;
		
				status = receiveData(portNum, &BFresponse[0], 5);

				if (status == I3DMGX3_COMM_OK) {
					status = I3DMGX3_OK;
					wChecksum = convert2ushort(&Bresponse[responseLength-2]);
					wCalculatedCheckSum = i3dmgx3_Checksum(&BFresponse[0], responseLength-2); //calculate the checkusm, 29 = 31-2 don't include the checksum bytes
					if(wChecksum != wCalculatedCheckSum)
						return status = I3DMGX3_CHECKSUM_ERROR;
					nvalB = convert2ushort(&BFresponse[1]);
					
					ConvertBuff[1] = BFresponse[2];
				    ConvertBuff[0] = BFresponse[1];
					nvalF = FloatFromBytes(&ConvertBuff[0]);
					nvalL = convert2ulong(&ConvertBuff[0]);
					if (readFlag == 1){
						printf("  At addr.:0x%X where value is:   %ld \n", zoutbuff[3], nvalL);
						*readval = nvalL;
					}
					else{
						printf("  At addr.:0x%X where value is: %f \n", zoutbuff[3], nvalF);
						*readval = nvalF;
					}
				}else
					return status = I3DMGX3_COMM_READ_ERROR;
			}else{
					printf("  At addr.:0x%X where value is: %d \t0x%X\n", address, nvalA, nvalA);
					*readval = nvalA;
			}
        }else
            return status = I3DMGX3_COMM_READ_ERROR;
    }
    return status;
}
コード例 #2
0
/*--------------------------------------------------------------------*/
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);
}