Ciao
Volevo migrare un programma che avevo realizzato in VB6 per acquisire dati dalla porta RS232 in VB.NET ma non riesco a capire perchè non funzioni.
Codice VB6:
Option Explicit
Private Sub Form_Load()
MSComm1.CommPort = 1 ' Selezioniamo la COM1
MSComm1.Settings = "4800,N,7,1" ' Le impostazioni della seriale
MSComm1.RThreshold = 1 ' voglio essere informato della ricezione di ogni singolo carattere
MSComm1.PortOpen = True ' Apriamo la porta.
End Sub
Private Sub MSComm1_OnComm()
Dim Rx$
Rx$ = MSComm1.Input ' Leggo il contenuto del buffer di ricezione (e svuoto .Input)
If Len(Rx$) Then ' Se ho ricevuto qualcosa lo scrivo nella TextBox
Rx$ = CStr(CLng(Rx$))
quotax.Text = (Rx$ / 100)
End If
End Sub
Codice VB.net:
Imports System.IO.Ports
Public Class frmdro
Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnect.Click
If SerialPort1.IsOpen Then
SerialPort1.Close()
End If
Try
With SerialPort1
.PortName = "COM1"
.BaudRate = 4800
.DataBits = 7
.Parity = IO.Ports.Parity.None
.StopBits = IO.Ports.StopBits.One
.ReceivedBytesThreshold = 1
End With
SerialPort1.Open()
cmdDisconnect.Enabled = True
cmdConnect.Enabled = False
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub cmdDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDisconnect.Click
Try
SerialPort1.Close()
cmdDisconnect.Enabled = False
cmdConnect.Enabled = True
Catch ex As Exception
End Try
End Sub
Public Delegate Sub myDelegate()
Public Sub updateTextBox()
With txtDataRecieved
.Text = SerialPort1.ReadExisting
End With
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
txtDataRecieved.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {}) ', New Object()
End Sub
Private Sub txtDataRecieved_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDataRecieved.TextChanged
Dim data As String
data = txtDataRecieved.Text
quotax.Text = data
End Sub
End Class
Non riesco a capire perchè in VB6 funziona alla perfezione e in VB.net sembra non ricevere i dati.
Ho provato di tutto ma niente da fare.
Grazie