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); } }
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); } }
void SEND() { if (!data.connOK || CanDevice == null) { if (!data.PLAY) { pFORM.AddText("-Отмена- нет связи\n"); } return; } msgs[0].Id = data.adr; // msgs[0].Size = data.sz; // 8; msgs[0].Data = new byte[8]; msgs[0].Data[0] = data.b1; msgs[0].Data[1] = data.b2; msgs[0].Data[2] = data.b3; msgs[0].Data[3] = data.b4; msgs[0].Data[4] = data.b5; msgs[0].Data[5] = data.b6; msgs[0].Data[6] = data.b7; msgs[0].Data[7] = data.b8; // msgs[0].Flags = VSCAN.VSCAN_FLAGS_EXTENDED; msgs[0].Flags = VSCAN.VSCAN_FLAGS_STANDARD; // send CAN frames CanDevice.Write(msgs, 1, ref Written); // send immediately CanDevice.Flush(); if (Written == 0) { pFORM.AddText("--- Сбой отправки попытка 2"); Thread.Sleep(200); CanDevice.Write(msgs, 1, ref Written); CanDevice.Flush(); if (Written == 0) { pFORM.AddText("--- Сбой отправки попытка 3"); Thread.Sleep(500); CanDevice.Write(msgs, 1, ref Written); CanDevice.Flush(); if (Written == 0) { GLOBERR++; pFORM.AddText("\n&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n--- Сбой отправки НЕ УДАЕТСЯ ОТПРАВИТЬ!! \n"); } } } }