首页 上一页 1 2 下一页 尾页

用VB 写的上位机modbus rtu模式通讯源码,已在台达PLC上调试通过 点击:7738 | 回复:28



happinessman88

    
  • 精华:0帖
  • 求助:0帖
  • 帖子:36帖 | 122回
  • 年度积分:0
  • 历史总积分:789
  • 注册:2010年5月02日
发表于:2012-07-22 19:19:27
楼主

‘用VB 写的 modbus rtu模式通讯源码, 已在台达PLC上调试通过

Private Sub CmdOpen_Click()
On Error Resume Next

If (MSComm1.PortOpen) Then   ‘打开/关闭串口
MSComm1.PortOpen = False
Else
MSComm1.PortOpen = True
End If

If (MSComm1.PortOpen) Then
CmdOpen.Caption = "关闭串口"
Shape5.FillStyle = vbFSSolid
Else
CmdOpen.Caption = "打开串口"
Shape5.FillStyle = vbFSTransparent
End If

If Err Then
MsgBox Error$, 48, "错误码信息"
Exit Sub
End If
End Sub

Private Sub Combo1_Click()
MSComm1.CommPort = Combo1.ListIndex + 1
End Sub

Private Sub Combo2_Click()
Call Setting
End Sub

Private Sub Combo3_Click()
Call Setting
End Sub

Private Sub Combo4_Click()
Call Setting
End Sub

Private Sub Combo5_Click()
Call Setting
End Sub

Private Sub Command1_Click()
‘Shape1.FillStyle = vbFSSolid
Dim Y0_status As Byte
Dim Sendstr As String
Dim i As Integer, j As Integer
Sendstr = "01 01 05 00 00 10 "
HexSend (Sendstr)


Sleep (30)

HexSend (Sendstr)
End Sub

Private Function HexSend(Sendstr As String) As Integer

Dim outbuf() As Byte
Dim Temp(0) As Byte
Dim crc As String, Sendstrls As String
Dim sendlen As Integer
Dim i As Integer, j As Integer

If Sendstr = "" Then
MsgBox "发送数据不能为空!"
HexSend = 0
Exit Function
End If

Sendstrls = Trim(Sendstr)   ‘去掉空格
sendlen = Len(Sendstrls) + 1 ‘取长度
j = 0
ReDim outbuf(1 To sendlen \ 3) As Byte
For i = 1 To sendlen Step 3
j = j + 1
outbuf(j) = Val("&H" & CStr(Mid(Sendstrls, i, 2)))
Next i

crc = Crc16(outbuf)
ReDim Preserve outbuf(1 To (sendlen \ 3 + 2)) As Byte       ‘加上CRC校验码
outbuf(sendlen \ 3 + 1) = Val("&H" & CStr(Mid(crc, 1, 2)))
outbuf(sendlen \ 3 + 2) = Val("&H" & CStr(Mid(crc, 3, 2)))

For i = 1 To (sendlen \ 3 + 2)
Temp(0) = outbuf(i)
MSComm1.Output = Temp
Next i
For i = 1 To 2000
Next i
HexSend = 1
End Function

Private Function Setting()
MSComm1.Settings = CStr(Combo2.Text) & "," & CStr(Combo3.Text) & "," & CStr(Combo4.Text) & "," & CStr(Combo5.Text)
End Function

Private Sub Command2_Click()
‘If (MSComm1.RThreshold = 0) Then
‘MSComm1.RThreshold = 1

‘Else
‘MSComm1.RThreshold = 0
‘End If
Label11.Caption = "接收个数:" & CStr(ReceCount) & " " & "接收帧数:" & CStr(Framecount)
End Sub

Private Sub Form_Load()
Combo1.AddItem ("COM1")
Combo1.AddItem ("COM2")
Combo1.AddItem ("COM3")
Combo1.AddItem ("COM4")
Combo1.AddItem ("COM5")
Combo1.ListIndex = 0

Combo2.AddItem ("2400")
Combo2.AddItem ("4800")
Combo2.AddItem ("9600")
Combo2.AddItem ("11520")
Combo2.ListIndex = 0

Combo3.AddItem ("E")
Combo3.AddItem ("O")
Combo3.AddItem ("N")
Combo3.ListIndex = 2

Combo4.AddItem ("6")
Combo4.AddItem ("7")
Combo4.AddItem ("8")
Combo4.ListIndex = 2

Combo5.AddItem ("1")
Combo5.AddItem ("2")
Combo5.ListIndex = 0

ReceCount = 0
End Sub

Private Function Crc16(data() As Byte) As String
      Dim CRC16Lo As Byte, CRC16Hi As Byte      ‘CRC寄存器
      Dim CL As Byte, CH As Byte                ‘多项式码&HA001
      Dim CrcLo As String, CrcHi As String
      Dim SaveHi As Byte, SaveLo As Byte
      Dim i As Integer
      Dim Flag As Integer
      CRC16Lo = &HFF
      CRC16Hi = &HFF
      CL = &H1
      CH = &HA0
      For i = 1 To UBound(data)
        CRC16Lo = CRC16Lo Xor data(i) ‘每一个数据与CRC寄存器进行异或
        For Flag = 0 To 7
          SaveHi = CRC16Hi
          SaveLo = CRC16Lo
          CRC16Hi = CRC16Hi \ 2            ‘高位右移一位
          CRC16Lo = CRC16Lo \ 2            ‘低位右移一位
          If ((SaveHi And &H1) = &H1) Then ‘如果高位字节最后一位为1
            CRC16Lo = CRC16Lo Or &H80      ‘则低位字节右移后前面补1
          End If                           ‘否则自动补0
          If ((SaveLo And &H1) = &H1) Then ‘如果LSB为1,则与多项式码进行异或
            CRC16Hi = CRC16Hi Xor CH
            CRC16Lo = CRC16Lo Xor CL
          End If
        Next Flag
      Next i
      If Len(Hex(CRC16Hi)) = 1 Then
         CrcHi = "0" + Hex(CRC16Hi)
      Else
         CrcHi = Hex(CRC16Hi)
      End If
      If Len(Hex(CRC16Lo)) = 1 Then
         CrcLo = "0" + Hex(CRC16Lo)
      Else
         CrcLo = Hex(CRC16Lo)
      End If
         Crc16 = CrcLo & CrcHi
End Function

Private Sub MSComm1_OnComm()
Dim inpu() As Byte
Dim i As Integer
Dim tempstr As String, Strdata As String

Select Case MSComm1.CommEvent
Case comEvReceive    ‘接收事件

  tempstr = MSComm1.Input
  inpu() = tempstr
 
  Framecount = Framecount + 1                      ‘帧个数加1
  If (Framecount = 1) Then
  framepoint(Framecount) = UBound(inpu) + 1         ‘第一帧帧尾
  Else
  framepoint(Framecount) = framepoint(Framecount - 1) + UBound(inpu) + 1     ‘第二帧开始指针
  End If
 
 
  For i = 0 To UBound(inpu)                    ‘将字符转换为数组
  If (Len(Hex(inpu(i))) = 1) Then
  Strdata = Strdata & "0" & Hex(inpu(i)) & " "
  Else
  Strdata = Strdata & Hex(inpu(i)) & " "
  End If
  Next i

  For i = ReceCount + 1 To UBound(inpu) + 1     ‘数据进入缓冲区
  Recebuf(i) = inpu(i - 1)
  Next
 
  ReceCount = ReceCount + UBound(inpu) + 1
 
  TextReceive.Text = TextReceive.Text & Strdata
  Strdata = ""

Case comEvSend
 
End Select

End Sub

Private Function RtuCheck(data() As Byte) As Integer

Dim CrcHi As Byte, CrcLo As Byte
Dim Checkdata() As Byte
Dim i As Integer
Dim crc As String

CrcHi = data(UBound(data))
CrcLo = data(UBound(data) - 1)
ReDim Checkdata(1 To (UBound(data) - 1)) As Byte


For i = 1 To (UBound(data) - 1) ‘附值
Checkdata(i) = data(i - 1)
Next

crc = Crc16(Checkdata)
If (CrcLo = Val("&H" & CStr(Mid(crc, 1, 2))) And CrcHi = Val("&H" & CStr(Mid(crc, 3, 2)))) Then
RtuCheck = 1
Else
RtuCheck = 0
End If

End Function




koko

  • 精华:0帖
  • 求助:0帖
  • 帖子:2帖 | 3回
  • 年度积分:0
  • 历史总积分:531
  • 注册:2017年12月17日
发表于:2017-12-27 19:54:54
21楼

PLC端的通讯部分程序能借鉴一下吗?我现在也在做这,急需您的帮助

wang_hu_hi

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 221回
  • 年度积分:0
  • 历史总积分:331
  • 注册:2015年12月18日
发表于:2018-09-06 09:32:59
22楼

悲催啊,我也看不懂啊


SOIJOR

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 238回
  • 年度积分:0
  • 历史总积分:179
  • 注册:2016年8月03日
发表于:2021-02-24 17:33:10
23楼

好久不见VB了,用虚拟机装了xp系统就是为了用VB

摩米士

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 70回
  • 年度积分:46
  • 历史总积分:111
  • 注册:2017年8月11日
发表于:2021-03-09 12:13:21
24楼

回复内容:

对:亲吻那棵树 回复内容:对:happinessman88关于回复内容:对:亲吻那棵树关于只会梯子,连句子都看不懂,何况这么多字母内容的回复:也是,计算机编程就欺负工控人,那就爬着梯子上去吧。计算机编程就欺负工控人,这也是西门子WINCC吃香喝辣的原因内容的回复:大侠有时间的话给解释解释吧,俺们也想学习学习。向计算机编程者致敬!     内容的回复:

厉害,看不懂!!

飞龙819

  • 精华:0帖
  • 求助:0帖
  • 帖子:11帖 | 38回
  • 年度积分:128
  • 历史总积分:195
  • 注册:2008年12月16日
发表于:2021-11-16 11:09:02
25楼

"aqxjsss" 的回复,发表在20楼
        对内容: 【学习,非常感谢分享!!!】进行回复:

        -----------------------------------------------------------------


楼主能不能上传一个这个VB通讯的例程,谢谢


工控人1

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 18回
  • 年度积分:93
  • 历史总积分:161
  • 注册:2021年6月03日
发表于:2021-12-21 21:04:54
26楼

悲催啊,我也看不懂啊


热门招聘
相关主题

官方公众号

智造工程师
    首页 上一页 1 2 下一页 尾页