⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gps_interface.cpp

📁 基于USB接口的GPS应用程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
                sprintf(infobuf, "An error occured reading data from usb handle ");
                PrintDbgInfo(infobuf);
	 return false;
	}
        memcpy(out,reg_buf,63);
	return true;
  } // end of if( fileOpened )
  else
  return false;
}


//---------------------------------------------------------------------------
/*++
  Routine Description:
  To read the register of GP2021
  Argument:
          addr:  the address of register to be read;
          out :  the pointer that store the value of the register (16bit)
  return:
          if successful , rutren true; otherwise return false
--*/

bool IoInterface::read_gp2021_reg(const unsigned char addr, unsigned int *out)
{
 char infobuf[256];
 unsigned char buf[6]={0xaa, 'q', 0,0,0,0};
 int nBytes;
 if( handleOpened )
 {
	buf[2] = addr;
	buf[5] = (buf[0]+buf[1]+buf[2]) % 256; // calculate the checksum

	if(!DeviceIoControl(gpsHandle,
			IOCTL_USBGPS_BULK_WRITE,
			buf,
			6,
			buf,
			6,
			(unsigned long *)&nBytes,
			NULL) )
	{
                sprintf(infobuf, "An error occured writing data to usb handle ");
                PrintDbgInfo(infobuf);
		return false;
	}


   	if(!DeviceIoControl(gpsHandle,
			IOCTL_USBGPS_BULK_READ,
			NULL,
			0,
			buf,
			6,
			(unsigned long *)&nBytes,
			NULL) )
	{
                sprintf(infobuf, "An error occured reading data from usb handle ");
                PrintDbgInfo(infobuf);
	return false;
	}
	*out = buf[1]*256 + buf[0];
	return true;
  } // end of if( handleOpened )
  else
  return false;
}
//---------------------------------------------------------------------------
/*++
  Routine Description:
  To write to GP2021 regs ,
  Argument:
          addr :  address of GP2021 reg
          in   :  value to be written
  return:
          if successful , rutren true; otherwise return false
--*/
bool IoInterface::write_gp2021_reg(const unsigned char addr, const unsigned int in)
{
  char infobuf[256];
  unsigned char buf[6]={0xaa, 't', 0,0,0,0};
  int nBytes;
  if(handleOpened)
  {
	buf[2] = addr;
	buf[3] = in & 0xff;
	buf[4] = (in >> 8) & 0xff;
	buf[5] = (buf[0]+buf[1]+buf[2]+buf[3]+buf[4]) % 256;   // checksum

	if(!DeviceIoControl(gpsHandle,
			IOCTL_USBGPS_BULK_WRITE,
			buf,
			6,
			buf,
			6,
			(unsigned long *)&nBytes,
			NULL) )
	{
                sprintf(infobuf, "An error occured writing data to usb handle ");
                PrintDbgInfo(infobuf);
		return false;
	}

	return true;
   } // end of if(handleOpened)
   else
   return false;
}

//---------------------------------------------------------------------------
/*++
  Routine Description:
  To read all the registers of GP2021 , just for debugging
  Argument:
          out :  the pointer that store the values of the registers
  return:
          if successful , rutren true; otherwise return false
--*/

bool IoInterface::read_gp2021_all( unsigned int *out)
{
  char infobuf[256];
  unsigned char buf[6]={0xaa, 'a', 0,0,0,0};
  unsigned char outbuf[449];
  int nBytes;
     if( handleOpened )
     {
        buf[5] = (buf[0]+buf[1]) % 256;

	if(!DeviceIoControl(gpsHandle,
			IOCTL_USBGPS_BULK_WRITE,
			buf,
			6,
			buf,
			6,
			(unsigned long *)&nBytes,
			NULL) )
	{
                sprintf(infobuf, "An error occured get_all_gp2021 to usb handle ");
                PrintDbgInfo(infobuf);
		return false;
	}


   	if(!DeviceIoControl(gpsHandle,
			IOCTL_USBGPS_BULK1_READ,       // From TXD3 , change this also requires firmware changes
                        //IOCTL_USBGPS_BULK_READ, // From TXD1
			NULL,
			0,
			outbuf,
			449,
			(unsigned long *)&nBytes,
			NULL) )
	{
                sprintf(infobuf, "An error occured reading get_all_gp2021 data from usb handle, %d ", nBytes);
                PrintDbgInfo(infobuf);
		return false;
	}
        for( int i = 0 ; i<188; i++)
        *(out + i) = outbuf[2*i] + outbuf[2*i + 1] * 256;
	return true;
      }  // end of ( if( handleOpened ))
      else
      return false;
}

//---------------------------------------------------------------------------
/*++
  Routine Description:
  To write a bunch of data to GP2021 , just for debugging
  Argument:
          in :  the pointer that store the values to be written
          size: size of data buffer
  return:
          if successful , rutren true; otherwise return false
--*/
bool IoInterface::write_gp2021_all( unsigned char *in, unsigned int size)
{
  int nBytes;
  char infobuf[256];
       if( handleOpened )
       {
    	if(!DeviceIoControl(gpsHandle,
			IOCTL_USBGPS_BULK1_WRITE,
			in,
			size,
			NULL,
			0,
			(unsigned long *)&nBytes,
			NULL) )
	{
                sprintf(infobuf, "An error occured writing all data to usb handle ");
                PrintDbgInfo(infobuf);
		return false;
	}

	return true;
       } // end of  if( handleOpened )
       else
       return false;
}
//---------------------------------------------------------------------------
/*++
  Routine Description:
  To send command to GP2021 , then try to get the 4 collections;

  Argument:
          bufferfalg :  indicate which data buffer is transferring;
          needreturn :  if app need the 4 collections;
          out        : point to store the 4 collection if needreturn == true;
  return:
          if successful , rutren true; otherwise return false
--*/

bool IoInterface::rd_wr_gp2021_all(char bufferflag,bool needreturn, unsigned char *out)
{
 int nBytes;
 char infobuf[256];
 // every reg needs 3 bytes, plus this count byte

 assert( bufferflag == 0 || bufferflag ==1);

  if( handleOpened )
  {
   if( needreturn )
   {
    gp2021regs[bufferflag].flag = gp2021regs[bufferflag].flag | 0x01;  // needs to get return
    if(!DeviceIoControl(gpsHandle,
          		IOCTL_USBGPS_BULK1_RD_WR,
			(unsigned char*)(gp2021regs+bufferflag),
			sizeof(gp2021_regs),
			out,
			LENG_OF_ONE_FRAME,
			(unsigned long *)&nBytes,
			NULL) )
	{
         sprintf(infobuf, "An error occured writing & reading all data to usb handle: %d count: %d", nBytes, count);
         PrintDbgInfo(infobuf);

	return false;
	}
    }    // match of  if( needreturn)
    else   // don't need return , just send command to gp2021
    {
     gp2021regs[bufferflag].flag = gp2021regs[bufferflag].flag & 0xfe;
     	if(!DeviceIoControl(gpsHandle,
			IOCTL_USBGPS_BULK1_WRITE,
			(unsigned char*)(gp2021regs+bufferflag),
			sizeof(gp2021_regs),
			NULL,
			0,
			(unsigned long *)&nBytes,
			NULL) )
	{
         sprintf(infobuf, "An error occured writing all data to usb handle ");
         PrintDbgInfo(infobuf);

	 return false;
	}
    }  // end of if(needreturn)

    return true;
   } // match of  if( handleOpened )
   else
   return false;
}
//---------------------------------------------------------------------------
/*++
  Routine Description:
  control the timer of DS89c420,
  Argument:
      flag = 0:  reset and stop timer,
                thus stop collecting gp2021's tracking loop data
      flag = 1:  restart timer,
                thus start collecting gp2021's tracking loop data continuously
  return:
          if successful , rutren true; otherwise return false
--*/
bool IoInterface::control_gp2021(char flag)
{

 char infobuf[256];
 unsigned char buf[6]={0xaa, 'c', 0,0,0,0};
 int nBytes;
 if( handleOpened )
 {
	buf[2] = flag;
	buf[5] = (buf[0]+buf[1]+buf[2]) % 256; // calculate the checksum

	if(!DeviceIoControl(gpsHandle,
			IOCTL_USBGPS_BULK_WRITE,
			buf,
			6,
			buf,
			6,
			(unsigned long *)&nBytes,
			NULL) )
	{
          sprintf(infobuf, "An error occured writing data to usb handle ");
          PrintDbgInfo(infobuf);
	  return false;
	}
        return true;
 }
 else
 return false;
}
//---------------------------------------------------------------------------
/*++
  Routine Description:
  Wrapping of rd_wr_gp2021_all for application,
  Argument:
      pp_flag :  indicate the ping_pong flag
  return:
          if successful , rutren true; otherwise return false
--*/
bool IoInterface::WriteReadGP2021(unsigned char pp_flag)
{
// unsigned char outbuf[LENG_OF_ONE_FRAME];
 bool success;
 count ++;
 success = rd_wr_gp2021_all(pp_flag,true,tracking_data[pp_flag]);
 return( success );
}
//---------------------------------------------------------------------------
/*++
  Routine Description:
  copy collection to GPS_Nav thread, and switch pp_flag
  Argument:
         des: pointer of command buffer
  return:
         none;
--*/

void IoInterface::CopyData(unsigned char* des)
{
 // ping_pong_flag is current working buffer, so we need to toggel it
 unsigned char flag = (ping_pong_flag+1)%2;
 assert(des != NULL);
 memcpy((void*)des, (void*)tracking_data[flag], LENG_OF_ONE_FRAME);
}
//---------------------------------------------------------------------------
/*++
  Routine Description:
  copy command from GPS_Nav thread to current idle buffer,
  Argument:
      bufferflag :  indicate which buffer should receive this data buffer,

      src     :  pointer of data buffer
  return:
          none;
--*/
void IoInterface::GetCntlData(char bufferflag, unsigned char* src)
{

 assert(bufferflag == 0 || bufferflag == 1);
 assert(src != NULL);
 memcpy((void*)(gp2021regs+bufferflag), (void*)src, sizeof(gp2021_regs));
}

//---------------------------------------------------------------------------
/* function called in running thread ,                                     */
/*  its duty should set ping-pong flag to indicate available buffer        */
/***************************************************************************/

void IoInterface::set_pp_flag( char PPflag)
{
 ping_pong_flag = PPflag;
}
unsigned char IoInterface::toggle_pp_flag(void)
{
 ping_pong_flag  = (ping_pong_flag+1)%2;
 return ping_pong_flag;
}
unsigned char IoInterface::get_pp_flag(void)
{
 return ping_pong_flag;
}
unsigned char IoInterface::get_free_pp_flag(void)
{
 // ping_pong_flag indicate the buffer which is in use currently
 return (ping_pong_flag+1)%2;
}

//---------------------------------------------------------------------------
/*    Routine Description:                                                 */
/*    thread handling funtcions                                          */
/***************************************************************************/

void IoInterface::CreatSampleThrd()
{
 if(handleOpened)
 {
  clear_all_reg(0);
  clear_all_reg(1);
  s_thread = new SamplingThread(true, this);
  s_thread->Resume();
  thrdtermed = false;
 }
}
// terminate sampling thread
void IoInterface::ThrdTerminate()
{
 if(s_thread)
  s_thread->Terminate();
}
// return the terminate status of sampling thread
bool IoInterface::NavThrdIsTermed()
{
 return (thrdtermed);
}
 // function to set the terminate status of sampling thread
void IoInterface::SetThrdStatus(bool stat)
{
 thrdtermed = stat;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -