Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C Visual-Studio Algorithms , +
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:-
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 13-Sep-12 2:03am
Edited 13-Sep-12 5:17am
v2
Comments
aspnet_regiis at 13-Sep-12 8:16am
   
repost of http://www.codeproject.com/Questions/458424/Does-anyone-has-code-to-calculate-check-sum-throug
Richard MacCutchan at 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 at 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 at 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 at 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"
Richard MacCutchan at 13-Sep-12 11:19am
   
Hardly surprising since you closed the connection, so you are not likely to be able to receive any data from it.
Tarun Batra at 13-Sep-12 11:27am
   
check the following link http://msdn.microsoft.com/en-us/library/windows/desktop/ms737889(v=vs.85).aspx my code is same as client code,there i am recv data from server
Richard MacCutchan at 13-Sep-12 11:44am
   
Sorry, I misread your code. If you are not getting a response then you need to investigate what is happening at the device that is supposed to send you the data. Something that you will have to do for yourself as we have no idea how the device operates.
Tarun Batra at 17-Sep-12 11:20am
   
please reply this http://www.codeproject.com/Questions/460820/Can-u-give-me-an-idea-how-to-implement-this
Joan Murt at 13-Sep-12 10:14am
   
Repost...

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Your checksum is wrong (it should be 0x0F) because you forgot the one's complement step.
Below a function to test your routine:
#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;
}
  Permalink  
Comments
Tarun Batra at 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 at 14-Sep-12 3:45am
   
Yes, I suppose your right. Isn't any sample message in the documentation?
Tarun Batra at 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 at 14-Sep-12 3:56am
   
Yes, you may try. However I don't promise anything.
Tarun Batra at 14-Sep-12 3:58am
   
Can i have the mail id on which i should send the pdf?
CPallini at 14-Sep-12 4:07am
   
Ops, I forgot you can't send attachments with CodeProject's direct emails.
No, I don't want to post my email address on a public forum. You might put the PDF document on a free hosting server and grant me access to it.
Tarun Batra at 14-Sep-12 4:17am
   
sir u can mail me our email address on trn200190@gmail.com
That would be a great help from your side,when mailed post a reply here.Your mail id will not be revealed.Is it ok?
Tarun Batra at 14-Sep-12 4:27am
   
got the mail
Tarun Batra at 14-Sep-12 7:15am
   
check mail
Tarun Batra at 14-Sep-12 13:54pm
   
sir please tell this http://www.codeproject.com/Questions/459502/Please-tell-how-to-make-event-level-socket-program
Tarun Batra at 17-Sep-12 11:20am
   
please reply this http://www.codeproject.com/Questions/460820/Can-u-give-me-an-idea-how-to-implement-this

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

  Print Answers RSS
0 OriginalGriff 324
1 Sergey Alexandrovich Kryukov 182
2 Maciej Los 159
3 Hard_Rockz 143
4 Richard MacCutchan 125
0 OriginalGriff 5,374
1 Sergey Alexandrovich Kryukov 4,713
2 Peter Leow 2,944
3 DamithSL 2,465
4 Maciej Los 2,270


Advertise | Privacy | Mobile
Web04 | 2.8.140718.1 | Last Updated 13 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid