Visual Basic .NET Forums  
Click here to advertise with us

Go Back   Visual Basic .NET Forums > Database > Database General Discussion

Database General Discussion General discussion on database related topics

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-08-2009, 1:34 AM
VB.NET Forum Newbie
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Jul 2009
Age: 17
Posts: 2
Reputation: 0
bjfguitar is on a distinguished programming path ahead
Default Working with RS232 serial port

First of all I want to apologise for only signing up to ask for help, but I've been debugging, experimenting and searching for days for a solution to this problem. Posting is always the last resort.

I'm not very experienced with Visual Basic, although I am proficient in a variety of languages. I got asked to create a program for my fathers product that connects to it to read and write information.

I am using Visual Basic 2008 Express, and to connect I'm using the Serial Port class. I have everything working fine, but the problem is, every time I try to read data from it (with the Read function) the program crashes.

I wrote my own code at first, but through the debugging I decided to just download a pre-made code which I found and modify it to what I need.
Code:
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

    Private Sub DataReceived( _
       ByVal sender As Object, _
       ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
       Handles serialPort.DataReceived
        Dim rcvBuf(1024) As Byte
        serialPort.Read(rcvBuf, 0, rcvBuf.Length)
        'txtDataReceived.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
    End Sub

    Private Sub btnSend_Click( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles btnSend.Click
        Try
            Dim xmtBuf() As Byte = {&H96, &H1, &H50, &H51, &H74}
            serialPort.Write(xmtBuf, 0, xmtBuf.Length)
            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

    Public Delegate Sub myDelegate()
    Public Sub updateTextBox()
        With txtDataReceived
            .Font = New Font("Garamond", 12.0!, FontStyle.Bold)
            .SelectionColor = Color.Red
            .AppendText(serialPort.ReadExisting)
            .ScrollToCaret()
        End With
    End Sub

    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 = 96000
                .Parity = IO.Ports.Parity.None
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.One
                ' .Encoding = System.Text.Encoding.Unicode
            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
End Class
Thankyou to anyone who can help me!
Ben.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-08-2009, 8:09 AM
Robert_Zenz's Avatar
VB.NET Forum Idol
.NET Framework: .NET 2.0
 
Join Date: Jun 2008
Location: Vienna, Austria
Age: 22
Posts: 503
Reputation: 289
Robert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NET
Default

Hello.

What does this device send, and do you have to send a command so that it does it?

Bobby
__________________
Don't give TypeCasting Errors a chance, turn ON Option Strict!
Greatest Obfuscator ever: EazFuscator (Freeware)
Greatest Reflection Tool ever: .NET Reflector (Freeware) with Add-Ins
Greatest Introspection Tool ever: Gendarme (GPL)
Greatest MySQL FrontEnd ever: MySQL-Front (Shareware), HeidiSQL (GPL)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-08-2009, 10:11 AM
VB.NET Forum Newbie
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Jul 2009
Age: 17
Posts: 2
Reputation: 0
bjfguitar is on a distinguished programming path ahead
Default

Hey Robert, thanks for the reply.

I send a hex code:
Dim xmtBuf() As Byte = {&H96, &H1, &H50, &H51, &H74}
serialPort.Write(xmtBuf, 0, xmtBuf.Length)

Which translates out to 96 01 50 51 74

The device I'm writing to reads this function and will respond as a hex as well, which I'm trying to read here:
Dim rcvBuf(1024) As Byte
serialPort.Read(rcvBuf, 0, rcvBuf.Length)

Thanks,
Ben.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-09-2009, 4:25 AM
Robert_Zenz's Avatar
VB.NET Forum Idol
.NET Framework: .NET 2.0
 
Join Date: Jun 2008
Location: Vienna, Austria
Age: 22
Posts: 503
Reputation: 289
Robert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NETRobert_Zenz master of VB.NET
Default

Does this reply always have a fixed length? If yes, you can set ReceivedBytesThreshold-Property to that value, so that the Received event will only fire if all data has arrived.

Quote:
Which translates out to 96 01 50 51 74
We are still talking about Hex-Values and not Decimal-Values, do we?

Try This:
Code:
    Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serialPort.DataReceived
        Dim rcvBuf(Me.serialPort.ReadBufferSize) As Byte
        Dim read As Integer = Int32.MinValue

        read = Me.serialPort.Read(rcvBuf, 0, rcvBuf.Length)

        Dim res As New StringBuilder()

        For i As Integer = 0 To read Step 1
            res.Append(rcvBuf(i).ToString("X2"))
        Next

        Debug.Print(res.ToString())
    End Sub
This should print every received data to the Debug-Window.

Bobby
__________________
Don't give TypeCasting Errors a chance, turn ON Option Strict!
Greatest Obfuscator ever: EazFuscator (Freeware)
Greatest Reflection Tool ever: .NET Reflector (Freeware) with Add-Ins
Greatest Introspection Tool ever: Gendarme (GPL)
Greatest MySQL FrontEnd ever: MySQL-Front (Shareware), HeidiSQL (GPL)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 12:19 PM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2


For advertising opportunities click here.