VB開發的串口與三菱FX PLC通訊源碼
vb源程序如下:
- Private Sub fraComm2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
- lblTopic = "設置連接PLC的通信端口以及PLC的站號(應與D8121相同)"
- End Sub
- Private Sub Frame1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
- lblTopic = "實時監控/設置PLC的XYMTCD設備值"
- End Sub
- Private Sub OptionD_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
- lblTopic = "數據以十進制方式返回或設置"
- End Sub
- Private Sub OptionH_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
- lblTopic = "數據以十六進制方式返回或設置"
- End Sub
- Private Sub SETRST_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
- lblTopic = "對位元件(X,Y,M,T,C)線圈置位與復位"
- End Sub
- Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
- SETRST.Enabled = False
- start = False
- fraComm1.Visible = True
- fraComm2.Visible = True
- End Sub
- Private Sub Text1_KeyPress(KeyAscii As Integer)
- char = Chr(KeyAscii)
- setaddr = Text1.Text
- If Len(setaddr) > 1 Then: setad = Right(setaddr, Len(setaddr) - 1)
- KeyAscii = Asc(UCase(char)) '轉換為大寫
-
- If KeyAscii = 13 Then '按回車鍵
- Device = Left(setaddr, 1)
- Text1.SelStart = 0
- Text1.SelLength = Len(setaddr)
- Text2.Enabled = True
- If (Device = "X" Or Device = "Y" And Oct(Val("&o" + setad)) = setad And Val(setad) < 178) Or (Device = "M" And CStr(Val(setad)) = setad And (Val(setad) < 1536 Or Val(setad) > 7999 And Val(setad) < 8256)) Then
- DevAdd = Right(("0000" + setad), 4)
- DevDat = cboStation.Text + "FFBR0" + Device + DevAdd + "01"
- DevType = "XYM"
- Text2.Enabled = False
- SETRST.Enabled = True
- start = True
- End If
- If Device = "D" And CStr(Val(setad)) = setad And (Val(setad) < 1000 Or Val(setad) > 7999 And Val(setad) < 8256) Then
- DevAdd = Right(("0000" + setad), 4)
- If Check1.Value = 0 Then
- DevDat = cboStation.Text + "FFWR0" + Device + DevAdd + "01" '使用WR命令讀16bit數據
- DevType = "D"
- Else
- DevDat = cboStation.Text + "FFWR0" + Device + DevAdd + "02" '使用WR命令讀32bit數據
- DevType = "2D"
- End If
- SETRST.Enabled = False
- start = True
- End If
- If Device = "T" And CStr(Val(setad)) = setad And Val(setad) < 256 Then
- DevAdd = Right(("000" + setad), 3)
- DevDatTC = cboStation.Text + "FFBR0" + Device + "S" + DevAdd + "01" 'T的線圈狀態
- DevDat = cboStation.Text + "FFWR0" + Device + "N" + DevAdd + "01" 'T的數據值
- DevType = "D"
- SETRST.Enabled = True
- start = True
- End If
- If Device = "C" And CStr(Val(setad)) = setad And Val(setad) < 256 Then
- DevAdd = Right(("000" + setad), 3)
- DevDatTC = cboStation.Text + "FFBR0" + Device + "S" + DevAdd + "01" 'C的線圈狀態
- If Val(setad) > 199 Then
- DevDat = cboStation.Text + "FFWR0" + Device + "N" + DevAdd + "01" 'C200以上的數據值
- DevType = "2D"
- Else
- DevDat = cboStation.Text + "FFWR0" + Device + "N" + DevAdd + "01" 'C200以下的數據值
- DevType = "D"
- End If
- SETRST.Enabled = True
- start = True
- End If
- fraComm1.Visible = False
- fraComm2.Visible = False
- Else
- start = False
- fraComm1.Visible = True
- fraComm2.Visible = True
- End If
- End Sub
-
- Private Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
- lblTopic = "輸入要監控或設置的地址回車確定,如D0,T10,Y7等"
- End Sub
- Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
- If KeyCode <> 13 Then
- start = False
- End If
- End Sub
- Private Sub Text2_KeyPress(KeyAscii As Integer)
- char = Chr(KeyAscii)
- KeyAscii = Asc(UCase(char))
- If Device = "D" And CStr(Val(setad)) = setad And Val(setad) < 1000 Then
- start = False
- End If
- If (Device = "T" Or Device = "C") And CStr(Val(setad)) = setad And Val(setad) < 256 Then
- start = False
- End If
- If KeyAscii = 13 Then
- If Val(DevAdd) > 7999 Then
- If MsgBox("改變系統參數可能對系統造成破壞,是否寫入?", vbOKCancel + vbCritical) = vbCancel Then Exit Sub
- ElseIf MsgBox("改變當前值可能對運行造成危險,是否寫入?", vbOKCancel + vbExclamation) = vbCancel Then Exit Sub
- End If
- If Device = "D" And Check1.Value = 0 Then
- DevAdd = Right(("0000" + setad), 4)
- ElseIf Device = "D" And Check1.Value = 1 Then
- DevAdd = Right(("0000" + setad), 4)
- Else
- DevAdd = Right(("000" + setad), 3)
- End If
- If OptionD.Value Then '十進制方式
- If Device = "C" And Val(setad) > 199 Then 'C200以上寫入
- If Val(Text2.Text) > 2847483647# Then: GoTo this
- DevDat1 = cboStation.Text + "FFWW0" + Device + "N" + DevAdd + "01" + Right("00000000" + Hex(Val(Text2.Text)), 8)
- ElseIf Device = "D" And Check1.Value = 1 Then '雙字節D寫入
- If Val(Text2.Text) > 2847483647# Then: GoTo this
- DevDat1 = Right("00000000" + Hex(Val(Text2.Text)), 8)
- DevDat1 = Right(DevDat1, 4) + Left(DevDat1, 4)
- DevDat1 = cboStation.Text + "FFWW0" + Device + DevAdd + "02" + DevDat1
- Else
- If Val(Text2.Text) > 32767 Then: GoTo this
- If Device = "D" Then '單字節D寫入
- DevDat1 = cboStation.Text + "FFWW0" + Device + DevAdd + "01" + Right("0000" + Hex(Val(Text2.Text)), 4)
- Else 'C200以下寫入
- DevDat1 = cboStation.Text + "FFWW0" + Device + "N" + DevAdd + "01" + Right("0000" + Hex(Val(Text2.Text)), 4)
- End If
- End If
- Else '十六進制方式
- If Device = "C" And Val(setad) > 199 Then 'C200以上寫入
- If Val("&H" + Text2.Text) > 2847483647# Then: GoTo this
- DevDat1 = cboStation.Text + "FFWW0" + Device + "N" + DevAdd + "01" + Right("00000000" + Text2.Text, 8)
- ElseIf Device = "D" And Check1.Value = 1 Then '雙字節D寫入
- If Val("&H" + Text2.Text) > 2847483647# Then: GoTo this
- DevDat1 = Right("00000000" + Text2.Text, 8)
- DevDat1 = Right(DevDat1, 4) + Left(DevDat1, 4)
- DevDat1 = cboStation.Text + "FFWW0" + Device + DevAdd + "02" + DevDat1
- Else
- If Val("&H" + Text2.Text) > 32767 Then: GoTo this
- If Device = "D" Then '單字節D寫入
- DevDat1 = cboStation.Text + "FFWW0" + Device + DevAdd + "01" + Right("0000" + Text2.Text, 4)
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
VB開發的串口與三菱FX PLC通訊源碼.rar
(19.54 KB, 下載次數: 140)
2017-9-13 10:17 上傳
點擊文件名下載附件
VB開發的串口與三菱FX PLC通訊源碼
|