Problem in getting data from Serial Port

shalabhkamboj

New member
Joined
Jan 30, 2011
Messages
3
Programming Experience
Beginner
Hi All,
I am facing problem reading data from Serial port. Actually I am connecting with the hardware device called Crimp Force Analyzer. It gives the data on hyperterminal and I have to read that data from my vb.net program. The data is somewhat like this:
18:49 29/01/2011,79,1,11.26,0,50,Pass.

I think this data is on serial port and I m trying to read it from there.
Can anybody help me out with this? Its urgent.

Thanks in advance.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,100
Location
Sydney, Australia
Programming Experience
10+
The .NET Framework has a SerialPort class. You create an instance, configure it appropriately and then read the data. Search the web and you should find plenty of examples. You would use basically the same settings for the SerialPort object as you do for HyperTerminal. Note also that the SerialPort class is a component and it's in your Toolbox by default, so you can add an instance to a form in the designer if desired.
 

shalabhkamboj

New member
Joined
Jan 30, 2011
Messages
3
Programming Experience
Beginner
I have already done it biut not been able to read the data.
The code is:

If SerialPort1.IsOpen Then SerialPort1.Close()
With SerialPort1
.PortName = cmbPorts.Text
.BaudRate = 57600
.StopBits = IO.Ports.StopBits.One
.DataBits = 8
.Parity = IO.Ports.Parity.None
.ReadTimeout = 50000
.WriteTimeout = 50000
'.RtsEnable = True
'.Handshake = IO.Ports.Handshake.None

End With
If SerialPort1.IsOpen = False Then
SerialPort1.Open()
End If

Dim strval As String = SerialPort1.ReadBufferSize()

MsgBox(strval)
MsgBox(SerialPort1.ReadLine())

I am getting value in strval - 4096.

But not able to read the data in string. And also it stuck on the message when I try to read the data on SerialPort.
 

shalabhkamboj

New member
Joined
Jan 30, 2011
Messages
3
Programming Experience
Beginner
Can anybody help me out of this problem. Its becoming bottleneck now.

This is what I have done.

Public Event DataReceived As Ports.SerialDataReceivedEventHandler

Private Sub btnWriteToPort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteToPort.Click

Dim SerialPort1 As New Ports.SerialPort

If SerialPort1.IsOpen Then SerialPort1.Close()
With SerialPort1
.PortName = cmbPorts.Text
.BaudRate = 57600
.StopBits = IO.Ports.StopBits.Two
.DataBits = 8
.Parity = IO.Ports.Parity.None
.ReadTimeout = 50000
.WriteTimeout = 50000
.RtsEnable = True
End With

If SerialPort1.IsOpen = False Then
SerialPort1.Open()
End If

If txtWriteOnPort.Text = "" Then
MsgBox("iNSERT SOME VALUE IN TXT BOX")
Else
SerialPort1.WriteLine(txtWriteOnPort.Text & Chr(13) & Chr(10))
SerialPort1.Close()
End If
End Sub

PrivateSub btnReadFromPort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadFromPort.Click
If SerialPort1.IsOpen Then SerialPort1.Close()
With SerialPort1
.PortName = cmbPorts.Text
.BaudRate = 57600
.StopBits = IO.Ports.StopBits.Two
.DataBits = 8
.Parity = IO.Ports.Parity.None
.ReadTimeout = 50000
.WriteTimeout = 50000
.RtsEnable =
True
End With

AddHandler SerialPort1.DataReceived, AddressOf DataReceviedHandler

If SerialPort1.IsOpen = False Then
SerialPort1.Open()
End If
SerialPort1.Close()
End Sub

PrivateShared Sub DataReceviedHandler(ByVal sender As Object, ByVal e As Ports.SerialDataReceivedEventArgs)
Dim sp As Ports.SerialPort = CType(sender, Ports.SerialPort)
Dim indata As String = sp.ReadExisting()

MsgBox("Data Received:")
MsgBox(indata)
End Sub

Can you please let me know where I am wrong. These setting of the port are suggested by the manufacturing company of the hardware device.

Thanks in advance
 
Top Bottom