SerialPort Question

Cliff

Member
Joined
Mar 31, 2006
Messages
8
Programming Experience
3-5
Hello everyone , I hope someone can help me with my problem. I’m having a problem with a serial port. I’ve connected a balance to my laptop and I want to get the data from the balance on my computer. So when I weigh something it should be possible to see the weight on my screen.
First off all I’m not very experienced with this matter. So I’m hoping for your help on this.

Here is my code , the settings are correct , I’ve got those from the supplier itself.
My question is, how can I call the mySerialPort_Datareceived method in the Form_Load method and will it work ? thanks for helping me out !


Public Class Form
'SerialPort object creation
WithEvents mySerialPort As New IO.Ports.SerialPort

Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'setting the mySerialPort settings
With mySerialPort
.PortName = "COM1"
.BaudRate = 9600
.Parity = IO.Ports.Parity.Even
.DataBits = 7
.StopBits = IO.Ports.StopBits.One
End With

'method to get the get the stream from the balance
Private Sub mySerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
Console.Write(mySerialPort.ReadExisting)
End Sub


Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.
AddHandler mySerialPort.DataReceived, AddressOfmySerialPort_DataReceived

End Sub


Kind Regards
Depreytere Cliff
POVLT @ Belgium
 
Thank you for the interesting site , but i've read it already and it could not help me to get my code running because there is no information about the SerialPort Class which I use in VB.NET
 
Cliff, you don't call an event handler, you 'start' the object that holds the event. If and when that object raises the event, your subscription to the objects event through the event handler will trigger.

In your form load after you have set up the mySerialPort object you start it by calling its Open method. When event is triggered you use one of the Read methods of SerialPort class to get data.
http://www.devx.com/dotnet/Article/31001/0/page/2
 
@JOHNH. : Thank you for the interesting site. I've studied the url you gave me last friday and made some important changes to my code.
My errorcodes are gone and my program is runnable but I don't get information on the screen.

Imports System.IO.Ports.SerialPort
Imports System

Public Class Form
'SerialPort object creation
WithEvents mySerialPort As New IO.Ports.SerialPort

Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To My.Computer.Ports.SerialPortNames.Count - 1
cbbPorts.Items.Add(My.Computer.Ports.SerialPortNames(i))
Next
btnDisconnect.Enabled = False
btnConnect.Enabled = True

End Sub

Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
If mySerialPort.IsOpen() Then
mySerialPort.Close()
End If

Try
With mySerialPort
.PortName = cbbPorts.Text
.BaudRate = 9600
.Parity = IO.Ports.Parity.Even
.DataBits = 7
.StopBits = IO.Ports.StopBits.One
End With
mySerialPort.Open()
lblMessage.Width = 100
lblMessage.Text = cbbPorts.Text & " connected"
btnConnect.Enabled = False
btnDisconnect.Enabled = True
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Me.updateTextBox()
End Sub

Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
Try
mySerialPort.Close()
lblMessage.Text = mySerialPort.PortName & " disconnected."
btnDisconnect.Enabled = False
btnConnect.Enabled = True
Catch ex As Exception

End Try
End Sub

Public Delegate Sub myDelegate()
Public Sub updateTextBox()
With txtDataReceived
.Font = New Font("Garamond", 12.0!, FontStyle.Bold)
.BackColor = Color.Lavender
.AppendText(mySerialPort.ReadExisting)
Console.WriteLine(mySerialPort.ReadExisting.ToString)

.ScrollToCaret()
End With

End Sub

Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles mySerialPort.DataReceived
txtDataReceived.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
End Sub

End Class

Kind Regards
Cliff Depreytere
 
So I can see the available ports now and I can click connect and then the suitable labeltext appears on the screen but I don't get any information about the weight on my screen . So I suppose there is something wrong with my DATARECEIVED method. Any ideas anyone ? Thanks in advance for your time

Kind Regards
Cliff Depreytere
 
I doubt this RS equipment is just sending out data all the time, usually once connected you interface it with its predefined command language. (did you see the third page in that article?)
 
@JOHNH,

yes I've read page 3 of that article , I really can't see what i'm doing wrong at this moment.
My balance is connected to my computer and I need the weighing information on my screen. I don't get any errors at this moment.
I don't know if I can do something with the things from page 3 of that article?
Can you please give me any explanation on what I'm doing wrong here ?

Kind Regard
Cliff Depreytere
 
Re-read the sentence in my last post. I didn't say you were doing anything wrong, don't looks like for now. Once connected (as you are doing now), it is most common to send commands to the equipment throught serial port connection, when you send commands the equipment replies, hopefully with the data you want. The most common command set is the AT/Hayes set (as used in the article example page 3), but I'm not sure if it's only used in modems/phones. If the manufacturer of your equipment haven't supplied an open command set, it could be that they don't want anybody messing with it except through their own approved software. Check the documentation for the equipment, it should be stated quite clearly both how it works and how (or if) it can be controlled.
 
@ JOHNH. ( or maybe someone else )
Pffff ,
I'm losing my motivation to find the solution for this , if there is one .... i've added a method like there is on page 3 with the commands I found in the manual of my serial device ( a Sartorius Balance ) but still I can't see my mistake.
I will add my code in an file on my webspace ( url at the end of this post). If someone can give me a hint of what's going wrong just tell me :)

Kind Regards
Depreytere Cliff

PS
http://users.pandora.be/plex/serieel.rar
 
Last edited:
Are you sure your settings are correct? The few references I find on web says Saritorius defaults to baud rate 1200 and parity odd (and DataBits 7, StopBits.One). These settings have to be correct.

Some place stated default prompt for weight on Saritorius balances was to send Escape+P, which in code is:
VB.NET:
Dim command As String = Chr(27) & "P"
You can also test out the connection and commands in Windows HyperTerminal program to verify connection and functionality before doing it in your own code.
 
Back
Top