Visual Basic.NET - Enviar y Recibir caracteres de control VB 2010

 
Vista:

Enviar y Recibir caracteres de control VB 2010

Publicado por Gianfranco (1 intervención) el 25/02/2015 22:04:41
Buenas tardes a todos tengo el siguiente problema, estoy intentando comunicar una computadora a una maquina BCPLUS3000

El problema surge a la hora de leer y enviar los caracteres de control, no se en que le estoy errando pero en ningùn momento recibo un EOT, ETX,EOF, etc. Con este problema de no leer correctamente los caracteres no puedo responder o realizar las acciones necesarias.

Dejo parte del codigo, no todo ya que no es necesario lo que falla es la lectura el resto funciona.
Imports System
Imports System.IO

Public Class Form1
Dim WithEvents serialport As New IO.Ports.SerialPort
Dim lacuenta As Integer = 0
Public Const STX = &H2
Public Const ETX = &H3
Public Const EOT = &H4
Public Const ENQ = &H5
Public Const ACK = &H6
Public Const EOFF = &H1A
Public laruta As String = ""
Public ruta As String = ""
Public stw As System.IO.StreamWriter 'esto es para escribir en un archivo los resultados'
Public Delegate Sub mydelegate()

Private Sub btnconnect_Click(sender As System.Object, e As System.EventArgs) Handles btnconnect.Click
If serialport.IsOpen Then
serialport.Close()
End If
Try

With serialport
.PortName = CbbCOMports.SelectedValue.ToString
.BaudRate = 9600
serialport.Encoding =System.Text.Encoding.Default
If OptEven.Checked = True Then
.Parity = Ports.Parity.Even
Else
If OptOdd.Checked = True Then
.Parity = Ports.Parity.Odd
Else
.Parity = Ports.Parity.None
End If
End If

If Opt7.Checked = True Then
.DataBits = 7
Else
.DataBits = 8
End If


If Opt1.Checked = True Then
.StopBits = 1
Else
.StopBits = 2
End If

End With
serialport.Open()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub

Private Sub serialport_DataReceived(sender As Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serialport.DataReceived
txtDataReceived.Invoke(New _
mydelegate(AddressOf updateTextBox), _
New Object() {})
End Sub

Public Sub updateTextBox()
Try


Dim cadena As String = ""
Dim contenido As String = serialport.ReadChar
If Val(contenido) = ENQ Or Val(contenido) = STX Then
serialport.Write(ACK)

End If



If Val(contenido) <> STX And Val(contenido) <> EOT And Val(contenido) <> ENQ And Val(contenido) <> ACK And Val(contenido) <> ETX And Val(contenido) <> EOFF Then
cadena = cadena & Chr(contenido)
stw.Write(cadena)
Else


If Val(contenido) = EOT Or Val(contenido) = ETX Or Val(contenido) = EOFF Then
cadena = cadena & vbCrLf
txtDataReceived.Text = cadena
stw.Write(cadena)
stw.Write(vbCrLf)
End If
End If
With txtDataReceived
.Font = New Font("Arial", 10.0!, FontStyle.Bold)
.SelectionColor = Color.Blue
.AppendText(cadena)
If contenido <> "" Then

St.Items.Clear()
St.Items.Add("Cantidad de renglones recibidos:" & " " & lacuenta)
End If

.ScrollToCaret()
End With
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub

End Class


Espero puedan darme una mano porque llevo un mes con esto sin exagerar y no logro encontrar el error, no se que me estoy salteando o en que le estoy errando, muchas gracias.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder