吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 936|回复: 7
收起左侧

[求助] VBA。这个程序怎么改。

[复制链接]
lololplp222 发表于 2024-1-7 21:56
[C] 纯文本查看 复制代码
Sub CalculateCRC()
    Dim dataRange As Range
    Dim dataCell As Range
    Dim dataArray() As Byte
    Dim crc As Integer
    Dim highByte As String
    Dim lowByte As String
    
    Set dataRange = Range("O38:AZ38")
    
    ReDim dataArray(0 To dataRange.Cells.Count - 1)
    For Each dataCell In dataRange
        dataArray(dataCell.Column - dataRange.Column) = Val("&H" & dataCell.Value)
    Next dataCell
    
    crc = CRC16_Modbus(dataArray, UBound(dataArray) + 1)
    
    highByte = "0x" & Right("00" & Hex((crc \ &H100) And &HFF), 2)
    lowByte = "0x" & Right("00" & Hex(crc And &HFF), 2)
    
    MsgBox "CRC16-Modbus校验值为:" & highByte & " " & lowByte
End Sub

Function CRC16_Modbus(data() As Byte, ByVal length As Integer) As Integer
    Dim crc As Integer
    Dim i As Integer
    Dim j As Integer
    
    crc = &HFFFF
    
    For i = 0 To length - 1 Step 1
        crc = crc Xor data(i)
        For j = 1 To 8
            If crc And &H1 Then
                crc = (crc \ 2) Xor &HA001
            Else
                crc = crc \ 2
            End If
        Next j
    Next i
    
    CRC16_Modbus = crc
End Function


代码为excel VBA。用这个程序计算CRC16-Modbus,将生成的值的高低位放在BA37、BB37。
上述代码为CHATGPT生成,实际应用中生成的CRC校验值不正确。有没有人帮忙改一下呀。

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

lzhlv666666 发表于 2024-1-8 02:15
[Visual Basic] 纯文本查看 复制代码
Sub CalculateCRC()
    Dim dataRange As Range
    Dim dataCell As Range
    Dim dataArray() As Byte
    Dim crc As Integer
    Dim highByte As String
    Dim lowByte As String
     
    Set dataRange = Range("O38:AZ38")
     
    ReDim dataArray(0 To dataRange.Cells.Count - 1)
    For Each dataCell In dataRange
        dataArray(dataCell.Column - dataRange.Column) = Val("&H" & dataCell.Value)
    Next dataCell
     
    crc = CRC16_Modbus(dataArray, UBound(dataArray) + 1)
     
    highByte = "0x" & Right("00" & Hex((crc \ &H100) And &HFF), 2)
    lowByte = "0x" & Right("00" & Hex(crc And &HFF), 2)

    MsgBox "CRC16-Modbus校验值为:" & highByte & " " & lowByte
        With ThisWorkbook.Worksheets("Sheet1")
        .Range("BA37").Value = highByte
        .Range("BB37").Value = lowByte
    End With
End Sub
 
Function CRC16_Modbus(data() As Byte, ByVal length As Integer) As Integer
    Dim crc As Integer
    Dim i As Integer
    Dim j As Integer
     
    crc = &HFFFF
     
    For i = 0 To length - 1 Step 1
        crc = crc Xor data(i)
        For j = 1 To 8
            If crc And &H1 Then
                crc = (crc \ 2) Xor &HA001
            Else
                crc = crc \ 2
            End If
        Next j
    Next i
     
    CRC16_Modbus = crc
End Function
whglaowang888 发表于 2024-1-8 06:27
AI生成代码还是有缺陷的,一是描述不准,二是功能不全。
头像被屏蔽
tl;dr 发表于 2024-1-8 08:57
a2599639 发表于 2024-1-8 11:44
这个校验出的值是干嘛用的
 楼主| lololplp222 发表于 2024-1-8 14:29
a2599639 发表于 2024-1-8 11:44
这个校验出的值是干嘛用的

CRC16-Modbus 工程上常用的校验方式
 楼主| lololplp222 发表于 2024-1-8 14:30
lzhlv666666 发表于 2024-1-8 02:15
[mw_shl_code=vb,true]Sub CalculateCRC()
    Dim dataRange As Range
    Dim dataCell As Range

?好像是一样的呀
 楼主| lololplp222 发表于 2024-1-8 14:31
whglaowang888 发表于 2024-1-8 06:27
AI生成代码还是有缺陷的,一是描述不准,二是功能不全。

不会写只能让AI帮忙编一编。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 17:28

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表