RS232 (Comm Port) Cant read the line HELP

Hi,
I was trying to read the text from comm poert w/ new SerialPOrt object but for some reson it dosnt work. But I can write everything though. I'm using all defult seetings for the port object. And here is the code that I have. Also when I chnded read timeout, I got an error in time out.

Public Class PORTs

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

sp.Open()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

sp.Close()

End Sub

Private Sub sp_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles sp.DataReceived

Dim inData As String = sp.ReadLine

MessageBox.Show("We got that data:" & inData)

End Sub

End Class




Answer this question

RS232 (Comm Port) Cant read the line HELP

  • dandesro

    just red that post, i'm feeling my knowlage is not enough to understand how that works :-(

  • hanafy

    yea aperantly that was chr(13)

  • paciv


    You may find a small sample program for serial port communication including source code in the knowledgebase on our homepage. The address is:

    http://www.innovatic.dk/knowledg/SerialCOM/SerialCOM.htm


    I think looking at a sample program will give you a better understanding of this functionality.

    And then maybe looking at the SerialPort class itself to understand further the way this simple app was constructed.

    http://msdn2.microsoft.com/en-us/library/30swa673(vs.80).aspx


  • Mukhthar

    Thanks but it looks like your app in .net 2003 isnt it

    I just gote my code to work partially by using ReadExisring, but it still separates the values in 2 or 3 parts and when I want to assign them to TextBox im getting Faill Safe write exeption, and I can figure how to use the fail safe methods... i alredy read everythind on MSDN but can figure that out



  • Anonymous Guest

    Hey I was able to make it work after reasembling the app, altough ReadLine still doesnt work, but Im fine w/ ReadExisting...

    Well I have one question left, when I read something w/ my scanner im getting this character at the end of the line which looks like 0 but square one... I tried to add vbCrLf

    TextBox1.AppendText(Buffer & vbCrLf)

    And this gets rids of the symbol but it brakes my string into 3 or 4 lines, depanding how big the barcode is :-(

    Any ideas how I can fix this Thnx



  • Hacker_594

    use String.Replace like so:

     

    Dim s As String

    s = TextBox1.Text

    TextBox2.Text = s.Replace(vbCrLf, "X")

    Substitue the X with a Space   Chr(32) or whatever you need.

    james

    aka:Trucker

     Edit: if you want one continous string just change the "X" to double quotes, "" . That will give you one continous line.

     


  • Brandon Paddock MS

    Start with this link in Help:

    ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_vbcn/html/8371ce2c-e1c7-476b-a86d-9afc2614b6b7.htm

    Here is the code sample with a simple function to open the port ( Comm1 in this case) and read any data that may be coming thru the port.

    Function ReceiveSerialData() As String
      ' Receive strings from a serial port.
      Dim returnStr As String = ""
    
      Using com1 As IO.Ports.SerialPort = _
          My.Computer.Ports.OpenSerialPort("COM1")
        Do
          Dim Incoming As String = com1.ReadLine()
          If Incoming Is Nothing Then
            Exit Do
          Else
            returnStr &= Incoming & vbCrLf
          End If
        Loop
      End Using
    
      Return returnStr
    End Function
    
    I don't currently have a way to test this code, but, it looks similar 
    code I have worked on in the past. 
    James
    aka:Trucker
     

  • rjm1963

    Check a couple of posts down from this one. There is another post concerning the same problems. There is a link to a good example program ( including source code) that should help you.

    james

    aka:Trucker

     

    Edit, follow the link in Carsten's post above. That is the example program that I wrote about.  There is a lot of good information on that site. Well worth the time reading and downloading the sample. Thanks to Carsten for making it available!!

     

     


  • RainMan82

    If your code gets input with ReadExisting and not with ReadLine it is probably because ReadLine will not return before a specified termination character has been received - usually Line Feed.

    ReadExisting will return when there are no more bytes available in the receive buffer. Therefore, your telegram may be split up in many parts - especially if a low communication speed is used. You must collect a full package of data yourself in a loop and then send this to a display routine.

    The "Fail Safe Write Exception" is probably coursed by the fact that the subroutine sp_DataReceived is event driven (handles sp.DataReceived) and therefore becomes its own thread. One thread is not allowed to write directly to the data area of another thread, and this will happen if you try to write to a TextBox in the main program. The decription of our small sample program has just been enhanced considerably and describes this in details - including how to write data to a TextBox in a thread safe way. As specified in another answer the address is:

    http://www.innovatic.dk/knowledg/SerialCOM/SerialCOM.htm

    Good luck

    Innovatic, Carsten Kanstrup

     

     

     


  • Latham

    Its not my app but the application looking at the source appears to be a 2005 application.   Either way 2003 code will work in 2005.

    So is you problem that you cant write to the textbox   If thats your issue then I'm taking an educated guess here and  would guess that this is related to threading.   The serial port communications is happening on a different thread to the UI and therefore when you are setting the focus you may not actually be setting the focus on the UI thread.

    A similar issue to this was reported and resolved a few weeks back and I'll point you to that URL where you can follow the thread and see if this helps.  As I dont have a way of recieving data on a serial port its more difficult to verify

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=340347&SiteID=1&PageID=1


  • Malik05

    Trucker, yea thats what in MS documentation, but I'm getting freez or connection time out exeption if im using ReadLine... so I used ReadExisting and to save CPU I was using on datarevive function that is avalible to the Comm Port instance :-(

  • RS232 (Comm Port) Cant read the line HELP