I'm pretty new at programming, and .net is the what I use to do the easy stuff I need. I created a program to read from a weight indicator that comes through the serial port and it was working just fine like this for an EL05 device:
Private Sub sppuerto_DataReceived( sender As Object, e As IO.Ports.SerialDataReceivedEventArgs ) Handles sppuerto.DataReceived
Dim buffer As String
'------- WORKS FOR EL05 -----------------
buffer = sppuerto.ReadLine
txtrecibe.Text = buffer.Substring(4, 5)
End Sub
But now I'm connecting a new device from another manufacturer and I get an exception with ReadLine
:
An unhandled exception of type 'System.IO.IOException' occurred in System.dll
Additional information: La operación de E/S se anuló por una salida de subproceso o por una solicitud de aplicación"
The English version of the exception message is
The I/O operation has been aborted because of either a thread exit or an application request
I got it to work with ReadExisting
but it keeps reading and never stops like it did with ReadLine
Private Sub sppuerto_DataReceived( sender As Object, e As IO.Ports.SerialDataReceivedEventArgs ) Handles sppuerto.DataReceived
Dim buffer As String
'------------- WORKS WITH NEW INDICATOR BUT UNREADABLE----------------
buffer = sppuerto.ReadExisting
txtrecibe.Text = buffer
End Sub
I looked around but all the posts refer to C# implementations and I really don't want to get into that since is completely different for what I read. Also in Java.
Has anyone tried this in VB.NET? I can paste more of the code if needed.
Edit: Adding the complete code by request (not really that long)
Imports System.IO.Ports
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CheckForIllegalCrossThreadCalls = False
buscarpuerto()
End Sub
Private Sub buscarpuerto()
Try
cmbPort.Items.Clear()
For Each puerto As String In My.Computer.Ports.SerialPortNames
cmbPort.Items.Add(puerto)
Next
If cmbPort.Items.Count > 0 Then
cmbPort.SelectedIndex = 0
Else
MsgBox(" NO HAY PUERTO DISPONIBLES ")
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
Private Sub btnconectar_Click(sender As Object, e As EventArgs) Handles btnconectar.Click
Try
With sppuerto
.BaudRate = Int32.Parse(CboBaudRate.Text)
.DataBits = 8
.Parity = IO.Ports.Parity.None
.StopBits = 1
.PortName = cmbPort.Text
.Open()
If .IsOpen Then
lblestado.Text = "CONECTADO"
Else
MsgBox("NO SE PUDO CONECTAR", MsgBoxStyle.Critical)
End If
End With
Catch ex As Exception
End Try
End Sub
Private Sub btndesconectar_Click(sender As Object, e As EventArgs) Handles btndesconectar.Click
sppuerto.Close()
lblestado.Text = "DESCONECTADO"
End Sub
Private Sub sppuerto_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs) Handles sppuerto.DataReceived
Dim buffer As String
Dim x As String
buffer = ""
'------- WORKS FOR EL05 -----------------
'buffer = sppuerto.ReadLine
'txtrecibe.Text = buffer.Substring(4, 5)
'------------- WORKS WITH NEW INDICATOR BUT UNREADABLE----------------
x = sppuerto.ReadExisting
buffer = buffer + x
txtrecibe.Text = buffer
End Sub
Private Sub btnenviar_Click(sender As Object, e As EventArgs) Handles btnenviar.Click
If sppuerto.IsOpen Then
sppuerto.WriteLine(txtenvia.Text)
Else
MsgBox("NO ESTAS CONECTADO", MsgBoxStyle.Exclamation)
End If
End Sub
Private Sub btnsalida_Click(sender As Object, e As EventArgs) Handles btnsalida.Click
If lblestado.Text = ("CONECTADO") Then
MsgBox("DESCONECTARSE DEL SISTEMA", MsgBoxStyle.Exclamation, "AYUDA")
Else
Close()
End If
End Sub
End Class