VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.UserControl glucose 
   ClientHeight    =   7200
   ClientLeft      =   0
   ClientTop       =   0
   ClientWidth     =   9090
   ScaleHeight     =   7200
   ScaleWidth      =   9090
   Begin VB.CommandButton Command8 
      Caption         =   "Check Meter Status"
      Height          =   375
      Left            =   480
      TabIndex        =   10
      Top             =   3360
      Width           =   1695
   End
   Begin VB.Frame Frame2 
      Caption         =   "Connect"
      Height          =   1215
      Left            =   0
      TabIndex        =   7
      Top             =   0
      Width           =   2775
      Begin VB.CommandButton Command5 
         Caption         =   "Close Com1"
         Height          =   735
         Left            =   1440
         TabIndex        =   9
         Top             =   360
         Width           =   975
      End
      Begin VB.CommandButton Command4 
         Caption         =   "Connect to Com1"
         Height          =   735
         Left            =   240
         TabIndex        =   8
         Top             =   360
         Width           =   975
      End
   End
   Begin VB.CommandButton Command7 
      Caption         =   "Get Serial Number"
      Height          =   375
      Left            =   480
      TabIndex        =   6
      Top             =   2760
      Width           =   1695
   End
   Begin VB.CommandButton Command6 
      Caption         =   "Get Software Version"
      Height          =   375
      Left            =   480
      TabIndex        =   5
      Top             =   2160
      Width           =   1695
   End
   Begin VB.CommandButton Command1 
      Caption         =   "Get Glucose Readings"
      Height          =   495
      Left            =   480
      TabIndex        =   4
      Top             =   1440
      Width           =   1695
   End
   Begin VB.Frame Frame4 
      Caption         =   "Output"
      Height          =   6495
      Left            =   3000
      TabIndex        =   1
      Top             =   120
      Width           =   5655
      Begin VB.TextBox Text2 
         Height          =   5535
         Left            =   360
         MultiLine       =   -1  'True
         TabIndex        =   3
         Text            =   "glucose.ctx":0000
         Top             =   360
         Width           =   4935
      End
      Begin VB.CommandButton Command2 
         Caption         =   "Clear"
         Height          =   375
         Left            =   1800
         TabIndex        =   2
         Top             =   6000
         Width           =   2055
      End
   End
   Begin VB.CommandButton Command3 
      Caption         =   "Quit"
      Height          =   375
      Left            =   480
      TabIndex        =   0
      Top             =   3960
      Width           =   1695
   End
   Begin MSCommLib.MSComm MSComm1 
      Left            =   0
      Top             =   4560
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
      DTREnable       =   -1  'True
   End
End
Attribute VB_Name = "glucose"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
'HIT Diabetes Software
'Written for Tufts University Healthcare Information Technology (HIT) Network
'Written by Julie Sousa
'Submitted: 12/05/2001

'General form declarations
    Dim ComPort As Integer
    Dim PortIsOpen As Boolean
Private Sub Command2_Click()
    'Clear the text box
    Text2.Text = ""
End Sub
Private Sub Command3_Click()
    'End the Program
    Unload Me
End Sub
Private Sub Command4_Click()

    'This is required to detect if another application uses the com port
    On Error GoTo ErrorHandler

    'Open the com port, enable and disable the appropriate controls
    MSComm1.CommPort = ComPort
    MSComm1.PortOpen = True
    Command4.Enabled = False
    Command5.Enabled = True
    Command6.Enabled = True
    Command7.Enabled = True
    Command8.Enabled = True
    Command1.Enabled = True
    PortIsOpen = True
    Exit Sub

ErrorHandler:
    MsgBox "COM" & ComPort & " is already used by another application." & Chr(10) & "Could not open port.", vbInformation
    Exit Sub

End Sub
Private Sub Command5_Click()
    'Close the com port, enable and disable the appropriate controls
    MSComm1.PortOpen = False
    Command4.Enabled = True
    Command5.Enabled = False
    Command6.Enabled = False
    Command7.Enabled = False
    Command8.Enabled = False
    Command1.Enabled = False
    PortIsOpen = False
End Sub
Private Sub Command6_Click()
'Get Software version
    Dim ModemResponded As Boolean
    Dim ModemData As String

    'Flush the input and output buffers
        MSComm1.InBufferCount = 0
        MSComm1.OutBufferCount = 0

        'Send command to the modem
        'DM? is the command to retrieve the software version
        MSComm1.Output = "DM?" + Chr(13)
            Text2.Text = Text2.Text + " WAIT...." + Chr(13) + Chr(10)
        X = Timer
        Do
            'Yield to other processes
            dummy = DoEvents()

            'If there is data in the buffer, read it
            If MSComm1.InBufferCount Then
                ModemResponded = True
                ModemData = ModemData + MSComm1.Input
            End If
        Loop Until Timer >= X + 3
    
    Dim getSoftware() As String
    Dim space As String

    'seperate response into software version and date
    space = Chr(32)
    getSoftware = Split(ModemData, space)
    
    'If modem did not respond, inform the user, otherwise show the data
    If ModemResponded Then
        Text2.Text = Text2.Text + "The Software version is: " + getSoftware(0) + Chr(13) + Chr(10)
        Text2.Text = Text2.Text + "Created: " + getSoftware(1) + Chr(13) + Chr(10)
    Else
        Text2.Text = Text2.Text + "Please turn your meter on!" + Chr(13) + Chr(10)
    End If
    'Note: the modem appends chr(10) + chr(13) to the end of its data string
End Sub
Private Sub Command7_Click()

    'get serial number
    Dim ModemResponded As Boolean
    Dim ModemData As String

    'Flush the input and output buffers
        MSComm1.InBufferCount = 0
        MSComm1.OutBufferCount = 0

        'Send command to the modem
        'DM@ is the serial number command recognized by meter
        MSComm1.Output = "DM@" + Chr(13)
            Text2.Text = Text2.Text + " WAIT...." + Chr(13) + Chr(10)
        X = Timer
        Do
            'Yield to other processes
            dummy = DoEvents()

            'If there is data in the buffer, read it
            If MSComm1.InBufferCount Then
                ModemResponded = True
                ModemData = ModemData + MSComm1.Input
            End If
        Loop Until Timer >= X + 3
        
Dim quote As String
Dim getSerial() As String
Dim space As String

    'Remove quotes and put in one dimensional array using space delimeter
    quote = Chr(34)
    space = Chr(32)
    getSerial = Split(Replace(ModemData, quote, ""), space)
    
    'If modem did not respond, inform the user, otherwise show the data
    If ModemResponded Then
        Text2.Text = Text2.Text + "The serial number is: " + getSerial(1) + Chr(13) + Chr(10)
    Else
        Text2.Text = Text2.Text + "Please turn your meter on!" + Chr(13) + Chr(10)
    End If
End Sub

Private Sub Command8_Click()
'check meter status
Dim ModemResponded As Boolean
Dim ModemData As String

    'Flush the input and output buffers
        MSComm1.InBufferCount = 0
        MSComm1.OutBufferCount = 0

        'Send command to the modem
        'DMS is the command to check meter status
        MSComm1.Output = "DMS" + Chr(13)
            Text2.Text = Text2.Text + " WAIT...." + Chr(13) + Chr(10)
        X = Timer
        Do
            'Yield to other processes
            dummy = DoEvents()
            'If there is data in the buffer, read it
            If MSComm1.InBufferCount Then
                ModemResponded = True
                ModemData = ModemData + MSComm1.Input
            End If
        Loop Until Timer >= X + 3
    
    'If modem did not respond, inform the user, otherwise show the data
    If ModemResponded Then
        Text2.Text = Text2.Text + "Meter is ON!" + Chr(13) + Chr(10)
    Else
        Text2.Text = Text2.Text + "Meter is OFF!" + Chr(13) + Chr(10)
    End If
End Sub

Private Sub MSComm1_OnComm()
    'Use the comm. Event comEvReceive to read data whenever one
    'character is received from the com port
    MSComm1.RThreshold = 1
    MSComm1.SThreshold = 1
    'If MSComm1.CommEvent = comEvReceive Then
        'Append the character to the modem text box
       'Text2.Text = Text2.Text + MSComm1.Input
    'End If
End Sub
Private Sub Command1_Click()
    Dim ModemResponded As Boolean
    Dim ModemData As String
    
    'Flush the input and output buffers
        MSComm1.InBufferCount = 0
        MSComm1.OutBufferCount = 0

        'Send command to the modem
        'DMP is the cmmand to retrieve glucose reading
        MSComm1.Output = "DMP" + Chr(13)
            Text2.Text = Text2.Text + " WAIT...." + Chr(13) + Chr(10)
        X = Timer
        Do
            'Yield to other processes
            dummy = DoEvents()

            'If there is data in the buffer, read it
            If MSComm1.InBufferCount Then
                ModemResponded = True
                ModemData = ModemData + MSComm1.Input
            End If
        Loop Until Timer >= X + 3
    
Dim quote As String
Dim myArray() As String
Dim arrEntries As String
Dim loopEntry As Integer
    
    'find number of glucose readings
    'get glucose readings only
    'this removes the header line
    arrEntries = Mid(ModemData, 81)

    'Remove quotes and put in one dimensional array using "," delimeter
    'each individual glucose reading has 5 strings
    'Sample: P "SUN","11/11/01","12:07:00 PM","C  99 ",  0 0889
    'Since the way I have split this string is by a comma delimeter
    'the end of the string: 0 0889 is attached to the beginning of the next string
    'The beginning of the next string is something like P "DDD"
    
    quote = Chr(34)
    myArray = Split(Replace(arrEntries, quote, ""), ",")

Dim i As Integer
Dim j As Integer
Dim k As String
Dim readNum As String

'If modem did not respond, inform the user, otherwise show the data
    If ModemResponded Then
        Text2.Text = Text2.Text + "Your blood glucose readings are:" + Chr(13) + Chr(10) + Chr(13) + Chr(10)
'Report individual reading back as array
'In this array we only need elements 1, 2, 3 then 5, 6, 7 then 9, 10, 11 and so on
    For i = 0 To (UBound(myArray))
        readNum = i + 1
        Text2.Text = Text2.Text + "Reading(" + readNum + ")" + Chr(13) + Chr(10)
    For j = 1 To 3
        If (j + i * 4) > UBound(myArray) Then
            Exit For
        End If
            Text2.Text = Text2.Text + myArray(j + 4 * i) + Chr(13) + Chr(10)
        Next
        Text2.Text = Text2.Text + Chr(13) + Chr(10)
    Next
    Else
        Text2.Text = Text2.Text + "Please turn your meter on!" + Chr(13) + Chr(10)
    End If
    'Note: the modem appends chr(10) + chr(13) to the end of its data string
End Sub
Private Sub Form_Load()
    'Initialize controls
    Text2.Text = ""
    Command1.Enabled = False
    Command5.Enabled = False
    Command6.Enabled = False
    Command7.Enabled = False
    Command8.Enabled = False

    'COM1 is the default port
    ComPort = 1

    'Specify the com port settings
    'Baud rate=9600, no parity, 8 bits, 1 stop bit
    MSComm1.Settings = "9600,n,8,1"
End Sub
Private Sub Form_Upload(Cancel As Integer)
    'Close the com port when form is unloaded
    If PortIsOpen Then MSComm1.PortOpen = False
End Sub




