[問題] RS485通訊排隊的問題...

看板C_and_CPP (C/C++)作者 (伊薩凱<L>)時間10年前 (2015/05/28 22:37), 編輯推噓1(102)
留言3則, 3人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) BCB 問題(Question): 小弟我使用BCB/VCL來寫RS485的通訊,使設備動作 使用THREAD來寫RS485的命令輸出。每次命令間隔為20ms, 如果把所有的情況都排入,會浪費沒有被觸發的條件時間像下程式碼中 像是manual==false && auto==true時,count8,10,12的時間會被浪費掉 或只有semiauto==true時,count8,10,12,14,16,18的時間會被浪費掉 要怎麼寫才可以把只有被觸發的條件時間向前排呢? 像是 manual==false && auto==true時, auto條件內,各設備的命時間cout14,16,18 可以向前排隊變成cout8,10,12 因為其實需要被觸發的條件很多,設備也很多, 要怎麼寫才可以簡單的讓只有被觸發的命令,時間向前且順序排好發出呢!? 程式碼(Code):(請善用置底文網頁, 記得排版) void __fastcall Execute(void) while (!Terminated) { int count=0; RS485Command(); Sleep(10); //10 ms } void __fastcall RS485Command() { if (count==0) 設備A() //polling 設備的反應時間約15ms if (count==2) 設備B() //polling if (count==4) 設備C() //polling if (count==6) 設備D() //polling if (manual==true) { //按鈕觸發且時間不定 if (count==8) 設備A() //吃飯 if (count==10) 設備B() //睡覺 if (count==12) 設備C() //打東東(此函數C()"內"有manual=false;) } if (auto==true) { //按鈕觸發且時間不定 if (count==14) 設備B() //吃飯 if (count==16) 設備C() //睡覺 if (count==18) 設備D() //打東東(此函數D()"內"有auto=false;) } if (semiauto==true) { //按鈕觸發且時間不定 if (count==20) 設備A() //打東東 if (count==22) 設備B() //吃飯 if (count==24) 設備C() //睡覺 if (count==26) 設備E() //打東東(此函數E()"內"有semiauto=false;) } count++; if (count>50) count=0; } 補充說明(Supplement): 感謝回答 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.115.68.237 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1432823868.A.717.html

05/29 02:11, , 1F
狀態機
05/29 02:11, 1F

05/29 12:45, , 2F
狀態機+1 而且你狀態直接用define或甚麼命名 別用count
05/29 12:45, 2F

05/29 12:58, , 3F
OK...感謝
05/29 12:58, 3F
文章代碼(AID): #1LPoWySN (C_and_CPP)
文章代碼(AID): #1LPoWySN (C_and_CPP)