none
DataReceived event not triggered in vb.Net until serial port is closed & opened again RRS feed

  • Question

  • I am developing an application which sends multiple AT commands to a modem in VB.Net. After writing an AT command to the modem, I see the dataReceived event is not getting triggered until I close the port & open it again & write the next AT command. This behavior is consistent. As soon as I write the next AT command the response of previous AT command is read. Even adding a delay between the two write operations does not work. I also added a readData between the two writes so that data is read if it exist in buffer or stream but is not triggering datareceived event because bytestoread is non-zero, but that does not hep too. Because of insufficient read operations the data in readbuffer grows(accumulating) & after sometime the modem stops receiving the AT commands. Please suggest a solution

    code for data received handler & read data function

     

    While comPort.BytesToRead > 0

        Dim msgCharArray(255) As Char

     

        If comPort.BytesToRead >= 256 Then

        comPort.Read(msgCharArray, 0, 256)

     

    Else

       comPort.Read(msgCharArray, 0, comPort.BytesToRead)

     

    End If

     

    End While

    Thursday, September 16, 2010 4:40 PM

Answers

  • If the handler is never added to the handler list it will never be called. 
    • Marked as answer by SamAgain Monday, September 27, 2010 6:53 AM
    Friday, September 17, 2010 4:41 PM
  • Since you aren't doing anything with the data, the only statement you need in the DataReceived event is:

    comPort.DiscardInBuffer()

    But you can just set the size of the ReadBufferSize to 1 and forget about the DataReceived event.

    • Marked as answer by SamAgain Monday, September 27, 2010 6:53 AM
    Friday, September 17, 2010 5:24 PM
  • So, all you are doing is emptying the Read buffer and the DataRecevied event quits firing?  How do you know there are bytes in the Read buffer when  the event stops firing?
    • Marked as answer by SamAgain Monday, September 27, 2010 6:53 AM
    Friday, September 17, 2010 5:38 PM
  • I really can't help.  The code you post seems to have no relationship to the code in your program.  Just keep troubleshooting your code as you are until you get the result you want.
    • Marked as answer by SamAgain Monday, September 27, 2010 6:53 AM
    Friday, September 17, 2010 6:09 PM

All replies

  • Post your complete DataReceived event and the code to add used to add the handler.  Use the code formatter:  last icon  on the right </>.
    Thursday, September 16, 2010 9:38 PM
  • Data
    #Region "comPort_DataReceived"
      ''' <summary>
      ''' method that will be called when theres data waiting in the buffer
      ''' </summary>
      ''' <param name="sender"></param>
      ''' <param name="e"></param>
      Private Sub comPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
        
        Try    
          Dim completeMsg As String = String.Empty
    
          While comPort.BytesToRead > 0
            Dim msgCharArray(255) As Char
            If comPort.BytesToRead >= 256 Then
              comPort.Read(msgCharArray, 0, 256)
            Else
              comPort.Read(msgCharArray, 0, comPort.BytesToRead)
            End If
            Dim msgString As String = CStr(msgCharArray)
            completeMsg += msgString
          End While
          Catch ex As Exception
          MessageBox.Show("Exception Occured: " + ex.Message)
        End Try
    
      End Sub
    #End Region
    
    Friday, September 17, 2010 4:23 PM
  • If the handler is never added to the handler list it will never be called. 
    • Marked as answer by SamAgain Monday, September 27, 2010 6:53 AM
    Friday, September 17, 2010 4:41 PM
  • The handler is being called but is random. I ran it again & noticed that it is being called, but not every time. looks like the BytesToRead are not becoming zero, due to which it is not invoked again. But how that is happening, I dont know.

    Regarding you point, please see the code for open below: -

    #Region "OpenPort"
      Public Function OpenPort() As Boolean
        Try
          If comPort.IsOpen = True Then
            comPort.Close()
          End If
          
          comPort.PortName = _portName
          comPort.DtrEnable = True
          comPort.Open()
    
          AddHandler comPort.DataReceived, AddressOf comPort_DataReceived
    
          While comPort.BytesToRead > 0
            Dim msgCharArray(255) As Char
            If comPort.BytesToRead >= 256 Then
              comPort.Read(msgCharArray, 0, 256)
            Else
              comPort.Read(msgCharArray, 0, comPort.BytesToRead)
            End If
          End While
          comPort.DiscardInBuffer()
          Return True
        Catch ex As Exception
          MessageBox.Show("Exception Occured: " + ex.Message)
          Return False
        End Try
      End Function
    #End Region
    

    I have to close & open the port again & again, because of this problem. Hence I added the while loop to read all the data from previous AT command, before proceeding with the next AT command

    Thanks

    Friday, September 17, 2010 5:06 PM
  • Since you aren't doing anything with the data, the only statement you need in the DataReceived event is:

    comPort.DiscardInBuffer()

    But you can just set the size of the ReadBufferSize to 1 and forget about the DataReceived event.

    • Marked as answer by SamAgain Monday, September 27, 2010 6:53 AM
    Friday, September 17, 2010 5:24 PM
  • I tried figuring the problem using portmon. I am running a series of commands and the problem start when I fire a command, to which UE responds in multiple steps. For eg.

    >AT+CGMI

    AT+CGMI....OK

    -response triggers a data received event

    >AT+command1

    AT+command1...OK

    -response triggers a data received event

    >AT + command2

    response 1 (response1 triggers a data received event)

    response 2 (after x ms of response 1)

    response 3 (after x ms of response 2)

    response 2 & 3  do not trigger data received event & no data received event is triggered thereafter

    Friday, September 17, 2010 5:29 PM
  • So, all you are doing is emptying the Read buffer and the DataRecevied event quits firing?  How do you know there are bytes in the Read buffer when  the event stops firing?
    • Marked as answer by SamAgain Monday, September 27, 2010 6:53 AM
    Friday, September 17, 2010 5:38 PM
  • because when I close the port & open it again and then write the next AT command, it still shows the response of previous command. This happens for all subsequent AT commands, after the multi response AT command is triggered.
    Friday, September 17, 2010 5:50 PM
  • Also, I am using System.Windows.Forms.Application.DoEvents() between 2 AT commands. Will this stop the data received event from being triggered, because it is executed on a secondary thread?
    • Edited by s-singh Friday, September 17, 2010 6:46 PM
    Friday, September 17, 2010 5:55 PM
  • I really can't help.  The code you post seems to have no relationship to the code in your program.  Just keep troubleshooting your code as you are until you get the result you want.
    • Marked as answer by SamAgain Monday, September 27, 2010 6:53 AM
    Friday, September 17, 2010 6:09 PM
  • Also, I am using System.Windows.Forms.Application.DoEvents() between 2 AT commands. Will this stop the data received event from being triggered, because it is executed on a secondary thread?

    I AM with Same problemas, did you already sol ed that issue?

    Thanks in advance

    Best regards

    Bruno Cardoso

    Tuesday, October 23, 2012 6:29 PM