public void readMSG() { UInt32 Read = 0; if (Read2 != 0) { if (Read2 != sizeFRAME) { for (int i = 0; i < Read2; i++) { msgs[i].Id = 0; msgs[i].Data[0] = 0; msgs[i].Data[1] = 0; msgs[i].Data[2] = 0; msgs[i].Data[3] = 0; msgs[i].Data[4] = 0; msgs[i].Data[5] = 0; msgs[i].Data[6] = 0; msgs[i].Data[7] = 0; } } else { for (int i = 0; i < sizeFRAME; i++) { msgs[i].Id = 0; msgs[i].Data[0] = 0; msgs[i].Data[1] = 0; msgs[i].Data[2] = 0; msgs[i].Data[3] = 0; msgs[i].Data[4] = 0; msgs[i].Data[5] = 0; msgs[i].Data[6] = 0; msgs[i].Data[7] = 0; } } } CanDevice.Read(ref msgs, sizeFRAME, ref Read); Read2 = Read; if (!data.start_buff) { Thread.Sleep(100); return; } if (Read != 0) { data.can++; if (data.can > 1) { data.can = 0; //индикатор квадрат } for (int i = 0; i < Read; i++) { data.test_read++; ДАННЫЕочередь buf = new ДАННЫЕочередь() { time = DateTime.Now,//data.getTIME(), strERROR = GetStatus(), adr = msgs[i].Id, sz = msgs[i].Size, b1 = msgs[i].Data[0], b2 = msgs[i].Data[1], b3 = msgs[i].Data[2], b4 = msgs[i].Data[3], b5 = msgs[i].Data[4], b6 = msgs[i].Data[5], b7 = msgs[i].Data[6], b8 = msgs[i].Data[7] }; ////////////////////////////////////////////////////////////////// //ПОМЕЩАЕМ В ОЧЕРЕДЬ if (!data.АБОТА_С_ОЧЕРЕДЬЮ("write", buf)) { data.пропущенные++; } } } }
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); } }
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); } }