LuxManix
Posts: 10
Joined: Tue Jul 31, 2012 5:30 am

Serial Read/Write Checksum

Tue Feb 12, 2013 8:06 pm

Good afternoon folks,

I have come once again to ask for your assistance. I have been working on a coding problem for the past couple of days and I just cannot seem to find the problem. I am attempting to communicate with a device over a USB/serial port. The object sending information to my code requires an initial command ("!SCVER?") which I send from the raspberry pi with the following code (hex equivalent):

Code: Select all

char version[8]   = {0x21,0x53,0x43,0x56,0x45,0x52,0x3F,'\r'};


I then immediately receive a response from the device. The structure of the incoming data packet is as follows:
"1.0(hex byte)(hex byte)(hex byte)(hex byte)(checksum1byte)(checksum2byte)"
The check sum is calculated by adding all of the previous hex values. '1' +'.'+'0'+'(hex byte 1..4)

However my problem occurs when calculating the checksum. I have checked the devices output NUMEROUS times on a PC serial terminal and it is outputting correct checksum values whenever I query it (in the 7th and 8th packet adress). However when I try extracting these values in my C code I get completely different results that do not equate to the checksum.... I am running the code at 38400 baud 8bit no parity. I am starting to wonder if the baud rate has anything to do with it or maybe I am allocating the wrong amount of memory to the buffer1 variable??

My C code is shown below. Any help would be very much appreciated.

Code: Select all

short sum1 = 0;
unsigned char buffer1[15];
char version[8]   = {0x21,0x53,0x43,0x56,0x45,0x52,0x3F,'\r'};
                write(tty_fd1,version,8);

                if(read(tty_fd1,buffer1,15)>0)
                {
                        if(buffer1[0] == '1' && buffer1[1] == '.' && buffer1[2] == '0') 
                        {
                            printf("Start of packet\n\r");
                            printf("\n\r");
                            for(u=0;u<7;u++)            //sum current buffer
                            {
                                printf("%x\n\r",buffer1[u]);
                                sum1+=buffer1[u];        //sum turns out to be 16 bit (hence short)
                            }

                            printf("Check1: %02x Check2: %02x\n\r",buffer1[7],buffer1[8]);  //INCORRECT
                            checksum1 = ((buffer1[8]<<8)+buffer1[7]);   //INCORRECT
                            
                            printf("check: %04x\n\r",checksum1);                  
                            printf("sum: %04x\n\r",sum1);

                            //if lowbyte of sum is equal to checksum
                            if(sum1 == checksum1){
                                currenttime = (buffer1[0]+(buffer1[1]<<8)+(buffer1[2]<<16)+(buffer1[3]<<24));
                            }
                            sum1 = 0;
                            printf("\n\r");
                        }
                }

User avatar
PeterO
Posts: 5951
Joined: Sun Jul 22, 2012 4:14 pm

Re: Serial Read/Write Checksum

Tue Feb 12, 2013 8:11 pm

Are you getting the correct 15 bytes in the buffer ? You have code to dump them out so what are you getting ?

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

LuxManix
Posts: 10
Joined: Tue Jul 31, 2012 5:30 am

Re: Serial Read/Write Checksum

Tue Feb 12, 2013 8:30 pm

PeterO wrote:Are you getting the correct 15 bytes in the buffer ? You have code to dump them out so what are you getting ?

PeterO
Here is the output of the code attached: ( I am only receiving 9 bytes total from the device but made the buffer a little larger) The first 7 bytes of the packet seem correct

Sometime the check sum is a very large hex value ffffffff09 etc..
Attachments
output.png
output.png (51.08 KiB) Viewed 2533 times

User avatar
PeterO
Posts: 5951
Joined: Sun Jul 22, 2012 4:14 pm

Re: Serial Read/Write Checksum

Tue Feb 12, 2013 8:39 pm

mmmm checking your results, the sums are correct already.
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

LuxManix
Posts: 10
Joined: Tue Jul 31, 2012 5:30 am

Re: Serial Read/Write Checksum

Tue Feb 12, 2013 8:50 pm

PeterO wrote:mmmm checking your results, the sums are correct already.
Hello PeterO,

I have found the problem. I changed the format of the checksum output of the device from hex to just regular byte (different way of sending the same byte)... Although they should be the same, it worked. Thank you for your help either way, posting here definitely made me look over my problem. Admins feel free to delete this post.

Best,
Robert

User avatar
PeterO
Posts: 5951
Joined: Sun Jul 22, 2012 4:14 pm

Re: Serial Read/Write Checksum

Tue Feb 12, 2013 8:55 pm

What does the fixed code look like ?
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

LuxManix
Posts: 10
Joined: Tue Jul 31, 2012 5:30 am

Re: Serial Read/Write Checksum

Tue Feb 12, 2013 10:24 pm

The C code is identical.

The parallax propeller SPIN code is modified as follows:

Code: Select all

PUB TimeRequest  | r, n    
    if U_Flag == 0               '' Normal version reply
       SendDataString(string("1.0"))
       chksum := "1" + "." + "0" 
       value := cnt
       'COMport1.dec(value)
       repeat n from 0 to 3
          'SendDataString(string(" "))
          SendDataByte(value.byte[n])
          'COMport1.hex(value.byte[n],2) 
          chksum += value.byte[n]
       repeat r from 0 to 1
          SendDataByte(chksum.byte[r])  
          'COMport1.str(string(" _ "))
          'COMport1.hex(chksum.byte[r],2)
       chksum := 0        
    else                           
       U_Flag := 0               '' Make the PSC compatible with the PSCI
       'SendDataString(string("!SCVER?",13,"1.0"))

Code: Select all

SendDataByte(chksum.byte[r])  
was used instead of

Code: Select all

COMport1.hex(chksum.byte[r],2)

Return to “C/C++”