<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          關(guān) 閉

          新聞中心

          EEPW首頁 > 工控自動化 > 設(shè)計(jì)應(yīng)用 > 基于VB的PLC與計(jì)算機(jī)間串行通信實(shí)現(xiàn)

          基于VB的PLC與計(jì)算機(jī)間串行通信實(shí)現(xiàn)

          作者: 時(shí)間:2010-11-05 來源:網(wǎng)絡(luò) 收藏

           ?。猧nput:將對方傳送至輸入緩沖區(qū)的字符讀入到程序。

           ?。猳utput:將字符寫入輸出緩沖區(qū)。

           ?。猧nbuffercount:傳回接收緩沖區(qū)中的字符數(shù)。

           ?。猳utbuffercount:傳回輸出緩沖區(qū)中的字符數(shù)。

           ?。猧nputlen:設(shè)定端口讀入字符串的長度。

            *inputmode:設(shè)定接收數(shù)據(jù)的方式。

           ?。猺threshold:設(shè)定引發(fā)接收事件的字符數(shù)。

           ?。猚ommevent:傳回ONcomm事件發(fā)生時(shí)的數(shù)值碼

           ?。猳ncomm事件:無論是錯(cuò)誤或事件發(fā)生,都會觸發(fā)此事件。

           ?。?) 控件參數(shù)的初始化

            初始化程序如下:

            mscomm.comport=2 `使用串口com2

            mscomm.settings=9600, e, 7, 2 `波特率9600,偶校驗(yàn),7位數(shù)據(jù)位,2位停止位

            mscomm.portopen=true `打開通信端口,準(zhǔn)備通信

           ?。?) 計(jì)算校驗(yàn)碼fcs,計(jì)算fcs的vb自定義函數(shù)如下:

            function fcs(byval inputSTr as string) as string

            dim slen, i, xorresult as integer

            dim tempfes as string

            slen=len(inputstr) `求輸入字符串長度

            xorresult = 0

            for i = 1 to slen

            xorresult = xorresult xor asc(mid$(inputstr, i, 1)) `按位異或

            next i

            tempfes=hex$(xorresult) `轉(zhuǎn)化為16進(jìn)制

            if len(tempfes)=1then tempfes =“0”+tempfes

            fcs = tempfes

            end function

           ?。?) 通信程序

            主要是一個(gè)自定義函數(shù)。

            function readdata(byval inputstr as string, byval num as integer) as string

            dim outputstr as string

            dim instring as string

            dim returnstr as string

            dim endstring as string

            dim fcsstring as string

            dim returnfcsstring as string)

            mscomm.inbuffercount=0

            utputstr=inputstr+fcs(inputstr)+“*” `給出命令幀

            mscomm.output=outputstr+chr$(13) `向傳送命令幀

            do

            doevents

            loop while mscomm.inbuffercount 15

            instring=mscomm.input `獲取的響應(yīng)幀

           ?。嘟Y(jié)束碼判斷

            endstring = mid$(instring, len(instring) -

            num- 5, 2)



          評論


          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();