Hanko 0 Newbie Poster

hello I have a problem with a program i am trying to write.

overview

  1. i have a timer (timer1) who is "hearing" on a serial port communciation for any communication comming in. this part works ok
  2. i have a backgroundworker that i use to send firmware to a device via serial, the device will respond (and here is the problem)

PROBLEMS:

  1. i need the timer1 & backgroundworker (BWK) to work in tandem, meaning, i send a string of code, and i need to wait for to receive (right now, i send all and receive all, but separately), after i receive, depending on the response, i will either
    a) if the response starts with chr(3), do as stated on the case (working) >> this is for other communications comming from the serial port, not affected by the BWK
    b) if it starts with Chr (6) or (ACK)- for now i am just trying to read " GOOD", then continue sending the firmware file and wait for response, and so on unti finished
    c) if i receive a Chr(21) (or NAK) - for now i am just trying to read "FAIL", then, do not continue to the next line, resend the same line and try for 3 times, if of during the 3 attemps, continue to the next line, if not ok after 3 times, save a message in the listview1 -' Unable to send line XXX - and the line in the string," and continue to the next line.

i tried doing the case 6 and 21 but if "goofs" up my routine since it will not do what i need AND will also confuse the reception...

any suggestions

thanks!

'' timer
  ''** TIMER TO RECEIVE DATA
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        receiveddata()
    End Sub
    Private Sub receiveddata()
        'receivedata()
        Dim Msgs As New List(Of String)
        Dim i, iub As Integer
        Dim sbuf() As Byte = {}
        Dim cnt As Int32
        Dim fld() As String
        Dim c As String

        'Dim item As New ListViewItem()

        Static msg As String = ""
        Static scn As String = ""

        Try
            With Me.MsComm1
                cnt = .BytesToRead
                If cnt > 0 Then
                    ReDim sbuf(cnt - 1)
                    .Read(sbuf, 0, cnt)
                Else
                    Exit Try
                End If
            End With
            iub = sbuf.Length - 1
            For i = 0 To iub
                c = Chr(sbuf(i))

                Select Case c
                    Case Chr(&H2)
                        'Start of Scan
                        If msg.Length > 0 Then
                            'Received Partial Message
                            ' Me.SerialPortCommsLog.AddMessage(Encoding.ASCII.GetBytes(msg))
                        End If
                        msg = c

                    Case Chr(&H3)
                        'End of Scan
                        'TextBox2.Text = (msg)
                        receivemsg = msg

                        ListView1.Items.Add(Now.ToString(“MMM-dd, HH:mm:ss.ffff”) & ":<< " & receivemsg).ForeColor = Color.Blue
                        ListView1.EnsureVisible(ListView1.Items.Count - 1)
                        File.AppendAllText(generallog, ((Now.ToString(“MMM-dd, HH:mm:ss.ffff”) & ":<< " & receivemsg) & vbCrLf))
                        'ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
                        'ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
                        Received_LOG(receivemsg)

                    Case Chr(&H8)
                        'Start of Sensor Data
                        If msg.Length > 0 Then
                            'Received Partial Message
                            ' Me.SerialPortCommsLog.AddMessage(Encoding.ASCII.GetBytes(msg))
                        End If
                    Case Chr(&H9)
                        'Start of Flash Data
                        If msg.Length > 0 Then
                            'Received Partial Message
                            ' Me.SerialPortCommsLog.AddMessage(Encoding.ASCII.GetBytes(msg))
                        End If

                        'Case Chr(&H6)

                        '    receivemsg = msg

                        '    ListView1.Items.Add(Now.ToString(“MMM-dd, HH:mm:ss.ffff”) & ":<< " & receivemsg).ForeColor = Color.Blue
                        '    ListView1.EnsureVisible(ListView1.Items.Count - 1)
                        '    File.AppendAllText(generallog, ((Now.ToString(“MMM-dd, HH:mm:ss.ffff”) & ":<< " & receivemsg) & vbCrLf))
                        '    '                    msg &= c

                        'Case Chr(&H21)

                        '    Do
                        '        Dim RemEP As IPEndPoint
                        '        Dim sendbytes() As Byte

                        '        RemEP = New IPEndPoint(IPAddress.Parse(IP), port) 'RemEP = New IPEndPoint(IPAddress.Parse("192.168.199.200"), 3100)
                        '        sendbytes = ASCII.GetBytes(firmaretopass)
                        '        MsComm1.Write(firmaretopass)
                        '        listener.Send(sendbytes, sendbytes.Length, Converter)
                        '        '     msg &= c
                        '    Loop

                    Case Else
                        msg &= c
                End Select
                ' File.AppendAllText(generallog, (Now.ToString(“MMM-dd, HH:mm:ss.ffff”) & ":<< " & receivemsg))' & vbCrLf))
            Next
        Catch ex As Exception
            ' MyProg.Log.AddMessage("Exception in MicroPhotoArray.SerialPortSendMessaage", True)
            ' MyProg.Log.AddMessage(ex.Message, True)
        Finally
        End Try

        '  ListView1.Update()
    End Sub

    ''***********************

 '' SEND FIRMWARE BUTTON
    Private Sub firmwareBT_Click(sender As Object, e As EventArgs) Handles firmwareBT.Click
        'Timer1.Stop()
        ' TimerFirmware.Stop()
        ' FRMTBSELECT.Clear()
        'TBFIRMWARE.Clear()
        TabControl1.SelectedTab = FMWARE
        backFIRMWARE.RunWorkerAsync()
    End Sub

    '' FIRMWARE BACKGROUNDWORER
    Private Sub backFIRMWARE_DoWork(sender As Object, e As DoWorkEventArgs) Handles backFIRMWARE.DoWork
     ''  Control.CheckForIllegalCrossThreadCalls = False
        ''FOR SENDING
        If File.Exists(firmwarefile) = True Then
            Dim firmware As String = (firmwarefile) '"C:\1PROJECTS\ES\OUTPUT\production.hex"
            Dim fwmline As String = ""
            Dim fimrwareline(100000) As String '' has to be undefined length
            Dim firmaretopass As String
            Dim i As Integer = 0

            Dim sr As New StreamReader(firmware)
            Dim sendbytes() As Byte

            Dim RemEP As IPEndPoint

            Static pageNumber As Integer = 1

            '' $START TO BEGIN FIRMWARE UPDATE

            Try
                RemEP = New IPEndPoint(IPAddress.Parse(IP), port) 'RemEP = New IPEndPoint(IPAddress.Parse("192.168.199.200"), 3100)
                firmaretopass = ""
                firmaretopass &= Chr(2) & "HEX," & Path.GetFileName(firmwarefile) & "," & Chr(3)
                sendbytes = ASCII.GetBytes(firmaretopass)
                ''confirm on listview
                ListView1.Items.Add(Now.ToString(“MMM-dd, HH:mm:ss.ffff”) & ":>> " & firmaretopass) ' ListView1.Items.Add(Now.ToString(“MMM-dd, HH:mm:ss.ffff”) & ":>> " & "SENDING FIRMWARE") ' & vbCrLf)
                ListView1.EnsureVisible(ListView1.Items.Count - 1)

                ''display in firmware log
                TBFIRMWARE.AppendText(Now.ToString(“MMM-dd, HH:mm:ss.ffff”) & ":>> " & firmaretopass & vbCrLf)
                TBFIRMWARE.ScrollToCaret()

                MsComm1.Write(firmaretopass)
                listener.Send(sendbytes, sendbytes.Length, Converter)

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

            '' HEX FILE READ AND SENT

            Do Until sr.Peek = -1
                ''delay in send 
                Thread.Sleep(sleeptime)

                'grab each line of txt file
                fwmline = sr.ReadLine()

                'set each line in the array
                fimrwareline(i) = fwmline

                'PENDING define output of array
                firmaretopass = Chr(2) & fimrwareline(i) & "," ' firmaretopass = Chr(7) & fimrwareline(i) & Chr(3)
                firmaretopass &= Chr(firmaretopass.Length) & Chr(3)

                '' send to boards
                Try
                    RemEP = New IPEndPoint(IPAddress.Parse(IP), port) 'RemEP = New IPEndPoint(IPAddress.Parse("192.168.199.200"), 3100)
                    sendbytes = ASCII.GetBytes(firmaretopass)
                    MsComm1.Write(firmaretopass)
                    listener.Send(sendbytes, sendbytes.Length, Converter)

                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
                LabelProgress.Text = "Total= " & i
                TBFIRMWARE.AppendText(Now.ToString(“MMM-dd, HH:mm:ss.ffff”) & ":>> " & firmaretopass & vbCrLf)
                TBFIRMWARE.ScrollToCaret()

                'increment of loop
                i = i + 1

                ' add end of file loop
                If sr.EndOfStream = True Then
                    RemEP = New IPEndPoint(IPAddress.Parse(IP), port) 'RemEP = New IPEndPoint(IPAddress.Parse("192.168.199.200"), 3100)

                    firmaretopass = ""
                    firmaretopass &= Chr(2) & "TOTAL," & File.ReadAllLines(firmware).Length & "," & Chr(3)

                    If pageNumber < 255 Then pageNumber += 1 Else pageNumber = 1

                    MsComm1.Write(firmaretopass)
                    listener.Send(sendbytes, sendbytes.Length, Converter)

                    ListView1.Items.Add(Now.ToString(“MMM-dd, HH:mm:ss.ffff”) & ":>> " & firmaretopass.TrimEnd(Chr(3)))
                    ListView1.EnsureVisible(ListView1.Items.Count - 1)
                    TBFIRMWARE.AppendText(Now.ToString(“MMM-dd, HH:mm:ss.ffff”) & ":>> " & firmaretopass & vbCrLf)
                    TBFIRMWARE.ScrollToCaret()
                    LabelProgress.Text = firmaretopass.TrimEnd(Chr(3))

                    ' sr.Close()
                End If
                MsComm1.DiscardOutBuffer()

            Loop
            sr.Close()
            ' File.Delete(firmware)
        Else
            Exit Sub
        End If

        'File.Delete(autodelete) 
    End Sub