$TYPECHECK ON

'----- Code modules (Change paths to point to the correct directory on your system)
$INCLUDE "RapidQ.inc"                     'MUST be included before QChart



$IFNDEF TRUE
	$DEFINE TRUE  1
$ENDIF

'$IFNDEF BOOLEAN
	$DEFINE BOOLEAN  INTEGER
'$ENDIF
Declare Function GetLastError Lib "kernel32" Alias "GetLastError" () As Long

'SimpleIO-UM2011_08_dll
DECLARE SUB      MCP_InitMCP2200 LIB "SimpleIO-UM" ALIAS "InitMCP2200" ( VendorID AS WORD, ProductID AS WORD)    'TESTED OK
DECLARE FUNCTION MCP_IsConnected LIB "SimpleIO-UM" ALIAS "IsConnected" () AS BOOLEAN    'TESTED OK
'DECLARE FUNCTION MCP_ConfigureMCP2200 LIB "SimpleIO-UM" ALIAS "ConfigureMCP2200" ( IOMap AS WORD, BaudRateParam AS DWORD, RxLEDMode AS WORD, TxLEDMode AS WORD, FLOW AS BOOLEAN, ULOAD AS BOOLEAN, SSPND AS BOOLEAN) AS BOOLEAN
DECLARE FUNCTION MCP_ConfigureMCP2200 LIB "SimpleIO-UM" ALIAS "ConfigureMCP2200" ( IOMap AS WORD, BaudRateParam AS WORD, RxLEDMode AS WORD, TxLEDMode AS WORD, FLOW AS WORD, ULOAD AS WORD, SSPND AS WORD) AS BOOLEAN
DECLARE FUNCTION MCP_SetPin LIB "SimpleIO-UM" ALIAS "SetPin" ( pin AS WORD) AS BOOLEAN
DECLARE FUNCTION MCP_ClearPin LIB "SimpleIO-UM" ALIAS "ClearPin" ( pin AS WORD) AS BOOLEAN
DECLARE FUNCTION MCP_ReadPinValue LIB "SimpleIO-UM" ALIAS "ReadPinValue" ( pin AS WORD) AS integer
DECLARE FUNCTION MCP_ReadPin LIB "SimpleIO-UM" ALIAS "ReadPin" ( pin AS WORD, BYREF returnvalue AS WORD) AS BOOLEAN '*returnvalue AS WORD) AS BOOLEAN

DECLARE FUNCTION MCP_WritePort LIB "SimpleIO-UM" ALIAS "WritePort" ( portValue AS WORD) AS BOOLEAN
DECLARE FUNCTION MCP_ReadPort LIB "SimpleIO-UM" ALIAS "ReadPort" ( BYREF returnvalue AS WORD) AS BOOLEAN '*returnvalue AS WORD) AS BOOLEAN
DECLARE FUNCTION MCP_ReadPortValue LIB "SimpleIO-UM" ALIAS "ReadPortValue" () AS integer
DECLARE FUNCTION MCP_SelectDevice LIB "SimpleIO-UM" ALIAS "SelectDevice" ( uiDeviceNo AS WORD) AS integer
DECLARE FUNCTION MCP_GetSelectedDevice LIB "SimpleIO-UM" ALIAS "GetSelectedDevice" () AS integer
DECLARE FUNCTION MCP_GetNoOfDevices LIB "SimpleIO-UM" ALIAS "GetNoOfDevices" () AS WORD
DECLARE SUB      MCP_GetDeviceInfo LIB "SimpleIO-UM" ALIAS "GetDeviceInfo" ( uiDeviceNo AS WORD, strOutput AS STRING)
DECLARE SUB      MCP_GetSelectedDeviceInfo LIB "SimpleIO-UM" ALIAS "GetSelectedDeviceInfo" ( strOutput AS STRING)
DECLARE FUNCTION MCP_ReadEEPROM LIB "SimpleIO-UM" ALIAS "ReadEEPROM" ( uiEEPAddress AS WORD) AS integer
DECLARE FUNCTION MCP_WriteEEPROM LIB "SimpleIO-UM" ALIAS "WriteEEPROM" ( uiEEPAddress AS WORD, cValue AS WORD) AS integer

'While ConfigureMCP2200 configures the device with one call, it may also be configured one parameter at a time:
DECLARE FUNCTION MCP_fnRxLED LIB "SimpleIO-UM" ALIAS "fnRxLED" ( mode AS WORD) AS BOOLEAN
DECLARE FUNCTION MCP_fnTxLED LIB "SimpleIO-UM" ALIAS "fnTxLED" ( mode AS WORD) AS BOOLEAN
DECLARE FUNCTION MCP_fnHardwareFlowControl LIB "SimpleIO-UM" ALIAS "fnHardwareFlowControl" ( onOff AS WORD) AS BOOLEAN
DECLARE FUNCTION MCP_fnULoad LIB "SimpleIO-UM" ALIAS "fnULoad" ( onOff AS DWORD) AS BOOLEAN
DECLARE FUNCTION MCP_fnSuspend LIB "SimpleIO-UM" ALIAS "fnSuspend" ( onOff AS DWORD) AS BOOLEAN
DECLARE FUNCTION MCP_fnSetBaudRate LIB "SimpleIO-UM" ALIAS "fnSetBaudRate" ( BaudRateParam AS DWORD) AS BOOLEAN
DECLARE FUNCTION MCP_ConfigureIO LIB "SimpleIO-UM" ALIAS "ConfigureIO" ( IOMap AS WORD) AS BOOLEAN
DECLARE FUNCTION MCP_ConfigureIoDefaultOutput LIB "SimpleIO-UM" ALIAS "ConfigureIoDefaultOutput" ( ucIoMap AS WORD, ucDefValue AS WORD) AS BOOLEAN

'Constants:WORD
const OFF = 0
const ON = 1
const TOGGLE = 3
const BLINKSLOW = 4
const BLINKFAST = 5

const E_WRONG_DEVICE_ID =&HFFFF ' (-1) - for a device ID that is out of range
const E_INACTIVE_DEVICE =&HFFFE '(-2) - for an inactive device

const E_WRONG_ADDRESS =&HFFFD '(-3) - in case the given EEPROM address is out of range
const E_CANNOT_SEND_DATA =&HFFFC '(-4) in case the function cannot send the command to the device.


'------------------------------------------------------------------------------------
declare function HexStrToDec (str as string) AS WORD

DECLARE SUB OnClick_InitMCP2200
DECLARE SUB OnClick_IsConnected
DECLARE SUB OnClick_ConfigureMCP2200
DECLARE SUB OnClick_SetPin
DECLARE SUB OnClick_ClearPin
DECLARE SUB OnClick_ReadPinValue
DECLARE SUB OnClick_ReadPin
DECLARE SUB OnClick_WritePort
DECLARE SUB OnClick_ReadPort
DECLARE SUB OnClick_ReadPortValue
DECLARE SUB OnClick_SelectDevice
DECLARE SUB OnClick_GetSelectedDevice
DECLARE SUB OnClick_GetNoOfDevices
DECLARE SUB OnClick_GetDeviceInfo
DECLARE SUB OnClick_GetSelectedDeviceInfo
DECLARE SUB OnClick_ReadEEPROM
DECLARE SUB OnClick_WriteEEPROM
DECLARE SUB OnClick_fnRxLED
  DECLARE SUB OnCHG_fnRxLED (SENDER AS QCOMBOBOX)
DECLARE SUB OnClick_fnTxLED
  DECLARE SUB OnCHG_fnTxLED (SENDER AS QCOMBOBOX)
DECLARE SUB OnClick_fnHardwareFlowControl
  DECLARE SUB OnCHG_fnHardwareFlowControl
DECLARE SUB OnClick_fnULoad
  DECLARE SUB OnCHG_fnULoad
DECLARE SUB OnClick_fnSuspend
  DECLARE SUB OnCHG_fnSuspend
DECLARE SUB OnClick_fnSetBaudRate
DECLARE SUB OnClick_ConfigureIO
DECLARE SUB OnClick_ConfigureIoDefaultOutput




'DEFint userSelection = 0' //User input when requested for main menu
DIM connectedStatus AS boolEAN 'Connection status of MCP2200
DEFint result = 0' //Result of function calls with uint returned //
DEFSTR DEVINFO$




CREATE frm AS QForm

  LEFT=10
  TOP=10
  Width = 800
  Height = 850
  Caption = "MCP2200 UNMANAGED SimpleIO-UM.DLL DEMO  "


    CREATE QB_InitMCP2200 AS QButton
      Caption = "InitMCP2200"
      OnClick = OnClick_InitMCP2200
      Top = 11
      LEFT = 12
      WIDTH =136
    END CREATE
        CREATE QL_VID AS QLABEL
        CAPTION = "VID: 0x"
        Top = QB_InitMCP2200.Top+3
        LEFT = 160
        END CREATE
        CREATE QE_VID AS QEDIT
        Text = "04D8"
        Top = QB_InitMCP2200.Top
        LEFT = 197
        WIDTH =40
        END CREATE
        CREATE QL_PID AS QLABEL
        CAPTION = "PID: 0x"
        Top = QB_InitMCP2200.Top+3
        LEFT = 260
        END CREATE
        CREATE QE_PID AS QEDIT
        Text = "00DF"
        Top = QB_InitMCP2200.Top
        LEFT = 297
        WIDTH =40
        END CREATE
        CREATE QL_InitMCP2200 AS QLABEL
        CAPTION = "???"
        Top = QB_InitMCP2200.Top+3
        LEFT = 354
        END CREATE

    CREATE QB_IsConnected AS QButton
      Caption = "IsConnected"
      OnClick = OnClick_IsConnected
      Top = QB_InitMCP2200.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
       CREATE QL_IsConnected AS QLABEL
        CAPTION = "???"
        Top = QB_IsConnected.Top+3
        LEFT = 264
        END CREATE

    CREATE QB_SelectDevice AS QButton
      Caption = "SelectDevice"
      OnClick = OnClick_SelectDevice
      Top = QB_IsConnected.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
        CREATE QE_SelectDevice AS QEDIT
        Text = "0"
        Top = QB_SelectDevice.Top
        LEFT = 197
        WIDTH =30
        END CREATE
        CREATE QL_SelectDevice AS QLABEL
        CAPTION = "???"
        Top = QB_SelectDevice.Top+3
        LEFT = 264
        END CREATE

    CREATE QB_GetSelectedDevice AS QButton
      Caption = "GetSelectedDevice"
      OnClick = OnClick_GetSelectedDevice
      Top = QB_SelectDevice.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
        CREATE QL_GetSelectedDevice AS QLABEL
        CAPTION = "???"
        Top = QB_GetSelectedDevice.Top+3
        LEFT = 264
        END CREATE

    CREATE QB_GetNoOfDevices AS QButton
      Caption = "GetNoOfDevices"
      OnClick = OnClick_GetNoOfDevices
      Top = QB_GetSelectedDevice.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
       CREATE QL_GetNoOfDevices AS QLABEL
        CAPTION = "???"
        Top = QB_GetNoOfDevices.Top+3
        LEFT = 264
        END CREATE

    CREATE QB_GetDeviceInfo AS QButton
      Caption = "GetDeviceInfo"
      OnClick = OnClick_GetDeviceInfo
      Top = QB_GetNoOfDevices.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
        CREATE QE_GetDeviceInfo AS QEDIT
        Text = "0"
        Top = QB_GetDeviceInfo.Top
        LEFT = 197
        WIDTH =30
        END CREATE
        CREATE QL_GetDeviceInfo AS QLABEL
        CAPTION = "???"
        Top = QB_GetDeviceInfo.Top+3
        LEFT = 264
        END CREATE

    CREATE QB_GetSelectedDeviceInfo AS QButton
      Caption = "GetSelectedDeviceInfo"
      OnClick = OnClick_GetSelectedDeviceInfo
      Top = QB_GetDeviceInfo.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
        CREATE QL_GetSelectedDeviceInfo AS QLABEL
        CAPTION = "???"
        Top = QB_GetSelectedDeviceInfo.Top+3
        LEFT = 264
        END CREATE

    CREATE QB_ConfigureMCP2200 AS QButton
      Caption = "ConfigureMCP2200"
      OnClick = OnClick_ConfigureMCP2200
      Top = QB_GetSelectedDeviceInfo.Top+30
      LEFT = 12
      WIDTH =136
      HEIGHT =55
    END CREATE
         CREATE QL_CIO1 AS QLABEL
        CAPTION = "I/O 0x"
        Top = QB_ConfigureMCP2200.Top+3
        LEFT = 163
        END CREATE
        CREATE QE_IOD AS QEDIT
        Text = "0"
        Top = QB_ConfigureMCP2200.Top
        LEFT = 197
        WIDTH =30
        END CREATE
        CREATE QL_BD AS QLABEL
        CAPTION = "BAUD"
        Top = QB_ConfigureMCP2200.Top+3
        LEFT = QE_IOD.LEFT+40
        END CREATE
        CREATE QE_Baud AS QEDIT
        Text = "9600"
        Top = QB_ConfigureMCP2200.Top
        LEFT = QL_BD.LEFT+30
        WIDTH =50
        END CREATE
        Create QC_RXLED As QComboBox
        Text = "?"
        Top = QB_ConfigureMCP2200.Top
        Left = QE_Baud.LEFT+55
        width = 140
        addItems "RXLED 0: OFF","RXLED 1: ---","RXLED 2: ---","RXLED 3: TOGGLE","RXLED 4: BLINKSLOW","RXLED 5: BLINKFAST"
        ItemIndex = 3
        End Create
        Create QC_TXLED As QComboBox
        Text = "?"
        Top = QB_ConfigureMCP2200.Top
        Left = QC_RXLED.LEFT+145
        width = 140
        addItems "TXLED 0: OFF","TXLED 1: ---","TXLED 2: ---","TXLED 3: TOGGLE","TXLED 4: BLINKSLOW","TXLED 5: BLINKFAST"
        ItemIndex = 3
        'onChange = OnCHG_
        'Visible=FALSE
        End Create
        Create QC_Flow As QComboBox
        Text = "?"
        Top = QB_ConfigureMCP2200.Top+30
        Left = 197  'QC_TXLED.LEFT+145
        width = 125
        addItems "0: NO FLOW CTRL","1: HW FLOW CTRL"
        ItemIndex = 0
        End Create
        Create QC_ULoad As QComboBox
        Text = "?"
        Top = QB_ConfigureMCP2200.Top+30
        Left = QC_Flow.LEFT+130
        width = 150
        addItems "0: GP1-NORMAL","1: GP1: USB config status"
        ItemIndex = 0
        End Create
        Create QC_SSPND As QComboBox
        Text = "?"
        Top = QB_ConfigureMCP2200.Top+30
        Left = QC_ULoad.LEFT+155
        width = 190
        addItems "0: GP0-NORMAL","1: GP0-USB Susp./Resume states"
        ItemIndex = 0
        End Create
        CREATE QL_ConfigureMCP2200 AS QLABEL
        CAPTION = "???"
        Top = QB_ConfigureMCP2200.Top
        LEFT = QC_TXLED.LEFT+145 'QC_SSPND.LEFT+200
        END CREATE

    CREATE QB_ConfigureIO AS QButton
      Caption = "ConfigureIO"
      OnClick = OnClick_ConfigureIO
      Top = QB_ConfigureMCP2200.Top+60
      LEFT = 12
      WIDTH =136
    END CREATE
        CREATE QL_CIO AS QLABEL
        CAPTION = "I/O 0x"
        Top = QB_ConfigureIO.Top+3
        LEFT = 163
        END CREATE
        CREATE QE_ConfigureIO AS QEDIT
        Text = "0"
        Top = QB_ConfigureIO.Top
        LEFT = 197
        WIDTH =30
        END CREATE
        CREATE QL_ConfigureIO AS QLABEL
        CAPTION = "???"
        Top = QB_ConfigureIO.Top+3
        LEFT = 354
        END CREATE

   CREATE QB_ConfigureIoDefaultOutput AS QButton
      Caption = "ConfigureIoDefaultOutput"
      OnClick = OnClick_ConfigureIoDefaultOutput
      Top = QB_ConfigureIO.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
        CREATE QL_IO1 AS QLABEL
        CAPTION = "I/O 0x"
        Top = QB_ConfigureIoDefaultOutput.Top+3
        LEFT = 163
        END CREATE
        CREATE QE_IO AS QEDIT
        Text = "0"
        Top = QB_ConfigureIoDefaultOutput.Top
        LEFT = 197
        WIDTH =30
        END CREATE
        CREATE QL_IO2 AS QLABEL
        CAPTION = "DATA 0x"
        Top = QB_ConfigureIoDefaultOutput.Top+3
        LEFT = 235
        END CREATE
        CREATE QE_IODAT AS QEDIT
        Text = "0"
        Top = QB_ConfigureIoDefaultOutput.Top
        LEFT = 281
        WIDTH =30
        END CREATE

        CREATE QL_ConfigureIoDefaultOutput AS QLABEL
        CAPTION = "???"
        Top = QB_ConfigureIoDefaultOutput.Top+3
        LEFT = 354
        END CREATE

    CREATE QB_fnSetBaudRate AS QButton
      Caption = "fnSetBaudRate"
      OnClick = OnClick_fnSetBaudRate
      Top = QB_ConfigureIoDefaultOutput.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
        CREATE QE_fnSetBaudRate AS QEDIT
        Text = "9600"
        Top = QB_fnSetBaudRate.Top
        LEFT = 197
        WIDTH =50
        END CREATE
        CREATE QL_fnSetBaudRate AS QLABEL
        CAPTION = "???"
        Top = QB_fnSetBaudRate.Top+3
        LEFT = 354
        END CREATE

    CREATE QB_fnRxLED AS QButton
      Caption = "fnRxLED"
      OnClick = OnClick_fnRxLED
      Top = QB_fnSetBaudRate.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
        CREATE QL_fnRxLED1 AS QLABEL
        CAPTION = "GP6"
        Top = QB_fnRxLED.Top+3
        LEFT = 170
        END CREATE
        Create QC_FNRXLED As QComboBox
        Text = "?"
        Top = QB_fnRxLED.Top
        Left = 197
        width = 105
        addItems "0: OFF","1: ---","2: ---","3: TOGGLE","4: BLINKSLOW","5: BLINKFAST"
        ItemIndex = 0
        onChange = OnCHG_fnRxLED
        'Visible=FALSE
        End Create
        CREATE QL_fnRxLED AS QLABEL
        CAPTION = "???"
        Top = QB_fnRxLED.Top+3
        LEFT = 354
        END CREATE

    CREATE QB_fnTxLED AS QButton
      Caption = "fnTxLED"
      OnClick = OnClick_fnTxLED
      Top = QB_fnRxLED.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
        CREATE QL_fnTxLED1 AS QLABEL
        CAPTION = "GP7"
        Top = QB_fnTxLED.Top+3
        LEFT = 170
        END CREATE
        Create QC_FNTXLED As QComboBox
        Text = "?"
        Top = QB_fnTxLED.Top
        Left = 197
        width = 105
        addItems "0: OFF","1: ---","2: ---","3: TOGGLE","4: BLINKSLOW","5: BLINKFAST"
        ItemIndex = 0
        onChange = OnCHG_fnTxLED
        'Visible=FALSE
        End Create
        CREATE QL_fnTxLED AS QLABEL
        CAPTION = "???"
        Top = QB_fnTxLED.Top+3
        LEFT = 354
        END CREATE

    CREATE QB_fnHardwareFlowControl AS QButton
      Caption = "fnHardwareFlowControl"
      OnClick = OnClick_fnHardwareFlowControl
      Top = QB_fnTxLED.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
        Create QC_fnHardwareFlowControl As QComboBox
        Text = "?"
        Top = QB_fnHardwareFlowControl.Top
        Left = 197
        width = 105
        addItems "0: NO","1: HW FLOW CONTROL"
        ItemIndex = 0
        onChange = OnCHG_fnHardwareFlowControl
        'Visible=FALSE
        End Create
        CREATE QL_fnHardwareFlowControl AS QLABEL
        CAPTION = "???"
        Top = QB_fnHardwareFlowControl.Top+3
        LEFT = 354
        END CREATE

    CREATE QB_fnULoad AS QButton
      Caption = "fnULoad"
      OnClick = OnClick_fnULoad
      Top = QB_fnHardwareFlowControl.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
        Create QC_fnULoad As QComboBox
        Text = "?"
        Top = QB_fnULoad.Top
        Left = 197
        width = 150
        addItems "0: GP1-NORMAL","1: GP1: USB config status"
        ItemIndex = 0
        onChange = OnCHG_fnULoad
        'Visible=FALSE
        End Create
        CREATE QL_fnULoad AS QLABEL
        CAPTION = "???"
        Top = QB_fnULoad.Top+3
        LEFT = 354
        END CREATE

    CREATE QB_fnSuspend AS QButton
      Caption = "fnSuspend"
      OnClick = OnClick_fnSuspend
      Top = QB_fnULoad.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
        Create QC_fnSuspend As QComboBox
        Text = "?"
        Top = QB_fnSuspend.Top
        Left = 197
        width = 195
        addItems "0: GP0-NORMAL","1: GP0-USB Susp./Resume states"
        ItemIndex = 0
        onChange = OnCHG_fnSuspend
        'Visible=FALSE
        End Create
        CREATE QL_fnSuspend AS QLABEL
        CAPTION = "???"
        Top = QB_fnSuspend.Top+3
        LEFT = 400
        END CREATE

    CREATE QB_SetPin AS QButton
      Caption = "SetPin"
      OnClick = OnClick_SetPin
      Top = QB_fnSuspend.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
        CREATE QE_SetPin AS QEDIT
        Text = "0"
        Top = QB_SetPin.Top
        LEFT = 197
        WIDTH =30
        END CREATE
        CREATE QL_SetPin AS QLABEL
        CAPTION = "???"
        Top = QB_SetPin.Top+3
        LEFT = 264
        END CREATE

    CREATE QB_ClearPin AS QButton
      Caption = "ClearPin"
      OnClick = OnClick_ClearPin
      Top = QB_SetPin.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
        CREATE QE_ClearPin AS QEDIT
        Text = "0"
        Top = QB_ClearPin.Top
        LEFT = 197
        WIDTH =30
        END CREATE
        CREATE QL_ClearPin AS QLABEL
        CAPTION = "???"
        Top = QB_ClearPin.Top+3
        LEFT = 264
        END CREATE

    CREATE QB_ReadPinValue AS QButton
      Caption = "ReadPinValue"
      OnClick = OnClick_ReadPinValue
      Top = QB_ClearPin.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
        CREATE QE_ReadPinValue AS QEDIT
        Text = "0"
        Top = QB_ReadPinValue.Top
        LEFT = 197
        WIDTH =30
        END CREATE
        CREATE QL_ReadPinValue AS QLABEL
        CAPTION = "???"
        Top = QB_ReadPinValue.Top+3
        LEFT = 264
        END CREATE

    CREATE QB_ReadPin AS QButton
      Caption = "ReadPin"
      OnClick = OnClick_ReadPin
      Top = QB_ReadPinValue.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
        CREATE QE_ReadPin AS QEDIT
        Text = "0"
        Top = QB_ReadPin.Top
        LEFT = 197
        WIDTH =30
        END CREATE
        CREATE QL_ReadPin AS QLABEL
        CAPTION = "???"
        Top = QB_ReadPin.Top+3
        LEFT = 264
        END CREATE

    CREATE QB_WritePort AS QButton
      Caption = "WritePort"
      OnClick = OnClick_WritePort
      Top = QB_ReadPin.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
        CREATE QL_WritePort1 AS QLABEL
        CAPTION = "0x"
        Top = QB_WritePort.Top+3
        LEFT = 181
        END CREATE

        CREATE QE_WritePort AS QEDIT
        Text = "00"
        Top = QB_WritePort.Top
        LEFT = 197
        WIDTH =30
        END CREATE
        CREATE QL_WritePort AS QLABEL
        CAPTION = "???"
        Top = QB_WritePort.Top+3
        LEFT = 264
        END CREATE

    CREATE QB_ReadPort AS QButton
      Caption = "ReadPort"
      OnClick = OnClick_ReadPort
      Top = QB_WritePort.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
        CREATE QL_ReadPort AS QLABEL
        CAPTION = "???"
        Top = QB_ReadPort.Top+3
        LEFT = 264
        END CREATE

    CREATE QB_ReadPortValue AS QButton
      Caption = "ReadPortValue"
      OnClick = OnClick_ReadPortValue
      Top = QB_ReadPort.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
        CREATE QL_ReadPortValue AS QLABEL
        CAPTION = "???"
        Top = QB_ReadPortValue.Top+3
        LEFT = 264
        END CREATE

    CREATE QB_ReadEEPROM AS QButton
      Caption = "ReadEEPROM"
      OnClick = OnClick_ReadEEPROM
      Top = QB_ReadPortValue.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
        CREATE QE_ReadEEPROM AS QEDIT
        Text = "0"
        Top = QB_ReadEEPROM.Top
        LEFT = 197
        WIDTH =30
        END CREATE
        CREATE QL_ReadEEPROM AS QLABEL
        CAPTION = "???"
        Top = QB_ReadEEPROM.Top
        LEFT = 264
        END CREATE
        CREATE QL_ReadEEPROM1 AS QLABEL
        CAPTION = "0x"
        Top = QB_ReadEEPROM.Top+3
        LEFT = 181
        END CREATE

    CREATE QB_WriteEEPROM AS QButton
      Caption = "WriteEEPROM"
      OnClick = OnClick_WriteEEPROM
      Top = QB_ReadEEPROM.Top+30
      LEFT = 12
      WIDTH =136
    END CREATE
        CREATE QL_EEADDRL AS QLABEL
        CAPTION = "ADDR 0x"
        Top = QB_WriteEEPROM.Top+3
        LEFT = 150
        END CREATE
        CREATE QE_EEADDR AS QEDIT
        Text = "0"
        Top = QB_WriteEEPROM.Top
        LEFT = 197
        WIDTH =30
        END CREATE
        CREATE QL_WriteEEPROM2 AS QLABEL
        CAPTION = "DATA 0x"
        Top = QB_WriteEEPROM.Top+3
        LEFT = 235
        END CREATE
        CREATE QE_EEDAT AS QEDIT
        Text = "0"
        Top = QB_WriteEEPROM.Top
        LEFT = 281
        WIDTH =30
        END CREATE
        CREATE QL_WriteEEPROM AS QLABEL
        CAPTION = "???"
        Top = QB_WriteEEPROM.Top+3
        LEFT = 354
        END CREATE

END CREATE

'OnClick_InitMCP2200
'OnClick_IsConnected


DEFWORD TT,TT2,TT3
DEFDWORD BAUD

SUB OnClick_InitMCP2200
    TT=HexStrToDec(QE_VID.TEXT)
    TT2=HexStrToDec(QE_PID.TEXT)
'connectedStatus=false
MCP_InitMCP2200(TT, TT2)
QL_InitMCP2200.CAPTION= "The MCP2200 dll initialized. VID:"+RIGHT$(HEX$(TT),4) +" PID:"+RIGHT$(HEX$(TT2),4) '

END SUB
SUB OnClick_IsConnected
    QL_IsConnected.CAPTION= ""
    connectedStatus = MCP_IsConnected()
if (connectedStatus = true) THEN
    QL_IsConnected.CAPTION= "CONNECTED"
else
   QL_IsConnected.CAPTION= "DISCONNECTED"
END IF

END SUB

SUB OnClick_SetPin
    TT=HexStrToDec(QE_SetPin.TEXT)
    IF TT>7 THEN
        QL_SetPin.CAPTION=STR$(TT)+" ?? ERROR: 0..7"
    ELSE
        IF MCP_SETPIN(TT)=TRUE THEN
        QL_SetPin.CAPTION="SET GP"+ STR$(TT)+" TO HIGH"
        ELSE
        QL_SETPin.CAPTION="BAD SET PIN "+ STR$(TT)
        END IF
    END IF

END SUB
SUB OnClick_ClearPin
    TT=HexStrToDec(QE_ClearPin.TEXT)
    IF TT>7 THEN
        QL_ClearPin.CAPTION=STR$(TT)+"?? ERROR: 0..7"
    ELSE
        IF MCP_ClearPin(TT)=TRUE THEN
        QL_ClearPin.CAPTION="CLEAR GP"+ STR$(TT)+" TO LOW"
        ELSE
        QL_ClearPin.CAPTION="BAD CLEAR PIN "+ STR$(TT)
        END IF
    END IF

END SUB
SUB OnClick_ReadPinValue
    TT=HexStrToDec(QE_ReadPinValue.TEXT)
    IF TT>7 THEN
        QL_ReadPinValue.CAPTION=STR$(TT)+"?? ERROR: 0..7"
    ELSE
        TT2=MCP_ReadPinValue(TT)
        IF TT2=&H8000 THEN
        QL_ReadPinValue.CAPTION="ERROR READ GP"+ STR$(TT)
        ELSE
        QL_ReadPinValue.CAPTION=CHR$(&H30+TT2)+" READ GP"+ STR$(TT)
        END IF
    END IF

END SUB
SUB OnClick_ReadPin
    TT=HexStrToDec(QE_ReadPin.TEXT)
    IF TT>7 THEN
        QL_ReadPin.CAPTION=STR$(TT)+"?? ERROR: 0..7"
    ELSE
        IF MCP_ReadPin(TT,TT2)=TRUE THEN
        QL_ReadPin.CAPTION=CHR$(&H30+TT2)+" READ GP"+ STR$(TT)
        ELSE
        QL_ReadPin.CAPTION="ERROR READ GP"+ STR$(TT)
        END IF
    END IF

END SUB
SUB OnClick_WritePort
        TT=HexStrToDec(QE_WritePort.TEXT)
    IF TT>255 THEN
        QL_WritePort.CAPTION=STR$(TT)+"?? ERROR: 00..FF"
    ELSE
        IF MCP_WritePort(TT)=TRUE THEN
        QL_WritePort.CAPTION=RIGHT$(HEX$(TT),2)+" WRITE PORT"
        ELSE
        QL_WritePort.CAPTION="ERROR WRITE PORT"
        END IF
    END IF

END SUB
SUB OnClick_ReadPort
    'TT2=0
       TT= MCP_ReadPort(TT2) AND &HFF
        IF TT=TRUE THEN
        'IF MCP_ReadPort(TT2)=TRUE THEN
        QL_ReadPort.CAPTION="0x"+RIGHT$(HEX$(TT2),2)+" READ PORT"
        ELSE
        QL_ReadPORT.CAPTION="ERROR READ PORT RET:"+HEX$(TT)+" VAL:0x"+HEX$(TT2)
        END IF
    'END IF

END SUB
SUB OnClick_ReadPortValue
        TT2= MCP_ReadPortValue()
        IF TT2=&H8000 THEN
        QL_ReadPORTValue.CAPTION="ERROR READ PORT "+HEX$(TT2)
        ELSE
        QL_ReadPortValue.CAPTION="0x"+RIGHT$(HEX$(TT2),2)+" READ PORT"
        END IF

END SUB
SUB OnClick_SelectDevice

'    Function returns 0 in case of selection success, otherwise it will return
'   E_WRONG_DEVICE_ID (-1) - for a device ID that is out of range
'   E_INACTIVE_DEVICE (-2) - for an inactive device

    TT=VAL(QE_SelectDevice.TEXT)
    TT2=MCP_SelectDevice(TT)
        IF TT2=0 THEN
        QL_SelectDevice.CAPTION=" SelectDevice: "+RIGHT$(HEX$(TT),2)
        ELSEIF TT2=E_WRONG_DEVICE_ID THEN
        QL_SelectDevice.CAPTION="ERROR device ID that is out of range"+RIGHT$(HEX$(TT2),4)
        ELSEIF TT2=E_INACTIVE_DEVICE THEN
        QL_SelectDevice.CAPTION="ERROR INACTIVE DEVICE"+RIGHT$(HEX$(TT2),4)

        END IF


END SUB
SUB OnClick_GetSelectedDevice
        TT=MCP_GetSelectedDevice()
        QL_GetSelectedDevice.CAPTION=" GetSelectDevice: "+RIGHT$(HEX$(TT),4)
END SUB

SUB OnClick_GetNoOfDevices
        TT=MCP_GetNoOfDevices
        QL_GetNoOfDevices.CAPTION=" GetNoOfDevices: "+STR$(TT)
END SUB

SUB OnClick_GetDeviceInfo
    DEVINFO$= SPACE$(256)
    TT=VAL(QE_GetDeviceInfo.TEXT)
    MCP_GetDeviceInfo(TT,DEVINFO$)
    QL_GetDeviceInfo.CAPTION=DEVINFO$
END SUB
SUB OnClick_GetSelectedDeviceInfo
    DEVINFO$= SPACE$(256)
    MCP_GetSelectedDeviceInfo(DEVINFO$)
    QL_GetSelectedDeviceInfo.CAPTION=DEVINFO$

END SUB
SUB OnClick_ReadEEPROM
    'Function returns any positive value as being the EEPROM's location
    'value E_WRONG_ADDRESS (-3) - in case the given EEPROM address is out of range
    'E_CANNOT_SEND_DATA (-4) in case the function cannot send the command to the device.

    TT=HexStrToDec(QE_ReadEEPROM.TEXT)
        IF TT>255 THEN
            QL_ReadEEPROM.CAPTION=STR$(TT)+"?? ERROR: 00..FF"
        ELSE
            TT2=MCP_ReadEEPROM(TT)
            IF TT2=E_WRONG_ADDRESS THEN
            QL_ReadEEPROM.CAPTION=RIGHT$(HEX$(TT),2)+" ERROR: WRONG ADDRESS"
            ELSEIF TT2=E_CANNOT_SEND_DATA THEN
            QL_ReadEEPROM.CAPTION=RIGHT$(HEX$(TT),2)+" ERROR: CANNOT SEND DATA"
            ELSE
            QL_ReadEEPROM.CAPTION="ReadEEPROM 0x"+RIGHT$(HEX$(TT2),2)+"   ADDR:"+RIGHT$(HEX$(TT),4)
            END IF
        END IF

END SUB
SUB OnClick_WriteEEPROM
     TT=HexStrToDec(QE_EEADDR.TEXT)
     TT2=HexStrToDec(QE_EEDAT.TEXT)
        IF (TT>255) OR (TT2>255) THEN
            QL_WriteEEPROM.CAPTION=RIGHT$(HEX$(TT),2)+" "+RIGHT$(HEX$(TT2),2)+"?? ERROR: 00..FF"
        ELSE
            TT3=MCP_WriteEEPROM(TT,TT2)
            IF TT3=E_WRONG_ADDRESS THEN
            QL_WriteEEPROM.CAPTION=RIGHT$(HEX$(TT),2)+" ERROR: WRONG ADDRESS"
            ELSEIF TT3=E_CANNOT_SEND_DATA THEN
            QL_WriteEEPROM.CAPTION=RIGHT$(HEX$(TT),2)+" ERROR: CANNOT SEND DATA"
            ELSE
            QL_WriteEEPROM.CAPTION="WRITE EEADDR 0x"+RIGHT$(HEX$(TT),2)+"   DAT 0x"+RIGHT$(HEX$(TT2),2)+"   RET 0x"+RIGHT$(HEX$(TT3),4)
            END IF
        END IF
END SUB

SUB OnClick_fnRxLED
    TT=MCP_fnRxLED(QC_FNRXLED.ItemIndex)
    IF (TT=TRUE) THEN
        QL_fnRxLED.CAPTION="OK, RETURN: 0x"+HEX$(TT)
    ELSE
        QL_fnRxLED.CAPTION="ERR, RETURN: 0x"+HEX$(TT)
    END IF
END SUB
SUB OnCHG_fnRxLED
        QL_fnRxLED.CAPTION=""
END SUB


SUB OnClick_fnTxLED
    TT=MCP_fnTxLED(QC_FNTXLED.ItemIndex)
    IF (TT=TRUE) THEN
        QL_fnTxLED.CAPTION="OK, RETURN: 0x"+HEX$(TT)
    ELSE
        QL_fnTxLED.CAPTION="ERR, RETURN: 0x"+HEX$(TT)
    END IF
END SUB
    SUB OnCHG_fnTxLED
        QL_fnTxLED.CAPTION=""
    END SUB

SUB OnClick_fnHardwareFlowControl
    TT=MCP_fnHardwareFlowControl(QC_fnHardwareFlowControl.ItemIndex)
    IF (TT=TRUE) THEN
        QL_fnHardwareFlowControl.CAPTION="OK, RETURN: 0x"+HEX$(TT)
    ELSE
        QL_fnHardwareFlowControl.CAPTION="ERR, RETURN: 0x"+HEX$(TT)
    END IF
END SUB
    SUB OnCHG_fnHardwareFlowControl
        QL_fnHardwareFlowControl.CAPTION=""
    END SUB


SUB OnClick_fnULoad
    IF QC_fnULoad.ITEMINDEX THEN TT2=&HFFFFFFFF
    'TT=MCP_fnULoad(QC_fnULoad.ItemIndex)
    TT=MCP_fnULoad(TT2)
    IF (TT=TRUE) THEN
        QL_fnULoad.CAPTION="OK, RETURN: 0x"+HEX$(TT)
    ELSE
        QL_fnULoad.CAPTION="ERR, RETURN: 0x"+HEX$(TT)
    END IF
END SUB
    SUB OnCHG_fnULoad
        QL_fnULoad.CAPTION=""
    END SUB



SUB OnClick_fnSuspend
    TT=MCP_fnSuspend(QC_fnSuspend.ItemIndex)
    IF (TT=TRUE) THEN
        QL_fnSuspend.CAPTION="OK, RETURN: 0x"+HEX$(TT)
    ELSE
        QL_fnSuspend.CAPTION="ERR, RETURN: 0x"+HEX$(TT)
    END IF
END SUB
    SUB OnCHG_fnSuspend
        QL_fnSuspend.CAPTION=""
    END SUB

SUB OnClick_fnSetBaudRate
    BAUD=VAL(QE_fnSetBaudRate.TEXT)
            TT2=MCP_fnSetBaudRate(BAUD)
            IF TT2=TRUE THEN
            QL_fnSetBaudRate.CAPTION=STR$(BAUD)+" Success"
            ELSE
            QL_fnSetBaudRate.CAPTION=STR$(BAUD)+" ERROR: Invalid command RET: 0x"+HEX$(TT2)
            END IF

END SUB
SUB OnClick_ConfigureIO
     TT=HexStrToDec(QE_ConfigureIO.TEXT)
            TT2=MCP_ConfigureIO(TT)
            IF TT2=TRUE THEN
            QL_ConfigureIO.CAPTION=RIGHT$(HEX$(TT),2)+" Success"
            ELSE
            QL_ConfigureIO.CAPTION=RIGHT$(HEX$(TT),2)+" ERROR: Invalid command RET: 0x"+HEX$(TT2)
            END IF

END SUB
SUB OnClick_ConfigureIoDefaultOutput
         TT=HexStrToDec(QE_IO.TEXT)
     TT2=HexStrToDec(QE_IODAT.TEXT)
        IF (TT>255) OR (TT2>255) THEN
            QL_ConfigureIoDefaultOutput.CAPTION=RIGHT$(HEX$(TT),2)+" "+RIGHT$(HEX$(TT2),2)+"?? ERROR: 00..FF"
        ELSE
            TT3=MCP_ConfigureIoDefaultOutput(TT,TT2)
            IF TT3=TRUE THEN
            QL_ConfigureIoDefaultOutput.CAPTION="I/O:0x"+RIGHT$(HEX$(TT),2)+" DEF DAT:0x"+RIGHT$(HEX$(TT2),2)+" SUCCESS"
            ELSE
            QL_ConfigureIoDefaultOutput.CAPTION=" ERROR: Invalid command RET: 0x"+HEX$(TT3)
            END IF
        END IF

END SUB
























FRM.ShowModal


function HexStrToDec (str as string) AS WORD
DEFINT I
DEFINT CHR
RESULT=0
FOR I=1 TO LEN(STR)
    CHR=ASC(UCASE$(MID$(STR,I,1)))
    CHR=CHR-&H30
    IF(CHR>9) THEN CHR=CHR-7
    RESULT= (RESULT*16)+CHR
NEXT I
END FUNCTION


'                       TT              TT2                     TT3                 TT4                 TT5             TT6             TT7
'ConfigureMCP2200" ( IOMap AS WORD, BaudRateParam AS WORD, RxLEDMode AS WORD, TxLEDMode AS WORD, FLOW AS BOOLEAN, ULOAD AS BOOLEAN, SSPND AS BOOLEAN) AS BOOLEAN
SUB OnClick_ConfigureMCP2200

'TT3=MCP_ConfigureMCP2200(&HFFFF, &HFFFF,&HFFFF, &HFFFF,&HFFFF,&HFFFF,&HFFFF)
TT3=MCP_ConfigureMCP2200(&HA55A, 96000,9600, 0,0,0,0)
'if (RESULT = true) THEN
 'PRINT "ConfigureMCP2200 Success";
'else
  '  PRINT "ConfigureMCP2200 Invalid command "
'END IF
QL_ConfigureMCP2200.CAPTION="??????"

TT=HexStrToDec(QE_IOD.TEXT)
BAUD=VAL(QE_Baud.TEXT)
'ConfigureMCP2200" ( IOMap AS WORD, Baud AS DWORD, RxLED AS WORD, TxLED AS WORD, FLOW AS BOOLEAN, ULOAD AS BOOLEAN, SSPND AS BOOLEAN) AS BOOLEAN

'TT3=MCP_ConfigureMCP2200(TT,     BAUD,   QC_RXLED.ItemIndex, QC_TXLED.ItemIndex,QC_Flow.ItemIndex, QC_ULoad.ItemIndex,QC_SSPND.ItemIndex)

    if (TT3 = true) THEN
        QL_ConfigureMCP2200.CAPTION ="Success"
    else
        QL_ConfigureMCP2200.CAPTION="Invalid command"
    END IF
QL_ConfigureMCP2200.CAPTION=QL_ConfigureMCP2200.CAPTION+" "+RIGHT$(HEX$(TT),2)+" "+STR$(BAUD)+" "+STR$(QC_RXLED.ItemIndex)+" "+STR$(QC_TXLED.ItemIndex)+" "+STR$(QC_Flow.ItemIndex)+" "+STR$(QC_ULoad.ItemIndex)+" "+STR$(QC_SSPND.ItemIndex)
END SUB












