基于MODBUS协议和PLC的通讯

很多人在找MODBUS通讯代码,我把以前用VB.NET写的一些代码贴上来供参考

'判断电机是否在转动
    Public Sub WaitMotorStop()
        '判断电机是否在转动
        Dim str1 As String = "1"
        Do Until str1 = "0"
            Comm2PLC(RCS, Relay901, "")
            System.Threading.Thread.Sleep(50)
            Dim str2 As String = FormMain.RS232.ReadExisting().ToString()
            FormMain.RS232.DiscardInBuffer()
            FormMain.RS232.DiscardOutBuffer()
            '先高低位转换,再16进制转换为10进制。例如把78563412转换为12345678
            If str2.Length > 6 Then
                str1 = str2.Substring(6, 1)
            End If
        Loop
        Thread.Sleep(200)
    End Sub
    ‘----------------------------------------------------------------’
    '写入PLC
    Public Sub Comm2PLC(ByRef _Code As String, ByRef _Address As String, ByVal _Command As String)
        Dim str1 As String = _Code & _Address & _Command         '代码字+中间继电器+命令字
        Dim str2 As String = Calculte_Vertification_bit(str1)
        Dim str3 As String = str1 & str2
        FormMain.RS232.WriteLine(str3 + vbCr)
        FormMain.RS232.DiscardOutBuffer()
    End Sub

    ‘----------------------------------------------------------------
    '计算校验位
    Function Calculte_Vertification_bit(ByVal str)
        Dim str2 As String = Nothing
        For i As Integer = 1 To Len(str)
            Dim str1 As String = Asc(Mid(str, i, 1))
            str2 = str2 Xor str1
        Next
        Return Hex(str2)
    End Function
    
    ‘----------------------------------------------------------------
'运动坐标读取
    Private Sub ReadCoord()
        System.Threading.Thread.Sleep(10)
        FormMain.RS232.DiscardInBuffer()
        Comm2PLC(RD, Register1, "")
        Try
            System.Threading.Thread.Sleep(200)
            Dim str1 As String = FormMain.RS232.ReadExisting().ToString()
            '先高低位转换,再16进制转换为10进制。例如把78563412转换为12345678
            If str1.Length > 3 Then
                Dim str2 As String = str1.Substring(3, 1)
                If str2 = "$" Then
                    str2 = Nothing
                    For i As Integer = 0 To 3
                        str2 = str2 + str1.Substring(12 - i * 2, 2)
                    Next
                    str2 = CInt("&H" & str2)
                End If
                txt_OriginCoordX.Text = str2
            End If
        Catch
        End Try
    End Sub

 '-----------------------------------------
 '回原点
 Comm2PLC(WCS, Relay5, "1")           'Comm2PLC("%01#WCS", "R0005", "1")
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
松下通讯协议(PLC) RCS Read single point of contact information 读取单个触点的状态信息 WCS Write single point of contact information 写入单个触点的状态信息 RCP Read plural point of contact information 读取多个触点的状态信息 WCP Write plural point of contact information 写入多个触点的状态信息 RCC Read word unit of contact information 读取字单位的触点的状态信息 WCC Write word unit of contact information 写入字单位的触点的状态信息 SC Preset word unit in contact area 预置字单位的触点 RD Read data area 读取数据寄存器值 WD Write data area 写入数据寄存器值 SD Preset of data area 预置数据寄存器值 RS Read timer and counter set value area 读取定时器/计数器目标值 WS Write timer and counter set value area 写入定时器/计数器目标值 RK Read timer and counter elapsed value area 读取定时器/计数器经过值 WK Write timer and counter elapsed value area 写入定时器/计数器经过值 MC Registration and reset of monitor contact 登录及复位监控触点 MD Registration and reset of monitor data 登录及复位监控数据 MG Monitor execution 执行监控 RR Read system register 读取系统寄存器 WR Write system register 写入系统寄存器 RT Read Programmable Controller (PC) status 读取PLC 当前状态 RP Read program 读取程序 WP Write program 写入程序 RM Remote control (RUN/PROGRAM mode switching) 遥控(运行/编程模式切换) AB Transmission abort command 传输终止指令
The "RCS Commander" (RCS = Remote Control System) tool supports you in your day-to-day work with SINUMERIK solution line control systems. The "RCS Commander" tool makes it easy to manage files on your PC and on SINUMERIK solution line control systems. The "RCS Commander" tool offers the following functions: Exchanging data between the SINUMERIK and your PC. Generating and restoring an image from the CompactFlash Card of your control system to save data. Writing images to a CompactFlash Card. Loading commissioning files directly to the control system and from the control system to your PC. This function is used to support commissioning. Managing the control system's NC data. You can copy files directly from the PC to the NC and from the NC to the PC. Monitoring processes and remote control of the SINUMERIK via a remote control function. Saving the control system's current HMI view as a screen on your PC. This function is used to represent your control system and improves support in the event of an error. Editing the following user files in the OFFLINE mode on the PC: PLC alarm texts (oem_alarms_plc) Cycle alarm texts (oem_alarms_cycles) Part program messages (oem_partprogram_messages) EasyScreen files Tool management texts EasyExtend files (oem_aggregate) Maintenance planner files (oem_maintenance) Managing user files with the following options: Creating a project OFFLINE. Create a project, which automatically copied the files from the control. Copy individual files using project dialog to the control system or copy from the control system into the project. Delete user files using project dialog on the control

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值