Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi,

i am developing a bidirectional communication with one of CRO Machine , i already received data which send by machine but i have a error when sending a data to the machine that is negative acknowledge while sending data..

ideas welcome
Posted
Updated 24-Dec-14 19:33pm
v2
Comments
Sergey Alexandrovich Kryukov 25-Dec-14 1:33am    
Questions welcome.
—SA

1 solution

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:
VB
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
 
Share this answer
 
v2
Comments
Rahul JR 25-Dec-14 4:53am    
After reading your ans i goes through my interface documention i cant understand my checksum algorithm it will be great if you could help me out to understand my algorithm

it is like

hexa
number Sum

31h 31h
54h 85h up to this it ok i can understand but after this i cant
65h EAh
73h 15Dh
74h 1D1h
03h 1D4h

then my check sum will be last two digit of last sum that is "D" and "4"

It will be great if you could help me out i badly need help
OriginalGriff 25-Dec-14 5:01am    
Use a byte.
That's all you need to do - don't use integers, just use bytes.
That will "throw away" the overflow and and leave you with the byte-wise sum of the values, as a byte which you can send directly.
Rahul JR 25-Dec-14 5:17am    
right now i just go through with one software that is SimpleTerm Gold it accept the data in hexa value if i am able to do bidirectional communication then and only than i can go with my application so right now i can just go with hexa number if you want a screen shot of my communication with SimpleTerm Gold then comment your email address i will send a you a screen shot. by the way thanks for your prompt reply..
Rahul JR 25-Dec-14 6:29am    
sorry @OriginalGriff after depth study i fill my checksum is ok..
if there is any other reason for NAK than guide me
OriginalGriff 25-Dec-14 7:06am    
Not normally - unless the specific device sends NAK because it doesn't like some part of your message content - but that's rare.

Are you sure you are using the right checksum algorithm?
Byte-wise addition is a pretty poor sumcheck...XOR is a lot more common, followed by CRC-16 and so forth.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900