Jemboy Posted February 7, 2022 Share Posted February 7, 2022 (edited) For some serial data check I have to check if the data was send ok by using LRC. I am trying to convert the code below into Autoit however without any succes (the LRC does not match). public static byte calculateLRC(byte[] bytes) { byte LRC = 0; for (int i = 0; i < bytes.Length; i++) { LRC ^= bytes[i]; } return LRC; } So far I create this code but the LRC does not match the expected value. //Array Data is Hex values $LongStr = "30 30 43 31 30 30 30 30 30 30 30 30 31 30 30 30 30 30 30 30 30 30 30 31 20 20 20 20 20 20 20 " $LongStr = $LongStr & "20 20 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30 31 30 30 30 30 35 30 31 45 55 52" ; Expected LRC = 06 $aArray = StringSplit($LongStr, " ", $STR_CHRSPLIT ) $lrc = 0 $b = 0 For $t=1 to $aArray[0] $b = Dec($aArray[$t]) $lrc = BitXOR ($lrc,$b) Next MsgBox (0,"LRC","LRC (Dec) = " & $lrc & " Hex: " & Hex ($lrc) ) Any help would be much appreciated 😉 Edited February 7, 2022 by Jemboy Link to comment Share on other sites More sharing options...
Danyfirex Posted February 7, 2022 Share Posted February 7, 2022 I tested your C# calculateLRC function with your sample data and it returns 0x05. Saludos Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut Link to comment Share on other sites More sharing options...
Jemboy Posted February 7, 2022 Author Share Posted February 7, 2022 (edited) It seems my translation from C# to Autoit does the same calculation. I need the LRC as a checksum to determine if the RS232 communication to and from our pay terminal data was without errors. Both the C# and Autoit calculate the same LRC value of: 0x05 Also with other data the LRC in both scripts have the same results. IN THE ABOVE CASE, The pay terminal generatest LRC of: 0x06 (instead of 0x05) Almost all my LRCs are 1,2 or 3 digits off. Mostly only 1. I have to ivestigate further 😉 Edited February 7, 2022 by Jemboy Danyfirex 1 Link to comment Share on other sites More sharing options...
Jemboy Posted February 7, 2022 Author Share Posted February 7, 2022 I am getting some headache from this problem.😁 The pay terminal is configured to receive baudrate:9600. data:7, parity:e and stopbit: 1 from the serial port. My data is 8 bit (althoug all bytes are < 127). Could this have an effect on my data stream ? Does anyone know if I would have to convert my data before sending it ? Link to comment Share on other sites More sharing options...
Nine Posted February 7, 2022 Share Posted February 7, 2022 Have you tried to add the even parity bit to the other 7 bits ? “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Jemboy Posted February 7, 2022 Author Share Posted February 7, 2022 @NineI am really at a loss, Could you give me a hint how to do that ? Link to comment Share on other sites More sharing options...
Nine Posted February 8, 2022 Share Posted February 8, 2022 I tried it myself but it does not give the expected result. Only thing I could think of, is that bits may be received in reverse order ? Maybe you could check on Web for that specific machine, what is their algorithm ? “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Jemboy Posted February 8, 2022 Author Share Posted February 8, 2022 (edited) I found out the problem and I must say I am a little bit ashamed 😚 The protocol to send the data to the serial could be visualized like this: <STX> <DATA> <ETX> <LRC> (with STX=02 and ETX=03) STX stands for Start of Text and ETX stands for End of Text. I presumed the LRC should only be calculated using the <DATA>, but upon rereading the protocol I saw that the <ETX> also needed to be included. Now, when I add the ETX to the array, the LRC indeed becomes: 0x06. I have added the 03 to the array (see below) and my LRC is now as expected. At least if some one needs a LRC implementation for Autoit, there is one working implementation in this forum. 😃 //Array Data is Hex values $LongStr = "30 30 43 31 30 30 30 30 30 30 30 30 31 30 30 30 30 30 30 30 30 30 30 31 20 20 20 20 20 20 20 " $LongStr = $LongStr & "20 20 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30 31 30 30 30 30 35 30 31 45 55 52 03" ; Expected LRC = 06 $aArray = StringSplit($LongStr, " ", $STR_CHRSPLIT ) $lrc = 0 $b = 0 For $t=1 to $aArray[0] $b = Dec($aArray[$t]) $lrc = BitXOR ($lrc,$b) Next MsgBox (0,"LRC","LRC (Dec) = " & $lrc & " Hex: " & Hex ($lrc) ) Edited February 8, 2022 by Jemboy spudw2k and Danyfirex 2 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now