Public Function A2DValue( _ ByVal ChannelIndex As Integer) As Integer 'Read data from a/d converter MAX187 Dim BitIndex As Integer Dim TempData As Integer Dim NbrLoops As Integer With frmMain.SerialPort(ChannelIndex) 'Start convert (CS-bar high to low) .RTSEnable = True 'CS-bar low Do While .DSRHolding DoEvents NbrLoops = NbrLoops + 1 If NbrLoops >= 100 Then Exit Function End If Loop 'Read data bits from msb to lsb (Clock MAX187 SCLK 12 times) For BitIndex = 11 To 0 Step -1 .DTREnable = False 'SCLK high .DTREnable = True 'SCLK low 'read bit value TempData = TempData + Abs(Not .DSRHolding) * 2 ^ BitIndex Next 'One more clock cycle .DTREnable = False 'SCLK high .DTREnable = True 'SCLK low 'End convert (CS-bar low to high) .RTSEnable = False 'CS-bar high End With 'returned value A2DValue = TempData End Function