[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Serial and Parallel communication using VB 2005

Posted on 2006-04-24
9
Medium Priority
?
285 Views
Last Modified: 2012-05-05
Hi Experts Team,

I need help with thse two question:

First: I need to communicate with an analytical balance using VB 2005. I already have a form that could pick an available serial port and send and receive data to it but seems to me that I am sending and receiveing data in a wrong way.

First I need to send a command to the balance (CMD=PI) to get the weight on the balance back to the form.  The issue is that what I get back from the balance are totally wrong numbers. Maybe my form is not sending or receiveing the data in an appropiate way. Could anyone please help me checking the code of my form? I am attaching the code of the form (..and yes, I know the code is not elegant at all. It is actually an adaptation from another application... I am still learning how to do this!)

The second question is related with parallel ports. I have a really important piece of lab equipment that only communicates with computers using the parallel port. Could someone please tell me how can I establish communication via the parallel port using Visual Basic 2005? This sort of the same question but via parallel port

Thank you very very very much

David

Here is the code of the form in question one.

Public Class Form1
    Dim WithEvents serialPort As New IO.Ports.SerialPort
    Private Sub Form1_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
            cbbCOMPorts.Items.Add( _
               My.Computer.Ports.SerialPortNames(i))
        Next
        btnDisconnect.Enabled = False
    End Sub

    '-------------------------------------------
    ' Event handler for the Connect button
    '-------------------------------------------
    Private Sub btnConnect_Click( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles btnConnect.Click

        If serialPort.IsOpen Then
            serialPort.Close()
        End If

        Try
            With serialPort
                .PortName = cbbCOMPorts.Text
                .BaudRate = 2400
                .Parity = IO.Ports.Parity.None
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.One
            End With
            serialPort.Open()

            lblMessage.Text = cbbCOMPorts.Text & " connected."
            btnConnect.Enabled = False
            btnDisconnect.Enabled = True
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    '-------------------------------------------
    ' Event handler for the Disconnect button
    '-------------------------------------------
    Private Sub btnDisconnect_Click( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles btnDisconnect.Click
        Try
            serialPort.Close()
            lblMessage.Text = serialPort.PortName & " disconnected."
            btnConnect.Enabled = True
            btnDisconnect.Enabled = False
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    '-------------------------------------------
    ' Event handler for the Send button
    '-------------------------------------------
    Private Sub btnSend_Click( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles btnSend.Click
        Try
            serialPort.WriteLine(txtDatatoSend.Text & vbCrLf)
            With txtDataReceived
                .SelectionColor = Color.Black
                .AppendText(txtDatatoSend.Text & vbCrLf)
                .ScrollToCaret()
            End With
            txtDataToSend.Text = String.Empty
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    '-------------------------------------------
    ' Event handler for the DataReceived
    '-------------------------------------------
    Private Sub DataReceived( _
       ByVal sender As Object, _
       ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
       Handles serialPort.DataReceived

        txtDataReceived.Invoke(New _
                       myDelegate(AddressOf updateTextBox), _
                       New Object() {})
    End Sub
    '------------------------------------------------------
    ' Delegate and subroutine to update the Textbox control
    '------------------------------------------------------
    Public Delegate Sub myDelegate()
    Public Sub updateTextBox()
        With txtDataReceived
            .Font = New Font("Garamond", 12.0!, FontStyle.Bold)
            .SelectionColor = Color.Red
            .AppendText(serialPort.ReadChar)
            .ScrollToCaret()
        End With
    End Sub



    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDataReceived.TextChanged

    End Sub
End Class
0
Comment
Question by:DavidGalvez
  • 5
  • 3
8 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16527935
>> It is actually an adaptation from another application...
What type of project?

What type of values are you receiving?

Bob
0
 

Author Comment

by:DavidGalvez
ID: 16528219
Hi Bob,

The original code was part of an application to send and receive text between two computers via serial ports. When I send the connection command to the balance (the command is "PI" and i have no idea waht this means but is what must to be send to the balance in order to establish communication) the values coming back from the balance are four digit numbers. These numbers are totally unrelated with the weight on the balance.

Thanks a lot Bob

David

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16528237
David,

Here are some more explicit questions:

1) Was this a VB6 application?
2) What type of balance?  Manufacturer?  Model?
3) What results are you getting back from the send command?

Bob
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:DavidGalvez
ID: 16528368
Hi Bob,

1) No, the original application was not a VB6 app. It was code from scratch using VB 2005.

2) The Balance is an digital analytical balance made by Mettler. The model is AE200.

3) When I send the connection command (PI) I got back a four digit number (8073) that is totally unrelated with the weight display by the balance on it screen. If I send any other command (i.e. IPIP) i got back other four digit number.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16528468
1) Did this ever work?

2) Wow, I haven't heard of a Mettler in a long time.  I looked on their site and the AE200 has been phased out since 1986.  You gotta love manufacturing companies and there inhuman desire to keep costs low.

3) Are you using Option 11 data output?

http://us.mt.com/mt/products/products/AE200_0x000010083f798f544000926e.jsp?v=V0bmVjX3Ryb3BwdXMicm

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16528817
I don't see any reference to the PI command, but I do see one for S, SI, and T.

Bob
0
 

Author Comment

by:DavidGalvez
ID: 16557997
Hi Bob-

My old computer had one of those catastrophic days with blue screens... so I got a new one. You were right the connection command is SI instead of PI. Anyway I am not able to get the right values back from the balance. Do you see anything wrong in the code with the way that the form1 is sending or receiving the data?

David
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1000 total points
ID: 16578370
1) Do you have the communications settings correct?

2) What is the Receive threshold set to?  I see serialPort.ReadChar, so I am assuming that it is set to 1.

3) The SerialPort constructor takes arguments, and makes it easier to set communication settings:

    serialPort= New System.IO.Ports.SerialPort(Me.cbbCOMPorts.Text, 2400, Parity.None, 8, StopBits.One)

4) Do you have HyperTerminal to test the communications?

Bob
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Loops Section Overview
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question