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ù)
在編寫基于串口的計算機工業(yè)測控時,通常需要由下位機向PC機發(fā)送數(shù)據(jù)以使PC機了解系統(tǒng)的測試數(shù)據(jù)或下位機的運行狀態(tài),并進而控制下位機的行為。利用Spcomm串口控件接收下位機發(fā)送的數(shù)據(jù)信息的示例代碼如下:
//事件驅(qū)動方式接收數(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)注意在不使用串口時應(yīng)及時關(guān)閉串口,釋放系統(tǒng)資源,否則可能會影響系統(tǒng)的其它應(yīng)用。關(guān)閉串口的代碼如下:
procedure TForm1.FormClose ( Sender TObj ect:var Action:TCIoseAction )
begin
comml.StopComm
end
標簽:
commflg
procedure
senddata
integer
上傳時間:
2014-01-26
上傳用戶:懶龍1988