程序初始化的部分代码:
YbCommDevice1->PackageInterval = 40; //间隔为 40 毫秒, 根据实际情况设置
YbCommDevice1->PackageSize = 1024; //产生一次事件最多能够接收的字节数, 根据实际情况设置
YbCommDevice1->PackageType = cptFrameTimeout; //用判断时间间隔的方法接收数据
YbCommDevice1->UsePackage = true;
串口OnPackage部分代码:
void __fastcall TMainForm::YbCommDevice1Package(TObject *Sender,
int NotifyType)
{
if(NotifyType & EV_RXCHAR) //如果 NotifyType 的 RXCHAR 位为 1
{
unsigned char Buf[8192]; //收到的字节数不会超过串口缓存的容量, 所以分配一个缓存容量相同的Buf
int n = YbCommDevice1->ReadPackage(Buf,8192); //收到 n 个字节
if(n == 0) return;
AnsiString s;
AnsiString msg;
static int dataCount1 = 0;
static int dataCount2 = 0;
for(int i=0; i switch(iComm1Status)
{
case 2: // 谱图采集状态
{
if(s==("55555500"+sPCCode+"FC"))
{
//Memo3->Lines->Add("已经停止数据采集!");
msg = DateTimeToStr(Now())+ " 已经停止数据采集!";
mm.AddMessage(3, msg.c_str());
dxbtnStart->Enabled = true;
dxbtnStop->Enabled = false;
sbtnFunctionTest->Enabled = true;
sbtnTempTest->Enabled = true;
dataCount1 = 0;
fclose(datafile);
iComm1Status = 0;
return;
}
break;
}
case 1: // 基线测试状态
{
if(s==("55555500"+sPCCode+"008555FFFF55"))
{
//Memo3->Lines->Add("已经停止基线测试!");
msg = DateTimeToStr(Now())+ " 已经停止基线测试!";
mm.AddMessage(3, msg.c_str());
dxbtnAdjust->Enabled = true;
dxbtnEnd->Enabled = false;
sbtnFunctionTest->Enabled = true;
sbtnTempTest->Enabled = true;
dataCount2 = 0;
iComm1Status = 0;
return;
}
int tmpY;
double Y;
tmpY = Buf[0]*256 + Buf[1];
Y = RoundTo(2500.0*tmpY/65535.0, -2);
stTime->Caption = dataCount2;
stVolt->Caption = Y;
SampleSeries->AddXY(dataCount2, Y);
dataCount2++;
break;
}
case 0: // 命令状态
{
if(s==("55555500"+sPCCode+"8A"))
{
//vMemo[iNowPageIndex]->Lines->Add(DateTimeToStr(Now())+ " 开始接收数据……");
msg = DateTimeToStr(Now())+ " 开始接收数据……";
mm.AddMessage(3, msg.c_str());
iComm1Status = 2;
dxbtnCompute1->Enabled = false;
dxbtnStart->Enabled = false;
dxbtnStop->Enabled = true;
sbtnFunctionTest->Enabled = false;
sbtnTempTest->Enabled = false;
}
else if(s==("55555500"+sPCCode+"2B0155FFFF55"))
{
bIsSuccess = true;
msg = DateTimeToStr(Now())+ " 成功建立通讯!";
mm.AddMessage(1, msg.c_str());
bTongxunOK = true;
ltvwTestContent->Items->Item[0]->SubItems->Strings[0] = testObj[0].Command[0].Status;
}
break;
}
default:
break;
}
}
}
--------------------next---------------------
阅读(1165) | 评论(0) | 转发(0) |