📄 interface.cpp
字号:
//---------------------------------------------------------------------------
#pragma hdrstop
#include "Interface.h"
#include "CH375DLL.H"
#include "Unit1.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
__fastcall TInterface::TInterface()
: TObject()
{
}
//---------------------------------------------------------------------------
__fastcall TInterface::~TInterface()
{
}
//---------------------------------------------------------------------------
void TInterface::USBDataDown(UCHAR code1, UCHAR code2, UCHAR code3, UCHAR code4)
{
if ( CH375OpenDevice(0) == INVALID_HANDLE_VALUE ) // 使用之前必须打开设备
{
Application->MessageBox("无法打开USB接口设备", "information", MB_ICONSTOP | MB_OK );
}
DDBuf.code1=code1; DDBuf.code2=code2; DDBuf.code3=code3; DDBuf.code4=code4;
DataLength = 0x04;
if ( CH375WriteData( 0, (void *)&DDBuf, (unsigned long *)&DataLength ) ) // 发送成功
{
if ( DataLength == 4 )
{
CH375QueryBufUpload(0);
}
}
}
//---------------------------------------------------------------------------
UCHAR * TInterface::PortUpData()
{
if ( CH375OpenDevice(0) == INVALID_HANDLE_VALUE ) // 使用之前必须打开设备
{
if (Application->MessageBox("无法打开USB接口设备\n是否退出", "information", MB_ICONSTOP | MB_OKCANCEL ) == MB_OK)
{
Form1->Close();
}
}
UCHAR mBuf[16];
BOOL DataRead;
for(int i=0;i<16;i++)
mBuf[i] = 0;
DataLength = sizeof(mBuf)/sizeof(*mBuf);
DataRead = CH375ReadData(0, &mBuf, &DataLength);
if(DataRead)
pktlength = (UINT)DataLength;
else
pktlength = 0;
return mBuf;
}
//---------------------------------------------------------------------------
int TInterface::CommandReturn()
{
int CMD;
UCHAR * accept = new(UCHAR);
accept = PortUpData();
if(pktlength==4)
{
if(* accept == 0xd1)
{
CMD = *(accept+1)<<16|*(accept+2)<<8|*(accept+3);
delete accept;
accept = NULL;
return CMD;
}
}
else return 0;
}
//---------------------------------------------------------------------------
bool TInterface::DelUpLong(int total)
{
UCHAR * accept = new(UCHAR);
LARGE_INTEGER frequent,interval,previous,current;
for(int i=0;i<total;i++)
{
QueryPerformanceFrequency(&frequent);
interval.QuadPart=total*(float)frequent.QuadPart/1000; //1 ms的触发次数
QueryPerformanceCounter(&previous);
current=previous;
while(current.QuadPart-previous.QuadPart<interval.QuadPart)
QueryPerformanceCounter(¤t); //延时t ms采集数
accept = PortUpData();
if(pktlength == 12)
{
for(int i=0;i<pktlength;i++)
{
Form1->ADC[i] = *(accept+i);
}
delete accept;
accept = NULL;
return true;
}
else
continue;
}
return false;
}
//---------------------------------------------------------------------------
bool TInterface::Delay(int cmd, int t, int total)
{
LARGE_INTEGER frequent,interval,previous,current;
for(int i=0;i<total;i++)
{
QueryPerformanceFrequency(&frequent);
interval.QuadPart=t*(float)frequent.QuadPart/1000; //t ms的触发次数
QueryPerformanceCounter(&previous);
current=previous;
while(current.QuadPart-previous.QuadPart<interval.QuadPart)
QueryPerformanceCounter(¤t); //延时t ms采集数
int DelayData = CommandReturn();
if(DelayData>>16 == cmd)
{
Form1->speedh = (DelayData&0xff00)>>8;
Form1->speedl = (DelayData&0xff);
return true;
break;
}
else
{
continue;
}
}
return false;
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -