Exemple #1
0
 static void SendData()
 {
     if (p != null && p.IsOpen)
     {
         Message_t.WriteMessage(p, ref msgOut);
     }
 }
Exemple #2
0
 private void clearColorBtn_Click(object sender, EventArgs e)
 {
     msgOut.Command = Command_t.ClearColor;
     if (p != null && p.IsOpen)
     {
         Message_t.WriteMessage(p, ref msgOut);
     }
 }
Exemple #3
0
        public unsafe static void ReadMessage(SerialPort port, ref Message_t msg)
        {
            byte[] data = new byte[Marshal.SizeOf(typeof(Message_t))];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = (byte)port.ReadByte();
            }

            fixed(byte *b = data)
            {
                msg = (Message_t)Marshal.PtrToStructure((IntPtr)b, typeof(Message_t));
            }
        }
Exemple #4
0
        public unsafe static void WriteMessage(SerialPort port, ref Message_t msg)
        {
            byte[] data = new byte[Marshal.SizeOf(typeof(Message_t))];
            fixed(byte *b = data)
            {
                Marshal.StructureToPtr(msg, (IntPtr)b, true);
            }

            //Console.WriteLine($"Sending {data.Length} bytes..");
            //foreach (byte d in data)
            //{
            //    Console.Write($"{d:X2} ");
            //}
            //Console.WriteLine();
            try
            {
                port.Write(data, 0, data.Length);
            }
            catch
            {
            }

            //Console.WriteLine("[Success]");
        }
Exemple #5
0
        private static SerialPort GetArduinoPort()
        {
            string[] ports = SerialPort.GetPortNames();
            foreach (var port in ports)
            {
                if (port != "COM12")
                {
                    continue;
                }
                SerialPort p = new SerialPort(port, 9600);
                p.DtrEnable = false;
                //p.WriteTimeout = 1000;
                //Prepare messages
                Message_t msgIn = new Message_t();
                //Message_t msgOut = new Message_t();
                msgOut.Magic   = Message_t.MESSAGE_MAGIC;
                msgOut.Command = Command_t.Ping;

                try
                {
                    try
                    {
                        p.Close();
                    }
                    catch
                    {
                    }
                    Console.Write($"Open {port}...");
                    p.Open();
                    Console.WriteLine($"[Success]");
                    //Write ping-message
                    Message_t.WriteMessage(p, ref msgOut);
                    //Console.WriteLine($"Send ping...");

                    //Wait about a second for the arduino to reply
                    Thread.Sleep(1000);

                    //No response within a second, go check the next port
                    if (!Message_t.MessageReady(p))
                    {
                        Console.WriteLine($"No response!");
                        try
                        {
                            p.Close();
                        }
                        catch
                        {
                        }
                        continue;
                    }

                    //Read message from serial, check for valid magic and reponse
                    Message_t.ReadMessage(p, ref msgIn);
                    if (msgIn.Magic != Message_t.MESSAGE_MAGIC)
                    {
                        Console.WriteLine($"Invalid Megic!");
                        try
                        {
                            p.Close();
                        }
                        catch
                        {
                        }
                        continue;
                    }

                    if (msgIn.Command != Command_t.Pong)
                    {
                        Console.WriteLine($"Response error: {msgIn.Command}");
                        continue;
                    }
                    else
                    {
                        Console.WriteLine($"Response OK: {msgIn.Command}");
                    }

                    return(p);
                }
                catch
                {
                    Console.WriteLine($"[Fail!]");
                    //Close port in case we ran into any exception (should never happen though)
                    //p.Close();
                }
                try
                {
                    p.Close();
                }
                catch
                {
                }
            }
            return(null);
        }