VERSION 5.00
Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "MSFLXGRD.OCX"
Object = "{BDC217C8-ED16-11CD-956C-0000C04E4C0A}#1.1#0"; "TABCTL32.OCX"
Object = "{65E121D4-0C60-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCHRT20.OCX"
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form frmDisplay 
   Caption         =   "SPA Instruments "
   ClientHeight    =   8235
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   11880
   Icon            =   "frmDisplay.frx":0000
   LinkTopic       =   "Form1"
   MDIChild        =   -1  'True
   ScaleHeight     =   8235
   ScaleWidth      =   11880
   WindowState     =   2  'Maximized
   Begin VB.CommandButton Command3 
      Caption         =   "Command3"
      Height          =   375
      Left            =   3600
      TabIndex        =   33
      Top             =   7560
      Width           =   1095
   End
   Begin VB.TextBox Text2 
      BackColor       =   &H80000000&
      BeginProperty Font 
         Name            =   "Calisto MT"
         Size            =   9.75
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   330
      Left            =   1815
      TabIndex        =   32
      Top             =   7560
      Width           =   1095
   End
   Begin VB.TextBox Text4 
      BackColor       =   &H80000000&
      BeginProperty Font 
         Name            =   "Calisto MT"
         Size            =   9.75
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   330
      Left            =   120
      TabIndex        =   31
      Text            =   "First Poll Time "
      Top             =   7560
      Width           =   1695
   End
   Begin VB.TextBox Text5 
      BackColor       =   &H80000000&
      BeginProperty Font 
         Name            =   "Calisto MT"
         Size            =   9.75
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   330
      Left            =   6600
      TabIndex        =   30
      Text            =   "Present Poll Time "
      Top             =   7560
      Width           =   1935
   End
   Begin VB.TextBox Text3 
      BackColor       =   &H80000000&
      BeginProperty Font 
         Name            =   "Calisto MT"
         Size            =   9.75
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   330
      Left            =   8520
      TabIndex        =   29
      Top             =   7560
      Width           =   855
   End
   Begin MSCommLib.MSComm MSComm1 
      Left            =   1395
      Top             =   225
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
      DTREnable       =   -1  'True
      RThreshold      =   2
   End
   Begin VB.Timer Timer1 
      Interval        =   5
      Left            =   480
      Top             =   120
   End
   Begin VB.CommandButton Command1 
      Cancel          =   -1  'True
      Caption         =   "&Close"
      Height          =   315
      Left            =   10320
      TabIndex        =   6
      Top             =   7560
      Width           =   945
   End
   Begin TabDlg.SSTab SSTab1 
      Height          =   7470
      Left            =   0
      TabIndex        =   0
      Top             =   0
      Width           =   11835
      _ExtentX        =   20876
      _ExtentY        =   13176
      _Version        =   393216
      Tabs            =   10
      TabsPerRow      =   5
      TabHeight       =   520
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "Calisto MT"
         Size            =   9
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      TabCaption(0)   =   "&Settings"
      TabPicture(0)   =   "frmDisplay.frx":000C
      Tab(0).ControlEnabled=   -1  'True
      Tab(0).Control(0)=   "Frame1"
      Tab(0).Control(0).Enabled=   0   'False
      Tab(0).Control(1)=   "Text1"
      Tab(0).Control(1).Enabled=   0   'False
      Tab(0).ControlCount=   2
      TabCaption(1)   =   "&Graph"
      TabPicture(1)   =   "frmDisplay.frx":0028
      Tab(1).ControlEnabled=   0   'False
      Tab(1).Control(0)=   "Frame2"
      Tab(1).ControlCount=   1
      TabCaption(2)   =   "&Details"
      TabPicture(2)   =   "frmDisplay.frx":0044
      Tab(2).ControlEnabled=   0   'False
      Tab(2).Control(0)=   "Frame3"
      Tab(2).Control(1)=   "Frame4"
      Tab(2).ControlCount=   2
      TabCaption(3)   =   "&Mimic"
      TabPicture(3)   =   "frmDisplay.frx":0060
      Tab(3).ControlEnabled=   0   'False
      Tab(3).Control(0)=   "Image1"
      Tab(3).ControlCount=   1
      TabCaption(4)   =   "Graph 1-4 "
      TabPicture(4)   =   "frmDisplay.frx":007C
      Tab(4).ControlEnabled=   0   'False
      Tab(4).Control(0)=   "Frame5"
      Tab(4).ControlCount=   1
      TabCaption(5)   =   "Graph 5 - 8 "
      TabPicture(5)   =   "frmDisplay.frx":0098
      Tab(5).ControlEnabled=   0   'False
      Tab(5).Control(0)=   "Frame6"
      Tab(5).ControlCount=   1
      TabCaption(6)   =   "Graph 9 - 12 "
      TabPicture(6)   =   "frmDisplay.frx":00B4
      Tab(6).ControlEnabled=   0   'False
      Tab(6).Control(0)=   "Frame7"
      Tab(6).ControlCount=   1
      TabCaption(7)   =   "Graph 13 -16 "
      TabPicture(7)   =   "frmDisplay.frx":00D0
      Tab(7).ControlEnabled=   0   'False
      Tab(7).Control(0)=   "Frame8"
      Tab(7).ControlCount=   1
      TabCaption(8)   =   "Graph 17 - 20"
      TabPicture(8)   =   "frmDisplay.frx":00EC
      Tab(8).ControlEnabled=   0   'False
      Tab(8).Control(0)=   "Frame9"
      Tab(8).ControlCount=   1
      TabCaption(9)   =   "Graph 21 - 24 "
      TabPicture(9)   =   "frmDisplay.frx":0108
      Tab(9).ControlEnabled=   0   'False
      Tab(9).Control(0)=   "Frame10"
      Tab(9).ControlCount=   1
      Begin VB.Frame Frame10 
         Caption         =   "Channels 21 - 24 "
         BeginProperty Font 
            Name            =   "Calisto MT"
            Size            =   9.75
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   6615
         Left            =   -74880
         TabIndex        =   27
         Top             =   720
         Width           =   11295
         Begin MSChart20Lib.MSChart MSChart8 
            Height          =   6255
            Left            =   120
            OleObjectBlob   =   "frmDisplay.frx":0124
            TabIndex        =   28
            Top             =   240
            Width           =   10935
         End
      End
      Begin VB.Frame Frame9 
         Caption         =   "Channels 17 - 20"
         BeginProperty Font 
            Name            =   "Calisto MT"
            Size            =   9.75
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   6495
         Left            =   -74880
         TabIndex        =   25
         Top             =   840
         Width           =   11295
         Begin MSChart20Lib.MSChart MSChart7 
            Height          =   6135
            Left            =   240
            OleObjectBlob   =   "frmDisplay.frx":24AA
            TabIndex        =   26
            Top             =   240
            Width           =   10935
         End
      End
      Begin VB.Frame Frame8 
         Caption         =   "Channels 13 - 16"
         BeginProperty Font 
            Name            =   "Calisto MT"
            Size            =   9.75
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   6615
         Left            =   -74880
         TabIndex        =   23
         Top             =   720
         Width           =   11175
         Begin MSChart20Lib.MSChart MSChart6 
            Height          =   6255
            Left            =   120
            OleObjectBlob   =   "frmDisplay.frx":4848
            TabIndex        =   24
            Top             =   240
            Width           =   10935
         End
      End
      Begin VB.Frame Frame7 
         Caption         =   "Channels  9 - 12 "
         BeginProperty Font 
            Name            =   "Calisto MT"
            Size            =   9.75
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   6495
         Left            =   -74880
         TabIndex        =   21
         Top             =   840
         Width           =   11295
         Begin MSChart20Lib.MSChart MSChart5 
            Height          =   6135
            Left            =   240
            OleObjectBlob   =   "frmDisplay.frx":6BE6
            TabIndex        =   22
            Top             =   240
            Width           =   10815
         End
      End
      Begin VB.Frame Frame6 
         Caption         =   "Channels  5 - 8"
         BeginProperty Font 
            Name            =   "Calisto MT"
            Size            =   9.75
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   6495
         Left            =   -74880
         TabIndex        =   19
         Top             =   840
         Width           =   11175
         Begin MSChart20Lib.MSChart MSChart4 
            Height          =   6135
            Left            =   120
            OleObjectBlob   =   "frmDisplay.frx":8F6C
            TabIndex        =   20
            Top             =   240
            Width           =   10935
         End
      End
      Begin VB.Frame Frame5 
         Caption         =   "Channels  1 - 4 "
         BeginProperty Font 
            Name            =   "Calisto MT"
            Size            =   9.75
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   6495
         Left            =   -74760
         TabIndex        =   17
         Top             =   840
         Width           =   11295
         Begin MSChart20Lib.MSChart MSChart3 
            Height          =   6135
            Left            =   120
            OleObjectBlob   =   "frmDisplay.frx":B2F2
            TabIndex        =   18
            Top             =   240
            Width           =   10575
         End
      End
      Begin VB.TextBox Text1 
         Height          =   285
         Left            =   120
         TabIndex        =   16
         Top             =   480
         Visible         =   0   'False
         Width           =   735
      End
      Begin VB.Frame Frame4 
         Caption         =   "Channels"
         Height          =   6540
         Left            =   -65880
         TabIndex        =   10
         Top             =   780
         Width           =   2325
         Begin MSFlexGridLib.MSFlexGrid msfChannels 
            Height          =   6225
            Left            =   120
            TabIndex        =   12
            Top             =   240
            Width           =   2160
            _ExtentX        =   3810
            _ExtentY        =   10980
            _Version        =   393216
            Rows            =   30
            Cols            =   7
            FixedCols       =   0
            ScrollBars      =   2
            FormatString    =   "Channel No. |    Data   |  Channel No. |    Data   |     ."
         End
      End
      Begin VB.Frame Frame3 
         Caption         =   "Graph"
         Height          =   6540
         Left            =   -74850
         TabIndex        =   9
         Top             =   780
         Width           =   8880
         Begin MSChart20Lib.MSChart MSChart2 
            Height          =   6255
            Left            =   240
            OleObjectBlob   =   "frmDisplay.frx":D678
            TabIndex        =   11
            Top             =   240
            Width           =   8535
         End
      End
      Begin VB.Frame Frame2 
         Caption         =   "Graphs"
         Height          =   6705
         Left            =   -74880
         TabIndex        =   7
         Top             =   600
         Width           =   11415
         Begin MSChart20Lib.MSChart MSChart1 
            Height          =   6285
            Left            =   120
            OleObjectBlob   =   "frmDisplay.frx":F9FE
            TabIndex        =   8
            Top             =   240
            Width           =   11175
         End
      End
      Begin VB.Frame Frame1 
         Caption         =   "Parameters"
         Height          =   5535
         Left            =   120
         TabIndex        =   1
         Top             =   840
         Width           =   11445
         Begin VB.Timer Timer2 
            Left            =   10920
            Top             =   4680
         End
         Begin VB.CommandButton Command2 
            Caption         =   "Download"
            Height          =   260
            Left            =   10455
            TabIndex        =   15
            Top             =   1260
            Width           =   855
         End
         Begin VB.CommandButton cmdConnect 
            Caption         =   "&Connect"
            Height          =   260
            Left            =   10455
            TabIndex        =   14
            Top             =   1005
            Width           =   855
         End
         Begin VB.CommandButton cmdDelete 
            Caption         =   "&Delete"
            Height          =   260
            Left            =   10455
            TabIndex        =   5
            Top             =   750
            Width           =   855
         End
         Begin VB.CommandButton cmdEdit 
            Caption         =   "&Edit"
            Height          =   260
            Left            =   10455
            TabIndex        =   4
            Top             =   495
            Width           =   855
         End
         Begin VB.CommandButton cmdAdd 
            Caption         =   "&Add"
            Height          =   260
            Left            =   10455
            TabIndex        =   3
            Top             =   240
            Width           =   855
         End
         Begin MSFlexGridLib.MSFlexGrid msfParameters 
            Height          =   5115
            Left            =   120
            TabIndex        =   2
            Top             =   210
            Width           =   10335
            _ExtentX        =   18230
            _ExtentY        =   9022
            _Version        =   393216
            Cols            =   10
            FixedCols       =   0
            FocusRect       =   0
            HighLight       =   0
            ScrollBars      =   2
            SelectionMode   =   1
            AllowUserResizing=   1
            FormatString    =   $"frmDisplay.frx":12D15
         End
         Begin VB.Label lblSrno 
            Height          =   240
            Left            =   10560
            TabIndex        =   13
            Top             =   1815
            Visible         =   0   'False
            Width           =   795
         End
      End
      Begin VB.Image Image1 
         Height          =   6480
         Left            =   -74760
         Picture         =   "frmDisplay.frx":12DC3
         Stretch         =   -1  'True
         Top             =   840
         Width           =   11160
      End
   End
End
Attribute VB_Name = "frmDisplay"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Public bNewFlag As Boolean
Public rownum As Integer
Dim dtPreviousTime As Date
Dim iIntervalValue As Integer
Dim blnTransferInProgress  As Boolean
Dim packetSize As Integer
Dim packet(25) As String
Dim iDeviceID, iFuncID, iChannels, iBaudRate  As Integer
Dim strDevName, strParam As String

Private Sub cmdAdd_Click()
Dim i, j As Integer


'---first clear the color...
For i = 1 To msfParameters.Rows - 1
    For j = 0 To msfParameters.Cols - 1
        msfParameters.Row = i
        msfParameters.Col = j
        msfParameters.CellBackColor = vbWhite
    Next
Next

 lblSrno.Caption = ""
 cmdEdit.Enabled = False
 cmdDelete.Enabled = False

 bNewFlag = True
 
 frmParameters.Show vbModal
 
 '---- POPULATING THE FLEX GRID...
 fnPopParameterFlex
  

End Sub

Private Sub cmdConnect_Click()
 '---set the port 'n other parameters
 rownum = 2
 Text2.Text = Time$
 
 fnConnect
 
End Sub

Private Sub cmdDelete_Click()
    Dim con As ADODB.Connection
    Dim rec As ADODB.Recordset
    Set con = modConn.getConnection
    Dim qry As String
    If lblSrno.Caption <> "" Then
        qry = ""
        qry = "delete from modbus where srno = " & lblSrno.Caption
        con.Execute qry
        
        '--- now refresh the flex...
        fnPopParameterFlex
        
        '--- clear the lbl, disable buttons
        lblSrno.Caption = ""
        
         '---disable the buttons
        cmdDelete.Enabled = False
        cmdEdit.Enabled = False

    End If
    
End Sub

Private Sub cmdEdit_Click()
  '---open the form
  frmParameters.Show vbModal
  
  '---disable the buttons
  cmdDelete.Enabled = False
  cmdEdit.Enabled = False

  '---refresh the flex
  fnPopParameterFlex
  
  '---remove the color
  fnRemoveColor
  
End Sub

Private Sub Command1_Click()

MsgBox " This will disocnnect the Device "
MsgBox " Do you really want to close the communication "

 Unload Me
End Sub

Private Sub Command2_Click()
MsgBox " Dowlading Data from RAM"
MsgBox " This will take some time  about 10 min.Please wait "
Call dodownload

Timer1.Enabled = False
End Sub


Private Sub Form_Load()
 ' Fire Rx Event Every Two Bytes
 MSComm1.RThreshold = 2
 
 MSComm1.EOFEnable = True
 MSComm1.InBufferCount = 0
 
 '---set focus on first tab
 SSTab1.Tab = 0
 
 '---disable the buttons
 cmdDelete.Enabled = False
 cmdEdit.Enabled = False
 
 '---populate the parameter flex
 fnPopParameterFlex

End Sub

Private Function fnPopParameterFlex()
Dim con As New ADODB.Connection
Dim rec As New ADODB.Recordset
Dim qry As String
Dim X, icnt As Integer

Set con = getConnection

     '---
     qry = ""
     qry = " select count(*) " & _
            " from modbus "
    
      rec.Open qry, con, adOpenDynamic
      If Not rec.EOF Then icnt = CInt(rec.Fields(0)) + 1 '---1 forFormatString
      icnt = IIf(icnt = 1, 2, icnt) '-----default no. of rows in flex grid
      rec.Close
      
      msfParameters.Rows = icnt
     
      qry = " select * from modbus "
      rec.Open qry, con, adOpenDynamic
      
      X = 1 '---since zeroth row contains column headers
      While Not rec.EOF And X < icnt
            msfParameters.Row = X
            msfParameters.Col = 0
            msfParameters.Text = rec.Fields("DEVICE_NAME") & ""
    
            msfParameters.Row = X
            msfParameters.Col = 1
            msfParameters.Text = rec.Fields("STATUS") & ""
    
            msfParameters.Row = X
            msfParameters.Col = 2
            msfParameters.Text = rec.Fields("PARAMETER") & ""
    
            msfParameters.Row = X
            msfParameters.Col = 3
            msfParameters.Text = rec.Fields("DEVICE_ID") & ""
    
            msfParameters.Row = X
            msfParameters.Col = 4
            msfParameters.Text = rec.Fields("COMM_PORT") & ""
    
            msfParameters.Row = X
            msfParameters.Col = 5
            msfParameters.Text = rec.Fields("BAUD_RATE") & ""
    
            msfParameters.Row = X
            msfParameters.Col = 6
            msfParameters.Text = rec.Fields("POLLING_TIME") & ""
            
            msfParameters.Row = X
            msfParameters.Col = 7
            msfParameters.Text = rec.Fields("CHANNELS") & ""
         
            msfParameters.Row = X
            msfParameters.Col = 8
            msfParameters.Text = rec.Fields("function_id") & ""
            
            msfParameters.Row = X
            msfParameters.Col = 9
            msfParameters.Text = rec.Fields("srno") & ""
         
         rec.MoveNext
         X = X + 1
      Wend
    rec.Close
    
    msfParameters.FormatString = "Device                                |    Status     |  Parameter   | Device ID  |   Port    |   Baud Rate   |  Polling Time  |  Channels  |     Function    |      . "

End Function


Private Sub Form_Unload(Cancel As Integer)
 '---close the port
 If MSComm1.PortOpen Then MSComm1.PortOpen = False
End Sub

Private Sub MSComm1_OnComm()
Dim sData As Variant
Dim temp As Variant
Dim lHighByte As Long
Dim lLowByte As Long
Dim lByte As Long
Dim i As Integer

   
'     If sData <> "" Then
'       fnPopChannelFlex sData
'       fnAddData sData
'       fnPopChart sData
     
     
     'End If
 'End If
 'End If
End Sub

Private Sub msfParameters_Click()
Dim i, j, iTemp As Integer


    '----changing the background color of the selected row
    iTemp = msfParameters.Row
    '---first clear the color...
    For i = 1 To msfParameters.Rows - 1
        For j = 0 To msfParameters.Cols - 1
            msfParameters.Row = i
            msfParameters.Col = j
            msfParameters.CellBackColor = vbWhite
        Next
    Next
    
    msfParameters.Row = iTemp

    For j = 0 To msfParameters.Cols - 1
        msfParameters.Col = j
        msfParameters.CellBackColor = RGB(170, 185, 253) '---light blue
    Next

'---resetting the value of label
lblSrno.Caption = ""

 msfParameters.Col = 9
 If msfParameters.Row > 0 And msfParameters.Text <> "" Then
      lblSrno.Caption = msfParameters.Text
          
      cmdEdit.Enabled = True
      cmdDelete.Enabled = True
    
  Else
      cmdEdit.Enabled = False
      cmdDelete.Enabled = False
  End If

End Sub

Private Function fnRemoveColor()
Dim i, j, iTemp As Integer


'----changing the background color of the selected row
    iTemp = msfParameters.Row
    '---first clear the color...
    For i = 1 To msfParameters.Rows - 1
        For j = 0 To msfParameters.Cols - 1
            msfParameters.Row = i
            msfParameters.Col = j
            msfParameters.CellBackColor = vbWhite
        Next
    Next
    
    msfParameters.Row = iTemp

End Function


Private Function fnPopChart(ByVal str As String)

Static ChannelData(1 To 61, 1 To 24) As Variant
Static ChannelData3(1 To 61, 1 To 24) As Variant
Static ChannelData4(1 To 61, 1 To 24) As Variant
Static ChannelData5(1 To 61, 1 To 24) As Variant
Static ChannelData6(1 To 61, 1 To 24) As Variant
Static ChannelData7(1 To 61, 1 To 24) As Variant
Static ChannelData8(1 To 61, 1 To 24) As Variant

Dim i, j, iTemp, iChann, iTemp3, iTemp4, cdata As Integer
Dim fir, sec As Integer

 '------ Graph for the Common Channels
 
  For i = 1 To 24
  ChannelData(1, i) = "Channel " & i
  Next i
 
  iChann = Asc(Mid$(str, 3, 1))
 
  MSChart1.ColumnCount = iChann / 2
  MSChart1.ShowLegend = True
  MSChart2.ColumnCount = iChann / 2
  MSChart2.ShowLegend = True
         
  For i = 1 To 4
  ChannelData(1, i) = "Channel " & i
  Next i
        
         
  MSChart3.ColumnCount = 4
  MSChart3.ShowLegend = True
  MSChart4.ColumnCount = 4
  MSChart4.ShowLegend = True
  MSChart5.ColumnCount = 4
  MSChart5.ShowLegend = True
  MSChart6.ColumnCount = 4
  MSChart6.ShowLegend = True
  MSChart7.ColumnCount = 4
  MSChart7.ShowLegend = True
  MSChart8.ColumnCount = 4
  MSChart8.ShowLegend = True
         
   '------ Common Graphs
   
    For i = 4 To 11 '       (iChann + 3)
    fir = Asc(Mid$(str, i, 1))
    i = i + 1
    sec = Asc(Mid$(str, i, 1))
    iTemp = fir * 256 + sec
    ChannelData(rownum, ((i - 1) / 2) - 1) = iTemp
    Next i
   
  '-------for Graph 1 - 4
  
    For i = 4 To 11       '(iChann + 3)
    fir = Asc(Mid$(str, i, 1))
    i = i + 1
    sec = Asc(Mid$(str, i, 1))
    iTemp = fir * 256 + sec
    ChannelData3(rownum, ((i - 1) / 2) - 1) = iTemp
    Next i
  '----------------------------

'  '-------for Graph 5 - 8

    For i = 12 To 19        '(iChann + 3)
    fir = Asc(Mid$(str, i, 1))
    i = i + 1
    sec = Asc(Mid$(str, i, 1))
    iTemp = fir * 256 + sec
    ChannelData4(rownum, ((i - 1) / 2) - 1) = iTemp
    Next i
'  '----------------------------
  
  '-------for Graph 9 - 12

    For i = 20 To 27         '(iChann + 3)
    fir = Asc(Mid$(str, i, 1))
    i = i + 1
    sec = Asc(Mid$(str, i, 1))
    iTemp = fir * 256 + sec
    ChannelData5(rownum, ((i - 1) / 2) - 1) = iTemp
    Next i
  '----------------------------

  '-------for Graph 13 - 16

    For i = 28 To 35         '(iChann + 3)
    fir = Asc(Mid$(str, i, 1))
    i = i + 1
    sec = Asc(Mid$(str, i, 1))
    iTemp = fir * 256 + sec
    ChannelData6(rownum, ((i - 1) / 2) - 1) = iTemp
    Next i
  '----------------------------

  '-------for Graph 17 - 20

    For i = 36 To 43         '(iChann + 3)
    fir = Asc(Mid$(str, i, 1))
    i = i + 1
    sec = Asc(Mid$(str, i, 1))
    iTemp = fir * 256 + sec
    ChannelData7(rownum, ((i - 1) / 2) - 1) = iTemp
    Next i
  '----------------------------


  '-------for Graph 21 - 24

    For i = 44 To 51         '(iChann + 3)
    fir = Asc(Mid$(str, i, 1))
    i = i + 1
    sec = Asc(Mid$(str, i, 1))
    iTemp = fir * 256 + sec
    ChannelData8(rownum, ((i - 1) / 2) - 1) = iTemp
    Next i
'  '----------------------------
'
   
   MSChart1.ChartData = ChannelData
   MSChart2.ChartData = ChannelData
   MSChart3.ChartData = ChannelData3
   MSChart4.ChartData = ChannelData4
   MSChart5.ChartData = ChannelData5
   MSChart6.ChartData = ChannelData6
   MSChart7.ChartData = ChannelData7
   MSChart8.ChartData = ChannelData8

   rownum = rownum + 1
   If rownum = 62 Then
   Erase ChannelData
   Erase ChannelData3
   Erase ChannelData4
   Erase ChannelData5
   Erase ChannelData6
   Erase ChannelData7
   Erase ChannelData8
   
   rownum = 2
   Text2.Text = Time$
   End If
 
End Function

Private Sub Timer1_Timer()

Dim dtCurrentTime As Date
'---------------------------------------
Dim sData As Variant
Dim temp As Variant
Dim lHighByte As Long
Dim lLowByte As Long
Dim lByte As Long
Dim i, tmr As Integer

'---------------------------------------
dtCurrentTime = Now
Timer1.Enabled = False
If MSComm1.PortOpen = True Then
'If elapsed time since the last transfer is more than
'the selected seconds, minutes, or hours, do a data transfer.
    If DateDiff("s", dtPreviousTime, dtCurrentTime) >= _
        iIntervalValue Then
        dtPreviousTime = dtCurrentTime
        'But don't start a new transfer if one is in progress.
        If blnTransferInProgress = False Then
        'Call TransferData(DataTransferFormat)
         fnSend
            
   '----------------------------------------------------
    If MSComm1.CommEvent = comEvReceive Then
    sData = MSComm1.Input ' Get data
     
       If sData <> "" Then
       Text3.Text = Time$
       
      '-------------- led for recieve
      
    Do While tmr <> 1500
    tmr = tmr + 1
    Text1.Text = tmr
    Loop
    tmr = 0
    FillColor = RGB(192, 192, 192)
    FillStyle = vbFSSolid
    Circle (ScaleWidth / 50 + 9500, ScaleHeight / 50 + 7550), ScaleHeight / 50
   
      ' cheku data here
        fnPopChannelFlex sData
        fnAddData sData
        fnPopChart sData
             
        End If
       End If
 
 
 '-----------------------------------------------
       
            'MSComm1_OnComm
         End If
    End If
 End If

 
 Timer1.Enabled = True
End Sub

Private Function fnConnect()
 Dim con As New ADODB.Connection
 Dim rec As New ADODB.Recordset
 Dim loopcount As Integer
 Dim cLo, cHi, Hi, Lo As Byte
 Dim i, j As Integer
 Dim err, val As Long
 Dim CRCHi, val1, val2 As Byte
 Dim CRCLo As Byte
 
 Dim qry, strPort As String
 
 '---close already opened port
 If MSComm1.PortOpen Then MSComm1.PortOpen = False
     
 '---set the baud rate 'n port, for the selected device
 If (lblSrno.Caption) <> "" Then
  
 Set con = getConnection
 
 qry = ""
 qry = " Select * from modbus where srno = " & Trim(lblSrno.Caption)
 rec.Open qry, con, adOpenForwardOnly
  
  If Not rec.EOF Then
    iBaudRate = CInt(Left(Trim(rec.Fields("baud_rate")), Len(Trim(rec.Fields("baud_rate"))) - 4))
    strPort = rec.Fields("comm_port") & ""
    iIntervalValue = rec.Fields("polling_time")
    
       
    '---select function and device id also
    iDeviceID = rec.Fields("device_id")
    iChannels = rec.Fields("channels")
    
    '---set device name, parameter for chart
    strDevName = rec.Fields("device_name") & ""
    strParam = rec.Fields("parameter") & ""
    
    Select Case Trim(rec.Fields("function_id"))
     Case "Read Registers (04)"
      iFuncID = 4
     Case "Write Registers(01)"
       iFuncID = 1
    End Select
    
  End If '---rec.EOF
  
  rec.Close
 Else
  MsgBox "Please select the device.", vbInformation, "Select Device"
  Exit Function
 End If '---(lblSrno.Caption) <> ""
 
  MSComm1.RThreshold = 2
 
 '---set the port
 Select Case strPort
  Case "COM1"
    MSComm1.CommPort = 1
  Case "COM2"
    MSComm1.CommPort = 2
  Case Else
    MsgBox "Please select the valid port.", vbInformation, "InValid Port """
    msfParameters.SetFocus
    Exit Function
 End Select
 
 '---set the baud rate alongwith  no parity, 8 data bits, 1 stop bit
 MSComm1.Settings = iBaudRate & ",N,8,1"
    
 ' Open the port
 MSComm1.PortOpen = True

 '---set the packet size according to no. of bytes required
 packetSize = 2 + 4

 dtPreviousTime = Now
 blnTransferInProgress = False
 
 '---set the no. of columns for graph as per no. of channels
' MSChart1.ColumnCount = iChannels
' MSChart1.Title.Text = "Device : " & strDevName & "          Parameter : " & strParam
'
' MSChart2.ColumnCount = iChannels
' MSChart2.Title.Text = "Device : " & strDevName & "          Parameter : " & strParam
'
 '---form the packet
 packet(0) = iDeviceID       '0x01; /*device no*/
 packet(1) = iFuncID         '0x04; /*function*/
 packet(2) = 0               '0x00; /*starting address HI*/
 packet(3) = 0               '0x00; /*starting address LO*/
 packet(4) = 0               '0x00;
 packet(5) = iChannels       'channelNos;
 
 
     '--- the two bytes of the Error Code
     
'pointer1:

    CRCHi = &HFF
    CRCLo = &HFF
    err = CLng(CRCHi) * &H100 Or CLng(CRCLo)

   '---- the Anding Number cal as Hex A001
   
    val1 = &HA0
    val2 = &H1
    val = CLng(val1) * &H100 Or CLng(val2)
    
    
   '----  the CRC calculation starts from here
   
    For i = 0 To packetSize - 1  ' --- for the packet size
   
    err = CLng(err) Xor packet(i)
    
    For j = 0 To 7           ' --- for the 8 bits of each byte

    If (CLng(err) And CLng(1)) Then  '-- if 1 in the LSB bit

    err = CLng(err) \ CLng(2)
    err = CLng(err) Xor CLng(val)
    Else
    
    err = CLng(err) \ CLng(2)     '-- zero in the LSB bit
    End If

    Next j
    Next i
   
    cLo = CLng(err) And CLng(CRCHi)   ' --- the cLo Byte
    cHi = CLng(err) \ CLng(2 ^ 8)     ' ----- the cHi Byte

    packet(6) = cLo
    packet(7) = cHi
    fnSend
    If cmdConnect.Caption = "&Connect" Then cmdConnect.Caption = "&DisConn." Else cmdConnect.Caption = "&Disconnect"
 
 
 'Timer1.Enabled = True
 
End Function

Private Function fnSend()
Dim j As Integer
packetSize = 8
 '---set the trnsfer flag to true
 blnTransferInProgress = True
  
  '------------ leds ON
  FillColor = RGB(0, 255, 0)
  FillStyle = vbFSSolid
  Circle (ScaleWidth / 50 + 9500, ScaleHeight / 50 + 7550), ScaleHeight / 50
  
  '------------ turn off
  
  
 '---transmit the data
 For j = 0 To packetSize - 1
  MSComm1.Output = Chr$(packet(j))
 Next j

 '---set the trnasfer flag to false
 blnTransferInProgress = False
 
End Function

Private Function fnPopChannelFlex(ByVal str As String)
Dim i, iRow, iChann    As Integer
 Dim fir, sec As Integer
 Dim iTemp As Long
 iChann = Asc(Mid$(str, 3, 1))
 
 msfChannels.Clear
 msfChannels.FormatString = "Channel No. |    Data   |  Channel No. |    Data   |     ."
 
 For i = 4 To (iChann + 3)
   'If i Mod 2 = 0 Then MSChart1.Column = i
   'DoEvents
   
   'MsgBox (Len(str))
   
   
'   iTemp = Asc(Mid$(str, i, 1)) * 256 + Asc(Mid$(str, i + 1, 1))
   fir = Asc(Mid$(str, i, 1))
   i = i + 1
   sec = Asc(Mid$(str, i, 1))
   iTemp = fir * 256 + sec
   '---set channel no.
   msfChannels.Col = 0
   msfChannels.Row = (i - 1) / 2 - 1
   msfChannels.Text = (i - 1) / 2 - 1
   
   '---set channel data
   msfChannels.Col = 1
   msfChannels.Row = (i - 1) / 2 - 1
   msfChannels.Text = iTemp
  
 Next i
End Function

Private Function fnAddData(ByVal str As String)

Dim con As New ADODB.Connection
Dim iChann  As Integer
Dim qry As String
Dim i As Integer
Dim iTemp As Long
Dim curTime As String
Dim fir, sec As Integer

Set con = getConnection

iChann = Asc(Mid$(str, 3, 1))
'iChann = Left(str, 7)
curTime = Format(Now, "hh:mm:ss dd-mm-yy")
qry = " INSERT INTO channelData ( Time1,Ch1 ,Ch2,Ch3,Ch4,Ch5,Ch6,Ch7,Ch8, " & _
                               " Ch9,Ch10,Ch11,Ch12,Ch13,Ch14,Ch15,Ch16," & _
                               " Ch17,Ch18,Ch19,Ch20,Ch21,Ch22,Ch23,Ch24) values ( '" & _
                                 Now() & "' , "

   For i = 4 To (iChann + 3)

   fir = Asc(Mid$(str, i, 1))
   i = i + 1
   sec = Asc(Mid$(str, i, 1))
   iTemp = fir * 256 + sec

    'i = i + 1

    If i < (iChann + 2) Then
    qry = qry & iTemp & ", "
    Else
          qry = qry & iTemp & ")"
    End If
    ' i = i + 1
     Next i
     'qry = qry & ")"

    ' MsgBox "qry :: " & qry
     con.Execute qry

     End Function
Private Function dodownload()
    
 Dim con As New ADODB.Connection
 Dim rec As New ADODB.Recordset
 Dim loopcount As Long
 Dim cLo, cHi, Hi, Lo As Byte
 Dim i, j As Integer
 Dim err, val As Long
 Dim CRCHi, val1, val2 As Byte
 Dim CRCLo As Byte
 Dim qry, strPort As String
 Dim tmr As Long
 Dim dtCurrentTime As Date
'---------------------------------------
Dim sData As Variant
Dim temp As Variant
Dim lHighByte As Long
Dim lLowByte As Long
Dim lByte As Long
'---------------------------------------
dtCurrentTime = Now
      
    Dim cnt As Integer
    cnt = 1
    
      
 '---close already opened port
 If MSComm1.PortOpen Then MSComm1.PortOpen = False
     
 '---set the baud rate 'n port, for the selected device
 If (lblSrno.Caption) <> "" Then
  
 Set con = getConnection
 
 qry = ""
 qry = " Select * from modbus where srno = " & Trim(lblSrno.Caption)
 rec.Open qry, con, adOpenForwardOnly
  
  If Not rec.EOF Then
    iBaudRate = CInt(Left(Trim(rec.Fields("baud_rate")), Len(Trim(rec.Fields("baud_rate"))) - 4))
    strPort = rec.Fields("comm_port") & ""
    iIntervalValue = rec.Fields("polling_time")
    
       
    '---select function and device id also
    iDeviceID = rec.Fields("device_id")
    iChannels = rec.Fields("channels")
    
    '---set device name, parameter for chart
    strDevName = rec.Fields("device_name") & ""
    strParam = rec.Fields("parameter") & ""
    
    Select Case Trim(rec.Fields("function_id"))
     Case "Read Registers (04)"
      iFuncID = 4
     Case "Write Registers(01)"
       iFuncID = 1
    End Select
    
  End If '---rec.EOF
  
  rec.Close
 Else
  MsgBox "Please select the device.", vbInformation, "Select Device"
  Exit Function
 End If '---(lblSrno.Caption) <> ""
 
  MSComm1.RThreshold = 2
 
 '---set the port
 Select Case strPort
  Case "COM1"
    MSComm1.CommPort = 1
  Case "COM2"
    MSComm1.CommPort = 2
  Case Else
    MsgBox "Please select the valid port.", vbInformation, "InValid Port """
    msfParameters.SetFocus
    Exit Function
 End Select
 
 '---set the baud rate alongwith  no parity, 8 data bits, 1 stop bit
 MSComm1.Settings = iBaudRate & ",N,8,1"
  
       
 ' Open the port
 MSComm1.PortOpen = True
 
'For cnt = 0 To 20
'        loopcount = loopcount + 1

packetSize = 6
 'dtPreviousTime = Now
 blnTransferInProgress = False
 
 
 '-------------------check device
 
        packet(0) = iDeviceID    '/*device no*/
        packet(1) = 8         'iFuncID         '0x04; /*function*/
        packet(2) = 0               '0x00; /*starting address HI*/
        packet(3) = 0               '0x00; /*starting address LO*/
        packet(4) = 0               '0x00;
        packet(5) = 0               'iChannels channelNos;
              
        
    CRCHi = &HFF
    CRCLo = &HFF
    err = CLng(CRCHi) * &H100 Or CLng(CRCLo)

   '---- the Anding Number cal as Hex A001
   
    val1 = &HA0
    val2 = &H1
    val = CLng(val1) * &H100 Or CLng(val2)
    
    
   '----  the CRC calculation starts from here
   
    For i = 0 To packetSize - 1  ' --- for the packet size
   
    err = CLng(err) Xor packet(i)
    
    For j = 0 To 7           ' --- for the 8 bits of each byte

    If (CLng(err) And CLng(1)) Then  '-- if 1 in the LSB bit

    err = CLng(err) \ CLng(2)
    err = CLng(err) Xor CLng(val)
    Else
    
    err = CLng(err) \ CLng(2)     '-- zero in the LSB bit
    End If

    Next j
    Next i
   
    cLo = CLng(err) And CLng(CRCHi)   ' --- the cLo Byte
    cHi = CLng(err) \ CLng(2 ^ 8)     ' ----- the cHi Byte

    packet(6) = cLo
    packet(7) = cHi
    cnt = cnt + 1
    
    '-----timer
    
    Do While tmr <> 1000
    tmr = tmr + 1
    Text1.Text = tmr
    Loop
    tmr = 0
    
    If MSComm1.PortOpen = True Then
    fnSend
    End If
    
    If MSComm1.CommEvent = comEvReceive Then
        
     sData = MSComm1.Input ' Get data
      End If
 
  If sData <> "" Then
 '      fnPopChannelFlex sData
      
      ' cheku data here
       fnAddData1 (sData)
       
       End If
        
 '----------------- end the check

 ' loopcount = 500
   For cnt = 0 To 500
   loopcount = loopcount + 1
  
  
  '----------------
  FillColor = RGB(0, 255, 0)
  FillStyle = vbFSSolid
  Circle (ScaleWidth / 50 + 9500, ScaleHeight / 50 + 7550), ScaleHeight / 50
  
        'loopcount = loopcount + 1
    '---set the packet size according to no. of bytes required
        packetSize = 2 + 4
        
    Lo = CLng(loopcount) And CLng(CRCHi)   ' --- the cLo Byte
    Hi = CLng(loopcount) \ CLng(2 ^ 8)     ' ----- the cHi Byte
        
'        Hi = loopcount And &HFF00
'        Lo = loopcount And &HFF
        
        packet(0) = iDeviceID    '/*device no*/
        packet(1) = 8         'iFuncID         '0x04; /*function*/
        packet(2) = (Hi)               '0x00; /*starting address HI*/
        packet(3) = (Lo)               '0x00; /*starting address LO*/
        packet(4) = 0               '0x00;
        packet(5) = 0               'iChannels channelNos;
        
        
    CRCHi = &HFF
    CRCLo = &HFF
    err = CLng(CRCHi) * &H100 Or CLng(CRCLo)

   '---- the Anding Number cal as Hex A001
   
    val1 = &HA0
    val2 = &H1
    val = CLng(val1) * &H100 Or CLng(val2)
    
    
   '----  the CRC calculation starts from here
   
    For i = 0 To packetSize - 1  ' --- for the packet size
   
    err = CLng(err) Xor packet(i)
    
    For j = 0 To 7           ' --- for the 8 bits of each byte

    If (CLng(err) And CLng(1)) Then  '-- if 1 in the LSB bit

    err = CLng(err) \ CLng(2)
    err = CLng(err) Xor CLng(val)
    Else
    
    err = CLng(err) \ CLng(2)     '-- zero in the LSB bit
    End If

    Next j
    Next i
   
    cLo = CLng(err) And CLng(CRCHi)   ' --- the cLo Byte
    cHi = CLng(err) \ CLng(2 ^ 8)     ' ----- the cHi Byte

    packet(6) = cLo
    packet(7) = cHi
    'cnt = cnt + 1
    
    '-----timer
    
    Do While tmr <> 10000
    tmr = tmr + 1
    Text1.Text = tmr
        Loop
    tmr = 0
    
    If MSComm1.PortOpen = True Then
    fnSend
    End If
    
   
  '      If MSComm1.CommEvent = comEvReceive Then
        
     sData = MSComm1.Input ' Get data
            
       If sData <> "" Then
 '      fnPopChannelFlex sData
      
      ' cheku data here
      
      FillColor = RGB(0, 0, 255)
      FillStyle = vbFSSolid
      Circle (ScaleWidth / 50 + 9500, ScaleHeight / 50 + 7550), ScaleHeight / 50
  
    Do While tmr <> 2000
    tmr = tmr + 1
    Text1.Text = tmr
        Loop
    tmr = 0
  
       fnAddData1 (sData)
        
'       fnPopChart sData
           
       End If
  
   ' End If
   
    Next
   'If cnt = 20 Then
        
        MsgBox "Download complete !!"
        MsgBox " Data Stored at :C:\Program Files\Project1\mod.mdb"
        'End If
      
        End Function
Private Function fnAddData1(ByVal str As String)

Dim con As New ADODB.Connection
Dim iChann  As Integer
Dim iChann1 As Integer
Dim qry As String
Dim i As Integer
Dim iTemp As Long
Dim nos, nos1, no2 As Integer
Dim nos4, nos5, nos6, nos8, nos9 As Integer

Dim curTime, dltime As String
Dim fir, sec As Integer

Set con = getConnection
If Len(str) <= 10 Then Exit Function
  ' i = Len(str) - 2
'If Len(str) > 59 Then Exit Function
'MsgBox Len(str)
iChann1 = Asc(Mid$(str, 3, 1))

'iChann = Asc(Mid$(str, 4, 1))
'nos1 = Hex(iChann)
'For i = 5 To 9
iTemp = Asc(Mid$(str, 4, 1))
nos4 = Hex(iTemp)
iTemp = Asc(Mid$(str, 5, 1))
nos5 = Hex(iTemp)
iTemp = Asc(Mid$(str, 7, 1))
nos6 = Hex(iTemp)
iTemp = Asc(Mid$(str, 8, 1))
nos8 = Hex(iTemp)
iTemp = Asc(Mid$(str, 9, 1))
nos9 = Hex(iTemp)
dltime = nos4 & "/" & nos5 & "/0" & nos6 & " " & nos8 & ":" & nos9 & ""
'qry = qry & nos & ", "
'Next i
'iChann = ""


'curTime = Format(Now, "hh:mm:ss dd-mm-yy")
'qry = " INSERT INTO downloaddata ( Date1,Month1,Mill,Year1,Hr,Minn,Ch1 ,Ch2,Ch3,Ch4,Ch5,Ch6,Ch7,Ch8, " & _
'                               " Ch9,Ch10,Ch11,Ch12,Ch13,Ch14,Ch15,Ch16," & _
'                               " Ch17,Ch18,Ch19,Ch20,Ch21,Ch22,Ch23,Ch24) values ( " & _
'                                 iChann & ", "
'qry = " INSERT INTO downloaddata ( Date1,Month1,Mill,Year1,Hr,Minn,Ch1 ,Ch2,Ch3,Ch4,Ch5,Ch6,Ch7,Ch8, " & _
'                               " Ch9,Ch10,Ch11,Ch12,Ch13,Ch14,Ch15,Ch16," & _
'                               " Ch17,Ch18,Ch19,Ch20,Ch21,Ch22,Ch23,Ch24) values ( " & _
'                                 nos1 & ", "
 qry = " INSERT INTO downloaddata ( Time1,Ch1 ,Ch2,Ch3,Ch4,Ch5,Ch6,Ch7,Ch8, " & _
                               " Ch9,Ch10,Ch11,Ch12,Ch13,Ch14,Ch15,Ch16," & _
                               " Ch17,Ch18,Ch19,Ch20,Ch21,Ch22,Ch23,Ch24) values ( '" & _
                                 dltime & "', "
'If i = 58 Then Exit Function

'For i = 5 To 9
'iTemp = Asc(Mid$(str, i, 1))
'nos = Hex(iTemp)
'qry = qry & nos & ", "
'Next i

   For i = 10 To (iChann1 + 3)

   fir = Asc(Mid$(str, i, 1))
   i = i + 1
   sec = Asc(Mid$(str, i, 1))
   iTemp = fir * 256 + sec

    'i = i + 1

    If i < (iChann1 + 2) Then
    qry = qry & iTemp & ", "
    Else
          qry = qry & iTemp & ")"

    End If
    ' i = i + 1
    'If i = 58 Then Exit Function
    
     Next
     'qry = qry & ")"

 '  MsgBox "qry :: " & qry
 On Error Resume Next
     con.Execute qry
 
  
     End Function

