lololplp222 发表于 2024-1-7 21:56

VBA。这个程序怎么改。

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校验值不正确。有没有人帮忙改一下呀。
https://pic.ziyuan.wang/user/guest/2024/01/1704635591513_2b3dd2df149c3.png

lzhlv666666 发表于 2024-1-8 02:15

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
Sub CalculateCRC()
    Dim dataRange As Range
    Dim dataCell As Range


?好像是一样的呀

lololplp222 发表于 2024-1-8 14:31

whglaowang888 发表于 2024-1-8 06:27
AI生成代码还是有缺陷的,一是描述不准,二是功能不全。

不会写只能让AI帮忙编一编。
页: [1]
查看完整版本: VBA。这个程序怎么改。