Foros del Web » Programación para mayores de 30 ;) » .NET »

Puerto serie con F#

Estas en el tema de Puerto serie con F# en el foro de .NET en Foros del Web. Hola: Estoy sintiende curiosidad por el lenguaje F# .net, lo ignoraba en su tiempo porque no se podía poner en modo formulario, solo modo consola, ...
  #1 (permalink)  
Antiguo 08/12/2015, 06:00
 
Fecha de Ingreso: mayo-2007
Ubicación: PIC-16F84A
Mensajes: 727
Antigüedad: 16 años, 10 meses
Puntos: 8
Puerto serie con F#

Hola:

Estoy sintiende curiosidad por el lenguaje F# .net, lo ignoraba en su tiempo porque no se podía poner en modo formulario, solo modo consola, pero ahora me da igual, eso si, pensé que lelgaba a ser solo para automatización de tareas.

Me ha costado mucho encontrar algún ejemplo de enviar dator al puerto serie y recibirlo en el lenguaje F#, hasta que llegué aquí.

F# puerto serie.

Por si el enalce en el futuro desaparece, dejo el código fuente.
Código:
#light

// Program to read a paper tape to a file from a GNT4604 paper tape reader
// attached to serial port COM3:

module ReadTape
    
    open System.IO

    exception NoTape  // used to force exit when no further tape to read

    // Open serial port 
    let OpenPort () =
        let port = new System.IO.Ports.SerialPort ("COM3", 4800, Ports.Parity.None, 8, Ports.StopBits.One)
        port.Open ()
        port.DtrEnable   <- true
        port.RtsEnable   <- true
        port.ReadTimeout <- 1000
        port 

    let port = OpenPort ()                   

    let ReadRaw f =
        let buffer: byte[] = Array.zeroCreate (250*60*30) // 15 minutes of paper tape reading 
        let rec GetBytes i = 
            try 
                    buffer.[i] <- byte (port.ReadByte ()) 
                    GetBytes (i+1)
            with 
            _   ->  i // finish on timeout    
        printfn "Reading tape to %s" f
        printf "Load tape and type hit any key..."
        System.Console.ReadLine () |> ignore
        let count = (GetBytes 30)+30 // force 3 inches of leader and trailer)
        printfn "%d bytes input" (count-60)
        let rec TrimLeft i =
            if   i = count
            then printfn "Empty tape"
                 0
            elif buffer.[i] = 0uy 
            then TrimLeft (i+1) 
            else i
        let rec TrimRight i =
            if   i = 0
            then printfn "Empty Tape"
                 count
            elif buffer.[i] = 0uy 
            then TrimRight (i-1) 
            else i
        let start  = max 30 ((TrimLeft 0)-30)
        let finish = min (count-30) ((TrimRight count)+30)       
        let trimmed = buffer.[start..finish]
        printfn "%d bytes output" trimmed.Length
        try File.WriteAllBytes (f, trimmed) with
        e   -> System.Console.WriteLine f


    let  cmdLine = System.Environment.GetCommandLineArgs()
    if   cmdLine.Length < 2
    then System.Console.WriteLine "Error: READTAPE file"
    else ReadRaw cmdLine.[1]
Si lo ejecuto me sale esta línea.
Código:
then System.Console.WriteLine "Error: READTAPE file"
Partiendo de este enlace en C#.

Código:
using System;
using System.IO.Ports;

class PortDataReceived
{
    public static void Main()
    {
        SerialPort mySerialPort = new SerialPort("COM1");

        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;
        mySerialPort.RtsEnable = true;

        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

        mySerialPort.Open();

        Console.WriteLine("Press any key to continue...");
        Console.WriteLine();
        Console.ReadKey();
        mySerialPort.Close();
    }

    private static void DataReceivedHandler(
                        object sender,
                        SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();
        Console.WriteLine("Data Received:");
        Console.Write(indata);
    }
}
Lo que me cuesta interpretar C#,

Eso si, he intentado poner un título a la ventana. Con C# se hace así:

Código:
System.Console.Title = "Título del programa";
En F# parece que es así, pero sin el ; al final.
Código:
System.Console.Title = "Título del programa"
Pero no lo es porque da error.

¿Alguna ayuda?

Un cordial saludo.
__________________
Meta Shell, VERSIÓN 1.2.2
Descargar

Etiquetas: net, puerto, serie
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 13:56.