TODOPIC
Lenguajes de programación para PC => Visual Basic => Mensaje iniciado por: IAO en 27 de Septiembre de 2007, 14:52:05
-
Holaaaaaa:
Algúno de los cerebros en VB, podría decirme por qué el programa no me agrega nada en el txt?.
Me aparece como si se grabó algo, pero cuando abro el txt no hay nada.
Gracias anticipadas por responder.
Option Explicit
'''Boton Closecomm1
Private Sub Closecomm1_Click()
'''Close the port if it is open
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
'''Close le userform at the end of the reception
'Form1.Hide
End Sub
'''Boton Opencomm1
Private Sub Opencomm1_Click()
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
Call Receive
End Sub
Private Sub Form_Load()
'''Chose correct settings for your device
Dim Settings As String
Settings = "9600,n,8,1"
MSComm1.CommPort = 1
MSComm1.Settings = Settings
With MSComm1
'''Receive chr by chr
MSComm1.InputLen = 1
MSComm1.RThreshold = 0
MSComm1.SThreshold = 0
MSComm1.InBufferSize = 1024 '''10kb
MSComm1.InputMode = comInputModeText
MSComm1.Handshaking = 0 '''None
End With
End Sub
Private Sub Receive()
Dim FileName As String
Dim RxTxt As Variant
Dim inbuff As Variant
FileName = App.Path & "\RS232.txt"
Open FileName For Output As #1
Do
DoEvents
RxTxt = MSComm1.Input
'''Start capture if you get some input you may also wait for some other specific character
Print #1, inbuff
Loop Until RxTxt > vbNullString
inbuff = vbNullString
Do
DoEvents
RxTxt = MSComm1.Input
'''Data is supposed to be separated by Chr(10) chose another separationmark if neccesary
If RxTxt = Chr(10) And Len(inbuff) > 0 Then
If inbuff = Left(inbuff, 1) = Chr(10) Then inbuff = Right(inbuff, Len(inbuff) - 1)
Print #1, inbuff
inbuff = vbNullString
Else
If Len(RxTxt) > 0 Then
inbuff = inbuff + RxTxt
End If
End If
'''Data is supposed to end with Chr(3) chose another endmark if neccesary.
Loop Until RxTxt = Chr(3)
Close #1
End Sub
Bye('_').
-
Holaaaaaaa:
Asi lo pude hacer funcionar, más o menos bastante aceptable.
Option Explicit
'''Boton Closecomm1
Private Sub Closecomm1_Click()
'''Close the port if it is open
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
End Sub
Private Sub Form_Load()
Dim FileName As String
FileName = App.Path & "\RS232.txt"
Open FileName For Output As #1
'''Chose correct settings for your device
Dim CommPort As String, Settings As String
MSComm1.CommPort = 1 'puerto com1
MSComm1.Settings = "9600,n,8,1" 'configuracion del puerto serie
MSComm1.PortOpen = True 'se abre el puerto para que funcione
MSComm1.RThreshold = 1 'cada byte recibido, dispara el evento OnComm
End Sub
Private Sub MSComm1_OnComm()
Dim Inbuff As String
If MSComm1.CommEvent = comEvReceive Then 'si el puerto comienza a recibir .....
Inbuff = MSComm1.Input 'en esta variable se almacena lo que entra por el puerto serie
Print #1, Inbuff
End If
End Sub
Private Sub Form_Terminate() '''Cuando se finaliza el Formulario
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If
Close #1
End Sub
Allí se los dejo, para que sufran menos al momento de querer hacer algo parecido. :)
Bye('_').