added matlab functions to communicate with the apd counter software
added extra checks to the read loop to not end up in deadlocks