Ejemplo n.º 1
0
        static public byte[] CommandStruct_to_Bytes(CTP_packet command)
        {
            byte[] arr = new byte[Marshal.SizeOf(command)];
            IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(command));

            Marshal.StructureToPtr(command, ptr, true);
            Marshal.Copy(ptr, arr, 0, Marshal.SizeOf(command));
            Marshal.FreeHGlobal(ptr);
            return(arr);
        }
Ejemplo n.º 2
0
 public byte SendCTP_Command(CTP_packet _command)
 {
     if (connection.IsOpen == false)
     {
         return(Convert.ToByte(0));
     }
     connection.Write(CommandStruct_to_Bytes(_command), 0, Marshal.SizeOf(_command) - 1);
     byte[] buffer = new byte[1];
     connection.Read(buffer, 0, 1);
     //Console.WriteLine($"Recieved after command: {Convert.ToInt32(buffer[0])}");
     logger?.LogDebug($"Recieved after command: {Convert.ToInt32(buffer[0])}");
     return(buffer[0]);
 }
Ejemplo n.º 3
0
 public Task <byte> SendCTP_CommandAsync(CTP_packet _command)
 {
     return(Task.Factory.StartNew((command) => {
         if (connection.IsOpen == false)
         {
             return Convert.ToByte(0);
         }
         CTP_packet p = (CTP_packet)command;
         connection.Write(CommandStruct_to_Bytes(p), 0, Marshal.SizeOf(p) - 1);
         byte[] buffer = new byte[1];
         connection.Read(buffer, 0, 1);
         //Console.WriteLine($"Recieved after command: {Convert.ToInt32(buffer[0])}");
         logger?.LogDebug($"Recieved after command: {Convert.ToInt32(buffer[0])}");
         return buffer[0];
     }, _command));
 }
Ejemplo n.º 4
0
 public byte SendCTP_Command(CTP_packet _command)
 {
     if (connection.IsOpen == false)
     {
         return(Convert.ToByte(0));
     }
     Console.WriteLine("Sending");
     connection.Write(CommandStruct_to_Bytes(_command), 0, Marshal.SizeOf(_command) - 1);
     byte[] buffer = new byte[1];
     try
     {
         connection.Read(buffer, 0, 1);
     }
     catch (Exception ex) { Console.WriteLine(ex.Message);
                            return(0); };
     //Console.WriteLine($"Recieved after command: {Convert.ToInt32(buffer[0])}");
     Console.WriteLine($"Recieved after command: {Convert.ToInt32(buffer[0])}");
     return(buffer[0]);
 }
Ejemplo n.º 5
0
        static private bool Check_phisically_connected()
        {
            command = new CTP_packet();
            const int number_of_ports_to_check = 6;
            bool      is_connected             = false;

            byte[] buffer = new byte[1];
            // Expected response - Unxepected pin
            command.command_type = 1;
            command.pin_number   = 255;
            command.duty         = 0;
            command.frequency    = 0;
            for (int port_number = number_of_ports_to_check; port_number > 0; port_number--)
            {
                connection.PortName = "COM" + port_number.ToString();
                try
                {
                    connection.Open();
                    connection.Write(CommandStruct_to_Bytes(command), 0, Marshal.SizeOf(command) - 1);
                    connection.Read(buffer, 0, 1);
                    Console.WriteLine("{0} : {1}", connection.PortName, buffer[0]);
                }
                catch (Exception)
                {
                    connection.Close();
                    continue;
                }
                //Console.WriteLine($"Comparison result: {string.Compare(response, expected_response)}");
                if (Convert.ToInt32(buffer[0]) == 101)
                {
                    //Console.WriteLine("Arduino connected!!!");
                    is_connected = true;
                    break;
                }
                connection.Close();
            }
            return(is_connected);
        }
Ejemplo n.º 6
0
 public Task <byte> SendCTP_CommandAsync(CTP_packet _command)
 {
     return(Task.Run(() => SendCTP_Command(_command)));
 }