procedure senddata
var
i:integer commflg : Boolean
begin
commflg:=true
for i:=1 to 8 do
begin
if not fcomm comml writecommdata(sendbutter,i) then
begin
Commflg=false
break
end
end
end
(4) 接收數(shù)據(jù)
在編寫基于串口的計(jì)算機(jī)工業(yè)測(cè)控時(shí),通常需要由下位機(jī)向PC機(jī)發(fā)送數(shù)據(jù)以使PC機(jī)了解系統(tǒng)的測(cè)試數(shù)據(jù)或下位機(jī)的運(yùn)行狀態(tài),并進(jìn)而控制下位機(jī)的行為。利用Spcomm串口控件接收下位機(jī)發(fā)送的數(shù)據(jù)信息的示例代碼如下:
//事件驅(qū)動(dòng)方式接收數(shù)據(jù)程序
procedure TForm1.CommlReceiveData(Sender:Tobject Buffer:Pointer bufferLength:Word)
var
ReceiveData:array of byte
begin
sleep(100) //等待100ms,保證接收到所有數(shù)據(jù)
move(buffef ,ReceiveData,bufferlength)
//將接收緩存區(qū)中的數(shù)據(jù)轉(zhuǎn)移到數(shù)組中
……
end
(5) 關(guān)閉串口
在系統(tǒng)開發(fā)中,應(yīng)注意在不使用串口時(shí)應(yīng)及時(shí)關(guān)閉串口,釋放系統(tǒng)資源,否則可能會(huì)影響系統(tǒng)的其它應(yīng)用。關(guān)閉串口的代碼如下:
procedure TForm1.FormClose ( Sender TObj ect:var Action:TCIoseAction )
begin
comml.StopComm
end
標(biāo)簽:
commflg
procedure
senddata
integer
上傳時(shí)間:
2014-01-26
上傳用戶:懶龍1988