Exemplo n.º 1
0
        public static bool SendData(VSCAN canAdapter, UInt32 id, byte[] data)
        {
            var    msgs    = new VSCAN_MSG[1];
            UInt32 written = 0;

            try
            {
                msgs[0].Data  = data;
                msgs[0].Id    = id;
                msgs[0].Size  = 8;
                msgs[0].Flags = VSCAN.VSCAN_FLAGS_EXTENDED;

                canAdapter.Write(msgs, 1, ref written);
                canAdapter.Flush();
                Console.WriteLine("");
                Console.WriteLine($"Send CAN frames: {written}");
                for (var i = 0; i < written; i++)
                {
                    Console.WriteLine("");
                    Console.WriteLine($"CAN frame {i}");
                    Console.WriteLine($"ID: {msgs[i].Id.ToString("X")}");
                    Console.WriteLine($"Size: {msgs[i].Size}");
                    Console.Write("Data: ");

                    for (var j = 0; j < msgs[i].Size; j++)
                    {
                        Console.Write($"{msgs[i].Data[j].ToString("X")} ");
                    }

                    Console.WriteLine("");
                }

                return(true);
            }

            catch (Exception e)
            {
                Console.WriteLine($"Message not transmitted {e.Message} {canAdapter.ToString()}");
                return(false);
            }
        }