線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2444
推到 Plurk!
推到 Facebook!

請教前輩,spcomm与plc通信中斷

尚未結案
82547013
一般會員


發表:1
回覆:1
積分:0
註冊:2005-11-12

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-12-02 12:44:39 IP:59.40.xxx.xxx 未訂閱
多謝長輩了,程式運行大約10多秒,就不再發送資料了,而且, 數據顯示有時也不是正確,先謝謝長輩們。 程式如下: var Harmonics: THarmonics; sbuf:array[1..8] of byte; rbuf:array[1..100] of byte; DateDecAu:array[1..100] of word; DateDecBu:array[1..100] of word; DateDecCu:array[1..100] of word; DateDecAi:array[1..100] of word; DateDecBi:array[1..100] of word; DateDecCi:array[1..100] of word; Signal:boolean; implementation uses Oscilloscope; var choice,readnum:Integer; {$R *.dfm} function hex(c:char):Integer ; var x:integer; begin if c=' ' then x:=0 else if (Ord(c)>=ord('0')) and (Ord(c)<=ord('9')) then x:=Ord(c)-Ord('0') else if (Ord(c)>=ord('a')) and (Ord(c)<=ord('f')) then x:=Ord(c)-Ord('a') 10 else if (Ord(c)>=ord('A')) and (Ord(c)<=ord('F')) then x:=Ord(c)-Ord('A') 10 else //輸入錯誤 x:=-1; Result:=x; end; function HexToInt(S:String): Integer; var tmpInt1,tmpInt2:Integer ; tmpInt3,tmpInt4:Integer ; begin tmpInt1:=hex(S[1]); tmpInt2:=hex(S[2]); tmpInt3:=hex(S[3]); tmpInt4:=hex(S[4]); Result:=tmpInt1*4096 tmpInt2*256 tmpInt3*16 tmpInt4; end; //發送資料自定義的發送過程 procedure senddata; var i:integer; commflg:boolean; begin commflg:=true; for i:=1 to 8 do begin if not Harmonics.comm1.writecommdata(@sbuf[i],1) then begin commflg:=false; break; end; if not commflg then messagedlg('發送失敗!',mterror,[mbyes],0); end; end; //發送資料初始化過程 { 01 03 02 00 00 32 C5 A7 :Ua 01 03 03 00 00 32 C4 5B :Ub 01 03 04 00 00 32 C5 2F :Uc 01 03 05 00 00 32 C4 D3 :Ia 01 03 06 00 00 32 C4 97 :Ib 01 03 07 00 00 32 C5 6B :Ic } procedure THarmonics.sendClick(Sender: TObject); begin //Ic sbuf[1]:=byte($01); //幀頭 sbuf[2]:=byte($03); //命令號 sbuf[3]:=byte($07); sbuf[4]:=byte($00); sbuf[5]:=byte($00); sbuf[6]:=byte($32); sbuf[7]:=byte($C5); sbuf[8]:=byte($6B); //幀尾 choice:=1;readnum:=2; senddata;//調用發送函數 end; //Ub procedure THarmonics.send2Click(Sender: TObject); begin sbuf[1]:=byte($01); sbuf[2]:=byte($03); sbuf[3]:=byte($03); sbuf[4]:=byte($00); sbuf[5]:=byte($00); sbuf[6]:=byte($32); sbuf[7]:=byte($C4); sbuf[8]:=byte($5B); choice:=2;readnum:=3; senddata;//調用發送函數 end; //Uc procedure THarmonics.send3Click(Sender: TObject); begin sbuf[1]:=byte($01); sbuf[2]:=byte($03); sbuf[3]:=byte($04); sbuf[4]:=byte($00); sbuf[5]:=byte($00); sbuf[6]:=byte($32); sbuf[7]:=byte($C5); sbuf[8]:=byte($2F); choice:=3;readnum:=4; senddata;//調用發送函數 end; //Ia procedure THarmonics.send4Click(Sender: TObject); begin sbuf[1]:=byte($01); sbuf[2]:=byte($03); sbuf[3]:=byte($05); sbuf[4]:=byte($00); sbuf[5]:=byte($00); sbuf[6]:=byte($32); sbuf[7]:=byte($C4); sbuf[8]:=byte($D3); choice:=4;readnum:=5; senddata;//調用發送函數 end; //Ib procedure THarmonics.send5Click(Sender: TObject); begin sbuf[1]:=byte($01); sbuf[2]:=byte($03); sbuf[3]:=byte($06); sbuf[4]:=byte($00); sbuf[5]:=byte($00); sbuf[6]:=byte($32); sbuf[7]:=byte($C4); sbuf[8]:=byte($97); choice:=5;readnum:=6; senddata;//調用發送函數 end; //Ua procedure THarmonics.send6Click(Sender: TObject); begin sbuf[1]:=byte($01); sbuf[2]:=byte($03); sbuf[3]:=byte($02); sbuf[4]:=byte($00); sbuf[5]:=byte($00); sbuf[6]:=byte($32); sbuf[7]:=byte($C5); sbuf[8]:=byte($A7); choice:=6;readnum:=1; senddata;//調用發送函數 end; //計時器在指定的事件內觸發該事件,實現資料的定時發送 procedure THarmonics.timer1Timer(Sender: TObject); begin if readnum=1 then begin signal:=false; send.Click; end else if readnum=2 then begin send2.Click; end else if readnum=3 then begin send3.Click; end else if readnum=4 then begin send4.Click; end else if readnum=5 then begin send5.Click; end else if readnum=6 then send6.Click; end; procedure THarmonics.FormShow(Sender: TObject); begin comm1.StartComm; choice:=0;readnum:=1; end; procedure THarmonics.FormClose(Sender: TObject; var Action: TCloseAction); begin comm1.StopComm; end; //接受數據 procedure THarmonics.Comm1ReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Word); var i,j:integer; s:string; begin // 從源到目標複製位元組 begin move(buffer^,pchar(@rbuf)^,bufferlength); i:=4;j:=1; while ((i<=bufferlength-5) and (j<=100)) do begin s:=inttohex(rbuf[i],2) inttohex(rbuf[i 1],2); if choice=6 then begin DateDecAu[j]:=HexToInt(s); end else if choice=2 then begin DateDecBu[j]:=HexToInt(s); end else if choice=3 then begin DateDecCu[j]:=HexToInt(s); end else if choice=4 then begin DateDecAi[j]:=HexToInt(s); end else if choice=5 then begin DateDecBi[j]:=HexToInt(s); end else if choice=1 then begin DateDecCi[j]:=HexToInt(s); end; i:=i 2;j:=j 1; end; end; // 資料顯示 if choice=6 then begin signal:=true; //Ua THDUa.text:=floattostr(DateDecAu[1]/100); Edit1.text:=floattostr(DateDecAu[3]/100); Edit2.text:=floattostr(DateDecAu[5]/100); Edit3.text:=floattostr(DateDecAu[7]/100); Edit4.text:=floattostr(DateDecAu[9]/100); Edit5.text:=floattostr(DateDecAu[11]/100); Edit6.text:=floattostr(DateDecAu[13]/100); Edit7.text:=floattostr(DateDecAu[15]/100); Edit8.text:=floattostr(DateDecAu[17]/100); Edit9.text:=floattostr(DateDecAu[19]/100); Edit10.text:=floattostr(DateDecAu[21]/100); Edit11.text:=floattostr(DateDecAu[23]/100); Edit12.text:=floattostr(DateDecAu[25]/100); Edit13.text:=floattostr(DateDecAu[27]/100); Edit14.text:=floattostr(DateDecAu[29]/100); Edit15.text:=floattostr(DateDecAu[31]/100); //Ub THDUb.text:=floattostr(DateDecBu[1]/100); Edit17.text:=floattostr(DateDecBu[3]/100); Edit18.text:=floattostr(DateDecBu[5]/100); Edit19.text:=floattostr(DateDecBu[7]/100); Edit20.text:=floattostr(DateDecBu[9]/100); Edit21.text:=floattostr(DateDecBu[11]/100); Edit22.text:=floattostr(DateDecBu[13]/100); Edit23.text:=floattostr(DateDecBu[15]/100); Edit24.text:=floattostr(DateDecBu[17]/100); Edit25.text:=floattostr(DateDecBu[19]/100); Edit26.text:=floattostr(DateDecBu[21]/100); Edit27.text:=floattostr(DateDecBu[23]/100); Edit28.text:=floattostr(DateDecBu[25]/100); Edit29.text:=floattostr(DateDecBu[27]/100); Edit30.text:=floattostr(DateDecBu[29]/100); Edit31.text:=floattostr(DateDecBu[31]/100); //Uc THDUc.text:=floattostr(DateDecCu[1]/100); Edit32.text:=floattostr(DateDecCu[3]/100); Edit33.text:=floattostr(DateDecCu[5]/100); Edit34.text:=floattostr(DateDecCu[7]/100); Edit35.text:=floattostr(DateDecCu[9]/100); Edit36.text:=floattostr(DateDecCu[11]/100); Edit37.text:=floattostr(DateDecCu[13]/100); Edit38.text:=floattostr(DateDecCu[15]/100); Edit39.text:=floattostr(DateDecCu[17]/100); Edit40.text:=floattostr(DateDecCu[19]/100); Edit41.text:=floattostr(DateDecCu[21]/100); Edit42.text:=floattostr(DateDecCu[23]/100); Edit43.text:=floattostr(DateDecCu[25]/100); Edit44.text:=floattostr(DateDecCu[27]/100); Edit45.text:=floattostr(DateDecCu[29]/100); Edit46.text:=floattostr(DateDecCu[31]/100); //Ia THDIa.text:=floattostr(DateDecAi[1]/100); Edit47.text:=floattostr(DateDecAi[3]/100); Edit48.text:=floattostr(DateDecAi[5]/100); Edit49.text:=floattostr(DateDecAi[7]/100); Edit50.text:=floattostr(DateDecAi[9]/100); Edit51.text:=floattostr(DateDecAi[11]/100); Edit52.text:=floattostr(DateDecAi[13]/100); Edit53.text:=floattostr(DateDecAi[15]/100); Edit54.text:=floattostr(DateDecAi[17]/100); Edit55.text:=floattostr(DateDecAi[19]/100); Edit56.text:=floattostr(DateDecAi[21]/100); Edit57.text:=floattostr(DateDecAi[23]/100); Edit58.text:=floattostr(DateDecAi[25]/100); Edit59.text:=floattostr(DateDecAi[27]/100); Edit60.text:=floattostr(DateDecAi[29]/100); Edit61.text:=floattostr(DateDecAi[31]/100); //Ib THDIb.text:=floattostr(DateDecBi[1]/100); Edit62.text:=floattostr(DateDecBi[3]/100); Edit63.text:=floattostr(DateDecBi[5]/100); Edit64.text:=floattostr(DateDecBi[7]/100); Edit65.text:=floattostr(DateDecBi[9]/100); Edit66.text:=floattostr(DateDecBi[11]/100); Edit67.text:=floattostr(DateDecBi[13]/100); Edit68.text:=floattostr(DateDecBi[15]/100); Edit69.text:=floattostr(DateDecBi[17]/100); Edit70.text:=floattostr(DateDecBi[19]/100); Edit71.text:=floattostr(DateDecBi[21]/100); Edit72.text:=floattostr(DateDecBi[23]/100); Edit73.text:=floattostr(DateDecBi[25]/100); Edit74.text:=floattostr(DateDecBi[27]/100); Edit75.text:=floattostr(DateDecBi[29]/100); Edit76.text:=floattostr(DateDecBi[31]/100); //Ic THDIc.text:=floattostr(DateDecCi[1]/100); Edit77.text:=floattostr(DateDecCi[3]/100); Edit78.text:=floattostr(DateDecCi[5]/100); Edit79.text:=floattostr(DateDecCi[7]/100); Edit80.text:=floattostr(DateDecCi[9]/100); Edit81.text:=floattostr(DateDecCi[11]/100); Edit82.text:=floattostr(DateDecCi[13]/100); Edit83.text:=floattostr(DateDecCi[15]/100); Edit84.text:=floattostr(DateDecCi[17]/100); Edit85.text:=floattostr(DateDecCi[19]/100); Edit86.text:=floattostr(DateDecCi[21]/100); Edit87.text:=floattostr(DateDecCi[23]/100); Edit88.text:=floattostr(DateDecCi[25]/100); Edit89.text:=floattostr(DateDecCi[27]/100); Edit90.text:=floattostr(DateDecCi[29]/100); Edit91.text:=floattostr(DateDecCi[31]/100); end; end; procedure THarmonics.Chart1Click(Sender: TObject); begin column.show; end; procedure THarmonics.Exit1Click(Sender: TObject); begin self.close; end; end.
DavidLo
高階會員


發表:17
回覆:225
積分:168
註冊:2004-07-21

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-12-05 09:39:25 IP:61.228.xxx.xxx 未訂閱
Tiemr rate為多少?
82547013
一般會員


發表:1
回覆:1
積分:0
註冊:2005-11-12

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-12-09 13:20:37 IP:59.40.xxx.xxx 未訂閱
DavidLo好,似乎與時間沒有關係的,我設定為500MS,其實設定1000ms也一樣的情況,另外,她是modbus協議,串列傳輸速率:9600。謝謝
DavidLo
高階會員


發表:17
回覆:225
積分:168
註冊:2004-07-21

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-12-09 17:21:30 IP:220.139.xxx.xxx 未訂閱
那麼請問Timer程序是否尚在執行中?也就是說PC還在送出資料嗎? RS232 TXD 有訊號變動?
HomeSound
中階會員


發表:44
回覆:178
積分:94
註冊:2002-08-31

發送簡訊給我
#5 引用回覆 回覆 發表時間:2016-08-25 01:35:35 IP:219.69.xxx.xxx 訂閱
您好:用spcomm元件於Win7若沒有透過windows update的話會有你說的狀態產生,
要找到一個Prolific -Other hardware-prolific USB-to-Serial comm Port更新包,是微軟的
不知是不是這個問題,之前我也發生過,註冊MSCOMM32.OXC後再更新,Win7 x86及64就都可正常收發了
以上僅供參考,如有誤謬拜訪指正.
------
--==多看.多學.多聽==--
系統時間:2018-01-21 13:02:40
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!