mariaseban123 0 Newbie Poster

Dear programmers,

My name is Thomas . I am a really bad situation now . Plz help me becoz i dont know VB .net . But suprisingly i had to look after a telephone call capturing system in a famous hotel in dubai . The s/w is developed using vb .net . It reads data using a tcp ip connection from another system on the network . But my pblm is the software i am mentioned is not reading data properly . A lot of missing in values are there .Up to this we thought that this was a port pblm and we thought that system is not getting values fromt he port . We check the port through hyperterminal . But the values are coming . But while running our s/w . It seesm to be stuck on mid night and all most 50 rows its missing . I am in situation that i dotn know what to do . Plz help .This is my last door .Plzz help me your brother . I will paste the code here from which the connection is made

Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Imports System.Net.Sockets
Imports System.Text

Public Class RTCPIPClass
    Dim RIESever As TcpListener
    Dim RIEClient As TcpClient
    Private WithEvents _SerialPort As SerialPort
    Private _bContinueSerialAccess As Boolean = False
    Private WithEvents _form1 As Form1
    Private _Errmsg As ErrorMessage
    Private _bDataArrived As Boolean
    Private _tServerReadingThread As Thread
    Private _tServerWritingThread As Thread
    Private _tConnectionAliveCheckingThread As Thread
    Private _tDatareceivedThread As Thread
    Private IDCode As String
    Private IPAdress As String
    Private PortNo As Integer
    Private RIELogFileHandler As RIELogFileHandler
    Private RIEICodeParameters As RIEICodeParameters
    Private Queue As Queue = New Queue
    Private CompositeQueue As Queue = New Queue
    Private pLastMessageReceived As Date = Now()
    Private pSPILinkOpen As Boolean
    Private NumberOfTries As Integer
    Private QueueUnderProcess As Boolean = False
    Private CompositeQueueUnderProcess As Boolean = False
    Private networkStream As NetworkStream
    Private Errmsg As ErrorMessage

    Public ReadOnly Property LastMessageReceived() As Date
        Get
            Return pLastMessageReceived
        End Get
    End Property

    Public Property SPILinkOpen() As Boolean
        Set(ByVal value As Boolean)
            pSPILinkOpen = value
        End Set
        Get
            Return pSPILinkOpen
        End Get
    End Property

    Public Sub New(ByVal RIELogFileHandler As RIELogFileHandler, ByVal RIEICodeParameters As RIEICodeParameters) 'Default Constructor
        Try

            IDCode = RIEICodeParameters.IDCode
            PortNo = RIEICodeParameters.PortNO
            IPAdress = RIEICodeParameters.IPAddress
            _Errmsg = New ErrorMessage()
           
            Try
                RIESever = New TcpListener(PortNo)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

            Try
                ServerStarts()
                RIELogFileHandler.QueueMessage("Server Port " & RIEICodeParameters.PortNO & " opened and reserved for communication with IDCode " & RIEICodeParameters.IDCode, "L")
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Sever Starts")
            End Try

            Try
                Me.RIELogFileHandler = RIELogFileHandler
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

            Try
                Me.RIEICodeParameters = RIEICodeParameters
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

            Try
                _bContinueSerialAccess = True
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

            Try
                _tDatareceivedThread = New Thread(AddressOf Port_DataReceived)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            Try
                _tDatareceivedThread.Start()
                RIELogFileHandler.QueueMessage("Port Reader Checking(_tDatareceivedThread) started for " & RIEICodeParameters.PortNO & " and reserved for communication with IDCode " & RIEICodeParameters.IDCode, "L")
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

            Try
                _tServerReadingThread = New Thread(AddressOf ReadFromPort)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            Try
                _tServerReadingThread.Start()
                RIELogFileHandler.QueueMessage("Port Reader (_tServerReadingThread) started for " & RIEICodeParameters.PortNO & " and reserved for communication with IDCode " & RIEICodeParameters.IDCode, "L")
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

            Try
                _tServerWritingThread = New Thread(AddressOf WriteToPort)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

            Try
                _tServerWritingThread.Start()
                RIELogFileHandler.QueueMessage("Port Writer (_tServerWritingThread) started for " & RIEICodeParameters.PortNO & " and reserved for communication with IDCode " & RIEICodeParameters.IDCode, "L")
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

            If RIEICodeParameters.ConnectionAliveChecking = "Y" Then
                'Create New Thread to Check Communication Link
                Try
                    _tConnectionAliveCheckingThread = New Thread(AddressOf ConnectionAliveChecker)
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try

                Try
                    _tConnectionAliveCheckingThread.Start()
                    RIELogFileHandler.QueueMessage("Connection Alive Checking (_tConnectionAliveCheckingThread) started for " & RIEICodeParameters.PortNO & " - IDCode " & RIEICodeParameters.IDCode & " - Checking Interval - " & RIEICodeParameters.ConnectionTimeOutInterval.ToString, "L")
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try
            End If

            NumberOfTries = 0
            pSPILinkOpen = True

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub ServerStarts()
        Try
            RIESever.Start()
            RIEClient = RIESever.AcceptTcpClient()
            networkStream = RIEClient.GetStream()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub Port_DataReceived()
        While networkStream.CanRead()
            _bDataArrived = True
            pLastMessageReceived = Now()
        End While
    End Sub


    Private Sub ConnectionAliveChecker()
        Dim CurrentDateTime As Date
        Dim ErrorMessage As String
        Dim Minutes As Integer
        Dim IntervalInMinutes As Integer
        Dim lretval As Long
        IntervalInMinutes = CType(RIEICodeParameters.ConnectionTimeOutInterval / 60000, Integer)

        While True
            CurrentDateTime = Now()

            Minutes = DateDiff(DateInterval.Minute, pLastMessageReceived, CurrentDateTime)

            If Minutes >= IntervalInMinutes Then

                ErrorMessage = "No Communication from Port " & RIEICodeParameters.PortNO.ToString() & " At " & RIEICodeParameters.RIECompanyName & " Interfacing with " & RIEICodeParameters.RIEIDCompanyName & " for the past " & Minutes.ToString & " Minutes. Kindly verify if the Interface is Active."
                RIELogFileHandler.QueueMessage(ErrorMessage, "L")
                lretval = Shell("net send " & RIEICodeParameters.NotificationPCIP & " " & """" & ErrorMessage & """", vbHide)
                lretval = Shell("net send " & RIEICodeParameters.AlternateNotificationPCIP & " " & """" & ErrorMessage & """", vbHide)

                RaiseEvent ConnectionDead(ErrorMessage)
            End If
            System.Threading.Thread.Sleep(RIEICodeParameters.ConnectionTimeOutInterval)
        End While
    End Sub
    Public Event ConnectionDead(ByVal ErrorMsg As String)

    Private Sub ReadFromPort()
        Dim bytesFrom(20048) As Byte
        Dim _ReadDataString As String
        While _bContinueSerialAccess
            If _bDataArrived Then
                Try
                    ReDim bytesFrom(RIEClient.ReceiveBufferSize)
                    networkStream.Read(bytesFrom, 0, CInt(RIEClient.ReceiveBufferSize))
                    _ReadDataString = System.Text.Encoding.ASCII.GetString(bytesFrom)
                    Array.Clear(bytesFrom, 0, bytesFrom.Length - 1)

                    Try
                        _ReadDataString = _ReadDataString
                    Catch ex As Exception
                        RIELogFileHandler.QueueMessage("Assign Error : " & ex.Message, "E")
                    End Try
                    Try
                        _bDataArrived = False
                    Catch ex As Exception
                        MessageBox.Show("_bDataArrived = False" & ex.Message)
                    End Try
                    Try
                        RaiseEvent ReceiveData(IDCode, _ReadDataString)
                    Catch ex As Exception
                    End Try
                Catch ex As Exception
                    RIELogFileHandler.QueueMessage("Read Error : " & ex.Message, "E")
                    _Errmsg.ErrormessageString = " Error in Reading Data : " & ex.Message
                    _form1.SendMailer()
                    _form1.Shutdown()
                End Try
            End If
        End While
    End Sub

    Public Event ReceiveData(ByVal IDCode As String, ByVal DataString As String)

    Public Event CommunicationLinkDown()

    Public Sub NAKMsg()
        ' Retransmit Data If NAK Message is received from the Interface
        ' Nothing to be done in Philips SOHO yet, Perhaps Maginet could behave different
        ' If NEgative ACK is received and If the Message is generated from the Front End, 
        ' then it could be a mistake. Log the NEgative ACK and Delete the Message from the
        ' Queue
        If QueueUnderProcess Then
            If Queue.Count > 0 Then
                Dim QueueEnumerator As IEnumerator = Me.Queue.GetEnumerator
                QueueEnumerator.MoveNext()
                QueueEnumerator.Current.MsgStatus = True
                QueueEnumerator = Nothing
                RIELogFileHandler.QueueMessage("Received NAK. The Current Message shall not be send and is deleted from the SendQueue[S] : " & QueueEnumerator.Current.Msg, "E")
            End If
        End If
        If CompositeQueueUnderProcess Then
            If CompositeQueue.Count > 0 Then
                Dim QueueEnumerator As IEnumerator = Me.CompositeQueue.GetEnumerator
                QueueEnumerator.MoveNext()
                QueueEnumerator.Current.MsgStatus = True
                QueueEnumerator = Nothing
                RIELogFileHandler.QueueMessage("Received NAK. The Current Message shall not be send and is deleted from the SendQueue[C] : " & QueueEnumerator.Current.Msg, "E")
            End If
        End If

    End Sub

    Public Sub ACKMsg()
        'Taken from Form1.vb
        If SPILinkOpen = False Then
            SPILinkOpen = True
            NumberOfTries = 0
            If RIEICodeParameters.ResetTransmissionControlCount = "Y" Then
                'If Reset is Configured, the Reinit the Transmission Control Count - Check Out ??? 
                RIEICodeParameters.ResetTransmissionControlCountFlag = "Y"
            End If
            RIEICodeParameters.IDStatus = "O" ' Update ID Code Status to Open
            Try
                RIELogFileHandler.QueueMessage("Schdeuled Messager (tScheduledMessager) Re started for IDCode " & RIEICodeParameters.IDCode, "L")
                RIELogFileHandler.QueueMessage("Comminucation re-established between Reliance Interface Engine and IDCode " & RIEICodeParameters.IDCode, "L")
            Catch ex As Exception
                RIELogFileHandler.QueueMessage("Could not ReStart Schdeuled Messager (tScheduledMessager) for IDCode " & RIEICodeParameters.IDCode, "E")
                RIELogFileHandler.QueueMessage("Could not re-establish Communication between Reliance Interface Engine and IDCode " & RIEICodeParameters.IDCode, "E")
            End Try
        End If
        If CompositeQueueUnderProcess Then
            If CompositeQueue.Count > 0 Then
                Dim QueueEnumerator As IEnumerator = Me.CompositeQueue.GetEnumerator
                QueueEnumerator.MoveNext()
                QueueEnumerator.Current.MsgStatus = True
                QueueEnumerator = Nothing
            End If
        End If
        If QueueUnderProcess Then
            If Queue.Count > 0 Then
                Dim QueueEnumerator As IEnumerator = Me.Queue.GetEnumerator
                QueueEnumerator.MoveNext()
                QueueEnumerator.Current.MsgStatus = True
                QueueEnumerator = Nothing
            End If
        End If
    End Sub

    Public Sub WriteToPort(ByVal _WriteDataString As String, ByVal WritetoPort As Boolean, ByVal Logflag As Boolean)
        Try
            Dim sendBytes As [Byte]()
            If WritetoPort Then
                sendBytes = Encoding.ASCII.GetBytes(_WriteDataString)
                networkStream.Write(sendBytes, 0, sendBytes.Length)
                networkStream.Flush()
            End If
            If Logflag Then
                RIELogFileHandler.QueueMessage(" ID -> RIE : [D] " & _WriteDataString & " [" & Now() & "]", "L") 'chngd test purpose
            End If
        Catch ex As Exception
            ErrorMessage.ErrorMsg = "Error in Writing Response : " & ex.Message.ToString()
           
        End Try
    End Sub

    Private Sub WriteToPort()
        Dim _WriteDataString As String
        Dim sendBytes As [Byte]()
        While _bContinueSerialAccess
            If NumberOfTries >= RIEICodeParameters.IDSendingRetryCount And RIEICodeParameters.IDSendingRetryCount <> 0 And SPILinkOpen Then
                SPILinkOpen = False
                RIELogFileHandler.QueueMessage("ID Not Responding [NumberOfTries exceed IDSendingRetryCount], communication error with IDCode " & RIEICodeParameters.IDCode, "L")
                RIELogFileHandler.QueueMessage("Clearing SendMessage Queue. Please send the following messages again : ", "L")
                If Queue.Count > 0 Then
                    Dim QueueEnumerator As IEnumerator = Me.Queue.GetEnumerator
                    While QueueEnumerator.MoveNext()
                        RIELogFileHandler.QueueMessage("Could not send the message : " & QueueEnumerator.Current.Msg(), "L")
                    End While
                    Queue.Clear()
                    QueueUnderProcess = False
                End If
                If CompositeQueue.Count > 0 Then
                    Dim QueueEnumerator As IEnumerator = Me.CompositeQueue.GetEnumerator
                    While QueueEnumerator.MoveNext()
                        RIELogFileHandler.QueueMessage("Could not send the message : " & QueueEnumerator.Current.Msg(), "L")
                    End While
                    CompositeQueue.Clear()
                    CompositeQueueUnderProcess = False
                End If
                RIELogFileHandler.QueueMessage("All queued messages purged. No messages will be send till the connection is re-established.", "L")
                RIELogFileHandler.QueueMessage("Port Writer (_tServerPortWritingThread) in Sleep State for Port " & RIEICodeParameters.PortNO & " with IDCode " & RIEICodeParameters.IDCode, "L")
                RaiseEvent CommunicationLinkDown()
            End If

            'If SPILinkOpen Then
            ' Get the Top Object and check the Status
            Dim TopMsgObj As MsgSentStatus
            'Check the Queue to see if any pending messages to send
            If Queue.Count > 0 And CompositeQueueUnderProcess <> True Then
                TopMsgObj = Queue.Peek()
                ' If ACK Received then POP the Msg from the Queue
                If TopMsgObj.MsgStatus = True Then
                    TopMsgObj = Queue.Dequeue()
                    NumberOfTries = 0
                    QueueUnderProcess = False
                Else
                    _WriteDataString = TopMsgObj.Msg
                    Try
                        sendBytes = Encoding.ASCII.GetBytes(_WriteDataString)
                        networkStream.Write(sendBytes, 0, sendBytes.Length)
                        If TopMsgObj.MaintainEvenLog = "Y" Then
                            RIELogFileHandler.QueueMessage("Write Port- RIE -> ID : [S] " & _WriteDataString & " [" & TopMsgObj.TimeStamp & "]", "L")
                        End If
                        If SPILinkOpen Then
                            NumberOfTries += 1
                        End If
                        QueueUnderProcess = True
                    Catch ex As Exception
                        _tServerReadingThread.Join()
                        RIELogFileHandler.QueueMessage("Port Write Error. Message : " & _WriteDataString & " " & ex.Message, "E")
                    End Try
                End If
            End If

            If CompositeQueue.Count > 0 And QueueUnderProcess <> True Then
                TopMsgObj = CompositeQueue.Peek()
                ' If ACK Received then POP the Msg from the Queue
                If TopMsgObj.MsgStatus = True Then
                    TopMsgObj = CompositeQueue.Dequeue()
                    NumberOfTries = 0
                    CompositeQueueUnderProcess = False
                Else
                    _WriteDataString = TopMsgObj.Msg
                    Try
                        sendBytes = Encoding.ASCII.GetBytes(_WriteDataString)
                        networkStream.Write(sendBytes, 0, sendBytes.Length)
                        If TopMsgObj.MaintainEvenLog = "Y" Then
                            RIELogFileHandler.QueueMessage("Write Port - RIE -> ID : [C] " & _WriteDataString & " [" & TopMsgObj.TimeStamp & "]", "L")
                        End If
                        If SPILinkOpen Then
                            NumberOfTries += 1
                        End If
                        CompositeQueueUnderProcess = True
                    Catch ex As Exception
                        _tServerReadingThread.Join()
                        RIELogFileHandler.QueueMessage("Port Write Error. Message : " & _WriteDataString & " " & ex.Message, "E")
                    End Try
                End If
            End If
            Thread.Sleep(RIEICodeParameters.IDCodeReSendingInterval)
        End While
    End Sub


    Public Sub WriteToPortQueue(ByVal _WriteDataString As String, ByVal IDRequestType As Char, ByVal TimeStamp As Date, ByVal MaintainEvenLog As Char)
        ' If The Transmission is not Control Messages Store the Previous Message
        Dim sendBytes As [Byte]()
        If (RIEICodeParameters.ACKMsgString <> _WriteDataString _
            And RIEICodeParameters.ACKMsgString1 <> _WriteDataString _
            And RIEICodeParameters.NAKMsgString <> _WriteDataString _
            And RIEICodeParameters.INITMsgString <> _WriteDataString) Then
            Dim MsgSentStatus As MsgSentStatus = New MsgSentStatus()
            MsgSentStatus.Msg = _WriteDataString
            MsgSentStatus.MsgStatus = False
            MsgSentStatus.TimeStamp = TimeStamp
            MsgSentStatus.MaintainEvenLog = MaintainEvenLog
            ' IF the MEssage is a Part of Single Response, then write to Single Queue
            ' Else write to Multiple Message Queue ..Eg. Response to Init Message
            If IDRequestType = "I" Then
                Queue.Enqueue(MsgSentStatus)
            Else
                CompositeQueue.Enqueue(MsgSentStatus)
            End If
        Else
            'Write the Simulated Control Message to the Serial Port 
            'For Testing Environment
            sendBytes = Encoding.ASCII.GetBytes(_WriteDataString)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            Array.Clear(sendBytes, 0, sendBytes.Length)
            networkStream.Flush()
            If MaintainEvenLog = "Y" Then
                RIELogFileHandler.QueueMessage("write Port Que RIE -> ID : [D] " & _WriteDataString & " [" & TimeStamp & "]", "L")
            End If
        End If
    End Sub


    Public Sub StopThreads()

        If _tServerReadingThread.ThreadState <> ThreadState.Aborted Or _
            _tServerReadingThread.ThreadState <> ThreadState.AbortRequested Then
            Try
                _tServerReadingThread.Abort()
                RIELogFileHandler.QueueMessage("Port Reader (_tDataReadingThread) for " & RIEICodeParameters.PortNO & " stopped for IDCode " & RIEICodeParameters.IDCode, "L")
            Catch ex As Exception
                RIELogFileHandler.QueueMessage("Error in StopThreads- (_tDataReadingThread) for " & RIEICodeParameters.PortNO & " for IDCode " & RIEICodeParameters.IDCode & " " & ex.Message, "E")
            End Try
        End If

        If _tServerWritingThread.ThreadState <> ThreadState.Aborted Or _
            _tServerWritingThread.ThreadState <> ThreadState.AbortRequested Then
            Try
                _tServerWritingThread.Abort()
                RIELogFileHandler.QueueMessage("Port Writer (_tDataWritingThread) for " & RIEICodeParameters.PortNO & " stopped for IDCode " & RIEICodeParameters.IDCode, "L")
            Catch ex As Exception
                RIELogFileHandler.QueueMessage("Error in StopThreads- (_tDataWritingThread) for " & RIEICodeParameters.PortNO & " for IDCode " & RIEICodeParameters.IDCode & " " & ex.Message, "E")
            End Try
        End If
        If Not _tConnectionAliveCheckingThread Is Nothing Then
            If _tConnectionAliveCheckingThread.ThreadState <> ThreadState.Aborted Or _
                        _tConnectionAliveCheckingThread.ThreadState <> ThreadState.AbortRequested Then
                Try
                    _tConnectionAliveCheckingThread.Abort()
                    RIELogFileHandler.QueueMessage("Connection Alive Checking (_tConnectionAliveCheckingThread) for " & RIEICodeParameters.PortNO & " stopped for IDCode " & RIEICodeParameters.IDCode, "L")
                Catch ex As Exception
                    RIELogFileHandler.QueueMessage("Error in StopThreads- (_tConnectionAliveCheckingThread) for " & RIEICodeParameters.PortNO & " for IDCode " & RIEICodeParameters.IDCode & " " & ex.Message, "E")
                End Try
            End If
            _tConnectionAliveCheckingThread = Nothing
        End If

        If _tDatareceivedThread.ThreadState <> ThreadState.Aborted Or _
            _tDatareceivedThread.ThreadState <> ThreadState.AbortRequested Then
            Try
                _tDatareceivedThread.Abort()
                RIELogFileHandler.QueueMessage("Port Checking (_tDatareceivedThread) for " & RIEICodeParameters.PortNO & " stopped for IDCode " & RIEICodeParameters.IDCode, "L")
            Catch ex As Exception
                RIELogFileHandler.QueueMessage("Error in StopThreads- (_tDatareceivedThread) for " & RIEICodeParameters.PortNO & " for IDCode " & RIEICodeParameters.IDCode & " " & ex.Message, "E")
            End Try
        End If

        _tServerReadingThread = Nothing
        _tServerWritingThread = Nothing
        _tDatareceivedThread = Nothing
    End Sub

    Private Class MsgSentStatus
        Public Msg As String
        Public MsgStatus As Boolean
        Public IDRequestCode As String
        Public SaveOnWaitSate As Char
        Public TimeStamp As Date
        Public MaintainEvenLog As Char
    End Class

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.