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.
Thankyou to anyone who can help me!
Ben.
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.
VB.NET:
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.