Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi to all,
 
I have one application in which there is one machine (a tub which gives amount of liter) on display board. It is having Serial Port RS232 in machine. So what i would thinking to do is that through RS232 port i directly want to display it in my application in one text box. So how can i access data from RS232 port in VB.NET (I am using Visual Studio 2008). I know there is Class called System.IO available. If anybody know this please help me!!!! Also want to know that how to test the code that it is working or not, without attaching it to machine through a serial port RS232?
 

Thank you
Posted 18-Apr-11 18:49pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

'Serial Port Interfacing with VB.net 2010 Express Edition
'Copyright (C) 2010 Richard Myrick T. Arellaga
'
'This program is free software: you can redistribute it and/or modify
'it under the terms of the GNU General Public License as published by
'the Free Software Foundation, either version 3 of the License, or
'(at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
'GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see .
 

Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports
Public Class frmMain
Dim myPort As Array 'COM Ports detected on the system will be stored here
Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
 
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box.
myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available
cmbBaud.Items.Add(9600) 'Populate the cmbBaud Combo box to common baud rates used
cmbBaud.Items.Add(19200)
cmbBaud.Items.Add(38400)
cmbBaud.Items.Add(57600)
cmbBaud.Items.Add(115200)
 
For i = 0 To UBound(myPort)
cmbPort.Items.Add(myPort(i))
Next
cmbPort.Text = cmbPort.Items.Item(0) 'Set cmbPort text to the first COM port detected
cmbBaud.Text = cmbBaud.Items.Item(0) 'Set cmbBaud text to the first Baud rate on the list
 
btnDisconnect.Enabled = False 'Initially Disconnect Button is Disabled
 
End Sub
 
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
SerialPort1.PortName = cmbPort.Text 'Set SerialPort1 to the selected COM port at startup
SerialPort1.BaudRate = cmbBaud.Text 'Set Baud rate to the selected value on
 
'Other Serial Port Property
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8 'Open our serial port
SerialPort1.Open()
 
btnConnect.Enabled = False 'Disable Connect button
btnDisconnect.Enabled = True 'and Enable Disconnect button
 
End Sub
 
Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
SerialPort1.Close() 'Close our Serial Port
 
btnConnect.Enabled = True
btnDisconnect.Enabled = False
End Sub
 
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
SerialPort1.Write(txtTransmit.Text & vbCr)
'The text contained in the txtText will be sent to the serial port as ascii
'plus the carriage return (Enter Key) the carriage return can be ommitted if the other end does not need it
End Sub
 
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting()) 'Automatically called every time a data is received at the serialPort
End Sub
Private Sub ReceivedText(ByVal [text] As String)
'compares the ID of the creating Thread to the ID of the calling Thread
If Me.rtbReceived.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.rtbReceived.Text &= [text]
End If
End Sub
 
Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.PortName = cmbPort.Text 'pop a message box to user if he is changing ports
Else 'without disconnecting first.
MsgBox("Valid only if port is Closed", vbCritical)
End If
End Sub
 
Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaud.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.BaudRate = cmbBaud.Text 'pop a message box to user if he is changing baud rate
Else 'without disconnecting first.
MsgBox("Valid only if port is Closed", vbCritical)
End If
End Sub
 
Private Sub rtbReceived_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtbReceived.TextChanged
 
End Sub
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
rtbReceived.Text = " "
 
End Sub
 

 
Private Sub txtTransmit_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTransmit.TextChanged
 
End Sub
 
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
SerialPort1.Write(txtTransmit.Text & vbCr)
End Sub
End Class
  Permalink  
Comments
Nitish Shetye at 11-Aug-12 7:56am
   
Thanks a ton Solution 2 works v well....
ShivendraNirmalkar at 15-Mar-13 1:57am
   
Nice one.keep it up.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You could have easily googled that on your own. Here is a step by step introduction from the MSDN:
http://support.microsoft.com/kb/823179/EN-US[^]
  Permalink  
Comments
Keyuraashra at 19-Apr-11 0:59am
   
Thanks for your help, but i don't want hundreds line of code. Rather it could also be done through a simple one. Thanks for your reply!!!!!
kalyan12345 at 25-Sep-13 13:17pm
   
I have some serial device on my web server, I want to use the web forms to control the serial device. How do I do? Win form works for me - but I am not able to implement using web forms. I use vb.net for my programming
JF2015 at 19-Apr-11 1:03am
   
This is a rather simple aproach - you could always only use the parts that you really need - first thing though is to understand what it does before you can try to include the parts that you need in your own application. No need to downvote...
SAKryukov at 19-Apr-11 1:33am
   
Keyuraashra, and it that the reason for your vote of 1?
--SA
SAKryukov at 19-Apr-11 1:33am
   
My 5. Quite a correct answer.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 25 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100