關 閉

        新聞中心

        EEPW首頁 > 工控自動化 > 設計應用 > 基于VB的PLC與計算機間串行通信實現

        基于VB的PLC與計算機間串行通信實現

        作者: 時間:2010-11-05 來源:網絡 收藏

          *input:將對方傳送至輸入緩沖區的字符讀入到程序。

          *output:將字符寫入輸出緩沖區。

          *inbuffercount:傳回接收緩沖區中的字符數。

          *outbuffercount:傳回輸出緩沖區中的字符數。

          *inputlen:設定端口讀入字符串的長度。

          *inputmode:設定接收數據的方式。

          *rthreshold:設定引發接收事件的字符數。

          *commevent:傳回ONcomm事件發生時的數值碼

          *oncomm事件:無論是錯誤或事件發生,都會觸發此事件。

          (1) 控件參數的初始化

          初始化程序如下:

          mscomm.comport=2 `使用串口com2

          mscomm.settings=9600, e, 7, 2 `波特率9600,偶校驗,7位數據位,2位停止位

          mscomm.portopen=true `打開通信端口,準備通信

          (2) 計算校驗碼fcs,計算fcs的vb自定義函數如下:

          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) `轉化為16進制

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

          fcs = tempfes

          end function

          (3) 通信程序

          主要是一個自定義函數。

          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 `獲取的響應幀

          `結束碼判斷

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

          num- 5, 2)



        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 浪卡子县| 光山县| 福海县| 夹江县| 日土县| 德兴市| 柳河县| 铜梁县| 石台县| 张家界市| 新田县| 澳门| 台南市| 冕宁县| 卓尼县| 佛教| 贡山| 林口县| 土默特左旗| 文昌市| 兴宁市| 烟台市| 金寨县| 五原县| 华池县| 咸宁市| 寻乌县| 南投市| 垫江县| 大宁县| 米林县| 如皋市| 巴楚县| 阜宁县| 遵义县| 旬阳县| 朝阳市| 栖霞市| 秦安县| 两当县| 喀喇沁旗|