无码精品人妻一区二区三区入口-无码精品人妻一区二区三区夜夜嗨-无码精品人妻一区二区三区颖A片-无码精品日本一区二区桃花岛-无码精品网站-无码精品一

趙云龍
級別: 正式會(huì)員
精華主題: 0
發(fā)帖數(shù)量: 6 個(gè)
工控威望: 53 點(diǎn)
下載積分: 256 分
在線時(shí)間: 8(小時(shí))
注冊時(shí)間: 2023-11-23
最后登錄: 2024-12-03
查看趙云龍的 主題 / 回貼
樓主  發(fā)表于: 2024-09-17 16:49
Option Explicit

Private Sub Form_Load()

  '設(shè)置本地任意可用端口,這樣系統(tǒng)會(huì)自動(dòng)分配一個(gè)未被占用的端口

  Winsock1.LocalPort = 0

  Winsock2.LocalPort = 0

  '設(shè)置通信協(xié)議為 TCP 協(xié)議

  Winsock1.Protocol = sckTCPProtocol

  Winsock2.Protocol = sckTCPProtocol

  '開始監(jiān)聽,等待客戶端連接

  Winsock1.Listen

  Winsock2.Listen

  '初始化PLC地址,IP,端口數(shù)據(jù)

  Open App.Path & "\data\add.ini" For Binary As #1

  Add = StrConv(InputB$(LOF(1), 1), vbUnicode)

  Close #1

  Open App.Path & "\data\ip.ini" For Binary As #1

  ip = StrConv(InputB$(LOF(1), 1), vbUnicode)

  Close #1

  Open App.Path & "\data\port.ini" For Binary As #1

  port = StrConv(InputB$(LOF(1), 1), vbUnicode)

  Close #1

  reg.Text = "0"

  high.Text = "0"

  low.Text = "0"

End Sub



Private Sub SendData_Click()

  '如果 Winsock 處于已連接狀態(tài)

  If Winsock1.State = sckConnected Then

    '構(gòu)造 Modbus TCP 請求數(shù)據(jù)

    Dim PLC_Add As Long

    Dim dataToSend As Integer

    Dim dataToSend1 As Integer

    Dim dataToSend2 As Integer

    'Add PLC起始地址,reg:寄存器地址,high/low高低字節(jié)位(數(shù)據(jù))。

    PLC_Add = Val(Add.Text)

    To_reg = Val(reg.Text)

    To_high = Val(high.Text)

    To_low = Val(low.Text)

    'MODBUSTCP報(bào)文

     Dim request As String

    request = Chr(&H0) & Chr(&H1) & Chr(&H0) & Chr(&H0) & Chr(&H0) & Chr(&H6) & Chr(&H1) & Chr(&H6) & Chr(PLC_Add - 40001) & Chr(To_reg) & Chr(To_high) & Chr(To_low)

      '發(fā)送構(gòu)造好的請求數(shù)據(jù)給 PLC

      Winsock1.SendData request

    Exit Sub

  

  Else

    '如果未連接到 PLC,彈出消息提示

    MsgBox "Not connected to PLC."

  End If

  

End Sub


Private Sub Timer1_Timer()

  '定時(shí)器事件,用于周期性檢查連接狀態(tài)并更新界面

  If Winsock1.State = sckConnected Then

    '如果連接成功,將標(biāo)簽的背景色設(shè)置為綠色(十六進(jìn)制顏色值 &HC000&)

    connection.BackColor = &HC000&

  Else

    '如果未連接,將標(biāo)簽的背景色設(shè)置為紅色(十六進(jìn)制顏色值 &HFF&)

    connection.BackColor = &HFF&

  End If

    

End Sub



Private Sub Timer2_Timer()

  '定時(shí)器事件,用于周期性檢查連接狀態(tài)并重新連接

  If connection.BackColor = &HFF& Then

  '檢查 Winsock 的狀態(tài),如果不是已關(guān)閉狀態(tài)

  If Winsock1.State <> sckClosed Then

    '關(guān)閉當(dāng)前連接,以便重新連接到 PLC

    Winsock1.Close

    '確認(rèn)連接IP地址及端口

    Timer3.Interval = 1

  End If

  If Winsock2.State <> sckClosed Then

    '關(guān)閉當(dāng)前連接,以便重新連接到 PLC

    Winsock2.Close

    '確認(rèn)連接IP地址及端口

    Timer3.Interval = 1

  End If

  

    '變量

    Dim plc_ip As String

    Dim plc_port As Integer

    '讀取IP及端口參數(shù)

    plc_ip = ip.Text

    plc_port = port.Text

    '連接到指定的 PLC IP 地址和端口號(hào),這里需替換為實(shí)際的 PLC IP 和端口

    Winsock1.Connect plc_ip, plc_port

    Winsock2.Connect plc_ip, plc_port

    '停止IP地址及端口確認(rèn)

    Timer3.Interval = 0

  End If

End Sub



Private Sub Command1_Click()

    '打開通訊設(shè)置窗口

    Form2.Show

End Sub



Private Sub ConnectToPLC_Click()

  '判斷通訊是否啟動(dòng)連接

  If Timer2.Interval = 0 Then

     '開始連接

    Timer2.Interval = 1

  ElseIf Timer2.Interval = 1 Then

    '判斷通訊是否啟動(dòng)連接

    If Winsock1.State <> sckClosed Then

    '停止連接

    Timer2.Interval = 0

    '斷開連接

    Winsock1.Close

    Winsock2.Close

    

    End If

    

  End If



End Sub



Private Sub Timer3_Timer()

    '讀取起始地址

    Open App.Path & "\data\add.ini" For Binary As #1

    '更新起始地址

    Add = StrConv(InputB$(LOF(1), 1), vbUnicode)

    Close #1

    '讀取IP地址

    Open App.Path & "\data\ip.ini" For Binary As #1

    '更新IP地址

    ip = StrConv(InputB$(LOF(1), 1), vbUnicode)

    Close #1

    '讀取端口

    Open App.Path & "\data\port.ini" For Binary As #1

    '更新端口

    port = StrConv(InputB$(LOF(1), 1), vbUnicode)

    Close #1

    '停止更新

    Timer3.Interval = 0

End Sub





Private Sub TCP0_Click(Index As Integer)

  '對齊寄存器地址

  If reg.Text <> 0 Then

    reg.Text = 0

  End If

  '開關(guān)量轉(zhuǎn)換

  If reg.Text = 0 Then

    If low.Text = 0 Then

      low.Text = 1

      SendData_Click

    ElseIf low.Text = 1 Then

      low.Text = 0

      SendData_Click

    End If

  End If



End Sub



Private Sub TCP1_Click(Index As Integer)

  '對齊寄存器地址

  If reg.Text <> 1 Then

    reg.Text = 1

  End If

  '開關(guān)量轉(zhuǎn)換

  If reg.Text = 1 Then

    If low.Text = 0 Then

      low.Text = 1

      SendData_Click

    ElseIf low.Text = 1 Then

      low.Text = 0

      SendData_Click

    End If

  End If



End Sub



Private Sub TCP2_Click(Index As Integer)

  '對齊寄存器地址

  If reg.Text <> 2 Then

    reg.Text = 2

  End If

  '開關(guān)量轉(zhuǎn)換

  If reg.Text = 2 Then

    If low.Text = 0 Then

      low.Text = 1

      SendData_Click

    ElseIf low.Text = 1 Then

      low.Text = 0

      SendData_Click

    End If

  End If



End Sub



Private Sub TCP3_Click(Index As Integer)

  '對齊寄存器地址

  If reg.Text <> 3 Then

    reg.Text = 3

  End If

  '開關(guān)量轉(zhuǎn)換

  If reg.Text = 3 Then

    If low.Text = 0 Then

      low.Text = 1

      SendData_Click

    ElseIf low.Text = 1 Then

      low.Text = 0

      SendData_Click

    End If

  End If



End Sub



Private Sub TCP4_Click(Index As Integer)

  '對齊寄存器地址

  If reg.Text <> 4 Then

    reg.Text = 4

  End If

  '開關(guān)量轉(zhuǎn)換

  If reg.Text = 4 Then

    If low.Text = 0 Then

      low.Text = 1

      SendData_Click

    ElseIf low.Text = 1 Then

      low.Text = 0

      SendData_Click

    End If

  End If



End Sub



Private Sub TCP5_Click(Index As Integer)

  '對齊寄存器地址

  If reg.Text <> 5 Then

    reg.Text = 5

  End If

  '開關(guān)量轉(zhuǎn)換

  If reg.Text = 5 Then

    If low.Text = 0 Then

      low.Text = 1

      SendData_Click

    ElseIf low.Text = 1 Then

      low.Text = 0

      SendData_Click

    End If

  End If


End Sub



Private Sub end_Click()

End

End Sub



主站蜘蛛池模板: 18禁男女污污污午夜网站免费 | 国产特级全黄一级毛片不卡 | 久久精品综合一区二区三区 | 国产精品日韩在线播放 | 国产白袜脚足j棉袜在线观看 | 国产精品一区二区日韩91 | 成人国产AV精品久久久久 | 91大神国内精品免费观看 | 国产AV亚洲一区精午夜麻豆 | 老熟妇仑乱视频一区二区 | 水蜜桃传媒B站 | 欧美日韩国产专区 | 日本一卡二卡三卡四卡无卡免费播放 | 国内精品久久 | 成人综合网站在线 | 97人妻久久久精品系列A片 | 2025国产三级在线观看 | 免费观看a毛片一区二区不卡 | 女人18毛片a级毛片一区②区 | 久久久久久久久久毛片精品美女 | 国产成人综合在线 | 国内自拍小视频 | 色情成人韩国在线视频 | 国产精品白丝av嫩草影院 | 久久久精品宅男一区二区三区免费 | 91制片厂制作果冻传媒 | 五月丁香综合缴情六月 | 日韩精品无码一本二本三本 | 91久久:助你畅享无忧体验 | 97无码人妻视频在线 | 国产欧美一级纯黄色片 | 国产啪在线 | 欧美成人免费xxx大片 | 亚洲成人未满十八禁网站 | 精品久久人人妻人人澡人人爽 | 国产欧美精品午夜理论片在线播放 | 国产丰满眼镜女在线观看 | 欧美日韩一区二区三区色综合 | 精品人妻系列无码人妻免费 | 欧美在线sm | 日韩精品一区二区大桥未久 |