COMO LEER DATOS DE UNA BASCULA CON VB.NET
Publicado por
jr (1 intervención) el 09/02/2023 16:42:30
Los valores de bit de stop, bauds, etc etc ponlos en una tablita de base de datos.
Y crea una pantalla de Windows forms donde pondras todos los objetos referidos en el codigo nombralos igual.
la cadena que se transmite: tambien debes saber donde inicia y donde termina cada campo o valor y cuanto mide la cadena en total.
En un boton poner el codigo :
If TomarParametrosYAbrirPuerto(ComboBoxPlataformaTara.SelectedValue) Then
Call ActivarLectura()
End If
Recuerda agregar un timer en la pantalla donde quieres tomar el peso
rutinas:
Sub ActivarLectura()
'Esto asegura que no se obtiene algo que haya quedado en el buffer de una transmision anterior
'tanto de envio como de recepcion.
Try
If SerialPort1.IsOpen Then
SerialPort1.DiscardInBuffer() 'Borrar el buffer de entrada
SerialPort1.DiscardOutBuffer() 'Borrar el buffer de salida
End If
MilisegundosTranscurridos = 0
Timer1.Start()
Catch ex As Exception
CerrarPuerto()
TextBoxMsj.Text = "Puerto " & SerialPort1.PortName & " " & Err.Description
End Try
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
MilisegundosTranscurridos = MilisegundosTranscurridos + 1
If SerialPort1.IsOpen = False Then
TextBoxMsj.Text = "Puerto cerrado"
Timer1.Stop()
ComboBoxPlataformaBoleta.Focus()
End If
If MilisegundosTranscurridos > g_tiempo_espera Then
MaskedTextBoxBrutoBoleta.ForeColor = Color.Red
MaskedTextBoxTaraBoleta.ForeColor = Color.Red
MaskedTextBoxNetoBoleta.ForeColor = Color.Red
TextBoxMsj.Text = "Tiempo agotado"
CerrarPuerto() 'Requiere ser inicializado
Timer1.Stop()
ComboBoxPlataformaBoleta.Focus()
End If
If l_leido_estado = g_valor_estable And Val(MaskedTextBoxBrutoBoleta.Text) > 0 Then
MaskedTextBoxBrutoBoleta.ForeColor = Color.Lime
MaskedTextBoxTaraBoleta.ForeColor = Color.Lime
MaskedTextBoxNetoBoleta.ForeColor = Color.Lime
TextBoxMsj.Text = "Peso estable"
Timer1.Stop()
'Trata de Obtener la fecha tara y la tara en el historial de taras
'nuevojr
Dim obtiene As fechatara = ObtieneFechaTaraRegistrada(ComboBoxMedioDeTransporteBoleta.SelectedValue, ComboBoxTipoTransaccionDisplayBoleta.SelectedValue, ComboBoxEconomicoBoleta.Text, ComboBoxClaseProductoBoleta.SelectedValue, "01/01/1900 00:00:00")
TextBoxFechaTaraBoleta.Text = obtiene.fecha
MaskedTextBoxTaraBoleta.Text = obtiene.tara
'Calcular Peso neto
If Val(MaskedTextBoxTaraBoleta.Text) = 0 Then
If ComboBoxMedioDeTransporteBoletaOk.SelectedValue = "F" Then
NuevaBoleta()
Else
MsgBox("Advertencia: La tara no está disponible")
End If
Else
Dim l_bruto As Decimal = Val(MaskedTextBoxBrutoBoleta.Text)
'Si es Ferrocarril y es: Tolva
'el peso bruto debe dividirse entre 2 siempre y cuando haya 2 tolvas en la bascula}
If ComboBoxMedioDeTransporteBoleta.SelectedValue = g_ffcc Then
If Mid(ComboBoxEconomicoBoleta.SelectedValue, 1, 1) = "V" Then
If Trim(TextBoxHay2Tolvas.Text) = "S" Then
TextBoxMsj.Text = "Sobre la plataforma Usted dice que hay 2 tolvas el peso bruto se dividira entre 2"
l_bruto = l_bruto / 2
End If
End If
End If
Dim l_neto As Decimal = l_bruto - Val(MaskedTextBoxTaraBoleta.Text)
MaskedTextBoxNetoBoleta.Text = Format(l_neto, "###0.000")
End If
'g_economico_estricto se usa para validar activacion de rafid
'Valida numeros economicos de tractor y caja1 cuando se trata de un camion
If ComboBoxMedioDeTransporteBoleta.SelectedValue = g_camion Then
If Trim(l_leido_economico_tractor) <> Trim(ComboBoxEconomicoBoleta.Text) And g_economico_estricto = "S" Then
If Not (aEcE()) Then
PoneEnCerosLosPesos(g_PalabraBoleta)
MsgBox("Economicos RFID: " & Trim(l_leido_economico_tractor) & " Seleccionado: " & Trim(ComboBoxEconomicoBoleta.Text) & " no coinciden")
ButtonObtenerPesoBoleta.Focus()
Return
End If
End If
If Trim(l_leido_economico_caja1) <> Trim(ComboBoxEconomicoBoleta.Text) And g_economico_estricto = "S" Then
If Not (aEcE()) Then
PoneEnCerosLosPesos(g_PalabraBoleta)
MsgBox("Economicos RFID: " & Trim(l_leido_economico_caja1) & " Seleccionado: " & Trim(ComboBoxEconomicoBoleta.Text) & " no coinciden")
ButtonObtenerPesoBoleta.Focus()
Return
End If
End If
End If
ButtonRegistrarBoleta.Focus()
End If
data_recibida += SerialPort1.ReadExisting()
If data_recibida.Contains(Chr(13)) Then
LeerCadena()
data_recibida = ""
End If
end sub
Private Sub LeerCadena()
Try
If data_recibida.Length > 0 Then
l_leido_estado = ""
l_leido_modo = ""
l_leido_polaridad = ""
l_leido_peso = 0
l_leido_um = ""
l_leido_economico_tractor = ""
l_leido_economico_caja1 = ""
l_leido_economico_caja2 = ""
'Para Desplegar resultados erroneos ---------------
If IsNumeric(Mid(data_recibida, g_peso_inicio, g_peso_long)) Then
l_leido_peso = Mid(data_recibida, g_peso_inicio, g_peso_long)
TextBoxMsj.Text = Format(l_leido_peso, "###0")
End If
'fin Desplegar -------------o-----------------------
If Mid(data_recibida, g_estado_inicio, g_estado_long) = g_valor_estable Then
'cerrar el puerto ok
CerrarPuerto()
l_leido_estado = Mid(data_recibida, g_estado_inicio, g_estado_long)
l_leido_modo = Mid(data_recibida, g_modo_inicio, g_modo_long)
l_leido_polaridad = Mid(data_recibida, g_polaridad_inicio, g_polaridad_long)
l_leido_peso = Mid(data_recibida, g_peso_inicio, g_peso_long)
l_leido_um = Mid(data_recibida, g_um_inicio, g_um_long)
l_leido_economico_tractor = Mid(data_recibida, g_economico_tractor_inicio, g_economico_tractor_long)
l_leido_economico_caja1 = Mid(data_recibida, g_economico_caja1_inicio, g_economico_caja1_long)
l_leido_economico_caja2 = Mid(data_recibida, g_economico_caja2_inicio, g_economico_caja2_long)
l_leido_peso = l_leido_peso / 1000
MaskedTextBoxBrutoBoleta.Text = Format(l_leido_peso, "###0.000")
If ComboBoxMedioDeTransporteBoleta.SelectedValue = g_camion Then
TextBoxEconomicoTractorBoleta.Text = l_leido_economico_tractor
TextBoxEconomicoCaja1Boleta.Text = l_leido_economico_caja1
Else
TextBoxEconomicoTractorBoleta.Text = ""
TextBoxEconomicoCaja1Boleta.Text = ""
End If
End If
End If
Catch ex As Exception
TextBoxMsj.Text = "Puerto " & SerialPort1.PortName & " Al estar leyecndo cadena " & Err.Description
End Try
End Sub
Private Sub CerrarPuerto()
Try
If SerialPort1.IsOpen Then
SerialPort1.Close()
End If
Catch ex2 As System.Exception
TextBoxMsj.Text = "Puerto " & SerialPort1.PortName & " No Pudo Cerrarse"
End Try
End Sub
Public Function TomarParametrosYAbrirPuerto(ByVal puerto As String) As Boolean
'no aplican estos parametros (se dejan para referecnias posteriores)
'SerialPort1.DtrEnable = True 'Activar de Data Terminal Ready
'SerialPort1.RtsEnable = True 'Activar Request To Send
'SerialPort1.ReadBufferSize = 1024 'Dimensionar tamaño buffer recepcion
'SerialPort1.WriteBufferSize = 1024 'Dimensionar tamaño buffer envio
CerrarPuerto()
SerialPort1.PortName = puerto
SerialPort1.Handshake = Handshake.XOnXOff 'Tipo control para recepcion/envio
SerialPort1.BaudRate = g_baud
SerialPort1.DataBits = g_dato_bit
If g_paridad = "None" Then
SerialPort1.Parity = Parity.None
End If
If g_paridad = "Odd" Then
SerialPort1.Parity = Parity.Odd
End If
If g_paridad = "Even" Then
SerialPort1.Parity = Parity.Even
End If
If g_paridad = "Mark" Then
SerialPort1.Parity = Parity.Mark
End If
If g_paridad = "Space" Then
SerialPort1.Parity = Parity.Space
End If
If g_bit_stop = "None" Then
SerialPort1.StopBits = StopBits.None
End If
If g_bit_stop = "One" Then
SerialPort1.StopBits = StopBits.One
End If
If g_bit_stop = "Two" Then
SerialPort1.StopBits = StopBits.Two
End If
If g_bit_stop = "OnePointFive" Then
SerialPort1.StopBits = StopBits.OnePointFive
End If
Try
SerialPort1.Open()
SerialPort1.DiscardInBuffer() 'Borrar el buffer de entrada
SerialPort1.DiscardOutBuffer() 'Borrar el buffer de salida
Return True
Catch ex As Exception
CerrarPuerto()
TextBoxMsj.Text = "Puerto " & SerialPort1.PortName & " No Pudo Abrirse " & Err.Description
Return False
End Try
End Function