try
{
if (YbCD->Active==true)
YbCD->Active = false;//关闭串口
YbCD->PortNo = 1; //设置串口号
YbCD->Active = true; //打开串口
YbCD->UsePackage = true; //使用数据包形式
YbCD->PackageType = cptFrameTimeout;
RadioButton1->Checked = true;
Button1->Enabled = true;
Button2->Enabled = true;
}
catch(Exception &e)
{
Application->MessageBox("打开串口失败,请检查串口!","提示",MB_OK);
RadioButton1->Checked = false;
return; //异常处理
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmAmmeter::RadioButton2Click(TObject *Sender)
{
try
{
if (YbCD->Active==true)
YbCD->Active = false;//关闭串口
YbCD->PortNo = 2; //设置串口号
YbCD->Active = true; //打开串口
YbCD->UsePackage = true; //使用数据包形式
YbCD->PackageType = cptFrameTimeout;
RadioButton2->Checked = true;
Button1->Enabled = true;
Button2->Enabled = true;
}
catch(Exception &e)
{
Application->MessageBox("打开串口失败,请检查串口!","提示",MB_OK);
RadioButton2->Checked = false;
return; //异常处理
}
}
//---------------------------------------------------------------------------
/************************************************
** 函数名:send
** 输 入:send
** send---要发送的数据
** 功能描述:将所给数据从串口发送出去
** 全局变量:
** 调用模块:
** 作 者:李冰
** 日 期:2003.7.19
** 修 改:
** 日 期:
** 版本:v1.0
************************************************/
void __fastcall TfrmAmmeter::send(AnsiString sends,bool crc)
{
int nBytes = 0; //设置接收的字节数
char Buffer[1000], *EndPtr;
AnsiString t,s = sends.Trim();
while(s.Length()>0)
{
int p = s.Pos(' '); //空格
if(p>0) //如果找到空格
{
t = s.SubString(1,p-1);
s = s.SubString(p+1,s.Length()).Trim();
Buffer[nBytes++] = strtol(t.c_str(), &EndPtr, 16); //十六进制字符串转成字节
}
else //没有找到,还剩下最后一个字节
{
t = s;
s = "";
Buffer[nBytes++] = strtol(t.c_str(), &EndPtr, 16); //十六进制字符串转成字节
}
}
if (crc)
Buffer[nBytes++]=CRC8(Buffer,nBytes);//crc校验
frmAmmeter->YbCD->Write(Buffer,nBytes);//发送数据 Package
RichEdit1->Lines->Add("发送数据 "+Now().TimeString());
AnsiString send;
for(int i=0; i send += IntToHex(Buffer[i],2).SubString(IntToHex(Buffer[i],2).Length()-1,2) + " ";
send = send.Trim(); //将接收的数据串换成字符串形式
if(!send.IsEmpty())
{
int len = RichEdit1->Text.Length();
RichEdit1->Lines->Add(send); //显示字符串,便于校正程序
if (send.Length()>50)
{
RichEdit1->SelStart = len+24;
RichEdit1->SelLength = 5;
RichEdit1->SelAttributes->Color=clRed;
RichEdit1->SelStart = len+48;
RichEdit1->SelLength = 5;
RichEdit1->SelAttributes->Color=clBlue;
RichEdit1->SelStart = len+57;
RichEdit1->SelLength = 2;
RichEdit1->SelAttributes->Color=clFuchsia;
RichEdit1->SelLength = 0;
}
SendMessage(RichEdit1->Handle,EM_SCROLLCARET,0,0);
}
}
//-----------------------------------------------------------------------------
void __fastcall TfrmAmmeter::YbCDPackage(TObject *Sender, int NotifyType)
{
const BufSize = 8192; //串口使用缓存
unsigned char Buffer[BufSize]; //接受数据的字符串
int nBytes = 0; //接收字符串长度
while(nBytes=YbCD->ReadPackage(Buffer,BufSize)>0)//接收数据不为空
{
//////////////////////////////////////////////////
RichEdit2->Lines->Add("接收数据 "+Now().TimeString());
AnsiString s;
for(int i=0; i s += IntToHex(Buffer[i],2) + " ";
s = s.Trim(); //将接收的数据串换成字符串形式
if(!s.IsEmpty())
{
int len = RichEdit2->Text.Length();
RichEdit2->Lines->Add(s); //显示字符串,便于校正程序
RichEdit2->SelStart = len;
RichEdit2->SelLength = 2;
RichEdit2->SelAttributes->Color=clRed;
SendMessage(RichEdit2->Handle,EM_SCROLLCARET,0,0);
}
//////////////////////////////////////////////////
}
}
--------------------next---------------------
阅读(1077) | 评论(0) | 转发(0) |