Victor大大:
你好!
我把你的控件用在多机通信中,发现了一些问题。程序如下:
unsigned char temp;
bool err;
err = false;
YbCommDevice1->Parity = TYbCommDevice::ptMarkParity; // TB9=1,准备发送地址 ①
try
{
YbCommDevice1->Active = true; }
catch(Exception &e)
{
err = true;
ShowMessage(e.Message); //错误处理
}
if(!err){ // 正常
Data_send = false; // 标志复位
Chk_sum=0;
Chk_sum+=U_address;
Txd_cnt+=YbCommDevice1->Write(&U_address,1); // 发送地址
while(!Data_send){} // 等待发送完成 Data_send标志状态在CommNotify接收
YbCommDevice1->Parity = TYbCommDevice::ptSpaceParity; // TB9=0 ②
YbCommDevice1->Active = true;
Chk_sum+=C_length;
Txd_cnt+=YbCommDevice1->Write(&C_length,1); // 发送数据长度
Chk_sum+=U_command;
Txd_cnt+=YbCommDevice1->Write(&U_command,1); // 发送命令
temp=Chk_sum/256;
Txd_cnt+=YbCommDevice1->Write(&temp,1); // 发送校验和 HI
temp=Chk_sum-temp*256;
Txd_cnt+=YbCommDevice1->Write(&temp,1); // 发送校验和 LOW
}
void __fastcall TForm1::YbCommDevice1CommNotify(TObject *Sender,
int NotifyType)
{
if(NotifyType==EV_RXCHAR)
{
Form1->Timer1->Enabled=false;
Form1->Timer1->Enabled=true; // 定时器复位
}
if(NotifyType==EV_TXEMPTY)
{ Data_send = true; }
}
问题:我的机器是C366,波特率38.4k,经过示波器的观察,从①到②,需要200ms时间,
从②到数据发送完,需要40ms时间。每秒钟最多只能发送4组命令,跟期望相差太远。
而且时间的间隔不是固定的,同时运行的程序越多,时间间隔越长。我想问题是在切换
状态和重新打开串口上,从MarkParity切换到SpaceParity,必须等待从机地址发送完。
Victor大大,有没有更有效的办法呢?
--------------------next---------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
YbCommDevice1->PortNo = 1;
YbCommDevice1->Baud = TYbCommDevice::br38400;
RichEdit1->Lines->Add("AAAA "+FormatDateTime("yyyy-mm-dd hh:nn:ss.zzz",Now()));
for(int i=0; i<100; i++)
{
bDataSent=false;
YbCommDevice1->Parity = TYbCommDevice::ptMarkParity;
YbCommDevice1->Active = true;
YbCommDevice1->Write(&i,4);
RichEdit1->Lines->Add(AnsiString().sprintf("%04d ",i)+FormatDateTime("yyyy-mm-dd hh:nn:ss.zzz",Now()));
while(!bDataSent){Application->ProcessMessages();}
bDataSent=false;
YbCommDevice1->Parity = TYbCommDevice::ptSpaceParity;
YbCommDevice1->Active = true;
YbCommDevice1->Write(&i,4);
RichEdit1->Lines->Add(AnsiString().sprintf("%04d ",i)+FormatDateTime("yyyy-mm-dd hh:nn:ss.zzz",Now()));
Application->ProcessMessages();
while(!bDataSent){Application->ProcessMessages();}
}
RichEdit1->Lines->Add("EEEE "+FormatDateTime("yyyy-mm-dd hh:nn:ss.zzz",Now()));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::YbCommDevice1CommNotify(TObject *Sender, int NotifyType)
{
if(NotifyType & EV_TXEMPTY) //事件里面包含 EV_TXEMPTY 用 & 判断
{
bDataSent = true;
}
}
//---------------------------------------------------------------------------
--------------------next---------------------
阅读(1020) | 评论(0) | 转发(0) |