Untitled
vbscript
2 months ago
6.4 kB
0
Indexable
Never
Imports System.IO.Ports Public Class Form1 'Utilizaremos un string como buffer de recepcion Dim Recibidos As String Public Sub New() InitializeComponent() ' Ejecutar la funcion Recepcion por disparo del Evento 'DataReived' AddHandler SerialPort1.DataReceived, AddressOf Recepcion End Sub ' Al recibir los datos Private Sub Recepcion(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) ' Acumular los carácteres recibidos a nuestro 'buffer' (string) Recibidos += SerialPort1.ReadExisting() ' Invocar o llamar al proceso de tramas Me.Invoke(New EventHandler(AddressOf Actualizar)) End Sub ' Procesar los datos recibidos en el buffer y extraer tramas completas Private Sub Actualizar(ByVal s As Object, ByVal e As EventArgs) ' Asignar el valor de la trama al textBox Dim MEM As String Dim MEMA As String MEM = "" MEMA = 0 MEMA = Mid(Recibidos, Len(Recibidos), 1) If MEMA = "*" Then MEMA = Mid(Recibidos, 1, 1) If MEMA = "@" Then TextBox1.Text = Recibidos Recibidos = "" Call informacion() Else Recibidos = "" End If Else MEM = MEM + Recibidos Recibidos = MEM End If End Sub Private Sub SalirToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SalirToolStripMenuItem.Click End End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.CenterToScreen() On Error GoTo CONTROLERROR 'ABRIR EL PUERTO If SerialPort1.IsOpen = True Then SerialPort1.Close() End If 'ASIGNACION DE VARIABLES Dim puerto As String Dim baudrate As String puerto = " " baudrate = " " 'CREACION DE ARCHIVO POR PRIMERA VEZ FileOpen(1, "C:\interface.sys", OpenMode.Append) FileClose(1) 'ABRO EL ARCHIVO PARA LEER FileOpen(1, "C:\interface.sys", OpenMode.Input) 100: If EOF(1) Then FileClose(1) Else Input(1, puerto) Input(1, baudrate) GoTo 100 End If 'MANDO INFORMACION DE EL ARCHIVO Label1.Text = "Puerto: Comm " + puerto Label2.Text = "Baud Rate: " + baudrate SerialPort1.PortName = "COM" + puerto SerialPort1.BaudRate = Val(baudrate) SerialPort1.Open() SerialPort1.Write("0") GoTo 30 CONTROLERROR: If SerialPort1.IsOpen = True Then SerialPort1.Close() End If 30: REM TextBox2.Focus() End Sub Private Sub ComunicaciónToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ComunicaciónToolStripMenuItem.Click On Error GoTo CONTROLERROR Dim puerto As String Dim baudrate As String If SerialPort1.IsOpen = True Then SerialPort1.Close() End If puerto = Val(InputBox("Número del puerto serial", "Serial", "3")) baudrate = Val(InputBox("Numero de Baudrate", "Baudrate", "9600")) FileOpen(1, "C:\PHANTOM.TXT", OpenMode.Append) WriteLine(1, puerto, baudrate) FileClose(1) Kill("C:\interface.sys") My.Computer.FileSystem.RenameFile("C:\PHANTOM.TXT", "interface.sys") SerialPort1.PortName = "COM" + puerto SerialPort1.BaudRate = Val(baudrate) SerialPort1.Open() Label1.Text = "Puerto: Comm " + Str(puerto) Label2.Text = "Baudrate: " + Str(baudrate) GoTo 30 CONTROLERROR: If SerialPort1.IsOpen = True Then SerialPort1.Close() End If 30: REM End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click SerialPort1.Write(TextBox2.Text) End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click SerialPort1.Write("1") ' PictureBox1.BackColor = Color.Red SerialPort1.Write("0") End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click SerialPort1.Write("2") ' PictureBox2.BackColor = Color.Red SerialPort1.Write("0") End Sub Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click SerialPort1.Write("3") ' PictureBox3.BackColor = Color.Red SerialPort1.Write("0") End Sub Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click SerialPort1.Write("4") 'PictureBox4.BackColor = Color.Red SerialPort1.Write("0") End Sub Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click SerialPort1.Write("5") ' PictureBox1.BackColor = Color.DarkRed SerialPort1.Write("0") End Sub Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click SerialPort1.Write("6") ' PictureBox2.BackColor = Color.DarkRed SerialPort1.Write("0") End Sub Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click SerialPort1.Write("7") ' PictureBox3.BackColor = Color.DarkRed SerialPort1.Write("0") End Sub Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click SerialPort1.Write("8") ' PictureBox4.BackColor = Color.DarkRed SerialPort1.Write("0") End Sub Sub informacion() ListBox1.Items.Clear() Dim DATO As String Dim LONGITUD As Integer Dim MEMORIA As String MEMORIA = "" LONGITUD = (Len(TextBox1.Text)) - 18 DATO = Mid(TextBox1.Text, 18, LONGITUD) For I = 1 To Len(DATO) If Mid(DATO, I, 1) = "," Then ListBox1.Items.Add(MEMORIA) MEMORIA = "" Else MEMORIA = MEMORIA + Mid(DATO, I, 1) End If Next ListBox1.Items.Add(MEMORIA) MEMORIA = "" If Mid(TextBox1.Text, 2, 1) = "0" Then PictureBox1.BackColor = Color.DarkRed Else PictureBox1.BackColor = Color.Red End If If Mid(TextBox1.Text, 4, 1) = "0" Then PictureBox2.BackColor = Color.DarkRed Else PictureBox2.BackColor = Color.Red End If If Mid(TextBox1.Text, 6, 1) = "0" Then PictureBox3.BackColor = Color.DarkRed Else PictureBox3.BackColor = Color.Red End If If Mid(TextBox1.Text, 8, 1) = "0" Then PictureBox4.BackColor = Color.DarkRed Else PictureBox4.BackColor = Color.Red End If If Mid(TextBox1.Text, 10, 1) = "0" Then PictureBox5.BackColor = Color.DarkRed Label11.Text = "0" Else PictureBox5.BackColor = Color.Red Label11.Text = "1" End If If Mid(TextBox1.Text, 12, 1) = "0" Then PictureBox6.BackColor = Color.DarkRed Label12.Text = "0" Else PictureBox6.BackColor = Color.Red Label12.Text = "1" End If If Mid(TextBox1.Text, 14, 1) = "0" Then PictureBox7.BackColor = Color.DarkRed Label13.Text = "0" Else PictureBox7.BackColor = Color.Red Label13.Text = "1" End If If Mid(TextBox1.Text, 16, 1) = "0" Then PictureBox8.BackColor = Color.DarkRed Label14.Text = "0" Else PictureBox8.BackColor = Color.Red Label14.Text = "1" End If Label19.Text = ListBox1.Items(0) Label20.Text = ListBox1.Items(1) Label21.Text = ListBox1.Items(2) Label22.Text = ListBox1.Items(3) End Sub End Class