paulscherrerinstitute / StreamDevice

EPICS Driver for message based I/O
GNU General Public License v3.0
28 stars 42 forks source link

Add a new checksum method #60

Closed ljsSally closed 3 years ago

ljsSally commented 3 years ago

StreamDevice has many checksum functions, but i have some devices which need LRC checksum, so i write a function about LRC checksum. example: :010310040002E6 here E6 is LRC checksum. Calculation process: 01+03+10+04+00+02=1A(hex), negative of sum is E5, then add 1 is E6. /////add a new checksum function///// //This function is to implement LRC verification //static unsigned int lrcsum(const unsigned char data,unsigned int len,unsigned int sum) static ulong lrcsum(const uchar data,ulong len,ulong sum) { //Add all hex digits,ignore all other bytes. unsigned int d; while(len--) { d=toupper(data++); if(isxdigit(d)) { if(isdigit(d)) d -= '0'; else d -= 'A'-0x0A; if(len%2) d = 16; sum += d;
} } return sum; } static checksum checksumMap[] = // You may add your own checksum functions to this map. { // name func init xorout bytes chk("123456789") {"sum", sum, 0x00, 0x00, 1}, // 0xDD {"sum8", sum, 0x00, 0x00, 1}, // 0xDD ... {"adler32", adler32, 0x00000001, 0x00000000, 4}, // 0x091E01DE {"hexsum8", hexsum, 0x00, 0x00, 1}, // 0x2D {"lrcsum8", lrcsum, 0x00, 0x00, 1},//add lrcsum8 {"-lrcsum8",lrcsum, 0xFF, 0xFF, 1}//add -lrcsum8 };

dirk-zimoch commented 3 years ago

Is it this one? https://en.wikipedia.org/wiki/Longitudinal_redundancy_check I will add your implmentation to the next release.

dirk-zimoch commented 3 years ago

Looks different. What does the name LRC mean if it is not Longitudinal Redundancy Check?

ljsSally commented 3 years ago

Yes,LRC is Longitudinal Redundancy Check.

-----原始邮件----- 发件人:"Dirk Zimoch" notifications@github.com 发送时间:2020-08-13 16:42:18 (星期四) 收件人: paulscherrerinstitute/StreamDevice StreamDevice@noreply.github.com 抄送: ljsSally ljs@impcas.ac.cn, Author author@noreply.github.com 主题: Re: [paulscherrerinstitute/StreamDevice] Add a new checksum method (#60)

Looks different. What does the name LRC mean if it is not Longitudinal Redundancy Check?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

ljsSally commented 3 years ago

Is it this one? https://en.wikipedia.org/wiki/Longitudinal_redundancy_check I will add your implmentation to the next release.

Yes.

dirk-zimoch commented 3 years ago

Not in this release, but I will have a look at it. First I need to understand your implementation. It does not do the checksum over all the bytes like the original LRC does, but over the hexadecimal ASCII representation of bytes (like my hexsum does). Thus I will probably give it another name and reserve "lrc" for the implementation following the wikipedia article.

marciodo commented 3 years ago

I'm working on this issue. Hopefully will finish until tomorrow.

dirk-zimoch commented 3 years ago

If I understand this correctly, the checksum does not work on the text bytes themselves (like most other checksums) but on their interpretation as hex numbers in ASCII text. The checksum function needs to consume 2 chars at each step, convert them to a 1 byte hex number and work on that one.

ljsSally commented 3 years ago

Yes, I agree with you. Here are some equipment checksums that need to be handled in this way.

marciodo commented 3 years ago

@ljsSally, I think you were going to paste some checksum examples from your equipment, but they were missing on the post. If you have them it would be nice.

marciodo commented 3 years ago

If I understand this correctly, the checksum does not work on the text bytes themselves (like most other checksums) but on their interpretation as hex numbers in ASCII text. The checksum function needs to consume 2 chars at each step, convert them to a 1 byte hex number and work on that one.

Thanks, @dirk-zimoch. This is the kind of clarification that I was expecting. So, I already have an LRC function that works like all the others, operating on the ASCII byte codes. Now I need to create another one interpreting the ASCII as hex numbers. I was not sure what the default behavior should be for Stream Device. @ljsSally, I'll take a look at your implementation for this.

ljsSally commented 3 years ago

My equipment checksums and command, for example: Send:":0117010000050109000000D8"
"D8" is the LRC checksum, and checksum calulation: 01+17+01+05+01+09=28(Hex), NOT(28)+1=D8 Recive:":01170CC1005EF40000000000000000C9" “C9” is the LRC checksum,and checksum calculation: 01+17+0C+C1+5E+F4=237(Hex), NOT(237)+1=C9

dirk-zimoch commented 3 years ago

Included in 2.8.20.