Question reading bytes from comport

yarmiaanuj

Member
Joined
May 21, 2012
Messages
12
Programming Experience
1-3
I am working on reading bytes. I went through samples on net and designed a code sample. but when i tried it on pc, it did not work, on the other hand when i tried "realterm" to read from comport, it showed the data. i know something is missing, but i cannot figure it out.....can anybody help ...PLZ
 
How can we possibly know what's missing from your code if we've never see your code?

below is the code...

Imports System.IO.Ports
Public Class Form1
    Private comBuffer As Byte()
    Private Delegate Sub UpdateFormDelegate()
    Private UpdateFormDelegate1 As UpdateFormDelegate
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i1 As Integer = 0 To My.Computer.Ports.SerialPortNames.Count - 1
            ComboBox1.Items.Add(My.Computer.Ports.SerialPortNames(i1))
        Next
        Button1.Text = "Open"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Button1.Text = "Open" Then
            With SerialPort1
                If ComboBox1.Text <> "" Then
                    .PortName = ComboBox1.Text
                Else
                    MsgBox("Choose a port first")
                End If
                .BaudRate = 38400
                .DataBits = 8
                .Parity = Parity.None
                .StopBits = StopBits.One
                .Handshake = Handshake.None
                If .IsOpen = False Then
                    Try
                        .Open()
                    Catch ex As Exception
                        MsgBox(ex.Message)
                    End Try
                End If
            End With
            Button1.Text = "Close"
        Else
            SerialPort1.Close()
            Button1.Text = "Open"
        End If
    End Sub


    Private Sub mySerialPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
        'Handles serial port data received events
        UpdateFormDelegate1 = New UpdateFormDelegate(AddressOf UpdateDisplay)
        Dim n As Integer = SerialPort1.BytesToRead 'find number of bytes in buf
        comBuffer = New Byte(n - 1) {} 're dimension storage buffer
        SerialPort1.Read(comBuffer, 0, n) 'read data from the buffer
        Me.Invoke(UpdateFormDelegate1) 'call the delegate
    End Sub

    Private Sub UpdateDisplay()
        'ListBox1.Items.Add(CStr(comBuffer(0)))
        TextBox1.Text = TextBox1.Text & Environment.NewLine & CStr(comBuffer(0))
        updateList()
    End Sub
    Private Sub updateList()
        ListBox1.Items.Add(CStr(comBuffer(0)))
    End Sub
End Class


i used a text box to add in data coming from comport and a listbox to show all the recieved bytes one by one.
 
Last edited by a moderator:
I've formatted your code correctly for you. Please see how it's done and do so for us in future.

Now, you say that it doesn't work. What exactly do you expect to see and what do you actually see? That will help us to know what to look for instead of just looking through all the code and hoping to see a red flag.
 
I've formatted your code correctly for you. Please see how it's done and do so for us in future.

Now, you say that it doesn't work. What exactly do you expect to see and what do you actually see? That will help us to know what to look for instead of just looking through all the code and hoping to see a red flag.

i am working on WSN demo application....the original code is written in C, my task is to develop it in VB.net. the coordinator will pass the parameters to PC on serial interface and my task is to retrieve those parameters. Initially i got extended ascii characters as output. after a lot of searching and sorting i found that the parameters were in 'byte' format, and hence i need to receive those bytes. for the task to be accomplished i designed the above code to receive bytes and display them in a textbox and listbox simultaneously, but after i compiled my code, i got nothing. to check if 'Private Sub mySerialPort Datareceived()' was working fine, i placed a msgbox within, but even the msgbox did not returned any value. I tried the same with a software called "Realterm" and it showed output. I tried to fetch an output with visual basics, but even it did not work. I am confused what portion i have left behind !!!
 
ight then. Now that you've given us the relevant information it's easy to see what the issue is. That's why you need to provide a FULL and CLEAR description in the first place. Your mySerialPort_DataReceived method isn't handling the event, which is why it isn't executed when the event is raised. You have a method there that handles the form's Load event and another that handles the Click event of Button1. Compare those to that method that's supposed to be handling the DataReceived event of your SerialPort and it should be fairly obvious what the difference is.
 
Back
Top