Results 1 to 7 of 7

Thread: VB2005 Serial Port

  1. #1
    DaBoonDockSaint is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jan 2009
    Location
    AZ
    Posts
    9
    Reputation
    0

    VB2005 Serial Port

    Still experimenting with the Serial Port component on VB2005 and I have been having issues with my data received event. I set my received bytes threshold to 38 bytes which is the number of bytes that I am expecting every 1 sec and when I get data I read it in my byte array. I use threading.thread.sleep and when I process this data I clear my port buffer using serialport.discardinbuffer method. Now my problem, it appears that ever so many odd seconds I get a problem with my stream and I end up having 44 sometimes 45 bytes coming in and I loose about two seconds of data im wondering if this is something with my read method or if its just the stream being delayed, any experienced programmers have any idea whats going on?
    NEWBEE

  2. #2
    Robert_Zenz's Avatar
    Robert_Zenz is offline VB.NET Forum Idol
    .NET Framework
    .NET 2.0
    Join Date
    Jun 2008
    Location
    Vienna, Austria
    Posts
    503
    Reputation
    340
    Hello.

    Hard to say. I wouldn't use Thread.Sleep, there's no need for that if you use the DataReceived Event...also this could be a timing issue which can be caused by that. I also experienced that the DiscardBuffer methods can take pretty long depending on the OS and hardware (Windows XP and normal Serial Port about some milliseconds...Windows 2003 Server and NPort Server up to 250ms...which is not, at least I think so, a problem of the Framework 'cause the DiscardBuffer methods are leading directly to the API-Call purgeCom()).

    Also some source-code would be of help to determine what's the problem on this. But try how it works without the sleep.

    Bobby
    Don't give TypeCasting Errors a chance, turn ON Option Strict!

  3. #3
    DaBoonDockSaint is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jan 2009
    Location
    AZ
    Posts
    9
    Reputation
    0
    Private Sub com1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
    Dim returnStr As String
    Dim intRead As Integer = 0
    Dim btCnt As Integer = SerialPort1.BytesToRead
    Dim InBuff(btCnt - 1) As Byte
    Dim pRet As Integer

    Application.DoEvents()
    Threading.Thread.Sleep(1)

    intRead = SerialPort1.Read(InBuff, 0, btCnt)

    RECIP = IPAddress.Parse(txtIP.Text)
    RecPortInt = txtPort.Text
    udpClient.Connect(RECIP, RecPortInt)
    bytCommand = InBuff 'Encoding.ASCII.GetBytes(txtInfo.Text)
    pRet = udpClient.Send(bytCommand, bytCommand.Length)


    CheckForIllegalCrossThreadCalls = False
    txtInfo.Text &= returnStr & vbCrLf
    SerialPort1.DiscardInBuffer()
    End Sub
    NEWBEE

  4. #4
    DaBoonDockSaint is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jan 2009
    Location
    AZ
    Posts
    9
    Reputation
    0
    Yea I know my discardInBuffer probably doesnt belong in my data received event. I think I will try using a timer
    NEWBEE

  5. #5
    Robert_Zenz's Avatar
    Robert_Zenz is offline VB.NET Forum Idol
    .NET Framework
    .NET 2.0
    Join Date
    Jun 2008
    Location
    Vienna, Austria
    Posts
    503
    Reputation
    340
    Hello.

    I tried to reqrite the code a little:
    Code:
    Private Sub com1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    	Dim returnStr As String = String.empty
    	Dim InBuff(SerialPort1.BytesToRead - 1) As Byte
    	Dim pRet As Integer = 0
    
    	Trace.TraceInformation("COM1 just read: " & SerialPort1.Read(InBuff, 0, InBuff.Length).ToString("N0") & " Bytes of Data")
    	
    	'probably not needed...since it clears automatically what we read...just try without it
    	'SerialPort1.DiscardInBuffer()
    	
    	'sending what we just read...
    	RECIP = IPAddress.Parse(txtIP.Text)
    	RecPortInt = txtPort.Text
    	udpClient.Connect(RECIP, RecPortInt)
    	bytCommand = InBuff 'Encoding.ASCII.GetBytes(txtInfo.Text)
    	pRet = udpClient.Send(bytCommand, bytCommand.Length)
    
    	CheckForIllegalCrossThreadCalls = False
    	txtInfo.Text &= returnStr & Enviromnent.NewLine
    End Sub
    I'm not sure if this changes anything. But there should be now Debug-Prints which are allowing you to follow how many bytes were read each time.

    Bobby
    Don't give TypeCasting Errors a chance, turn ON Option Strict!

  6. #6
    DaBoonDockSaint is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jan 2009
    Location
    AZ
    Posts
    9
    Reputation
    0
    I ended up finding out my problem it was partially the modems dropping out and partially me clearing the buffer before my data received event would receive the incoming data.
    NEWBEE

  7. #7
    James_Nguyen_Anewtech is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.0 (VS 2005/2008)
    Join Date
    Mar 2009
    Posts
    3
    Reputation
    0

    serial port

    send data:

    If COMPort.IsOpen Then
    COMPort.Close()
    End If
    Try
    COMPort.BaudRate = 9600
    COMPort.Parity = Parity.None
    COMPort.DataBits = 8
    COMPort.StopBits = 1
    COMPort.Open()
    COMPort.Write("data")
    catch ex as exception
    end try

    recieve data:

    Dim sdata As String = ""
    Dim i As Integer = 0
    Try
    If COMPort.IsOpen Then
    recieve = recieve & COMPort.ReadExisting
    If Len(recieve) > 7 Then
    For i = 0 To Len(recieve) - 1
    sdata = sdata & recieve(i).ToString
    Next
    textbox1.text =sdata.tostring
    catch ex as exception
    end try

Bookmarks

Posting Permissions

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