Compiling OpenSSL 1.0.2d on 64-bit Cygwin

Out of the box I couldn’t get OpenSSL to compile on 64-bit Cygwin, below are the steps I used to successfully compile it.

  1. Use “tar xf openssl-1.0.2d.tar.gz” to decompress the source
  2. Add  options=”$options no-asm” to line 913 of config
  3. Run ./config
  4. Change -march=i486 to -march=x86-64
  5. Run make
  6. All done

Note that I had to use tar to decompress it as 7zip wreaked the symlinks used in there which caused some #includes to fail.

Handling SIGUSR1 in GDB

I was debugging a project that used SIGUSR1 heavily, GDB stops on SIGUSR1 by default and it was making debugging a pain when I didn’t care when the signal was being generated. So here is now to set how GDB interprets signals.

By entering the following into the GDB prompt you can instruct it to not print, or stop when the signal happens and to pass it to the program.

handle SIGUSR1 nostop noprint pass

Building RTLSDR and GNURadio

I recently brought a USB DVB-T dongle that is based on the Realtek RTL2832U chip which, with RTLSDR, can be used as a really low cost SDR. This is because it can be setup to return the raw I/Q samples to the host PC.  Once you have the samples they can then be processed, I tend to use GNURadio for the processing.

There is a script available that downloads and installs everything, RTLSDR, GNURadio, and more. I recomend you try it first. I think there is also a new project from the GNURadio people that will automate building and installing. I had had some issues with building gr-audio when I wrote this so I use the manual method of building the code for now.

First of all make sure you have all of the dependencies GNURadio has, see for a list of them.

Once you have made sure you have all of the libraries that GNURadio needs you can build the code.

mkdir build
cd build
cmake ../
make && make test

sudo make install

This whole process can take a while to complete. On my work machine it took nearly 40mins! I had an issue building the gr-audio on my laptop that I still haven’t been able to resolve.

Next I built downloaded and extracted the RTLSDR source code. As with GNURadio we need to create a build directory and run cmake.

mkdir build
cmake ../

Then I ran a few commands to check that the code and the dongle worked correctly. The first tests to make sure samples are being returned at the correct rate and the second will receive and demodulate a WBFM station.

rtl_fm -f 97.1e6 -W -s 1000000 -r 48000 – | aplay -r 48k -f S16_LE

And that’s it! Now I’m going to play around with GRC and sound card I/O.

Playing Around with dwm

I use i3 as my main window manager but I like to play around with others so I know what’s out there. One of the window managers that I find very interesting is dwm. It is a very small window manager, about 2k lines of code and you configure it my changing a header file and re-compiling. Definitely not beginner-friendly!

To make playing easier I wanted to run dwm inside i3, that way I don’t have to keep logging in and out. I could have used a VM but the same problem would exist. Below is the shell script I use to do this. I found out how to do this from LinuxExchange.

xhost +local:<USER>
Xephyr -screen 800x600 -reset -terminate -extension GLX 2>/dev/null :1 &
export DISPLAY=:1

For some reason I need to run dwm twice before it works, the first time it says “dwm: cannot open display”. Anyone know why this is happening? Below is a screenshot of dwm running emacs, it is the bottom right panel. The panel to the left is emacs editing the config.h file.

So far I have modified the background colour of the status bar and set the status message. To set the status message all you need to do is set the WM_NAME property of the root window. Most systems have a utility that lets you do this in the command line.

xsetroot -name “Hello World”

You can also use this utility to change the background colour, I did a similar thing for i3.

xsetroot -solid “#222222″


Converting a String to a Double (with SI prefixes)

Engineers and scientists will often use SI prefixes to make writing down very large, and very small, numbers easier. Writing down 3 GV is much better than 3000000000 V :). I’m currently working on a couple of software projects, both at home and work, where I’d like the ability to enter numbers with SI prefixes for convenience.

First I decided to write down the different styles of input my code will have to support, below is the list of styles I came up with.

Now I know what I want the code to do I can start writing it. I created a dictionary containing the prefixe character and the associated multiplier. I am writing this code in C# by the way, and used LINQPad to try it all out. Once I had it working I put into the class library I was working on.

private readonly Dictionary Prefixes = new Dictionary(){
    {'P', 1e15},
    {'T', 1e12},
    {'G', 1e9},
    {'M', 1e6},
    {'k', 1e3},
    {'h', 1e2},
    {'d', 1e-1},
    {'c', 1e-2},
    {'m', 1e-3},
    {'u', 1e-6},
    {'n', 1e-9},
    {'p', 1e-12},
    {'f', 1e-15},

Then I wrote a method that will take in the input string and convert it to a double. The first thing the method does is check to see if it is a plain number that Double.Parse() can take care of, it does this check using a regex. If the Regex matched then it simply calls the Double.Parse() method and returns the result.

If the regex fails then it check using two more regexes if the number looks like it contains SI prefixes. If it does then we find out what prefix is used then remove the prefix and convert the number.

I am not very good with regular expressions so there may be better ways of writing them than this. I have tested this code quite a bit with different types of input and it seems pretty solid. It will throw a FormatException if anything goes wrong.

// These are the regexes used by the method. These are initialised in a constructor.
Regex plain_number_regex = new Regex(@"^[+-]?(?=[\.\d])\d*(\.\d+)?$"); // For .2222, 0.222, 2.32
Regex si_number_a_regex = new Regex(@"^[+-]?[\d]+[PTGMkcmunpf]?[\d]*$"); // for 2k, 2k2
Regex si_number_b_regex = new Regex(@"^[+-]?[\d]+(\.\d+)?[PTGMkcmunpf]?$"); // For 1.2k
public double ParseInputStringSI(string input)
    // Test to see if it is a plain number with no SI prefixes
    if (plain_number_regex.IsMatch(input)) {
        return Double.Parse(input);
    // Test to see if it is a number with an SI prefix.
    if(si_number_a_regex.IsMatch(input) || si_number_b_regex.IsMatch(input) ) {
        // Find where in the string the prefix is and what
        // kind of prefix it is.
        var input_prefix = from p in Prefixes.Keys
                           where input.IndexOf(p) > 0
                           select input[input.IndexOf(p)];
        // Make sure the above query worked. There should be
        // no reason for it to fail because the Regex checks
        // the prefix characters.
        if (input_prefix.Count() == 0) {
            throw new FormatException("Invalid Input");
        // Get the multiplier for the prefix
        var multiplier = Prefixes[input_prefix.First()];
        // Ether replace the prefix with a decimal point or
    // remove it entierly. Depends on the format of the
    // input.
    string inputp;
    if( si_number_a_regex.IsMatch(input) ) {
            inputp = Regex.Replace(input, @"[PTGMkhdcmunpf]", ".");
    } else {
        inputp = Regex.Replace(input, @"[PTGMkhdcmunpf]", "");
        // Attempt the conversion, multiply it then return it.
        var tmp = Double.Parse(inputp);
        return tmp * multiplier;
    } else {
        throw new FormatException("Input String is Invalid");

Getting a List of the Available COM Ports in C#

It’s really nice to show the user a list of the COM ports they actually have on their machines. All too often I have seen software that makes you type in the COM port name. Even worse are the applications that force you to select from a list of COM ports, usually COM1 to COM5, without the option of typing in a different one!

Below is some really simple code that generates a list of the available COM ports and inserts the list into a drop-down selection control in a WinForms application.

string[] ports = SerialPort.GetPortNames();
if (ports.Length > 0) {
    COMPort.Text = ports[0];
} else {
    COMPort.Text = "Unable to Detect COM ports";

Creating an IO Assignment Header File

When developing embedded firmware I like to define macros that provide aliases for the IO port registers that I need. The name of each of the macros will correspond to the net name on the schematic to make it easy to check for errors. I put all of these macros into a single header file that I can include, I have sometimes seen these called board support packages. They can sometime be entire libraries that abstract details of the hardware. In my case they are just simple header files as that is all I need for now.

Recently I decided to automate some of the work of creating these files as it can be very time consuming. This is the Awk script I ended up with.

#!/usr/bin/awk -f
    if ($0 == "") next;
    if ($2 == "PORTA") { port = "A"; }
    if ($2 == "PORTB") { port = "B"; }
    if ($2 == "PORTC") { port = "C"; }
    if ($2 == "PORTD") { port = "D"; }
    if ($2 == "PORTE") { port = "E"; }
    if ($2 == "PORTF") { port = "F"; }
    if ($2 == "PORTG") { port = "G"; }
    print "#define " $1 "_PIN " $3;
    print "#define " $1 "_TRIS TRIS" port "bits.TRIS" port $3;
    print "#define " $1 "_LAT LAT" port "bits.LAT" port $3;
    print "#define " $1 "_PORT PORT" port "bits.R" port $3;
    print "";

All I need to do is write a space separated file in which each line contains the name I want, the port it is on and the number of the pin that it is attached to. Then this simple script generates the C code. This was for a PIC24F series micro, I have a slightly different script for a project involving a PIC32 which I may post later.

This makes creating BSP header files really easy, especially if you need to modify the pin assignments!

Profiling Python Applications

I’ve been working on a project that’s written in Python, it continuously communicates with some external industrial equipment. It will poll the status of this equipment 4 times per second and also sends commands to them when requested to. My job this week was to raise the update rate to 5Hz…. I needed to make sure I had enough time to do this!

I decided before doing anything I should profile the code to find out how much time the main loop needs to run and what methods take the longest time. That way I’d know if the code can support 5Hz and if not what I can do about it.

Once again the Python standard library comes to the rescue, the cProfile module will monitor the execution of your program and generate a report. Below is an example of how to use it.

import cProfile"main()")

The next thing I did is write a simple bit of code that will print to stdout the current update rate of my application every second. It’s pretty much a Python port of the JavaScript library stats.js.

from __future__ import division
import time

class stats(object):
    def __init__(self):
        self.msMin = 1000
        self.msMax = 0
        self.msTime = 0
        self.fpsMin = 1000
        self.fpsMax = 0
        self.fps = 0;
        self.updates = 0
        self.startTime = int((time.time()+0.5)*1000)
        self.prevTime = int((time.time()+0.5)*1000)

    def begin(self):
        """Calling the method signifies the start of a frame
        self.startTime = int((time.time()+0.5)*1000)

    def end(self):
        """Calling this method signifies the end of a frame
        now = int((time.time()+0.5)*1000)

        self.msTime = now-self.startTime
        self.msMax = max(self.msMax, self.msTime)
        self.msMin = min(self.msMin, self.msTime)

        #print "ms: %i (%i - %i)" % (self.msTime, self.msMin, self.msMax)

        self.updates = self.updates + 1

        if now > (self.prevTime + 1000.0):
            self.fps = round((self.updates*1000.0)/float(now-self.prevTime))
            self.fpsMax = max(self.fpsMax, self.fps)
            self.fpsMin = min(self.fpsMin, self.fps)

            print "stats: %i fps (%.i fps - %i fps)" % (self.fps, self.fpsMin, self.fpsMax)

            self.prevTime = now
            self.updates = 0

        return now

        def update(self):
            self.startTime = self.end()

Simply add a call to begin() to the start of your loop and a corresponding call to end()  at the end of your loop. Now I get a nice counter that tells me how the code is performing. As I work on the code I can see how this affects the main loop performance.

Finding a File Descriptor Leak

My current project is a control system that runs on a small embedded PC running a Linux OS. I had a problem during development where I had to open/close two serial ports alternatively because they shared an interrupt. Doing this suddenly caused the software to crash after running for a few minutes. The problem turned out to be that I was leaking file descriptors!

I wrote a simple shell script that prints the the total file descriptors open on the serial ports. This helped me make sure my bug fix worked correctly.

while [ 1 -eq 1 ]; do
    lsof | awk '''
BEGIN { ttyS0 = ttyS1 = ttyS3 = ttyS4 = 0; }
/\/dev\/ttyS4/ { ttyS4++; }
/\/dev\/ttyS3/ { ttyS3++; }
/\/dev\/ttyS0/ { ttyS0++; }
/\/dev\/ttyS1/ { ttyS1++; }
    print " Descriptor    Counter ";
    print " ttyS4       " ttyS4;
    print " ttyS3       " ttyS3;
    print " ttyS1       " ttyS1;
    print " ttyS0       " ttyS0;
    sleep $TIME

In the end I manually control the serial port using the sys module instead of using pySerial. I still have no idea why pyserial started leaking resources.

Using CERN’s Root Library for Plotting

This is a post about something I did at least a year ago. I was modifying the firmware of a board as a customer wanted to change the way calibration of the product worked. The old method used a table containing points in a curve, each point contained the desired set point, and the actual value you get. The code then interpolated between the points in the curve as needed to get the actual value you need to set in order to get the desired value.

The customer wanted to use a single polynomial expression for calibration instead as that is what their existing products used. The new system would only need the coefficients of the polynomial instead of the large table of numbers. I wanted to experiment a bit with the polynomial code I had just written on the PC and at the time I did not know Scilab or numpy/matplotlib, but I had just read about CERN’s Root library which is a C++ library that includes really good plotting libraries. So I thought I’d try it out.

As you can see the plots look really good. Although I haven’t really used Root since I still remember it can sometimes consider it for doing plots even though I typically choose Gnuplot or matplotlib. On big advantage is that because it is a C++ library I can use the C code I wrote for the firmware directly without modification.

1 2 3