且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将数据发送到串行端口时获得否定确认

更新时间:2023-11-09 13:10:16

NAK通常是表示你错误地构造了一个校验和 - 所以看看你的代码,找到你是如何构建它的。



值得尝试从设备传递消息你的代码,看看收到的sumcheck和计算的sumcheck值有什么不同。



如果这没有帮助,你需要回到文档并仔细研究如何制定检查 - 有很多方法可以生成,所以我们不能告诉你这样做它会起作用。





确定了。在手册中有一个十六进制的总和a从FN(帧号)到ETX(文本结束)如果我的字符串是02(STX)31(FN 1)测试(它是我的文本)03(ETX)44(CS1)34(CS2)0D(CR) 0A(LF)最后,striing是0231546573740344340D0A,它从31到03,它变为1D4,我的校验和将成为它的最后两位数44和34(即D 34为44)这就是我的校验和的计算方法。抱歉,由于公司政策,我无法发送手册。



这很酷 - 这不是问题。

试试这个:

NAK is normally a sign that you have constructed a checksum wrongly - so look at your code, and find exactly how you are building it.

It's worth trying passing a message from the device through your code to see what the difference in the received sumcheck and the calculated sumcheck values are.

If that doesn't help, you will need to go back to the documentation and look closely at how the check should be worked out - there are a large number of ways it can be generated, so we can't tell you "do this" and it'll work.


"ok got it.in a manual there is a sum of hexa from FN(Frame Number) to ETX(End of Text) If my string is 02(STX)31(FN 1)Test(it is my Text)03(ETX)44(CS1)34(CS2)0D(CR)0A(LF) Finally the striing is 0231546573740344340D0A in it Sum from 31 to 03 it becomes a 1D4 and my checksum will beccome last two digit of it 44 and 34 (i.e 44 for D 34 for 4) This is how my checksum is calcuated. sorry i am not able to send manual due to company policy."

That's cool - it's not a problem.
Try this:
Private Sub button1_Click(sender As Object, e As EventArgs)
	frmMain.ActiveForm.Text = "Hello!"
	Dim inputData As Byte() = New Byte(6) {}
	inputData(0) = &H2
	inputData(1) = &H31
	inputData(2) = CByte("T"C)
	inputData(3) = CByte("e"C)
	inputData(4) = CByte("s"C)
	inputData(5) = CByte("t"C)
	inputData(6) = &H3
	Dim outData As Byte() = CalculateCheck(inputData)
End Sub
Private Shared ToHex As Char() = "0123456789ABCDEF".ToCharArray()
Private Function CalculateCheck(inputData As Byte()) As Byte()
    Dim outData As Byte() = New Byte(inputData.Length + 3) {}
    Dim check As Integer = -inputData(0)
    Dim i As Integer = 0
    For Each b As Byte In inputData
        outData(i) = b
        check += (CInt(b) And &Hff)
        i = i + 1;
    Next
    check = check And &Hff
    outData(i + 0) = CByte(ToHex(check >> 4))
    outData(i + 1) = CByte(ToHex(check And &Hf))
    outData(i + 2) = &Hd
    outData(i + 3) = &Ha
    Return outData
End Function