WIN2000下串口通讯(100分)

  • 主题发起人 主题发起人 chinahawk
  • 开始时间 开始时间
C

chinahawk

Unregistered / Unconfirmed
GUEST, unregistred user!
我要在WIN2000下实现串口通讯,并且要兼容98,我写了如下程序,可是读、写串口
都是失败的,应该如何做?
打开串口是正确的。
var
hcom:thandle;
timeout:TCommTimeouts;
dataout,datain:pchar;
overlapped:POverlapped;
buffersize:dword;
begin
hcom:=createfile('com1',generic_read or generic_write,0,nil,open_existing,
file_flag_overlapped,0);
timeout.readintervaltimeout:=10;
timeout.readtotaltimeoutmultiplier:=20;
timeout.readtotaltimeoutconstant:=30;
setcommtimeouts(hcom,timeout);
setevent(hcom);
dataout:='S';
getmem(overlapped,sizeof(tOverlapped));
buffersize:=4096;
getmem(datain,4096);
if writefile(hcom,dataout,1,buffersize,overlapped) then
showmessage('write right');
if readfile(hcom,datain,1,buffersize,overlapped) then
showmessage('read right');
freemem(datain);
freemem(overlapped);
closehandle(hcom);
end
 
请参考 :
http://www.delphibbs.com/delphibbs/dispq.asp?lid=331306
谢谢。
 
我使用turbopower的串口控件,很好用的。我的“产品”一直用它98/2000都兼容。
 
有一个流行的com 控件 spcom 很好用
另外windows里有com 的active控件可以用
 
正好能满足你的需求
http://www.delphibbs.com/delphibbs/dispq.asp?lid=532472
 
[8D]overlapped 是异步的。
 
[:)]不是秘密:
中断调用依然能用,而且 w2k/98/dos 下都能用。
 
有谁能给我一个完整的c++builder的程序?
多谢了!
另外有谁知道在c++builder中如何定义一个全局的变量,要在不同的unit中都可以使用的
在什么地方定义?
 
大虾们,我的要求做不到吗?
以前的帖子我看过了,也试过了,C++BUILDER在WIN2000下我没有实现!
 
用微软的MSCOMM控件(MSCOMM32.OCX)即可。
 
我就要用API不用控件!!!
 
为什么没有人帮我,我没有时间来自己研究了!
大小虾没,该出手时就出手吧!
 
void __fastcall TFMain::CommTVReceiveData(TObject *Sender, Pointer Buffer,
WORD BufferLength)
{
unsigned char buff[10000];
String sBuff;
sBuff.SetLength(BufferLength);
Move(Buffer,buff, BufferLength);
Move(Buffer,sBuff.c_str(),BufferLength);
if(buff[0]==0x0)
return;
String data;
for(int j=0;j<BufferLength;j++)
{
data=data+","+AnsiString(buff[j]);
}
RE->Lines->Add(data+" "+AnsiString(BufferLength)+" 个 ");
if(buff[0]==0xaa)
{
char a=0xaa;
while(sBuff.Pos(AnsiString(a))!=0)
sBuff.Delete(sBuff.Pos(AnsiString(a)),1);
wsprintf(buff,sBuff.c_str());
if(sBuff.Length()>=4)
goto LongBuff;
char temp[10];
temp[0]=0x55;
for(int i=0;i<10;i++)
{
if(CommTV->WriteCommData(temp,1))
break;
Application->Hint="通信失败!请检查线路";
MessageBeep(0);
Application->ShowHint=true;
}
return;
}
if(buff[1]==0xff)
{
if(bComm)
{
char tt=0x0d;
String s=C_O_RM+AnsiString(tt);
for(int i=0;i<10;i++)
if(!CommTV->WriteCommData(s.c_str(),s.Length()))
{
Application->Hint="通信失败!请检查线路";
Application->ShowHint=true;
MessageBeep(0);
}
else
break;
}
else
{
char tt=0x0c;
String s=C_O_RM+AnsiString(tt);
for(int i=0;i<10;i++)
if(!CommTV->WriteCommData(s.c_str(),s.Length()))
{
Application->Hint="通信失败!请检查线路";
Application->ShowHint=true;
MessageBeep(0);
}
else
break;
}
return;
}
if(buff[0]==0xff)
{
if(bComm)
{
char tt=0x0d;
String s=C_O_RM+AnsiString(tt);
for(int i=0;i<10;i++)
if(!CommTV->WriteCommData(s.c_str(),s.Length()))
{
Application->Hint="通信失败!请检查线路";
Application->ShowHint=true;
}
else
break;
}
else
{
char tt=0x0c;
String s=C_O_RM+AnsiString(tt);
for(int i=0;i<10;i++)
if(!CommTV->WriteCommData(s.c_str(),s.Length()))
{
Application->Hint="通信失败!请检查线路";
Application->ShowHint=true;
MessageBeep(0);
}
else
break;
}
return;
}
if(buff[0]==0xbb)
{
Open_CloseDT();
}
if(BufferLength==4)
{
if((buff[3]==0x0d)||(buff[3]==0x0c))
{
if(buff[3]==0x0c)
{
bWarning=true;
pbWar->Show();
}
else
bWarning=false;
char a=0xbb;
char temp[10];
temp[0]=a;
for(int i=0;i<10;i++)
{
if(CommTV->WriteCommData(temp,1))
break;
Application->Hint="通信失败!请检查线路";
Application->ShowHint=true;
MessageBeep(0);
}
//MainDTH=two+s[2]+s[3]+s[4];
String temp1=sBuff;
RM=temp1.SubString(1,3);
a=0x0a;
while(RM.Pos(AnsiString(a))!=0)
{
String sChange=RM;
sChange.Delete(RM.Pos(AnsiString(a)),1);
sChange.Insert("0",RM.Pos(AnsiString(a)));
RM=sChange;
}
StartCom();
return;
}
if(buff[3]==0x0f)
{
char a=0xbb;
char temp[10];
temp[0]=a;
for(int i=0;i<10;i++)
{
if(CommTV->WriteCommData(temp,1))
break;
Application->Hint="通信失败!请检查线路";
Application->ShowHint=true;
MessageBeep(0);
}
StopCom();
return;
}
}
if(BufferLength==5)
{
if(buff[4]==0x0c||buff[4]==0x0d)
{
if(buff[4]==0x0c)
{
bWarning=true;
pbWar->Show();
}
else
bWarning=false;
char a=0xbb;
char temp[10];
temp[0]=a;
for(int i=0;i<10;i++)
{
if(CommTV->WriteCommData(temp,1))
break;
Application->Hint="通信失败!请检查线路";
Application->ShowHint=true;
}
//MainDTH=two+s[2]+s[3]+s[4];
String temp1=sBuff;
RM=temp1.SubString(2,3);
a=0x0a;
while(RM.Pos(AnsiString(a))!=0)
{
String sChange=RM;
sChange.Delete(RM.Pos(AnsiString(a)),1);
sChange.Insert("0",RM.Pos(AnsiString(a)));
RM=sChange;
}
StartCom();
return;
}
if(buff[4]==0x0f)
{
char a=0xbb;
char temp[10];
temp[0]=a;
for(int i=0;i<10;i++)
{
if(CommTV->WriteCommData(temp,1))
break;
Application->Hint="通信失败!请检查线路";
Application->ShowHint=true;
MessageBeep(0);
}
StopCom();
return;
}
}
LongBuff:
if(BufferLength>=5)
{
char a=0xaa;
while(sBuff.Pos(AnsiString(a))!=0)
sBuff.Delete(sBuff.Pos(AnsiString(a)),1);
wsprintf(buff,sBuff.c_str());
if((buff[3]==0x0d)||(buff[3]==0x0c))
{
if(buff[3]==0x0c)
{
bWarning=true;
pbWar->Show();
}
else
bWarning=false;
char a=0xbb;
char temp[10];
temp[0]=a;
for(int i=0;i<10;i++)
{
if(CommTV->WriteCommData(temp,1))
break;
Application->Hint="通信失败!请检查线路";
Application->ShowHint=true;
MessageBeep(0);
}
String temp1=sBuff;
RM=temp1.SubString(1,3);
a=0x0a;
while(RM.Pos(AnsiString(a))!=0)
{
String sChange=RM;
sChange.Delete(RM.Pos(AnsiString(a)),1);
sChange.Insert("0",RM.Pos(AnsiString(a)));
RM=sChange;
}
StartCom();
return;
}
if(buff[3]==0x0f)
{
char a=0xbb;
char temp[10];
temp[0]=a;
for(int i=0;i<10;i++)
{
if(CommTV->WriteCommData(temp,1))
break;
Application->Hint="通信失败!请检查线路";
Application->ShowHint=true;
MessageBeep(0);
}
StopCom();
}
}
}
//---------------------------------------------------------------------------
inline void __fastcall TFMain::StartCom()
{
if(DM->TimeSub->Enabled)
StopCom();
Lbegin
Time->Caption=Now().FormatString("yyyy-mm-dd hh:nn:ss");
if(Mainbegin
Time==Lbegin
Time->Caption)
return;
Mainbegin
Time=Lbegin
Time->Caption;
DM->TimeSub->Enabled=true;
if(!CD->QSearch->Active)
{
CD->QSearch->DatabaseName=DataPath;
CD->QSearch->SQL->Clear();
CD->QSearch->SQL->Add("SELECT DTH, CH,RM,ZT,GS");
CD->QSearch->SQL->Add("FROM 'Setdata.dbf' Setdata");
CD->QSearch->Open();
}
TLocateOptions Opts;
Opts.Clear();
Opts << loPartialKey;
if(CD->QSearch->Locate("RM",RM,Opts))
{
MainCH=CD->QSearch->FieldByName("CH")->AsString;
MainDTH=CD->QSearch->FieldByName("DTH")->AsString;
}
else
{
DM->TimeSub->Enabled=false;
return;
}
FMain->SetMyi(0);
if(bWarning)
DM->tWaring->Enabled=true;
FMain->Icon=ITray->Picture->Icon;
TrayMessage(NIM_MODIFY);
if(LY)
{
String Temp;
try{
Temp=StrToDateTime(FMain->Mainbegin
Time).FormatString("yyyy-mm-dd hhnnss");
}
catch(...)
{
MessageBeep(0);
}
String FileName=FMain->DataPath+"//"+Temp+".wav";
/*
if(FileExists(FileName))
{
Temp=Now().FormatString("yyyy-mm-dd hhnnss");
if (!Audio1->Recorder->Stop())
Memo1->Lines->Add(Audio1->ErrorMessage);
DeleteFile(FileName);
}*/
Audio1->Recorder->RecordToFile(FileName, 0,0);
StartRecClick(this);
}
LDTH->Caption=MainDTH;
LCH->Caption=MainCH;
DM->RM=RM;
}
inline void __fastcall TFMain::StopCom()
{
if(!DM->TimeSub->Enabled)
return;
DM->TimeSub->Enabled=false;
iMain->Cursor=crDefault;
SetCursorPos(iMain->ClientOrigin.x+15,iMain->ClientOrigin.y+10);
MainStopTime=Now().FormatString("yyyy-mm-dd hh:nn:ss");
FMain->Icon=Application->Icon;
TrayMessage(NIM_MODIFY);
if(pbWar->Visible)
{
pbWar->Hide();
FMain->pbWar->Canvas->Refresh();
DM->tWaring->Enabled=false;
}
String StopTime=FMain->MainStopTime;
MainTimeSub=FormatFloat("0",Time_Cmp(Mainbegin
Time.c_str(),StopTime.c_str()));
//**********************Stop
if (!Audio1->Recorder->Stop())
Memo1->Lines->Add(Audio1->ErrorMessage);
audiorecording=false;
ButtonStatus("Idle");
//*********************Stop
CD->QGetWrite->RequestLive=true;
if(!CD->QGetWrite->Active)
{
CD->QGetWrite->DatabaseName=FMain->DataPath;
CD->QGetWrite->SQL->Clear();
// CD->QGetWrite->SQL->Add("SELECT DTH, CH,BTIME, STIME,TIMESUB,WARNING");
CD->QGetWrite->SQL->Add("SELECT * ");
CD->QGetWrite->SQL->Add("FROM 'Getdata.DBF' Getdata");
CD->QGetWrite->Open();
}
try
{
CD->QGetWrite->Edit();
CD->QGetWrite->Edit();
CD->QGetWrite->Append();
CD->QGetWrite->FieldByName("DTH")->Value=FMain->MainDTH;
CD->QGetWrite->FieldByName("CH")->Value=MainCH;
CD->QGetWrite->FieldByName("BTIME")->Value=Mainbegin
Time;
CD->QGetWrite->FieldByName("STIME")->Value=StopTime;
CD->QGetWrite->FieldByName("TIMESUB")->Value=MainTimeSub.ToInt();
if(bWarning)
CD->QGetWrite->FieldByName("WARNING")->Value="报警";
else
CD->QGetWrite->FieldByName("WARNING")->Value="正常";
CD->QGetWrite->Post();
}
catch(...)
{
Application->Hint="存盘错误!";
MainHint="存盘错误!";
Application->ShowHint=true;
CD->QGetWrite->Cancel();
MessageBeep(0);
}
if(Application->Hint!="存盘错误!")
MainHint="";
//StopPlayClick(this);
if(LY)
{
static int iTempCount=0;
REFileName->Lines->Add(Mainbegin
Time);
iTempCount++;
if(iTempCount>=20)
{
RE->Lines->Clear();
iTempCount=0;
char *lpRootPathName="d://";
String sDevice=FMain->DataPath;
sprintf(lpRootPathName,"%c://",sDevice[1]);
DWORD lpSectorsPerCluster;
DWORD lpBytesPerSector;
DWORD lpNumberOfFreeClusters;
DWORD lpTotalNumberOfClusters;
GetDiskFreeSpace(lpRootPathName,&amp;lpSectorsPerCluster,&amp;lpBytesPerSector,&amp;lpNumberOfFreeClusters,&amp;lpTotalNumberOfClusters );
if(lpNumberOfFreeClusters * lpBytesPerSector * lpSectorsPerCluster/(1024.0*1024.0)<=eDiskFree->Text.ToInt())
{
TDeleteMp3 *DelMp3;
DelMp3=new TDeleteMp3(true);
DelMp3->Priority = tpLower;
// set the priority lower than normal
DelMp3->Resume();
// now start the thread running
iTempCount=0;
}
}
}
}
/*
void __fastcall TFMain::OnMSGStop(TMessage &amp;
Message)
{
if(!DM->TimeSub->Enabled)
return;
MainStopTime=Now().FormatString("yyyy-mm-dd hh:nn:ss");
DM->TimeSub->Enabled=false;
FMain->Icon=Application->Icon;
TrayMessage(NIM_MODIFY);
//StopPlayClick(this);
//**********************Stop
if (!Audio1->Recorder->Stop())
Memo1->Lines->Add(Audio1->ErrorMessage);
audiorecording=false;
ButtonStatus("Idle");
//*********************Stop
String StopTime=FMain->MainStopTime;
MainTimeSub=FormatFloat("0",Time_Cmp(Mainbegin
Time.c_str(),StopTime.c_str()));
if(!CD->QGetWrite->Active)
{
CD->QGetWrite->DatabaseName=FMain->DataPath;
CD->QGetWrite->SQL->Clear();
CD->QGetWrite->SQL->Add("SELECT DTH, CH,BTIME, STIME,TIMESUB,WARNING");
CD->QGetWrite->SQL->Add("FROM 'Getdata.DBF' Getdata");
CD->QGetWrite->Open();
}
try
{
CD->QGetWrite->Edit();
CD->QGetWrite->Append();
CD->QGetWrite->FieldByName("DTH")->Value=FMain->MainDTH;
CD->QGetWrite->FieldByName("CH")->Value=MainCH;
CD->QGetWrite->FieldByName("BTIME")->Value=Mainbegin
Time;
CD->QGetWrite->FieldByName("STIME")->Value=StopTime;
CD->QGetWrite->FieldByName("TIMESUB")->Value=MainTimeSub.ToInt();
if(bWarning)
CD->QGetWrite->FieldByName("WARNING")->Value="报警";
else
CD->QGetWrite->FieldByName("WARNING")->Value="正常";
CD->QGetWrite->Post();
}
catch(...)
{
Application->Hint="存盘错误!";
Application->ShowHint=true;
CD->QGetWrite->Cancel();
MessageBeep(0);
}
if(LY)
REFileName->Lines->Add(Mainbegin
Time);
} */
/*
void __fastcall TFMain::OnMSGStart(TMessage &amp;
Message)
{
Lbegin
Time->Caption=Now().FormatString("yyyy-mm-dd hh:nn:ss");
if(Mainbegin
Time==Lbegin
Time->Caption)
return;
if(DM->TimeSub->Enabled)
Perform(MSGSTOP,0,0);
FMain->SetMyi(0);
/*
if(bWarning)
{
if(Mainbegin
Time==Lbegin
Time->Caption)
{
return;
//************
String temp=Lbegin
Time->Caption;
Lbegin
Time->Caption=temp.SubString(0,temp.Length()-1)+AnsiString(temp.SubString(temp.Length(),1).ToInt()+1);
if (!Audio1->Recorder->Stop())
Memo1->Lines->Add(Audio1->ErrorMessage);
DeleteFile(FMain->DataPath+"//"+Lbegin
Time->Caption+".wav");
//*******
}
} */
/*
Mainbegin
Time=Lbegin
Time->Caption;
DM->TimeSub->Enabled=true;
FMain->Icon=ITray->Picture->Icon;
TrayMessage(NIM_MODIFY);
if(LY)
{
// RecFileClick(this);
// if(SaveDialog1->Execute())
String Temp;
try{
Temp=StrToDateTime(FMain->Mainbegin
Time).FormatString("yyyy-mm-dd hhnnss");
//Temp=Now().FormatString("yyyy-mm-dd hhnnss");
}
catch(...)
{
//Application->MessageBox("文件不存在","警告",0);
MessageBeep(0);
}
String FileName=FMain->DataPath+"//"+Temp+".wav";
/*
if(FileExists(FileName))
{
Temp=Now().FormatString("yyyy-mm-dd hhnnss");
if (!Audio1->Recorder->Stop())
Memo1->Lines->Add(Audio1->ErrorMessage);
DeleteFile(FileName);
}*/
//Audio1->Recorder->RecordToFile(FileName, 0,0);
//Memo1->Lines->Add("Recording to file : " + ExtractFileName(FileName));
/*
if(Memo1->Lines->Count>50)
Memo1->Lines->Clear();
*/
/*
StartRecClick(this);
}
if(!CD->QSearch->Active)
{
CD->QSearch->DatabaseName=DataPath;
CD->QSearch->SQL->Clear();
CD->QSearch->SQL->Add("SELECT DTH, CH,RM,ZT,GS");
CD->QSearch->SQL->Add("FROM 'Setdata.dbf' Setdata");
CD->QSearch->Open();
}
TLocateOptions Opts;
Opts.Clear();
Opts << loPartialKey;
if(CD->QSearch->Locate("RM",RM,Opts))
{
MainCH=CD->QSearch->FieldByName("CH")->AsString;
MainDTH=CD->QSearch->FieldByName("DTH")->AsString;
}
else
{
MainCH="";
MainDTH="";
}
LDTH->Caption=MainDTH;
LCH->Caption=MainCH;
} */
void __fastcall TFMain::DBGrid1ColEnter(TObject *Sender)
{
EDTH->Text=CD->QSet->FieldByName("DTH")->AsString;
//Temp=Temp.SubString(Temp.Length()-2,3);
EP1CH->Text=CD->QSet->FieldByName("CH")->AsString;
MERM->Text=CD->QSet->FieldByName("RM")->AsString;
sTempRM=MERM->Text;
//MEP1Time->Text=CD->QSet->FieldByName("TIMES")->AsString;
}
//---------------------------------------------------------------------------
void __fastcall TFMain::HBLYClick(TObject *Sender)
{
String sTemp;
if(LY)
sTemp="真的要关闭录音";
else
sTemp="真的要打开录音";
int button=Application->MessageBox(sTemp.c_str(),"警告", MB_OKCANCEL +MB_DEFBUTTON1+MB_ICONQUESTION);
if (button == IDCANCEL)
return;
if(FileExists(FMain->DataPath+"//PassWord.dat"))
{
if(mrOk!=FPass->ShowModal())
return;
}
LY=!LY;
if(LY)
{
HBLY->Hint="录音器处于打开状态";
HBLY->FaceColor=(TColor)0x000080FF;
}
else
{
HBLY->Hint="录音器处于关闭状态";
HBLY->FaceColor=clGray;
if(audiorecording)
{
if (!Audio1->Recorder->Stop())
Memo1->Lines->Add(Audio1->ErrorMessage);
audiorecording=false;
ButtonStatus("Idle");
REFileName->Lines->Add(Mainbegin
Time);
}
}
MessageBeep(0);
}
//---------------------------------------------------------------------------
 
goddy
你是什么意识,一点说明都没有,另外你这是串口通讯吗?
 
已经解决了!
 
后退
顶部