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);
            }
        }
Exemplo n.º 2
0
        public static void Main(string[] args)

        {
            VSCAN_MSG[] msgs = new VSCAN_MSG[2];

            VSCAN CanDevice = new VSCAN();

            UInt32 Written = 0;

            UInt32 Read = 0;

            VSCAN_HWPARAM hw = new VSCAN_HWPARAM();

            VSCAN_API_VERSION api_ver = new VSCAN_API_VERSION();

            byte Flags = 0x0;



            try

            {
                // set debugging options

                CanDevice.SetDebug(VSCAN.VSCAN_DEBUG_NONE);

                CanDevice.SetDebugMode(VSCAN.VSCAN_DEBUG_MODE_FILE);



                // open CAN channel: please specify the name of your device according to User Manual

                CanDevice.Open(VSCAN.VSCAN_FIRST_FOUND, VSCAN.VSCAN_MODE_SELF_RECEPTION);



                // set some options

                CanDevice.SetSpeed(VSCAN.VSCAN_SPEED_1M);

                CanDevice.SetTimestamp(VSCAN.VSCAN_TIMESTAMP_ON);

                CanDevice.SetBlockingRead(VSCAN.VSCAN_IOCTL_ON);



                // get HW Params

                CanDevice.GetHwParams(ref hw);

                Console.WriteLine("Get hardware paramter:");

                Console.WriteLine("HwVersion:" + hw.HwVersion + " SwVersion:" + (hw.SwVersion >> 4) + "." + (hw.SwVersion & 0x0f));

                Console.WriteLine("SerNr:" + hw.SerialNr + " HwType:" + hw.HwType);



                // get API version

                CanDevice.GetApiVersion(ref api_ver);

                Console.WriteLine("");

                Console.WriteLine("API version: " + api_ver.Major + "." + api_ver.Minor + "." + api_ver.SubMinor);



                msgs[0].Id = 0x100;

                msgs[0].Size = 2;

                msgs[0].Data = new byte[8];

                msgs[0].Data[0] = 0xde;

                msgs[0].Data[1] = 0xad;

                msgs[0].Flags = VSCAN.VSCAN_FLAGS_EXTENDED;



                msgs[1].Id = 0x101;

                msgs[1].Size = 2;

                msgs[1].Data = new byte[8];

                msgs[1].Data[0] = 0xbe;

                msgs[1].Data[1] = 0xef;

                msgs[1].Flags = VSCAN.VSCAN_FLAGS_EXTENDED;



                // send CAN frames

                CanDevice.Write(msgs, 2, ref Written);

                // send immediately

                CanDevice.Flush();

                Console.WriteLine("");

                Console.WriteLine("Send CAN frames: " + Written);



                // read CAN frames

                CanDevice.Read(ref msgs, 2, ref Read);

                Console.WriteLine("");

                Console.WriteLine("Read CAN frames: " + Read);

                for (int i = 0; i < Read; i++)

                {
                    Console.WriteLine("");

                    Console.WriteLine("CAN frame " + i);

                    Console.WriteLine("ID: " + msgs[i].Id);

                    Console.WriteLine("Size: " + msgs[i].Size);

                    Console.Write("Data: ");

                    for (int j = 0; j < msgs[i].Size; j++)

                    {
                        Console.Write(msgs[i].Data[j] + " ");
                    }

                    Console.WriteLine("");

                    if ((msgs[i].Flags & VSCAN.VSCAN_FLAGS_STANDARD) != 0)
                    {
                        Console.WriteLine("VSCAN_FLAGS_STANDARD");
                    }

                    if ((msgs[i].Flags & VSCAN.VSCAN_FLAGS_EXTENDED) != 0)
                    {
                        Console.WriteLine("VSCAN_FLAGS_EXTENDED");
                    }

                    if ((msgs[i].Flags & VSCAN.VSCAN_FLAGS_REMOTE) != 0)
                    {
                        Console.WriteLine("VSCAN_FLAGS_REMOTE");
                    }

                    if ((msgs[i].Flags & VSCAN.VSCAN_FLAGS_TIMESTAMP) != 0)
                    {
                        Console.WriteLine("VSCAN_FLAGS_TIMESTAMP");
                    }

                    Console.WriteLine("TS: " + msgs[i].TimeStamp);
                }



                // get extended status and error flags

                CanDevice.GetFlags(ref Flags);

                Console.WriteLine("");

                Console.WriteLine("Extended status and error flags: " + Flags);

                DecodeFlags(Flags);



                // close CAN channel

                CanDevice.Close();
            }

            catch (Exception e)

            {
                Console.WriteLine("CAN opened " + e.Message);
            }
        }
Exemplo n.º 3
0
        public static VSCAN_MSG[] GetData(VSCAN canAdapter)
        {
            var    msgs = new VSCAN_MSG[1];
            UInt32 read = 0;

            try
            {
                canAdapter.Read(ref msgs, 1, ref read);
                Console.WriteLine("");
                Console.WriteLine($"Read CAN frames: {read}");
                for (var i = 0; i < read; 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("");

                    if ((msgs[i].Flags & VSCAN.VSCAN_FLAGS_STANDARD) != 0)
                    {
                        Console.WriteLine("VSCAN_FLAGS_STANDARD");
                    }

                    if ((msgs[i].Flags & VSCAN.VSCAN_FLAGS_EXTENDED) != 0)
                    {
                        Console.WriteLine("VSCAN_FLAGS_EXTENDED");
                    }

                    if ((msgs[i].Flags & VSCAN.VSCAN_FLAGS_REMOTE) != 0)
                    {
                        Console.WriteLine("VSCAN_FLAGS_REMOTE");
                    }

                    if ((msgs[i].Flags & VSCAN.VSCAN_FLAGS_TIMESTAMP) != 0)
                    {
                        Console.WriteLine("VSCAN_FLAGS_TIMESTAMP");
                    }

                    Console.WriteLine("TS: " + msgs[i].TimeStamp);
                }

                canAdapter.GetFlags(ref _flags);

                Console.WriteLine("");

                Console.WriteLine($"Extended status and error flags: {_flags}");

                DecodeFlags(_flags);

                return(msgs);
            }

            catch (Exception e)
            {
                Console.WriteLine($"Message not recieved {e.Message}");
                return(msgs);
            }
        }