Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.45/5 (6 votes)
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
Updated 19-Jun-18 22:05pm

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[^]
 
Share this answer
 
Comments
Keyuraashra 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 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 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...
Sergey Alexandrovich Kryukov 19-Apr-11 1:33am    
Keyuraashra, and it that the reason for your vote of 1?
--SA
Sergey Alexandrovich Kryukov 19-Apr-11 1:33am    
My 5. Quite a correct answer.
--SA
'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
 
Share this answer
 
Comments
Nitish Shetye 11-Aug-12 7:56am    
Thanks a ton Solution 2 works v well....
ShivendraNirmalkar 15-Mar-13 1:57am    
Nice one.keep it up.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900