Salve a tutti
ho un problema che mi affligge e mi sfianca da parecchio tempo.
Premetto che non sono un buon programmatore ma per necessità ho dovuto realizzare un software per la lettura di dati tramite seriale virtualizzata.
La macchina in questione è una bilancia industriale, è passiva quindi bisogna interrogarla ogni volta per avere il dato/peso.
l'ho strutturato come segue:
ho impostato un timer che ogni 2 secondi interroga la pesa per recuperare il peso, questa è la SUB
codice:
Private Sub TimerPesa1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerPesa1.Tick
Dim Request_Pesata1 As String
If Port1.IsOpen = False Then Port1.Open()
Request_Pesata1 = "?#0119" & vbCrLf
Port1.Write(Request_Pesata1)
End Sub
Poi ho creato un altra sub che riceve il dato e lo elabora:
codice:
Private Sub port_DataReceived1(ByVal sender As Object, ByVal e As _
System.IO.Ports.SerialDataReceivedEventArgs) Handles Port1.DataReceived
Dim SerialData1 As String
Control.CheckForIllegalCrossThreadCalls = False
If Port1.BytesToRead > 0 Then
SerialData1 = ""
Do
SerialData1 = SerialData1 & Chr(Port1.ReadByte)
If Port1.BytesToRead = 0 Then
Exit Do
End If
Loop
Port1.Close()
....
....
...
In questa parte che non posto continuo, elaborando il dato e salvandolo in un file di log e nel database.
Ora sembrerebbe funzionare tutto correttamente ma ad un certo punto non meglio specificato mi si presenta un errore
System.IO.IOException: Periodo di timeout del semaforo scaduto.
at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)
at System.IO.Ports.SerialPort.Open()
at Main.TimerPesa2_Tick(Object sender, EventArgs e)
at System.Windows.Forms.Timer.OnTick(EventArgs e)
at System.Windows.Forms.Timer.TimerNativeWindow.WndPr oc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Questo errore si verifica da quando ho inserito una seconda pesa su una seconda seriale sempre virtualizzata.
come faccio a fare catch dell'errore e gestiro facendo andare avanti il tutto?
Grazie