[VB2005] Reading from serial port, pls help!

Im currently working on serial port RS232 communication, im able to set configurations thru serial port to my device, but having problems reading from the port. Ive been advised that there're two ways of doing it, which include write the program so that the incoming data is stored inside certain file on computer and data can be extracted straight away from it..well another one, is extracting the data from the computer temporary memory..

but i have doubts regarding those methods and totally have no clue of performing the task, can someone please give me some guidelines of reading data from serial port
thanks in advance!


Answer this question

[VB2005] Reading from serial port, pls help!

  • friggityfraggity

    Thanks ahmedilyas and nobugz for the precious comments. But I have not yet tried the codes since the device is inside the lab. So i made some changes on the codes which appeared below this:

    my questions:

    1) is this the right way of declaring delegate and update textbox1 by putting ByVal rxbyte As String Does it mean that the data which is polled in the form of byte array has been converted to the string

    2)By simply declaring the textbox with AppendText(rxbyte), the data array can be appended to the textbox sequentially

    ANy suggestions and comments will be very much appreciated.

    Dim WithEvents SerialPort As New System.IO.Ports.SerialPort

    Dim rxbyte As New ArrayList

    Public Delegate Sub myDelegate(ByVal rxbyte As String)

    Public Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As _

    System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

    Do While SerialPort1.BytesToRead > 0

    Dim b As Byte = SerialPort1.ReadByte()

    rxbyte.Add(b)

    Loop

    TextBox1.Invoke(New myDelegate(AddressOf updateTextBox1), _

    New Object() {SerialPort1.ReadByte().ToString()})

    End Sub

    Public Sub updateTextBox1(ByVal rxbyte As String)

    With TextBox1

    .AppendText(rxbyte)

    End With

    End Sub


  • djm-web

    wencey wrote:
    But then, im confused because the fast changing figure are not really numbers we can read, instead i get something like, n E, etc.. What should i do with them so that i can read the actual value.

    If you are not getting the data back in the expected format you need to make sure that you have the correct baud rate, parity, and stop bits set for the com port



  • simon_

    to append to the textbox, what you have here is almost there but should be:

    Public Sub updateTextBox1(ByVal theText as string)

    With TextBox1

    .AppendText(theText)

    End With

    End Sub

    Then to invoke it:

    Me.TextBox1.Invoke(New myDelegate(AddressOf updateTextBox1), New Object() {SerialPort1.ReadByte().ToString()})

    it *maybe* ReadByte().ToString() - im not sure but this is how you would go about appending text to the textbox



  • Dietz

    I have to read continuos data of signals, and display them in a real time graph. but at the time being, im concentrating on receiving data from the serial port part first. so by using the class datareceived, this can be done what about the win32 api how to have the incoming data directly saved to a file

    thanks..


  • mouxaifeong

    Hi Wencey,

    Exactly, what is your problem DataReceived is an event - not a class, and this event is exactly what our small program uses to read the data, so why not take the time to read the code and the description By adding your transmission protocol to the Receiver subroutine it will be very easy to modify the program to do what you want.


  • Paul Deen

    if I understand correctly you wish to read data from a serial port correct if so, you should implement the DataRecieved event for that SerialPort instance. Check this out:

    http://msdn2.microsoft.com/en-us/library/system.io.ports.serialport.aspx

    http://msdn2.microsoft.com/en-us/library/system.io.ports.serialport.datareceived.aspx

     

    doing it this way is the correct way.



  • Paulustrious

    Hi Wencey,

    Sorry, but where do you read the data from the serial port I can find an Invoke statement, which is a little cryptical, but probably is used to transmit data to updateTextBox3, but there is no read statement. I miss something like SerialPort1.ReadByte or SerialPort1.ReadLine (ReadLine is probably the best choice since all telegrams seems to be terminated with Cr). Also the updateTextBox3 subroutine is empty, so I cannot see how the code you have published is able to work!

    The fast changing mysterious data could be a hardware (ground) error, but I think it is a software bug! Try to use our program to communicate with the spectrum analyzer and see if it answers correctly and that you do not receive mysterious bytes. Our program shows all received values as Hex, so you are able too see everything, which is transmitted. If everything is OK with our program, you know that it is an Error40, that is, the error can be found 40 cm in front of the screen ;-)

    The following link shows how to use the timer to generate an event at regular time intervals.

    http://www.vbdotnetheaven.com/Code/Jun2003/2084.asp

    You may use this event instead of Button8 to poll your spectrum analyzer.

    I can't help you with the graphical display, but I know that there has been questions about this in this forum so a little search may give you the answer.

    Besides, why are you using both chr(13) and vbCrLf You get two Cr's.


  • Barry Kwok

    Hi Wensey,

    In the knowledgebase on our homepage you may find a small program for serial port communication including source code and documentation. This program shows how to read (and write) from (to) the serial port. The URL is:

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

    Best regards

    Innovatic, Carsten Kanstrup


  • vzzvzz

    Im looking forward to comments and corrections regarding the following source codes:

    when i have put incoming data in an array, how am i going to append the data in textbox

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    If SerialPort1.IsOpen Then

    SerialPort1.Close()

    End If

    Try

    With SerialPort1

    .PortName = "COM1"

    .BaudRate = 9600

    .Parity = IO.Ports.Parity.None

    .DataBits = 8

    .StopBits = IO.Ports.StopBits.Two

    End With

    SerialPort1.Open()

    Catch ex As Exception

    MsgBox(ex.ToString)

    End Try

    End Sub

    Dim WithEvents SerialPort As New System.IO.Ports.SerialPort

    Dim rxbyte As New ArrayList

    Dim i As Integer

    Public Delegate Sub myDelegate()

    Public Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As _

    System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

    If SerialPort1.BytesToRead > 0 Then

    Do

    rxbyte.Add(SerialPort1.ReadByte)

    TextBox1.Invoke(New myDelegate(AddressOf updateTextBox1), _

    New Object() {SerialPort1.ReadByte})

    Exit Do

    Loop

    End If

    End Sub

    Public Sub updateTextBox1()

    What should be put here

    With TextBox1

    .AppendText(rxbyte(i))

    End With

    End Sub

    End Class


  • Palle

    dont use Win32 API - dont know if there is a way but if there is then don't use it - avoid it as it defeats the purpose of the .NET Framework and your performance will decrease - use the classes and events supplied in the .NET Framework. The DataRecieved event will be the one you are after to read data received on the serial port

  • Jaime Stuardo

    Watch out, you are calling ReadByte() twice in the loop, change it so you call it only once, for example:

    Do While SerialPort1.BytesToRead > 0
    Dim b As Byte = SerialPort1.ReadByte()
    rxbyte.Add(b)
    Loop
    TextBox1.Invoke(...)

    The Invoke call is also *very* expensive, consider taking it out of the loop (as I did) or buffer the data until enough characters are received or until you read a 'magic' byte value (like NewLine).



  • Justin_H

    Hi Carsten, thanks for your comments and suggestions...Im now still in the process of understanding your codes and everything. But now that im having some problem with opening my file. When i did the work inside the lab, everything is still ok, but when i come back, the file cannot be opened, and it says the location of the file has been moved or deleted. but i saved everything insides the lab...


  • SuperFox

    it really depends upon the type of data comming in on the port and how it comes in...there are several methods for reading the port...

    Dim MyPort As System.IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort("Com1")

    Dim Data As Integer = MyPort.ReadByte()

    Data = MyPort.ReadChar

    Dim ExistingStringData As String = MyPort.ReadExisting

    Dim CurrentLine As String = MyPort.ReadLine

    Dim Buffer(100) As Byte

    MyPort.Read(Buffer, 0, 100)



  • Ugur Umutluoglu

    Hi, i really appreciate those help and suggestions i get from this forum..i still have many questions to ask, hopefully i can get as many feedback as i can from this forum..

    For your information, i'm sending out command in ascii form, for example:

    I want to set the "run" state of spectrum analyzer, its command is Q1 CR

    therefore, inside a button event handler, i wrote:

    Try

    SerialPort1.Write(Chr(81) & Chr(49) & Chr(13))

    Catch ex As Exception

    MsgBox(ex.ToString)

    End Try

    As for reading from the serial port part, the problems come where, i put the below source codes, and the results is that the value is changing extremely fast like overflow that i cant really read the values measured. The values received from the spectrum analyzer are integer value. like, -70, etc...sometimes the value remain constant... how do i set the timer so that the port will poll for new incoming data at certain interval, say 1second,... and it discards the same incoming value as the present one, and only display an incoming data which is different from present.

    But then, im confused because the fast changing figure are not really numbers we can read, instead i get something like, n E, etc.. What should i do with them so that i can read the actual value.

    Besides, if i want to display the incoming data in an an real time graph, what should i use inside the toolbox

    'Read signals

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click

    SerialPort1.WriteLine(Chr(82) & Chr(51) & Chr(13) & vbCrLf)

    End Sub

    Private Sub DataReceived( _

    ByVal sender As Object, _

    ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _

    Handles SerialPort1.DataReceived

    TextBox3.Invoke(New _

    myDelegate(AddressOf updateTextBox3), _

    New Object() {})

    End Sub

    Public Delegate Sub myDelegate()

    Public Sub updateTextBox3()

    End Sub

    Your help is very much appreciated!


  • [VB2005] Reading from serial port, pls help!