You are here: Start Linux and Computers Interfacing the FreeStyle (Mini) Blood Glucose Monitoring System
Interfacing the FreeStyle (Mini) Blood Glucose Monitoring System PDF Print E-mail
Written by Andreas Böhler   
Monday, 08 September 2008 22:39

I recently got my hands on a FreeStyle mini Blood Glucose Monitoring System. Because I'm studying medical engineering, I had to play around with it.
As the FreeStyle has a 3.5mm Stereo-plug for data communication with a computer, I asked Abbott about the cable for studying and in fact they sent me one for free!
So, I quickly searched around and couldn't find ANY Linux-program that could interface a FreeStyle blood glucose monitor.
At the university, we started using Matlab, so I thought: Hey, why not implement this in Matlab? In fact, it was quite easy to read from the Serial port. A professor of mine told me about python, so I also started programming in python. Therefore, you can find code for Matlab and python below.

Python Code

The code below is a very simple python-script that reads from the first serial port and writes the data to a file called freestyle.csv. The data can then be analyzed using e.g. OpenOffice.org Calc.
#!/usr/bin/env python
#####################
# Query FreeSyle Blood Glucose Meter for Memory and write data to CSV file freestyle.csv
#
# (c) 2008 Andreas Boehler
# andy (dot) boehler (at) gmx (dot) at
#
# v0.1 2008/04/02
#
# ChangeLog
#
# v0.1 Initial Release, my very first python program :)
#
#####################


import serial

###################################
# Function to read from FreeStyle #
###################################

def freestyle_read():
ser = serial.Serial(0, 19200, timeout=1) #Open first serial port
print 'Port used: ' + ser.portstr #Print the used port
ser.write('mem') #Query FreeStyle
line = ''
line_vector=[]

while line.find('END') == -1: #Read from FreeStyle
line = ser.readline()
line_vector.append(line)

ser.close() #Close port

return line_vector

###########################################################
# Function to convert line to data and eliminate newlines #
###########################################################

def str2data(line): #Convert line to data, eliminating \r\n
str = line[0:len(line)-2]
data = str.split()
return data

#############################
# Main program starts below #
#############################

line_vector = freestyle_read() #Read from the meter

num_entries = int(line_vector[4])
print 'Number of records found: ' + str(num_entries)

data=[]

for i in range(num_entries): #Eliminate \r\n from Data and append it to list
line_data = str2data(line_vector[i+6])
del line_data[len(line_data)-2:len(line_data)]
data.append(line_data)

#print data
try:
fp = file('freestyle.csv', 'w')
except:
fp = None
print 'File could not be opened for writing (Permission?)'

if fp:
for i in range(num_entries): # Create CSV string from data
str = data[i]
#Append data to new string
out_str = str[0] + ';' + str[1] + ' ' + str[2] + ' ' + str[3] + ';' + str[4] + '\n'

fp.writelines(out_str) #Write string to file

#Close file and delete object

fp.close()
del fp

Matlab Code

The Matlab code is even easier and simpler: It just queries the first serial port, reads from the meter and returns a vector containing each line as a string.
%% string_vector = freestyle_read(portname)
% Fetches FreeStyle-Data from given Port
%
% (c)2008 Andreas Boehler
% andy (dot) boehler (at) gmx (dot) at
%
% This function opens a serial port given by portname and queries the
% hopefully connected FreeStyle-device for its memory. The whole memory is
% then returned.
%
% Tested on FreeStyle mini!
%
% v0.1 2008/04/02
%
% ChangeLog
%
% v0.1 Initial Release
%
%%%%%%%%%%%%%%%%%%%%

%% string_vector = freestyle_read(portname) starts below

function string_vector = freestyle_read(portname)

serial_object = serial(portname); % Create serial object and set parameters
set(serial_object,'BaudRate',19200);

fopen(serial_object); % Open our port and query FreeStyle
fprintf(serial_object,'mem');

ii = 1; % Set initial values
read_string = '';
string_vector = {};

while (isempty(strfind(read_string,'END'))); % Fetch data
read_string = fscanf(serial_object);
string_vector{ii} = read_string;
ii = ii+1;
end
fclose(serial_object); % Close the port

%% EOF
 
 
Attachments:
Download this file (freestyle.py)freestyle.py[Python-Code for interfacing the FreeStyle.]1 Kb
Download this file (freestyle_read.m)freestyle_read.m[Matlab-Code for reading data from the FreeStyle]0 Kb
Comments (0)
Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):S
:!::?::idea::arrow:
Security
Please input the anti-spam code that you can read in the image.
Last Updated on Tuesday, 09 September 2008 00:08
 
Template is Powered by www.schaffenskraft.info © 2008 | CSS 2.1 | XHTML 1.0