Exemplo n.º 1
0
void Camera::serialWrite(char buffer[]){
    clFlushPort(serialRefPtr);
    unsigned int bufferlen= 20;
    unsigned int serialTimeout = 500;
    try{
        checkSerialCom(clSerialWrite(serialRefPtr, buffer, &bufferlen, serialTimeout));
    }catch(string const& error){
        cout << "Write ERROR:" << error << endl;
	serialWrite(buffer);
    }
    char *mybuff= NULL;

    unsigned int numBytes=256;
    mybuff = (char *) malloc(numBytes);
    try{
        checkSerialCom(clSerialRead(serialRefPtr, mybuff, &numBytes, serialTimeout));
    }catch(string const& error){
        free(mybuff);
        cout << "Read ERROR:" << error << endl;
// 	serialWrite(buffer);
	return;
    }
    char temp[8];
    strncpy(temp, mybuff, 8);
    cout << "Camera response:" << temp << endl;
    free(mybuff);
}
Exemplo n.º 2
0
////////////////////////////////////////////////////////////////////////////////////////////
//
// CAMERA IO FUNCTION
//
//
////////////////////////////////////////////////////////////////////////////////////////////
DWORD CPco_cl_com::Control_Command(void *buf_in,DWORD size_in,
                                   void *buf_out,DWORD size_out)
{
  DWORD err=PCO_NOERROR;
  unsigned char buffer[PCO_SC2_DEF_BLOCK_SIZE];
  unsigned int size;
  WORD com_in,com_out;

/*
  if(hComMutex==NULL)
   writelog(COMMAND_M,hdriver,"Control_Command: No Mutex no wait");
  else
  {
   err = WaitForSingleObject(hComMutex,tab_timeout.command*3);
   writelog(COMMAND_M,hdriver,"Control_Command: Wait Mutex 0x%x done err=%d",hComMutex,err);
   switch (err)
   {
    case WAIT_OBJECT_0:
     break;
// Cannot get mutex ownership due to time-out.
    case WAIT_TIMEOUT:
    {
     writelog(ERROR_M,hdriver,"Control_Command: Timeout waiting for Mutex 0x%x",hComMutex);
     return PCO_ERROR_TIMEOUT | PCO_ERROR_DRIVER | PCO_ERROR_DRIVER_USB;
    }

// Got ownership of the abandoned mutex object.
    case WAIT_ABANDONED:
    {
     writelog(ERROR_M,hdriver,"Control_Command: Mutex abandoned");
     return PCO_ERROR_INVALIDHANDLE | PCO_ERROR_DRIVER | PCO_ERROR_DRIVER_USB;
    }
   }
  }
*/


//  if(clFlushPort)
  {
   err=clFlushPort(serialRef);
   if((int)err<0)
   {
    writelog(ERROR_M,hdriver,"Control_Command: pclSerialFlush error = %d",err);
   }
  }


  writelog(COMMAND_M,hdriver,"Control_Command: start= 0x%04x timeout %d",*(unsigned short*)buf_in,tab_timeout.command);

  com_out=0;
  com_in=*((WORD*)buf_in);
  memset(buffer,0,PCO_SC2_DEF_BLOCK_SIZE);

  size=size_in;
  err=build_checksum((unsigned char*)buf_in,(int*)&size);

  err=clSerialWrite(serialRef,(char*)buf_in,&size,tab_timeout.command);
  if((int)err<0)
  {
   writelog(ERROR_M,hdriver,"Control_Command: pclSerialWrite error = %d",err);
   err=PCO_ERROR_DRIVER_IOFAILURE | PCO_ERROR_DRIVER_CAMERALINK;
   goto sercom_out;
  }

  size=sizeof(WORD)*2;

  err=clSerialRead(serialRef,(char*)&buffer[0],&size,tab_timeout.command*2);
  if((int)err<0)
  {
   writelog(ERROR_M,hdriver,"Control_Command: pclSerialRead size:4 back %d error = %d",size,err);
   err=PCO_ERROR_DRIVER_IOFAILURE | PCO_ERROR_DRIVER_CAMERALINK;
   goto sercom_out;
  }

  com_out=*((WORD*)buffer);

  WORD *b;
  b=(WORD *)buffer; //size of packet is second WORD
  b++;
  size=(int)*b;
  if(size>PCO_SC2_DEF_BLOCK_SIZE)
   size=PCO_SC2_DEF_BLOCK_SIZE;
  size-=sizeof(WORD)*2;

  writelog(INTERNAL_1_M,hdriver,"Control_Command: before read com_out=0x%04x size %d",com_out,size);
  if((int)size<0)
  {
   writelog(ERROR_M,hdriver,"Control_Command: SerialRead remaining size<0 %d err %d com:0x%04x",size,err,*(WORD *)buffer);
   err=PCO_ERROR_DRIVER_IOFAILURE | PCO_ERROR_DRIVER_CAMERALINK;
   goto sercom_out;
  }

  if(com_in!=(com_out&0xFF3F))
  {
   writelog(ERROR_M,hdriver,"Control_Command: SerialRead comin  0x%04x != comout&0xFF3F 0x%04x",com_in,com_out&0xFF3F);
   err=PCO_ERROR_DRIVER_IOFAILURE | PCO_ERROR_DRIVER_CAMERALINK;
   goto sercom_out;
  }

  err=clSerialRead(serialRef,(char*)&buffer[sizeof(WORD)*2],&size,tab_timeout.command*2);
  if((int)err<0)
  {
   writelog(ERROR_M,hdriver,"Control_Command: pclSerialRead size:%d back %d error = %d",*b,size,err);
   err=PCO_ERROR_DRIVER_IOFAILURE | PCO_ERROR_DRIVER_CAMERALINK;
   goto sercom_out;
  }

  err=PCO_NOERROR;

  com_out=*((WORD*)buffer);
  if((com_out&RESPONSE_ERROR_CODE)==RESPONSE_ERROR_CODE)
  {
   SC2_Failure_Response resp;
   memcpy(&resp,buffer,sizeof(SC2_Failure_Response));
   err=resp.dwerrmess;
   if((err&0xC000FFFF)==PCO_ERROR_FIRMWARE_NOT_SUPPORTED)
    writelog(INTERNAL_1_M,hdriver,"Control_Command: com 0x%x FIRMWARE_NOT_SUPPORTED",com_in);
   else
    writelog(ERROR_M,hdriver,"Control_Command: com 0x%x RESPONSE_ERROR_CODE error 0x%x",com_in,err);
   size= sizeof(SC2_Failure_Response);
  }
  else
   size=size_out;

  if(err==PCO_NOERROR)
  {
   if(com_out!=(com_in|RESPONSE_OK_CODE))
   {
    err=PCO_ERROR_DRIVER_DATAERROR | PCO_ERROR_DRIVER_CAMERALINK;
    writelog(ERROR_M,hdriver,"Control_Command: Data error com_out 0x%04x should be 0x%04x",com_out,com_in|RESPONSE_OK_CODE);
   }
  }

  writelog(INTERNAL_1_M,hdriver,"Control_Command: before test_checksum read=0x%04x size %d",com_out,size);
  if(test_checksum(buffer,(int*)&size)==PCO_NOERROR)
  {
   size-=1;
   if(size<size_out)
    size_out=size;
   memcpy(buf_out,buffer,size_out);
  }
  else
   err=test_checksum(buffer,(int*)&size);

sercom_out:
/*  if(hComMutex)
  {
   if(!ReleaseMutex(hComMutex))
    writelog(ERROR_M,hdriver,"Control_Command: release Mutex failed");
   writelog(COMMAND_M,hdriver,"Control_Command: Release Mutex 0x%x done",hComMutex);
  }
*/

  writelog(COMMAND_M,hdriver,"Control_Command: Control_command end 0x%04x err 0x%x",com_out,err);
  return err ;
}