Salve
Avevo lo stesso problema ed ho risolto cosi:
Private Comando As String
Private StatoComm As Boolean
Private Dato As String
Private Sub Form_Load()
On Error Resume Next
'Imposta il titolo dell'applicazione
App.Title = "PCFluke45"
' la posizioe e il titolo del form
Me.Left = (Screen.Width - Me.Width) / 2
Me.Top = (Screen.Height - Me.Height) / 2
Me.Caption = App.Title
Llabel2.Caption = "-8.8.8.8.8"
Me.Show
End Sub
Private Sub Command1_Click()
If StatoComm = False Then
Command1.Caption = "Sconnetti"
StatoComm = False
Call ApriComm(StatoComm)
Else
Command1.Caption = "Connetti"
StatoComm = True
Call ApriComm(StatoComm)
Timer1.Enabled = StatoComm
Label1.Caption = "connesso = " & StatoComm
Label2.Caption = "" 'pulisce il display
Exit Sub
End If
Call InviaComando
Label1.Caption = "connesso = " & StatoComm
Timer1.Interval = 400 'Intervallo tra due letture consecutive
Timer1.Enabled = StatoComm
End Sub
Private Sub Timer1_Timer()
FrmFluke45.MSComm1.Output = "val?" + vbCrLf
Start = Timer
Ripeti:
Fine = Timer
If Fine - Start < 0.3 Then GoTo Ripeti 'ritardo tra invio comando e lettura dal buffer
InBuffer = MSComm1.Input 'Svuota il buffer
ElaboraDato (InBuffer)
End Sub
'+++++++++++++++++++++++++++++++++++++++++++++++
Private Function Leggi(InBuffer)
On Error Resume Next
' Comunica al controllo di leggere l'intero buffer
' quando viene utilizzata la proprietà Input.
' Apre la porta.
Comando = "val?; format1"
' Invia il comando di connessione e reset.
MSComm1.Output = Comando + vbCrLf
End Function
'+++++++++++++++++++++++++++++++++++++++++++++
Private Sub ElaboraDato(Lettura)
For i = 1 To Len(Lettura)
car = Asc(Mid(Lettura, i, 1))
If car > 42 And car < 60 Or car = 69 Then
Instring = Instring + Chr(car)
End If
Next i
Label2.Caption = Instring 'Scrive il dato elaborato
Lettura = ""
End Sub
'+++++++++++++++++++++++++++++++++++++++++++++++++
Private Sub ApriComm(StatoComm)
If StatoComm = False Then
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
With MSComm1
.InputLen = 0
.RThreshold = 1
.SThreshold = 0
.InBufferSize = 1024 '1kb
.InputMode = comInputModeText
.Handshaking = comNone 'None
End With
MSComm1.PortOpen = True
StatoComm = True
If MSComm1.DSRHolding = False Then
Msg = "Apparecchio probabilmente spento." & vbCrLf & _
"Controllare che sia acceso e in linea."
Invito = MsgBox(Msg, 1, Titolo, Help, Ctxt)
MSComm1.PortOpen = False
Esito = MSComm1.PortOpen
Exit Sub
End If
Else
MSComm1.PortOpen = False
StatoComm = False
End If
End Sub
Private Sub InviaComando()
Dim Comando As String
' Invia il comando di connessione e reset.
Comando = "rems; *rst"
MSComm1.Output = Comando + vbCrLf
Buffer = MSComm1.Input 'svuota il buffer
Comando = "ohms; format 1" + vbCrLf
' Predispone il comando che seleziona la misura di resistenza sul display 1.
MSComm1.Output = Comando + vbCrLf 'invia il comando
Buffer = MSComm1.Input 'svuota il buffer
End Sub
Spero sia utile.
|