I want to create a program in console mode with the F # language. That data can be sent to the serial port and received. I made one in C # and I want to adapt it to F #. As I do not have the necessary knowledge in F #, I ask for help.
Code C #:
using System;
using System.IO.Ports;
using System.Text;
namespace Arduino_Consola_cs
{
class Program
{
static void Main(string[] args)
{
// Título de la ventana.
Console.Title = "Arduino Consola C#";
// Tamaño ventana consola.
Console.WindowWidth = 55; // X. Ancho.
Console.WindowHeight = 18; // Y. Alto.
// Cree un nuevo objeto SerialPort con la configuración predeterminada.
SerialPort Puerto_serie = new SerialPort("COM4");
Puerto_serie.BaudRate = 115200;
Puerto_serie.Parity = Parity.None;
Puerto_serie.StopBits = StopBits.One;
Puerto_serie.DataBits = 8;
Puerto_serie.Handshake = Handshake.None;
Puerto_serie.RtsEnable = true;
// Establecer los tiempos de espera de lectura / escritura.
Puerto_serie.ReadTimeout = 500; // Milisegundos.
Puerto_serie.WriteTimeout = 500;
// Detecta cualquier dato recibido.
Puerto_serie.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
Puerto_serie.Open(); // Abrir puerto.
ConsoleKey tecla;
Console.WriteLine("Pulse tecla 1 para encender y 2 para apagar:");
do
{
tecla = Console.ReadKey(true).Key; // Espera pulsación de teclas.
switch (tecla)
{
case ConsoleKey.D1: // Tecla 1 del teclado estandar.
case ConsoleKey.NumPad1: // Tecla 1 del número del pad.
byte[] miBuffer1 = Encoding.ASCII.GetBytes("Luz_ON"); // Codificación ASCII y guarda en la variable array tipo byte.
Puerto_serie.Write(miBuffer1, 0, miBuffer1.Length); // Envía los datos del buffer todo su contenido.
Console.WriteLine("Comando \"Luz_ON\" enviado."); // Muestra en pantalla comandos enviado.
break;
case ConsoleKey.D2:
case ConsoleKey.NumPad2:
byte[] miBuffer2 = Encoding.ASCII.GetBytes("Luz_OFF");
Puerto_serie.Write(miBuffer2, 0, miBuffer2.Length);
Console.WriteLine("Comando \"Luz_OFF\" enviado.");
break;
default:
Console.WriteLine("Tecla el 1, el 2 y Escape para salir.");
break;
}
} while (tecla != ConsoleKey.Escape); // Pulsa Escape para salir del menú.
Console.WriteLine("Presione cualquier tecla para terminar...");
Console.WriteLine();
Console.ReadKey(); // Espera pulsar una tecla cualquiera.
Puerto_serie.Close(); // Cierra el puerto serie.
}
// Detecta cualquier dato entrante.
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string entradaDatos = sp.ReadExisting(); // Almacena los datos recibidos en la variable tipo string.
Console.WriteLine("Dato recibido desde Arduino: " + entradaDatos); // Muestra en pantalla los datos recibidos.
}
}
}