none
Serial port sending several commands simultaneously and it misses all but one command RRS feed

  • Question

  • Hi there, I am very new to Vb and have made a device to switch on my 5 heaters at different intervals. However, when I make the arguments in Vb to send to the serial port for all heaters to turn on at the same time, only one heater(usually the last one) actually turns on. I think it is a timing error and the code is completely missing preceding commands. I would really appreciate help but as I say, I am really new to this. I am using (TimeOfDay) and comparing a textbox (which I set on the screen to a pre determined time) and whether a panel is yellow or not(also set on the screen) to send a message to the serial port. When one of the compare/yellow box and correct time is used, it ouputs to the serial port but... if I turn on all the yellow boxes, when the curent time lines up with the active boxes etc. only the last activated serial port message is sent (ie) if I set 2 and 3, only three works or if I set 1 and 5, only 5 works.[ code ]

        Private Sub HeaterClockTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HeaterClockTimer.Tick

            TimeBox.Text = CStr(TimeOfDay)
            If CStr(TimeOfDay) = OnTimeLabel1.Text And Panel3.BackColor = Color.Yellow Then
                SerialPort1.WriteLine("T1" + ControlChars.CrLf)
            End If

            If CStr(TimeOfDay) = OnTimeLabel1.Text And Panel1.BackColor = Color.Yellow Then
                SerialPort1.WriteLine("T2" + ControlChars.CrLf)
            End If

            If CStr(TimeOfDay) = OnTimeLabel1.Text And Panel5.BackColor = Color.Yellow Then
                SerialPort1.WriteLine("T3" + ControlChars.CrLf)
            End If

            If CStr(TimeOfDay) = OnTimeLabel1.Text And Panel7.BackColor = Color.Yellow Then
                            SerialPort1.WriteLine("T4" + ControlChars.CrLf)

            End If

            If CStr(TimeOfDay) = OnTimeLabel1.Text And Panel6.BackColor = Color.Yellow Then
                SerialPort1.WriteLine("T5" + ControlChars.CrLf)

            End If
        End Sub


    [ /code ]

    Tuesday, April 9, 2019 5:30 AM

All replies

  • Hi,

    How do you accept the data of SerialPort1.Write() and process the data?

    like this?

    Private Sub serialPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
        Dim ilen As Integer = serialPort.BytesToRead
        Dim bytes As Byte() = New Byte(ilen - 1) {}
        serialPort.Read(bytes, 0, ilen)
        Dim xx As String = System.Text.Encoding.(Default).GetString(bytes)
    End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 9, 2019 7:03 AM
  • Hi Alex and thanks for the prompt reply.

    When I send data to the comport, I just write the following...SerialPort1.WriteLine("T5" + ControlChars.CrLf). This works Ok and sends "T5" to the serial port followed by a CrLf.

    However, this process is automated by sending it when the CStr(TimeOfDay) = OnTimeLabel1.Text And Panel6.BackColor = Color.Yellow.

    So, if I click the panel yellow and the system time lines up with the textbox time, it sends the "T5".

    The problem is that it only allows one procedure to work and if I set more than one box yellow etc on multiple lines, it only sends one command (ie)T5 and not the rest T1 to T4.

    I am not getting anywhere. It seems to only pick up one of the commands in the sub and forgets the rest. Is it because the program runs only once through the sub and sends the first thing it sees, or, is it because the system time has passed by the time it gets to the others?

    Tuesday, April 9, 2019 7:28 AM
  • Dear AshBasher,

    Your issue sounds a lot like the one I posted yesterday: SerialPort.Write() fail/bug to Arduino (terrific, I can't paste links...)

    I find no sense in this and still can't explain it to myself. I'm bumping my head against the wall for 3 days now... Still no clues. :(

    I'll let you know if I find a way around.

    Tuesday, April 9, 2019 8:35 AM
  • Hi there, OK, I have found out (by separating the systems and putting a serial monitor on my external processor board) that the VB code is actually working perfectly but... when I switch on all the timers in VB (to turn on all heaters at a pre-determined time), and when the system time lines up with the pre-set text box time, it sends all of the serial commands so fast that my external USB connected processor cannot read them all fast enough inside the parsing routine so it just processes one at random and turns on that heater.

    So, thanks for the inputs here but now I have to go to an AVR forum and get an answer about this. (I am not using "String class" in AVR but the method I am using is still too slow.

    If I knew how to delay the sending of each timer parameter sequentially from VB ie. (T1, T2, T3 etc.) then the system would work fine but I don't know how to make VB send the data with say a one or two second delay between each command  to the USB port. This would give my USB connected AVR time to process each of the "T" commands. 

    Wednesday, April 10, 2019 7:11 AM
  • That actually makes perfect sense as the behavior is exactly like it lacks time to read it all. I thought the 'writing' needs more time but never thought of the 'reading'.

    That's huge step forward so thank you! I'll work in that direction.

     
    Friday, April 12, 2019 5:25 AM

  • Dear AshBasher,

    Your tip was crucial for my outcome as well and for that you have my huge thanks.

    I did some research for Arduino's .read() and .readString() and it turned out it was a really bad idea to use .readString(). Changing my variable to 'char' and writing a single symbol in VB did the work for me.

    Thanks again!

    Monday, April 15, 2019 6:13 AM