Eso demuestra que querer es poder... felicitaciones dalr123.
Follow along with the video below to see how to install our site as a web app on your home screen.
Nota: This feature currently requires accessing the site using the built-in Safari browser.
private void Conectar_Click(object sender, EventArgs e)
{
// Abrir puerto cuando pulsemos conectar.
if (!serialPort1.IsOpen) // Si el puerto serial está cerrado...
{
try
{
serialPort1.Open(); // Trata de abrirlo.
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString()); // Si hay un error lo muetra en un msgbox.
}
}
// Ejecutar la función Recepción por disparo del evento Datareceived.
serialPort1.DataReceived += new
System.IO.Ports.SerialDataReceivedEventHandler(Recepcion);
}
private void Conectar_Click(object sender, EventArgs e)
{
// Abrir puerto cuando pulsemos conectar.
if (!serialPort1.IsOpen) // Si el puerto serial está cerrado...
{
try
{
serialPort1.Open(); // Trata de abrirlo.
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString()); // Si hay un error lo muetra en un msgbox.
}
}
}
private void button1_Click(object sender, EventArgs e)
{
serialPort1.Open(); // Abrir puerto.
byte[] miBuffer = new byte[1];
miBuffer[0] = 0x62; //ASCII letra "b".
serialPort1.Write(miBuffer, 0, miBuffer.Length);
serialPort1.Close(); // Cerrar puerto.
}
private void button2_Click(object sender, EventArgs e)
{
serialPort1.Open(); // Abrir puerto.
byte[] mBuffer = new byte[1];
mBuffer[0] = 0x74; //ASCII letra "t".
serialPort1.Write(mBuffer, 0, mBuffer.Length);
serialPort1.Close(); // Cerrar puerto.
}
Public Class Form1
'Enumerate Serial Ports on Machine
'Get Serial Port availability
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Just drag & drop combobox from toolbox to form designer
Me.ComboBox1.DrawMode = DrawMode.OwnerDrawVariable 'select my own drag mode
Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList 'aspect when drop down
Me.ComboBox1.DataSource = My.Computer.Ports.SerialPortNames.ToArray 'enumerate serialports
Me.ComboBox1.TabIndex = 0 'select first tabindex
AddHandler ComboBox1.DrawItem, AddressOf cmbo_SerialPorts_Status 'draw my personal cmbo
End Sub
'
' Draw our custom combo
Private Sub cmbo_SerialPorts_Status( _
ByVal sender As Object, _
ByVal CmboItem As System.Windows.Forms.DrawItemEventArgs)
' Draw the background of the item.
CmboItem.DrawBackground()
'
'Default Values if port is available
Dim status As String = "Available"
Dim brush As New SolidBrush(Color.Green)
Dim font As System.Drawing.Font = Me.Font
Dim fontbrush As Brush = Brushes.Black
Dim rectangle As Rectangle = New _
Rectangle(2, CmboItem.Bounds.Top + 2, _
CmboItem.Bounds.Height, _
CmboItem.Bounds.Height - 4)
'
'Check for port availability
Try
' If port open & close with no exception
' draw the item with default font and green rectangle
Dim PortTest As New System.IO.Ports.SerialPort
PortTest = My.Computer.Ports.OpenSerialPort(My.Computer.Ports.SerialPortNames(CmboItem.Index))
PortTest.Close()
Catch ex As Exception
' If port is not available
' draw the item with italic & strikeout font and red rectangle
brush = New SolidBrush(Color.Red)
status = "In Use"
font = New Font(FontFamily.GenericSansSerif, Me.Font.Size, FontStyle.Italic Xor FontStyle.Strikeout)
fontbrush = Brushes.DimGray
End Try
'fill combo item rectangle
CmboItem.Graphics.FillRectangle(brush, rectangle)
'write text with actual port status for this item
CmboItem.Graphics.DrawString( _
My.Computer.Ports.SerialPortNames(CmboItem.Index) + " - " + status, _
font, _
fontbrush, _
New _
RectangleF(CmboItem.Bounds.X + rectangle.Width, _
CmboItem.Bounds.Y, _
CmboItem.Bounds.Width, _
CmboItem.Bounds.Height) _
)
' Draw focus rectangle when mouse are over an item.
CmboItem.DrawFocusRectangle()
End Sub
End Class
* public class Form1
* {
* //Enumerate Serial Ports on Machine
* //Get Serial Port availability
*
* private void // ERROR: Handles clauses are not supported in C# Form1_Load(object sender, System.EventArgs e)
* {
* //Just drag & drop combobox from toolbox to form designer
* this.ComboBox1.DrawMode = DrawMode.OwnerDrawVariable;
* //select my own drag mode
* this.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
* //aspect when drop down
* this.ComboBox1.DataSource = My.Computer.Ports.SerialPortNames.ToArray;
* //enumerate serialports
* this.ComboBox1.TabIndex = 0;
* //select first tabindex
* ComboBox1.DrawItem += cmbo_SerialPorts_Status;
* //draw my personal cmbo
* }
* //
* // Draw our custom combo
* private void cmbo_SerialPorts_Status(object sender, System.Windows.Forms.DrawItemEventArgs CmboItem)
* {
*
* // Draw the background of the item.
* CmboItem.DrawBackground();
* //
* //Default Values if port is available
* string status = "Available";
* SolidBrush brush = new SolidBrush(Color.Green);
* System.Drawing.Font font = this.Font;
* Brush fontbrush = Brushes.Black;
* Rectangle rectangle = new Rectangle(2, CmboItem.Bounds.Top + 2, CmboItem.Bounds.Height, CmboItem.Bounds.Height - 4);
* //
* //Check for port availability
* try {
* // If port open & close with no exception
* // draw the item with default font and green rectangle
* System.IO.Ports.SerialPort PortTest = new System.IO.Ports.SerialPort();
* PortTest = My.Computer.Ports.OpenSerialPort(My.Computer.Ports.SerialPortNames(CmboItem.Index));
* PortTest.Close();
* }
* catch (Exception ex) {
* // If port is not available
* // draw the item with italic & strikeout font and red rectangle
* brush = new SolidBrush(Color.Red);
* status = "In Use";
* font = new Font(FontFamily.GenericSansSerif, this.Font.Size, FontStyle.Italic ^ FontStyle.Strikeout);
* fontbrush = Brushes.DimGray;
* }
*
* //fill combo item rectangle
* CmboItem.Graphics.FillRectangle(brush, rectangle);
* //write text with actual port status for this item
* CmboItem.Graphics.DrawString(My.Computer.Ports.SerialPortNames(CmboItem.Index) + " - " + status, font, fontbrush, new RectangleF(CmboItem.Bounds.X + rectangle.Width, CmboItem.Bounds.Y, CmboItem.Bounds.Width, CmboItem.Bounds.Height));
* // Draw focus rectangle when mouse are over an item.
* CmboItem.DrawFocusRectangle();
* }
* }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace Probando_02
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Variables
string pa = "Puerto abiero.";
string pc = "Puerto cerrado. Pulse botón 'Conectar'.";
string des = "Desconectar";
string con = "Conectar";
int open = 1;
int com = 2;
private void button_on_Click(object sender, EventArgs e)
{
try
{
byte[] mBuffer = new byte[1];
mBuffer[0] = 0x74; //ASCII letra "t".
serialPort1.Write(mBuffer, 0, mBuffer.Length);
button_on.Enabled = false;
button_off.Enabled = true;
label_.Text = "ON";
label_.ForeColor = Color.Green;
}
catch (InvalidOperationException)
{
label_mensaje.Text = pc;
}
}
private void button_off_Click(object sender, EventArgs e)
{
try
{
byte[] miBuffer = new byte[1];
miBuffer[0] = 0x62; //ASCII letra "b".
serialPort1.Write(miBuffer, 0, miBuffer.Length);
button_off.Enabled = false;
button_on.Enabled = true;
label_.Text = "OFF";
label_.ForeColor = Color.Red;
}
catch (InvalidOperationException)
{
label_mensaje.Text = pc;
}
}
private void button_conectar_Click(object sender, EventArgs e)
{
if (open == 1)
{
open = 2;
serialPort1.Close();
if (comboBox1.Items.Count > 0)
{
comboBox1.SelectedIndex = com;
}
serialPort1.Open();
label_mensaje.Text = pa;
button_conectar.Text = des;
}
else
{
open = 1;
serialPort1.Close();
label_mensaje.Text = pc;
button_conectar.Text = con;
button_on.Enabled = true;
button_off.Enabled = true;
label_.Text = " ";
}
}
}
}