CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 5 of 8 FirstFirst ... 2345678 LastLast
Results 61 to 75 of 107
  1. #61
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: recieving 3 value in uart

    The RThreshold should be set to 1. If set to 0 the OnComm() event would never fire.
    The code looks good so far. Try it with RThreshold = 1.

  2. #62
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: recieving 3 value in uart

    So I take it that you never hit the break point?

    Code:
    Private Sub MSComm1_OnComm()
    Dim p%
    Static RLine$
    RLine = RLine + MSComm1.Input
    p = Instr(RLine, vbCr)  ' Put your break point here and see what you get.
    If p Then ' a complete record with three numbers has been received now
    dim a$()
    a = Split(Left$(RLine,p-1), "/") 'split the line into the three numbers
    If UBound(a) = 2 Then
    Text1.Text = a(0) 'get first number
    Text2.Text = a(1) 'get second number
    Text3.Text = a(2) 'get third number
    End If
    RLine = Mid$(RLine,p+1) 'reset the receiver variable for the next line
    End If
    End Sub
    Always use [code][/code] tags when posting code.

  3. #63
    Join Date
    Jun 2010
    Posts
    41

    Re: recieving 3 value in uart

    I got an error on this line:

    "RLine = RLine + MSComm1.Input"

    says error reading comm device

    the value of P when I breakpoint is 0

  4. #64
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: recieving 3 value in uart

    Try this:

    Code:
    RLine = RLine & MSComm1.Input
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #65
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: recieving 3 value in uart

    Are you using a real hardware serial port or a usb adapter?
    Always use [code][/code] tags when posting code.

  6. #66
    Join Date
    Jun 2010
    Posts
    41

    Re: recieving 3 value in uart

    Still an error in:
    "RLine = RLine & MSComm1.Input"

    I'am using a Bluetooth serial port profile to connect serially . A Bluetooth module with a serial port profile is used, I connect to the Bluetooth module using a Bluetooth USB dongle.
    Last edited by feitanx; June 23rd, 2010 at 01:00 AM.

  7. #67
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: recieving 3 value in uart

    Probably aren't even connecting thru the USB. Those can cause problems.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #68
    Join Date
    Jun 2010
    Posts
    41

    Re: recieving 3 value in uart

    what do you mean? I can open the port


    Please tell me what could be wrong. Please help.
    Last edited by feitanx; June 23rd, 2010 at 05:48 AM.

  9. #69
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: recieving 3 value in uart

    the problem is most likely that the on comm event is firing for some reason other than data received and the code is trying to receive data when there is not data hence error reading serial port. Seems like I mentioned this could be a problem a few days ago. See post #7
    a simple solution is to do a inbuffer check before trying to read any data from the port.

    Add the test just before the input attempt and the corresponding end if at the bottom of the code just before the end sub

    e.g.

    Code:
    if MSComm1.InBufferCount>0 then
    Last edited by DataMiser; June 23rd, 2010 at 06:46 AM.
    Always use [code][/code] tags when posting code.

  10. #70
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: recieving 3 value in uart

    When using the onComm event to handle receiving of data the proper way is to use a case statement. Here is some sample code from MS.

    Code:
    Private Sub MSComm1_OnComm()
             Dim InBuff As String
    
             Select Case MSComm1.CommEvent
             ' Handle each event or error by placing
             ' code below each case statement.
    
             ' This template is found in the Example
             ' section of the OnComm event Help topic
             ' in VB Help.
    
             ' Errors
                Case comEventBreak   ' A Break was received.
                Case comEventCDTO    ' CD (RLSD) Timeout.
                Case comEventCTSTO   ' CTS Timeout.
                Case comEventDSRTO   ' DSR Timeout.
                Case comEventFrame   ' Framing Error.
                Case comEventOverrun ' Data Lost.
                Case comEventRxOver  ' Receive buffer overflow.
                Case comEventRxParity   ' Parity Error.
                Case comEventTxFull  ' Transmit buffer full.
                Case comEventDCB     ' Unexpected error retrieving DCB]
    
             ' Events
                Case comEvCD   ' Change in the CD line.
                Case comEvCTS  ' Change in the CTS line.
                Case comEvDSR  ' Change in the DSR line.
                Case comEvRing ' Change in the Ring Indicator.
                Case comEvReceive ' Received RThreshold # of chars.
                   InBuff = MSComm1.Input
                   Call HandleInput(InBuff)            
                Case comEvSend ' There are SThreshold number of
                               ' characters in the transmit buffer.
                Case comEvEOF  ' An EOF character was found in the
                               ' input stream.
             End Select
    
          End Sub
    http://support.microsoft.com/kb/194922

    That said testign the InBufferCount as I mentioned will read the data only if data is there which will prevent the error mentioned but as you can see there are several other things that can/will cause the oncomm event to fire.
    Always use [code][/code] tags when posting code.

  11. #71
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: recieving 3 value in uart

    @DataMiser: Your assumption is wrong.
    You can do at ANY time a a$=MSComm1.Input without producing an error, even if the input buffer has no characters. In this case .Input returns an empty string without giving an error.
    The reason that RLine = RLine + MSComm1.Input produces an error must lie elsewhere. Port not accessible or something.

    @feitanx: First I suggest you change the OnComm like this:
    Code:
    Private Sub MSComm1_OnComm()
      Dim p%
      Static RLine$
      Select Case MSComm1.CommEvent
        Case vbMSCommEvReceive
             RLine = RLine + MSComm1.Input
             p = InStr(RLine, vbCr)  ' Put your break point here and see what you get.
             If p Then ' a complete record with three numbers has been received now
                Dim a$()
                a = Split(Left$(RLine, p - 1), "/") 'split the line into the three numbers
                If UBound(a) = 2 Then
                   Text1.Text = a(0) 'get first number
                   Text2.Text = a(1) 'get second number
                   Text3.Text = a(2) 'get third number
                End If
                RLine = Mid$(RLine, p + 1) 'reset the receiver variable for the next line
             End If
        Case Else
             MsgBox "MSComm event: " + CStr(MSComm1.CommEvent), vbOKOnly, "Error"
      End Select
    End Sub
    This will only input when a character is received.
    Other mscomm events will be notified in a messagebox so as we can see what the error can be.

    I think you have to declare the vbMSComm events. The constants do not seem to come automatically when MSComm control is put to the form. So you have to add these private constants:
    Code:
    Private Const vbMSCommEvSend = 1
    Private Const vbMSCommEvReceive = 2
    on top of your form's code.

  12. #72
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: recieving 3 value in uart

    Quote Originally Posted by WoF View Post
    @DataMiser: Your assumption is wrong.
    Actually I got that directly from Microsoft. The error is caused by issuing a read when no data is present to be read. However I did not read far enough. This error happens only with some 3rd party serial drivers such as the one in use here and typically only happens when there is no data present and a read is issued.
    Always use [code][/code] tags when posting code.

  13. #73
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: recieving 3 value in uart

    btw if the port was not accessible the error would appear when an attempt is made to open the port.
    Always use [code][/code] tags when posting code.

  14. #74
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: recieving 3 value in uart

    Ah, ok. Didn't know that.
    When you put an MSComm control on, and have standart serial ports you can read as often as you want from the .Input property without error.

    But if this is a third party interface problem the above code will prevent that, beause it does only .Input when the fired event truly is a receive event.

  15. #75
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: recieving 3 value in uart

    On another note while in some or perhaps even many cases you can get away with reading from a port where no data is present it is bad practice to do so. VB let's you get away with a lot but not always. In this case MS says that the serial device must return Success when a read is issued but not all serial devices will do this if there is nothing to read and hence an error whereas if you check the buffer count first the read will not be issued when the buffer is empty and the error should nto occur.
    Always use [code][/code] tags when posting code.

Page 5 of 8 FirstFirst ... 2345678 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured