Question Exception Thrown When the APP get crashed

Status
Not open for further replies.

saba

Member
Joined
Jun 5, 2021
Messages
6
Programming Experience
1-3
I'm using TCP/IP to read the Data from PLC using C# APP , The APP run as normal and sometimes it getting an exception "Unable to read data from the transport connection: An established connection was aborted by the software in your host machine." ,and then the App is crashed .Don't know what makes this app to crash, Kindly advice
 
Last edited:
You need to provide a FULL and CLEAR explanation of the problem, which includes exactly what you're trying to achieve, exactly how you're trying to achieve it and exactly what happens when you try. You have partly provided the third of those and nothing else.
 
Also, the title of the thread should not be an error message. It should be a concise summary of the problem, e.g. "Exception Thrown When Connecting To Database Over Network".
 
Well, by default, when there is an unhandled exception, an application will crash. So if the question is how to stop it from crashing, then handle the exception somehow. If the question is how to prevent the connection from getting disconnected, then that's another matter entirely and may actually require beefing up your network.
 
i didn't received any errors during debug .i Run this application at my customer site , after a week it crashed and received this error on exception. I have error log in my program ,and i received this exception on that error log file .

VB.NET:
Imports System.Net.Sockets
Imports System.IO
Imports System.IO.Ports
Imports System.ServiceProcess
Imports System.Diagnostics



0 to 100kPa : 1 to 5vdc : 0 to 14.50PSI
'Analog Module using 0 to 5vdc






Public Class Main

    Public IPAddressData As String
    Dim clientSocket As New System.Net.Sockets.TcpClient()
    Dim SendmodbusWord As String = ""
    Dim ModbusAddress As Long = 512 '.............. READing starting fron this register +1
    Dim ModbusCountReg As Long = 6 '............ Number of register to read. V0 consider 2, V10 = 12

    Dim Data1, Data2, Data3, Data4, Data5, Data6, Data7, Data8 As Double

    'File writing variables:
    Dim swOutputFile As IO.StreamWriter
    Dim sFilename As String
    Dim EFilename As String
    Dim DataSet As String
    Dim ErrorData As String

    Dim csvlabel As String
    Dim FirstTime As Boolean

    'Dim connectionMySQL As New MySqlConnection("datasource=localhost;port=3306;username=root;password=")

    'update main form title
    'Public ProgramNameLocation
    'Public ProgramName As String






    'Hardware Setup Selection
    Public HaiwellCPUPort As Boolean
    Public HaiwellWirelessModule As Boolean
    Public HaiwellRS232Module As Boolean
    Public RS232_Modbus As Boolean
    Public USB As Boolean

    Public HardwareSelectionLocation As Integer
    Public HardwareSelection As String

    Public UsbPort1or2Location As Integer
    Public UsbPort1or2 As Integer

    Public IPAddressLocation As Integer
    Public IPAddress As String

    Public SerialPort1AddressLocation As Integer
    Public SerialPort2AddressLocation As Integer
    Public SerialPort_Port1 As String
    Public SerialPort_Port2 As String
    Public SerialPort_BaudRateLocation As Integer
    Public SerialPort_BaudRate As String
    Public SerialPort_DataBitsLocation As Integer
    Public SerialPort_DataBits As String
    Public SerialPort_ParityLocation As Integer
    Public SerialPort_Parity As String
    Public SerialPort_StopBitsLocation As Integer
    Public SerialPort_StopBits As String


    Public DataFormatCSVLocation As Integer
    Public DataFormatCSV As Integer
    Public DataFormatGraphLocation As Integer
    Public DataFormatGraph As Integer
    Public DataFormatMySQLLocation As Integer
    Public DataFormatMySQL As Integer
    Public ProjectTitleLocation As Integer
    Public ProjectTitle As String
    Public NumberOfDataLocation As Integer
    Public NumberOfData As Integer
    Public DataNameLocation As Integer
    Public DataName As String
    Public DataVerticalAxis1NameLocation As Integer
    Public DataVerticalAxis1Name As String
    Public DataVerticalAxis2NameLocation As Integer
    Public DataVerticalAxis2Name As String
    Public OffsetValue1Location As Integer
    Public OffsetValue1 As String
    Public OffsetValue2Location As Integer
    Public OffsetValue2 As String
    Public OffsetValue3Location As Integer
    Public OffsetValue3 As String
    Public OffsetValue4Location As Integer
    Public OffsetValue4 As String
    Public OffsetValue5Location As Integer
    Public OffsetValue5 As String
    Public OffsetValue6Location As Integer
    Public OffsetValue6 As String

    Public Alarm1_ValueLocation As Integer
    Public Alarm1 As String
    Public Alarm2_ValueLocation As Integer
    Public Alarm2 As String
    Public Alarm3_ValueLocation As Integer
    Public Alarm3 As String
    Public Alarm4_ValueLocation As Integer
    Public Alarm4 As String
    Public Alarm5_ValueLocation As Integer
    Public Alarm5 As String
    Public Alarm6_ValueLocation As Integer
    Public Alarm6 As String


    Public Alarm1_low As String
    Public Alarm1_High As String
    Public Alarm2_low As String
    Public Alarm2_High As String
    Public Alarm3_low As String
    Public Alarm3_High As String
    Public Alarm4_low As String
    Public Alarm4_High As String

    Public Alarm5_low As String
    Public Alarm5_High As String

    Public Alarm6_low As String
    Public Alarm6_High As String


    Public LogIntervalLocation As Integer
    Public LogInterval As String
    Public EquipmentNameLocation As Integer
    Public EquipmentName As String

    Public PortName1, PortName2, BaudRate, DataBits, Parity, Stopbits As String




    'RS232, USB Port1
    Public DataReceived1 As String
    Dim Length1 As Integer
    Dim ai4Opened_1 As Boolean
    Dim ai4_1
    Dim USBData1_1, USBData1_2, USBData1_3, USBData1_4 As Single


    'RS232, USB Port2
    Public DataReceived2 As String
    Dim Length2 As Integer
    Dim ai4Opened_2 As Boolean
    Dim ai4_2
    Dim USBData2_1, USBData2_2, USBData2_3, USBData2_4 As Single
    Public USBPort1_Only, USBPort1n2 As Boolean

    Public Dataname1, Dataname2, Dataname3, Dataname4, Dataname5, Dataname6, Dataname7, Dataname8 As String

 






    'End Sub

    Private Sub lblYAxis1_Click(sender As Object, e As EventArgs) Handles lblYAxis1.Click

    End Sub

    Private Sub MenuStrip1_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked

    End Sub

    'Check Date
    Dim StartDate

    Private Sub Display_Timer_Tick(sender As Object, e As EventArgs) Handles Display_Timer.Tick
        CheckConfig()
        Display_Label()
    End Sub

    Dim TodayDate

    Public YAxis1Label, YAxis2Label As String



    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        Alarm_Display()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        'outFile.Close()
        clientSocket.Dispose()
        End
    End Sub

    Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length > 1 Then
            Application.Exit()
        Else
            ' not running
        End If




        PnlListBox.Visible = True
        Pnl_Chart1.Visible = False
        Btn_Graph.Text = "Graph "
        Dim LblA(6) As Label
        Dim x As Integer
        For x = 1 To 6
            LblA(x) = New Label()
            LblA(x).Visible = False
        Next


        Try



            'update setup from config files
            CheckConfig()

            Display_Label()

            Connection()
            StartDate = DateTime.Today.ToString("dd-MMM-yyyy")
            CreateCSV()
            Timer1.Enabled = True
        Catch ex As Exception
            MessageBox.Show("error load")
        End Try
    End Sub

    Public Sub onDebug()


    End Sub


    Private Sub modbusWord()
        SendmodbusWord = Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(6) &
        Chr(0) & Chr(3) &
        Chr(ModbusAddress \ 256) & Chr((ModbusAddress) Mod 256) &
        Chr(ModbusCountReg \ 256) & Chr(ModbusCountReg Mod 256)


    End Sub




    Private Sub ModbusRS232()

    End Sub

    Public Sub Doupdate(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim SerialCommDataReceived As String

        SerialCommDataReceived = Mid(DataReceived1, 1, Length1 - 1)


        ListBox1.Items.Insert(0, SerialCommDataReceived)

        Return
    End Sub

    Private Sub TmrChkDate_Tick(sender As Object, e As EventArgs) Handles tmrChkDate.Tick
        TodayDate = DateTime.Today.ToString("dd-MMM-yyyy")
        If TodayDate <> StartDate Then
            StartDate = TodayDate
            CreateCSV()
        End If
    End Sub

    Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False
        DataReceived1 = SerialPort1.ReadLine
        Length1 = InStr(DataReceived1, vbCr)
        Dim a As String = Mid(DataReceived1, 1, Length1 - 1)

        Try


            DataSet = Now.Date + "," + Now.ToLongTimeString + "," + a

            Using swOutputFile As New StreamWriter(File.Open(sFilename, FileMode.Append))

                swOutputFile.WriteLine(DataSet)

                swOutputFile.Close()

            End Using

            Chartview.Series("Data1").Points.Add(DataReceived1)

        Catch ex As Exception
            'SerialPort1.Close()
            MessageBox.Show(ex.Message)

        End Try

        Me.Invoke(New EventHandler(AddressOf Doupdate))

    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If clientSocket.Connected Then
            ReadModbus()
        Else

            Disconnect()
            Connection()

        End If

    End Sub

    Private Sub BtnMySQLConnect_Click(sender As Object, e As EventArgs) Handles btnMySQLConnect.Click
        'connectionMySQL.Open()
        lblMySQLStatus.Text = "MySQL Connection Status  : Connected"

        Try

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

    Private Sub BtnConnect_Click(sender As Object, e As EventArgs) Handles btnConnect.Click

        Connection()
        Timer1.Enabled = True
    End Sub

    Private Sub Connection()
        Try
            Timer1.Enabled = True
            If Not clientSocket.Connected Then
                clientSocket = New System.Net.Sockets.TcpClient()
                clientSocket.Connect(IPAddress, 502)
                btnConnect.Enabled = False
                btnDisconnect.Enabled = True

                If clientSocket.Connected Then
                    lblStatus.Text = "Server Connected ..."
                    btnConnect.Enabled = False
                    btnDisconnect.Enabled = True
                Else
                    lblStatus.Text = "Server Not Connected ..."
                End If
                modbusWord()
            End If
            btnConnect.Enabled = False
            btnDisconnect.Enabled = True
        Catch ex As Exception
            'MessageBox.Show(ex.Message)
            Throw ex
            Exit Sub
        End Try
    End Sub


    Public Sub CheckConfig()
        Try


            Dim data1 = My.Computer.FileSystem.ReadAllText("C:\DataCollection-PPTC\Config-PressureMonitor.cfg")

            'Read All Text
            Dim quoteArray As New ArrayList
            FileOpen(1, "C:\DataCollection-PPTC\Config-PressureMonitor.cfg", OpenMode.Input)
            Do While Not EOF(1)
                quoteArray.Add(LineInput(1))
            Loop
            FileClose(1)


            'Project Title
            ProjectTitleLocation = InStr(quoteArray(1), "=")
            ProjectTitle = Mid(quoteArray(1), ProjectTitleLocation + 2)
            Me.Text = ProjectTitle

            'IPAddress
            IPAddressLocation = InStr(quoteArray(3), "=")
            IPAddress = Mid(quoteArray(3), IPAddressLocation + 2)


            'Number Of Data
            NumberOfDataLocation = InStr(quoteArray(5), "=")
            NumberOfData = Mid(quoteArray(5), NumberOfDataLocation + 2)

            'Data Name - put into excel row
            DataNameLocation = InStr(quoteArray(7), "=")
            DataName = Mid(quoteArray(7), DataNameLocation + 2)
            'Offset Value - put into excel row
            OffsetValue1Location = InStr(quoteArray(9), "=")
            OffsetValue1 = Mid(quoteArray(9), OffsetValue1Location + 2)

            OffsetValue2Location = InStr(quoteArray(10), "=")
            OffsetValue2 = Mid(quoteArray(10), OffsetValue2Location + 2)

            OffsetValue3Location = InStr(quoteArray(11), "=")
            OffsetValue3 = Mid(quoteArray(11), OffsetValue3Location + 2)

            OffsetValue4Location = InStr(quoteArray(12), "=")
            OffsetValue4 = Mid(quoteArray(12), OffsetValue4Location + 2)

            OffsetValue5Location = InStr(quoteArray(13), "=")
            OffsetValue5 = Mid(quoteArray(13), OffsetValue5Location + 2)

            OffsetValue6Location = InStr(quoteArray(14), "=")
            OffsetValue6 = Mid(quoteArray(14), OffsetValue6Location + 2)



            'Alarm1_Value
            Alarm1_ValueLocation = InStr(quoteArray(16), "=")
            Alarm1 = Mid(quoteArray(16), Alarm1_ValueLocation + 2)

            Alarm2_ValueLocation = InStr(quoteArray(17), "=")
            Alarm2 = Mid(quoteArray(17), Alarm2_ValueLocation + 2)

            Alarm3_ValueLocation = InStr(quoteArray(18), "=")
            Alarm3 = Mid(quoteArray(18), Alarm3_ValueLocation + 2)

            Alarm4_ValueLocation = InStr(quoteArray(19), "=")
            Alarm4 = Mid(quoteArray(19), Alarm4_ValueLocation + 2)

            Alarm5_ValueLocation = InStr(quoteArray(20), "=")
            Alarm5 = Mid(quoteArray(20), Alarm5_ValueLocation + 2)

            Alarm6_ValueLocation = InStr(quoteArray(21), "=")
            Alarm6 = Mid(quoteArray(21), Alarm6_ValueLocation + 2)

            'Logging Interval

            LogIntervalLocation = InStr(quoteArray(23), "=")
            LogInterval = Mid(quoteArray(23), LogIntervalLocation + 2)
            Timer1.Interval = LogInterval * 1000

            'Equipment Name

            EquipmentNameLocation = InStr(quoteArray(25), "=")
            EquipmentName = Mid(quoteArray(25), EquipmentNameLocation + 2)

        Catch ex As Exception

            MessageBox.Show("Config File Error")
        End Try
    End Sub



    Private Sub Display_Label()
        Try
            Dim strList As String() = DataName.Split(New Char() {","c})

            Select Case NumberOfData
                Case 4
                    GroupBox1.Visible = True
                    GroupBox1.Text = EquipmentName
                    label3.Text = EquipmentName
                    ' Pnl_Chart1.Visible = True
                    Panel1.Size = New Size(375, 131)
                    Panel1.Location = New Point(560, 4)
                    GroupBox1.Size = New Size(370, 119)
                    LblA5.Visible = False
                    LblA6.Visible = False
                    lblunit5.Visible = False
                    lblunit6.Visible = False
                    LblD_Name5.Visible = False
                    LblD_Name6.Visible = False
                    LblD_Name1.Text = strList(0)
                    Chartview.Series("Series1").Name = strList(0)
                    lblunit1.Text = strList(1)
                    LblD_Name2.Text = strList(2)
                    Chartview.Series("Series2").Name = strList(2)
                    lblunit2.Text = strList(3)
                    LblD_Name3.Text = strList(4)
                    Chartview.Series("Series3").Name = strList(4)
                    lblunit3.Text = strList(5)
                    LblD_Name4.Text = strList(6)
                    Chartview.Series("Series4").Name = strList(6)
                    lblunit4.Text = strList(7)
                    csvlabel = strList(0) & "," & strList(2) & "," & strList(4) & "," & strList(6)
                    Chartview.Series("Series5").IsVisibleInLegend = False
                    Chartview.Series("Series5").IsValueShownAsLabel = False
                    Chartview.Series("Series6").IsVisibleInLegend = False
                    Chartview.Series("Series6").IsValueShownAsLabel = False
                    '  Pnl_Chart1.Size = New Size(1203, 620)
                    Chartview.Size = New Size(1016, 608)
                    lblXAxis2.Location = New Point(1053, 550)
                Case 6



                    GroupBox1.Visible = True
                    GroupBox1.Text = EquipmentName
                    label3.Text = EquipmentName
                    '  Pnl_Chart1.Visible = True


                    ' GroupBox1.Size = New Size(404, 119)
                    LblD_Name1.Text = strList(0)
                    Chartview.Series("Series1").Name = strList(0)
                    lblunit1.Text = strList(1)
                    LblD_Name2.Text = strList(2)
                    Chartview.Series("Series2").Name = strList(2)
                    lblunit2.Text = strList(3)
                    LblD_Name3.Text = strList(4)
                    Chartview.Series("Series3").Name = strList(4)
                    lblunit3.Text = strList(5)
                    LblD_Name4.Text = strList(6)
                    Chartview.Series("Series4").Name = strList(6)
                    lblunit4.Text = strList(7)
                    csvlabel = strList(0) & "," & strList(2) & "," & strList(4) & "," & strList(6) & "," & strList(8) & "," & strList(10)
                    LblD_Name5.Text = strList(8)
                    Chartview.Series("Series5").Name = strList(8)
                    lblunit5.Text = strList(9)
                    LblD_Name6.Text = strList(10)
                    Chartview.Series("Series6").Name = strList(10)
                    lblunit6.Text = strList(11)
                    ' csvlabel = strList(0) & "," & strList(2) & "," & strList(4) & "," & strList(6) & "," & strList(8) & "," & strList(10)
                    ' Pnl_Chart1.Size = New Size(1203, 620)
                    ' Chartview.Size = New Size(1016, 608)
                    '  lblXAxis2.Location = New Point(1053, 550)

            End Select

        Catch ex As Exception
            'MessageBox.Show(ex.Message)
            Throw ex
            Return
        End Try
    End Sub

    Private Sub Btn_Graph_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btn_Graph.Click
        If Pnl_Chart1.Visible = False Then
            PnlListBox.Visible = False
            Pnl_Chart1.Visible = True
            Btn_Graph.Text = "List View "
        Else
            PnlListBox.Visible = True
            Pnl_Chart1.Visible = False
            Btn_Graph.Text = "Graph "
        End If
    End Sub


    Private Sub CreateCSV()


        Dim pathstring As String '= My.Computer.FileSystem.SpecialDirectories.MyDocuments        10-11-20 

        pathstring = "C:\" & ProjectTitle & "\" & StartDate                                

        ' Added 0n 10-11-20 sabarish

        'Create a file name based upon today's date and time right now.

        Dim CurrentDateAndTime As Date = Now
        System.IO.Directory.CreateDirectory(pathstring)

        sFilename = pathstring + "\" + ProjectTitle + " " + StartDate + ".csv"
        DataSet = "Date" + "," + "Time" + "," + DataName


        If IO.File.Exists(sFilename) Then

        Else
            Using swOutputFile As New StreamWriter(File.Open(sFilename, FileMode.Create))
                swOutputFile.WriteLine(DataSet)
                swOutputFile.Close()
            End Using
        End If


    End Sub

    Private Sub ReadModbus()
        Try


            Dim serverStream As NetworkStream = clientSocket.GetStream()
            Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes(Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(6) &
            Chr(0) & Chr(3) & Chr(ModbusAddress \ 256) & Chr((ModbusAddress) Mod 256) & Chr(ModbusCountReg \ 256) & Chr(ModbusCountReg Mod 256))
            serverStream.Write(outStream, 0, outStream.Length)
            serverStream.Flush()


            Dim inStream(21) As Byte ' read only 20byte : 00-00-00-00-00-09-00-03-06-03-78-00-00-01-20-00-00-00-00-00-00
            serverStream.Read(inStream, 0, 21) 'Read from Byte 0 to 30



            Console.WriteLine(BitConverter.ToString(inStream)) 'troubleshooting purpose...To read the whole 20 byte from address 512
            If UBound(inStream) < 14 Then
                ' error
            End If


            Dim V0 As Double = ((inStream(9) * 256) + inStream(10)) / 10
            Dim V1 As Double = ((inStream(11) * 256) + inStream(12)) / 10
            Dim V2 As Double = ((inStream(13) * 256) + inStream(14)) / 10
            Dim V3 As Double = ((inStream(15) * 256) + inStream(16)) / 10
            Dim V4 As Double = ((inStream(17) * 256) + inStream(18)) / 10
            Dim V5 As Double = ((inStream(19) * 256) + inStream(20)) / 10   'added 11/11/20

         

            If NumberOfData = 2 Then
                Try
                    ListBox1.Items.Insert(0, "V0" & "=" & V0)
                    ListBox1.Items.Insert(0, "V1" & "=" & V1)



                    Data1 = V0 + OffsetValue1
                    Data2 = V1 + OffsetValue2
                    LblA1.Visible = True
                    LblA2.Visible = True
                    LblA1.Text = Data1
                    LblA2.Text = Data2


                    DataSet = Now.Date + "," + Now.ToLongTimeString + "," + Str(Data1) + "," + Str(Data2)


                    Writedata()
                Catch ex As Exception

                    ' MessageBox.Show(ex.Message)
                    Throw ex
                End Try

            End If


        
       
        



            If NumberOfData = 6 Then
                Try


                    Data1 = V0 + OffsetValue1
                    Data2 = V1 + OffsetValue2
                    Data3 = V2 + OffsetValue3
                    Data4 = V3 + OffsetValue4
                    Data5 = V4 + OffsetValue5
                    Data6 = V5 + OffsetValue6


                    Data1 = Format(Data1, "0.00")

                    Data2 = Format(Data2, "0.00")
                    Data3 = Format(Data3, "0.00")
                    Data4 = Format(Data4, "0.00")
                    Data5 = Format(Data5, "0.00")
                    Data6 = Format(Data6, "0.00")
                    LblA1.Visible = True
                    LblA2.Visible = True
                    LblA3.Visible = True
                    LblA4.Visible = True
                    LblA5.Visible = True
                    LblA6.Visible = True

                    LblA1.Text = Data1
                    LblA2.Text = Data2
                    LblA3.Text = Data3
                    LblA4.Text = Data4
                    LblA5.Text = Data5
                    LblA6.Text = Data6
                    ListBox1.Items.Insert(0, Now.Date + "  ,  " + Now.ToLongTimeString + "  -  " + LblD_Name6.Text & "=" & Data6)
                    ListBox1.Items.Insert(0, Now.Date + "  ,  " + Now.ToLongTimeString + "  -  " + LblD_Name5.Text & "=" & Data5)
                    ListBox1.Items.Insert(0, Now.Date + "  ,  " + Now.ToLongTimeString + "  -  " + LblD_Name4.Text & "=" & Data4)

                    ListBox1.Items.Insert(0, Now.Date + "  ,  " + Now.ToLongTimeString + "  -  " + LblD_Name3.Text & "=" & Data3)
                    ListBox1.Items.Insert(0, Now.Date + "  ,  " + Now.ToLongTimeString + "  -  " + LblD_Name2.Text & "=" & Data2)

                    ListBox1.Items.Insert(0, Now.Date + "  ,  " + Now.ToLongTimeString + "  -  " + LblD_Name1.Text & "=" & Data1)



                    Chartview.Series(LblD_Name1.Text).Points.Add(Data1)
                    Chartview.Series(LblD_Name2.Text).Points.Add(Data2)
                    Chartview.Series(LblD_Name3.Text).Points.Add(Data3)
                    Chartview.Series(LblD_Name4.Text).Points.Add(Data4)
                    Chartview.Series(LblD_Name5.Text).Points.Add(Data5)
                    Chartview.Series(LblD_Name6.Text).Points.Add(Data6)
                    DataSet = Now.Date + "," + Now.ToLongTimeString + "," + Str(Data1) + "," + Str(Data2) + "," +
                    Str(Data3) + "," + Str(Data4) + "," + Str(Data5) + "," + Str(Data6)
                    Writedata()
                Catch ex As Exception

                    ' MessageBox.Show(ex.Message)
                    Throw ex
                End Try

            End If

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




    End Sub


    Private Sub Errorlog(Err As String)
        Try


            Dim path As String
            path = "C:\" & ProjectTitle
            Dim CurrentDateAndTime As Date = Now
            System.IO.Directory.CreateDirectory(path)
            Dim Errorlog As String = "ERRORLOG"

            EFilename = path + "\" + ProjectTitle + "_" + Errorlog + ".csv"
            ErrorData = CurrentDateAndTime + "," + (Err)

            Dim objERR As IO.StreamWriter = IO.File.AppendText(EFilename)
            If IO.File.Exists(EFilename) Then

                objERR.Write(ErrorData)
                objERR.Write(Environment.NewLine)

            Else
                Using swOutputFile As New StreamWriter(File.Open(EFilename, FileMode.Create))
                    swOutputFile.WriteLine(ErrorData)
                    swOutputFile.Close()
                End Using
            End If

            objERR.Close()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Private Sub Writedata()
        Timer2.Enabled = True
        Dim objSW As IO.StreamWriter = IO.File.AppendText(sFilename)

        If IO.File.Exists(sFilename) Then

            objSW.Write(DataSet)
            objSW.Write(Environment.NewLine)
        End If
        objSW.Close()

        'Exit Sub

    End Sub

    Private Sub Alarm_Display()
        Dim strList1 As String() = Alarm1.Split(New Char() {","c})
        Alarm1_low = strList1(0)
        Alarm1_High = strList1(1)

        If (Data1 <= Convert.ToInt32(Alarm1_low)) OrElse (Data1 >= Convert.ToInt32(Alarm1_High)) Then

            If LblA1.ForeColor = Color.Red Then
                LblA1.ForeColor = Color.Black
            Else
                LblA1.ForeColor = Color.Red
            End If
        Else

            If LblA1.ForeColor = Color.Yellow Then
                LblA1.ForeColor = Color.LimeGreen
            Else
                LblA1.ForeColor = Color.Yellow
            End If
        End If

        Dim strList2 As String() = Alarm2.Split(New Char() {","c})
        Alarm2_low = strList2(0)
        Alarm2_High = strList2(1)

        If (Data2 <= Convert.ToInt32(Alarm2_low)) OrElse (Data2 >= Convert.ToInt32(Alarm2_High)) Then

            If LblA2.ForeColor = Color.Red Then
                LblA2.ForeColor = Color.Black
            Else
                LblA2.ForeColor = Color.Red
            End If
        Else

            If LblA2.ForeColor = Color.Yellow Then
                LblA2.ForeColor = Color.LimeGreen
            Else
                LblA2.ForeColor = Color.Yellow
            End If
        End If

        Dim strList3 As String() = Alarm3.Split(New Char() {","c})
        Alarm3_low = strList3(0)
        Alarm3_High = strList3(1)
        If (Data3 <= Convert.ToInt32(Alarm3_low)) OrElse (Data3 >= Convert.ToInt32(Alarm3_High)) Then



            If LblA3.ForeColor = Color.Red Then
                LblA3.ForeColor = Color.Black
            Else
                LblA3.ForeColor = Color.Red
            End If
        Else

            If LblA3.ForeColor = Color.Yellow Then
                LblA3.ForeColor = Color.LimeGreen
            Else
                LblA3.ForeColor = Color.Yellow
            End If
        End If


        Dim strList4 As String() = Alarm4.Split(New Char() {","c})
        Alarm4_low = strList4(0)
        Alarm4_High = strList4(1)

        If (Data4 <= Convert.ToInt32(Alarm4_low)) OrElse (Data4 >= Convert.ToInt32(Alarm4_High)) Then

            If LblA4.ForeColor = Color.Red Then
                LblA4.ForeColor = Color.Black
            Else
                LblA4.ForeColor = Color.Red
            End If
        Else

            If LblA4.ForeColor = Color.Yellow Then
                LblA4.ForeColor = Color.LimeGreen
            Else
                LblA4.ForeColor = Color.Yellow
            End If
        End If

        Dim strList5 As String() = Alarm5.Split(New Char() {","c})
        Alarm5_low = strList5(0)
        Alarm5_High = strList5(1)

        If (Data5 <= Convert.ToInt32(Alarm5_low)) OrElse (Data5 >= Convert.ToInt32(Alarm5_High)) Then

            If LblA5.ForeColor = Color.Red Then
                LblA5.ForeColor = Color.Black
            Else
                LblA5.ForeColor = Color.Red
            End If
        Else

            If LblA5.ForeColor = Color.Yellow Then
                LblA5.ForeColor = Color.LimeGreen
            Else
                LblA5.ForeColor = Color.Yellow
            End If
        End If

        Dim strList6 As String() = Alarm6.Split(New Char() {","c})
        Alarm6_low = strList6(0)
        Alarm6_High = strList6(1)

        If (Data6 <= Convert.ToInt32(Alarm6_low)) OrElse (Data6 >= Convert.ToInt32(Alarm6_High)) Then

            If LblA6.ForeColor = Color.Red Then
                LblA6.ForeColor = Color.Black
            Else
                LblA6.ForeColor = Color.Red
            End If
        Else

            If LblA6.ForeColor = Color.Yellow Then
                LblA6.ForeColor = Color.LimeGreen
            Else
                LblA6.ForeColor = Color.Yellow
            End If
        End If


    End Sub

    Private Sub Disconnect_Network()


    End Sub



    Private Sub btnDisconnect_Click(sender As Object, e As EventArgs) Handles btnDisconnect.Click
        Disconnect()
    End Sub

    Private Sub Disconnect()
        'clientSocket.Close()
        'clientSocket.Dispose()
        'Me.Close()
        Timer1.Enabled = False
        btnConnect.Enabled = False
        btnDisconnect.Enabled = False
        Timer2.Enabled = False
        Dim result As DialogResult = MessageBox.Show("Disconnected,Press Ok to Reconnect",
                          "Title",
                          MessageBoxButtons.OK)

        If (result = DialogResult.OK) Then

            Timer1.Enabled = True
            Connection()
        End If

    

 
    End Sub

    Private Sub BtnMySQLDisconnect_Click(sender As Object, e As EventArgs) Handles btnMySQLDisconnect.Click
        'connectionMySQL.Close()
        lblMySQLStatus.Text = "MySQL Connection Status  : Disconnected"

        Try

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

    Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        ListBox1.Items.Clear()
    End Sub


    Private Sub Comstatus()
        Dim SM0 As Boolean
        If SM0 = 0 Then
            StatusBox.Image = My.Resources.PLCCommoff
        End If
        If SM0 = 1 Then
            StatusBox.Image = My.Resources.PLCCommon
        End If
    End Sub

    Private Sub StartStop()

        Dim service As ServiceController = New ServiceController("Service1Monitor")



        If ((service.Status.Equals(ServiceControllerStatus.Stopped)) Or (service.Status.Equals(ServiceControllerStatus.StopPending))) Then

            service.Start()

        Else



            service.Stop()

        End If

    End Sub

 
    'End Sub

End Class

Hi , i have attached my code , kindly take a look.Thanks
 
Last edited:
Status
Not open for further replies.
Back
Top Bottom