Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.83/5 (6 votes)
I have got the following packet format to be send to machine:-
    byte     byte     byte         byte
      2       2         1           1
+---------+-------+-------------+----------+
|  CM     |   00  |  check sum  |  CR      |
+---------+-------+-------------+----------+

I have the following instructions:-

All data isASCII character code without checksumand CR.
Calculate sum total from top till before checksum ( 1 bite unit ),then recognize this complement of 1 as checksum.
Finish of format is CR (Hexadecimal: OD)
I implemented the check sum algo given on the following link http://tools.ietf.org/rfc/rfc1071.txt[^]

I have to send the above packet to the Dialysis machine.
Can anyone tell me the Algorithm which is mostly used in Hardware machines.
Is there any check sum calculator from which i can directly calculate check sum and send the hard coded check sum value to the machine as my contents of packet are fixed.

[edit from OP]
i did like this have a look:-
C++
char data[6]="CM00";
data[4]=0xF0;
data[5]=0x0D;
iResult = send( ConnectSocket, data, (int)strlen(data), 0 );
if (iResult == SOCKET_ERROR) {
    printf("send failed with error: %d\n", WSAGetLastError());
    closesocket(ConnectSocket);
    WSACleanup();
    return 1;
    }

printf("Bytes Sent: %ld\n", iResult);
i get a display bytes sent 6
but still not getting the response please help
[/edit]
Posted
Updated 13-Sep-12 5:17am
v2
Comments
bbirajdar 13-Sep-12 8:16am    
repost of http://www.codeproject.com/Questions/458424/Does-anyone-has-code-to-calculate-check-sum-throug
Richard MacCutchan 13-Sep-12 9:34am    
Is the above description the actual requirements of the device that you are trying to use? If so it looks fairly simple: add up all the bytes in the message, take the complement of that value and that is your check sum. Which part of this are you unable to do?
Tarun Batra 13-Sep-12 10:50am    
Sir i did like this have a look:-
char data[6]="CM00";
data[4]=0xF0;
data[5]=0x0D;
iResult = send( ConnectSocket, data, (int)strlen(data), 0 );
if (iResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}

printf("Bytes Sent: %ld\n", iResult);
i get a display bytes sent 6
but still not getting the response please help
Richard MacCutchan 13-Sep-12 11:13am    
Not getting what response? I see no code to read back from the device you are using. Also you forgot to use the complement of your checksum, which should be 0x0F.
Tarun Batra 13-Sep-12 11:17am    
sorry for data[4]=0xF0; actually it is data[4]=0x0F;,sir if device respond i have a recv call which is as shown
iResult = send( ConnectSocket, data, (int)strlen(data), 0 );
if (iResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}

printf("Bytes Sent: %ld\n", iResult);

// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}


// Receive until the peer closes the connection
do {

iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 )
printf("Bytes received: %d\n", iResult);
else if ( iResult == 0 )
printf("Connection closed\n");
else
printf("recv failed with error: %d\n", WSAGetLastError());

} while( iResult >= 0 );
i didn't get any response i get following get printed "Connection closed"

1 solution

Your checksum is wrong (it should be 0x0F) because you forgot the one's complement step.
Below a function to test your routine:
C
#include <stdio.h>

unsigned char chk(unsigned char * buf, size_t size)
{
  unsigned char sum = 0;
  while (size--)
  {
    sum +=  *buf++;
  }
  return ~sum;
}
/* Usage example */
int main()
{
  unsigned char data[6] = "CM00";
  printf("chk = %02X\n", chk(data, 4));
  return 0;
}
 
Share this answer
 
Comments
Tarun Batra 14-Sep-12 2:00am    
Ya sir it is 0x0F i did that but still not getting any response,can u tell that according to following statement "Finish of format is CR (Hexadecimal: OD)" data byte should be data[5]=0x0D;Is it correct?
CPallini 14-Sep-12 3:45am    
Yes, I suppose your right. Isn't any sample message in the documentation?
Tarun Batra 14-Sep-12 3:52am    
No sir i have written the info i have regarding data,should i send u the pdf that i have?I would be a great help if u look at it
CPallini 14-Sep-12 3:56am    
Yes, you may try. However I don't promise anything.
Tarun Batra 14-Sep-12 3:58am    
Can i have the mail id on which i should send the pdf?

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