4 Replies - 2849 Views - Last Post: 08 February 2011 - 10:10 AM Rate Topic: -----

#1 pangetadrian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 04-January 11

Serial Communication with VB.net

Posted 07 February 2011 - 02:14 PM

hi sirs and mams..im a noobie in serial communication in vb.net..here is my code..


Imports System.Threading

Imports System.IO

Imports System.Text

Imports System.IO.Ports
Public Class Form1

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

        Dim i As String

        Do
            i = SerialPort1.ReadByte
            RichTextBox1.Text = RichTextBox1.Text + "<" + Str(i) + ">"
            RichTextBox1.Refresh()
            TextBox1.Text = "Hellow World"
        Loop While i <> 100
            End Sub

    Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        If SerialPort1.IsOpen() Then
            SerialPort1.Close()
        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.PortName = "Com5"
        SerialPort1.Open()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text = "Hellow World"
    End Sub
End Class




in my code,im inputting a byte coming from Arduino board microncontroller..im sending character 'N' to com5, and the code above is fetching that data from arduino board..my problem is the output string on the textbox is <78> which is the ASCII equivalent of 'N' how will i convert that to what is really being sent by the board which is 'N'..


anyone?..

Is This A Good Question/Topic? 0
  • +

Replies To: Serial Communication with VB.net

#2 Guest_Jim*


Reputation:

Re: Serial Communication with VB.net

Posted 07 February 2011 - 02:25 PM

Use the "Chr" function!

Jim
Was This Post Helpful? 0

#3 pangetadrian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 04-January 11

Re: Serial Communication with VB.net

Posted 08 February 2011 - 07:42 AM

View PostJim, on 07 February 2011 - 02:25 PM, said:

Use the "Chr" function!

Jim


sir jim, what if im not receiving a single char, what if 101 is being fed to com5 and my software will listen to that and output that to a textbox..my output will look like this "<49><48><49>" (49 is the eqiuvalent of 1 and 48 for 0)..
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Jim*


Reputation:

Re: Serial Communication with VB.net

Posted 08 February 2011 - 08:42 AM

The reason that your output looks like "<49><48><49>" is because that's what you're putting in the textbox here:

       Do
	            i = SerialPort1.ReadByte
	            RichTextBox1.Text = RichTextBox1.Text + "<" + Str(i) + ">"
	            RichTextBox1.Refresh()
	            TextBox1.Text = "Hellow World"
	        Loop While i <> 100



If you want to have "101" in your textbox, change this line

	            RichTextBox1.Text = RichTextBox1.Text + "<" + Str(i) + ">"



to this:

	            RichTextBox1.Text = RichTextBox1.Text & Chr(i)



Or, am I missing something?

Jim
Was This Post Helpful? 1

#5 pangetadrian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 04-January 11

Re: Serial Communication with VB.net

Posted 08 February 2011 - 10:10 AM

it worked..thanks!.. ^_^

i have another question..how can i set a condition that when 101 is read from the com5 port it will change the TextBox1.Text = "Hellow World"

i have this condtion

If i = 101 Then
    TextBox1.Text = "Hellow World"
End If



but wont work.. is my understanding of the serial output wrong?..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1