F sharp - Abrir y cerrar la bandeja con F#

 
Vista:
sin imagen de perfil
Val: 23
Ha mantenido su posición en F sharp (en relación al último mes)
Gráfica de F sharp

Abrir y cerrar la bandeja con F#

Publicado por Meta (12 intervenciones) el 16/11/2017 10:05:52
Buenas a todos y a todas:

disco-insterted-al-dvd-o-al-lector-de-cd-30302263

Quiero pasar este código en consola de C#, VB .net o el C++ CLR a F#. Lo que hace el código es si pulsas A o la letra C abre o cierra la bandeja del lector de discos. A parte de C#, también está en C++ CLR y VB .net por si lo entienden mejor. Lo que hace el código es abrir y cerrar la bandeja de discos del lector, sea IDE o SATA.

Código C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Runtime.InteropServices;
using System.Text;
 
namespace Lector_teclado_consola_cs
{
    class Program
    {
        [DllImport("winmm.dll")]
        public static extern Int32 mciSendString(string lpstrCommand, StringBuilder lpstrReturnString,
        int uReturnLength, IntPtr hwndCallback);
 
        public static StringBuilder rt = new StringBuilder(127);
 
        public static void DoEvents()
        {
            // Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { }));
            Console.SetCursorPosition(0, 6);
            Console.Write("Abriendo...");
        }
 
        public static void DoEvents2()
        {
            // Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { }));
            Console.SetCursorPosition(0, 6);
            Console.Write("Cerrando...");
        }
 
        static void Main(string[] args)
        {
            // Título de la ventana.
            Console.Title = "Control lector de bandeja. C#";
 
            // Tamaño ventana consola.
            Console.WindowWidth = 29; // X. Ancho.
            Console.WindowHeight = 8; // Y. Alto. 
 
            // Cursor invisible.
            Console.CursorVisible = false;
 
            // Posición del mansaje en la ventana.
            Console.SetCursorPosition(0, 0);
            Console.Write(@"Control bandeja del lector:

A - Abrir bandeja.
C - Cerrar bandeja.
===========================");
 
 
 
            ConsoleKey key;
            //Console.CursorVisible = false;
            do
            {
                key = Console.ReadKey(true).Key;
 
                string mensaje = string.Empty;
 
                //Asignamos la tecla presionada por el usuario
                switch (key)
                {
                    case ConsoleKey.A:
                        // mensaje = "Abriendo..."; 
                        Console.SetCursorPosition(0, 6);
                        DoEvents();
                        mciSendString("set CDAudio door open", rt, 127, IntPtr.Zero);
                        mensaje = "Abierto.";
                        break;
 
                    case ConsoleKey.C:
                        // mensaje = "Cerrando...";
                        Console.SetCursorPosition(0, 6);
                        DoEvents2();
                        mciSendString("set CDAudio door closed", rt, 127, IntPtr.Zero);
                        mensaje = "Cerrado.";
                        break;
                }
 
                Console.SetCursorPosition(0, 6);
                Console.Write("           ");
                Console.SetCursorPosition(0, 6);
                Console.Write(mensaje);
 
            }
            while (key != ConsoleKey.Escape);
        }
    }
}

Del .net me falta F# y acabo esta curiosidad y retillo que tengo pendiente desde hace vete a saber.

¿Algún atrevido para poder abrir y cerrar la bandeja del lector usando el lenguaje F#?

Tienes que tener iniciativa para empezar y convencido para terminarlo.

Un cordial saludos a todos y a todas. ;)

PD: Gracias por aceptar mi petición de hacer este foro de F#. Aqí mi primer tema sobre este lenguaje.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 23
Ha mantenido su posición en F sharp (en relación al último mes)
Gráfica de F sharp

[SOLUCIONADO] Abrir y cerrar la bandeja con F#

Publicado por Meta (12 intervenciones) el 19/11/2017 01:58:29
Hola:

Me respondo a mi mismo. Código resuelto y en perfecto funcionamiento. Ya puedes abrir y cerrar la bandeja con el elnguaje F#.

Código F#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Más información acerca de F# en http://fsharp.org
 
open System
open System.Runtime.InteropServices
open System.Text;
 
// importar librería o dll externo.
[<DllImport("winmm.dll")>]
extern int mciSendString(string lpstrCommand, StringBuilder lpstrReturnString,
        int uReturnLength, IntPtr hwndCallback)
 
let rt = StringBuilder(127)
 
// Evento.
let DoEvents (transition:string) =
    Console.SetCursorPosition(0, 6)
    Console.Write transition
 
let action state transition (mensaje:string) =
    Console.SetCursorPosition(0, 6);
    DoEvents transition;
    mciSendString(state, rt, 127, IntPtr.Zero) |> ignore
    Console.SetCursorPosition(0, 6)
    Console.Write("           ")
    Console.SetCursorPosition(0, 6)
    Console.Write(mensaje)
 
// Pulse letra A para abrir bandeja o C para cerrar bandeja.
let rec loop() =
    match Console.ReadKey(true).Key with
    | ConsoleKey.Escape -> ()
    | ConsoleKey.A -> action "set CDAudio door open" "Abriendo..." "Abierto."
                      loop()
    | ConsoleKey.C -> action "set CDAudio door closed" "Cerrando..." "Cerrado."
                      loop()
    | _ -> loop()
 
[<EntryPoint>]
let main argv =
    // Título de la ventana.
    Console.Title <- "F#"
 
    // Tamaño ventana consola.
    Console.WindowWidth <- 29 // X. Ancho.
    Console.WindowHeight <- 8 // Y. Alto.
 
    // Cursor invisible.
    Console.CursorVisible <- false
 
    // Posición del mansaje en la ventana.
    Console.SetCursorPosition(0, 0)
    Console.Write(@"Control bandeja del lector:

A - Abrir bandeja.
C - Cerrar bandeja.
===========================")
    loop()
    0 // return an integer exit code

A estas altura de la vida, la cantidad increible de falta de interés por el lenguaje F# que todavía no salgo de mi asombro. Lo estoy usando más bien por pura curiosidad más que por necesidad.

¿Para qué Microsoft ha perdido el tiempo en hacer este lenguaje que no usan apenas?

Saludos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar