Posts

Line Profiler

 This is my first time profiling code. I'm writing in Python and I'm having trouble using the built-in profiler, CProfiler, efficiently. CProfiler basically lists every process and how much time it takes. Although this is useful, a lot of the time consuming processes are built-in functions, and it is difficult to determine which lines in your code they are connected to. After googling, I stumbled upon line_profiler. Line_profiler profiles a function line by line, so you know how much time each line takes. I had some trouble running the line profiler from the command line. I was able to download the source, but could not get the kernprof part of the code to display anything more than the total time the code took. Luckily, I stumbled upon this StackOverFlow post:  https://stackoverflow.com/questions/23885147/how-do-i-use-line-profiler-from-robert-kern , which details how to create a line_profiler object in your code, and running it directly in your code. Since most of the

Converting the ASCII string value of a hex number to a hex number

HEX to ASCII to DECIMAL stuff So this took me way longer than it should have to figure out. Basically, in Python, receiving serial data can be really squirrely. You can receive a hex or decimal character as serial input, and then Python will print it out as a string of that number, and not the corresponding ASCII character. For example, if I send over \x04, which is equivalant to decimal 4, Python receives that variable, and then sets it to string '04', which is incorrect. I'm assuming this is because the ASCII value of \x04 does not exist. Anyways, to re-convert it back to hex, I found that the following method worked: x = '04' hex_val = int ( '0x{}' .format(x), 16 ) print ( 'hex_val=' , hex_val) hex_val= 4 Trying to format it using the method int('\x{}).format(x), 16) does not work. Hope this helps someone! -LL

8 bit binary checksums

Image
Today I needed to calculate an 8bit XOR checksum. I was reading an equipment manual to understand the serial data communication protocol for "Bayern-Hessen". The manual explained the message protocol. The "send" message was explained as the following: Binary Message Format. To ensure the integrity of messages transmitted over the network, all messages sent to or received from the instrument are formed into data packets consisting of a header character, a terminator character, and a block check code, as shown below.  <STX><message><ETX><BCC>  In the message above, and throughout this document, is the ASCII start-of-text character (ASCII code 2); is the ASCII end-of-text character (ASCII code 3); and  is the block check code, which is a one-byte value, represented by two hexadecimal characters. For example, a block check code of 0x43 would appear in a message as the characters “43.” (For non-programmers, 0x43 is the C language conve