Cómo configurar el control MSCOMM de programación VB para recibir datos desde el puerto serie
MSComm tiene muchas formas de recibir datos, las simples son las siguientes:
(Copia el código y guárdalo como Form1.frm, abre VB para agregar el control MSComm32)
VERSIÓN 5.00
Objeto = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "mscomm32.ocx"
Comienza VB. Formulario Form1
BorderStyle = 3 'Diálogo fijo
Caption = "Form1"
ClientHeight = 4170
ClientLeft = 45
ClientTop = 330 p>
ClientWidth = 10005
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 4170
ScaleWidth = 10005
ShowInTaskbar = 0 'False
StartUpPosition = 3 'Ventana predeterminada p>
Comenzar VB CommandButton Command2
Título = "Borrar"
Alto = 375
Izquierda = 1680
Estilo. = 1 'Gráfico
TabIndex = 3
Superior = 3720
Ancho = 1335
Fin
Inicio MSCommLib.MSComm MSComm1
Izquierda = 720
Arriba = 2040
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
DTREnable = -1 'True
Fin
Iniciar VB.CommandButton Comando1
Título = "Abrir"
Alto = 375
Izquierda = 120
Estilo = 1'
Gráfico
TabIndex = 0
Superior = 3720
Ancho = 1335
Fin
Inicio VB.Timer Temporizador1
Habilitado = 0 'Falso
Intervalo = 1
Izquierda = 240
Superior = 2160
Fin
Comenzar VB.TextBox Texto1
Alto = 3495
Izquierda = 120
MultiLine = -1 'Verdadero
Barras de desplazamiento = 3 'Ambos
TabIndex = 2
Texto = "Form1.frx":0000
Arriba = 120
Ancho = 9735
Fin
Inicio VB.Label Label1
BackStyle = 0 'Transparent
Caption = "Label1" p>
Alto = 255
Izquierda = 3840
TabIndex = 1
Arriba = 3840
Ancho = 2535 p>
Fin
Fin
Atributo VB_Name = "Form1"
Atributo VB_GlobalNameSpace = False
Atributo VB_Creatable = False
Atributo VB_PredeclaredId = Verdadero
Atributo VB_Exposed = Falso
Subcomando privado1_Click()
Temporizador1.Enabled = Verdadero
Command1.BackColor = vbGreen
End Sub
Sub privado Command2_Click()
Text1.Text = ""
End Sub
Private Sub Form_Load()
'Inicialización del puerto de comunicación:
Con MSComm1
.Settings = "9600,n,8
,2"
.CommPort = 3
.InputMode = comInputModeBinary
.InBufferCount = 0
.OutBufferCount = 0
.RTreshold = 0
.STreshold = 0
.PortOpen = True
Terminar con
Texto1.Texto = ""
End Sub
Private Sub Timer1_Timer()
'Usa el método round robin para recopilar datos
Dim inx() As Byte
Dim strTemp como cadena
Dim strTemp1 como cadena
DimceivedLen como entero
Timer1.Enabled = False 'Apagar el temporizador
Si MSComm1.InBufferCount > 0 Entonces
RecibidoLen = MSComm1.InBufferCount
inx = MSComm1.Input
Para i = 0 A UBound (inx)
strTemp1 = Hex(inx(i))
Si Len(strTemp1) > 1 Entonces
strTemp = strTemp & strTemp1 & " "
De lo contrario
strTemp = strTemp & "0" & strTemp1 & " "
Finalizar si
Siguiente i
Text1.Text = Text1.Text & Format(Segundo(Ahora), "00") & Right(Format(Str(Timer), "0.00"), 3) & " " & strTemp & vbCrLf
Text1.SelStart = Len(Text1.Text)
Finalizar si
Timer1.Enabled = True 'Abre el temporizador
Label1.Caption = Now()
End Sub
Sub privado Text1_Change()
Si Len(Text1.Text) > 10000 Entonces Text1.Text = ""
Fin del subtítulo