You know,I'm John Grundy.
阿新 • • 發佈:2019-02-15
#Region "傳送>讀>雙字浮點" Public Function fun讀雙字浮點_MITSUBISHI_PLC_FN2N(ByVal strAdrRead As String) As String '---------------------------------------------------------------------------------------------------- '傳送報文>Chr(2) 00 00 00 00 Chr(3) YY '含 義>起始字元 命令碼 地址高 地址低 位陣列 結尾字元 校驗 '---------------------------------------------------------------------------------------------------- '返回報文>Chr(2) 00 00 00 00 Chr(3) 00 '含 義>起始字元 資料高 資料低 資料高 資料低 結尾字元 校驗 '---------------------------------------------------------------------------------------------------- On Error Resume Next If strAdrRead = Nothing Then Return Nothing '轉換地址 Dim strAdrFx2n As String = fun地址_MITSUBISHI_FN2N(strAdrRead) Dim fxCmd As String Dim fxReadCmd As String '報文 fxCmd = "0" + strAdrFx2n + "04" fxReadCmd = fxCmd + Chr(3) fxReadCmd = Chr(2) + fxReadCmd + funCheckSum(fxReadCmd) '清除緩衝區 Dim strReStr As String = Me.comRS232.ReadExisting() '發出報文 Me.comRS232.Write(fxReadCmd) '發出報文 '返回資料 Return fun讀雙字浮點返回_MITSUBISHI_PLC_FN2N(12, intTimeWait) End Function #End Region #Region "返回>讀>雙字浮點" Private Function fun讀雙字浮點返回_MITSUBISHI_PLC_FN2N(ByVal intStrLen As Integer, ByVal DT As Integer) As String 'On Error Resume Next Dim TT As Integer TT = Environment.TickCount() '取得系統毫秒數 '設定時間內結束迴圈 '接收數量達到後跌出迴圈 Do Until Environment.TickCount() - TT > DT System.Windows.Forms.Application.DoEvents() If Me.comRS232.BytesToRead >= intStrLen Then Exit Do ' Loop If Me.comRS232.BytesToRead <> intStrLen Then Return Nothing '提取資料 Dim strReData As String = Me.comRS232.ReadExisting() '校驗位對比 If Not strReData.Substring(10, 2) = funCheckSum(strReData.Substring(1, 9)) Then Return Nothing '---------------------------------------------------------------------------------------------------- '整理資料 Dim strIntD(3) As Byte strIntD(0) = Val("&H" & (strReData.Substring(1, 2))) strIntD(1) = Val("&H" & (strReData.Substring(3, 2))) strIntD(2) = Val("&H" & (strReData.Substring(5, 2))) strIntD(3) = Val("&H" & (strReData.Substring(7, 2))) '返回 Return BitConverter.ToSingle(strIntD, 0) End Function #End Region