![]() |
Click here to advertise with us
|
|
|||||||
| Database General Discussion General discussion on database related topics |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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
Ben. |
|
||||
|
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) |
|
|||
|
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. |
|
||||
|
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:
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
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) |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|