public void Post(CanFrame frame) { using (CanRaw can = new CanRaw()) { can.WriteFrame(frame); } }
private static void ReceiveLoop() { using (CanRaw can = new CanRaw()) { CanFrame frame; while (true) { if (can.TryReadFrame(out frame)) { receivedFrames.Enqueue(frame); } } } }
private static void ReceiveAllExample() { Console.WriteLine("Listening for any id"); using (CanRaw can = new CanRaw()) { byte[] buffer = new byte[8]; while (true) { if (can.TryReadFrame(buffer, out int frameLength, out CanId id)) { Span <byte> data = new Span <byte>(buffer, 0, frameLength); string type = id.ExtendedFrameFormat ? "EFF" : "SFF"; string dataAsHex = string.Join(string.Empty, data.ToArray().Select((x) => x.ToString("X2"))); Console.WriteLine($"Id: 0x{id.Value:X2} [{type}]: {dataAsHex}"); }
public static bool TryReadFrame(this CanRaw can, out CanFrame frame) { var buffer = new byte[8]; var success = can.TryReadFrame(buffer, out int frameLength, out CanId id); frame = new CanFrame() { Data = new Span <byte>(buffer, 0, frameLength).ToArray(), Address = id.Value, ErrorFrame = id.Error, FrameFormat = id.ExtendedFrameFormat ? FrameFormat.Extended : FrameFormat.Standard, RemoteRequest = id.RemoteTransmissionRequest, DLC = (byte)frameLength }; return(success); }
public static void WriteFrame(this CanRaw can, CanFrame frame) { var canId = new CanId(); canId.RemoteTransmissionRequest = frame.RemoteRequest; if (frame.FrameFormat == FrameFormat.Extended) { canId.ExtendedFrameFormat = true; canId.Extended = frame.Address; } else { canId.ExtendedFrameFormat = false; canId.Standard = frame.Address; } byte[] data = new byte[frame.DLC]; Array.Copy(frame.Data, data, frame.DLC); can.WriteFrame(data, canId); }
private static void SendExample() { Console.WriteLine($"Sending to id = 0x{Id.Value:X2}"); using (CanRaw can = new CanRaw()) { byte[][] buffers = new byte[][] { new byte[8] { 1, 2, 3, 40, 50, 60, 70, 80 }, new byte[7] { 1, 2, 3, 40, 50, 60, 70 }, new byte[0] { }, new byte[1] { 254 }, }; if (!Id.IsValid) { // This is more form of the self-test rather than actual part of the sample throw new Exception("Id is invalid"); } while (true) { foreach (byte[] buffer in buffers) { can.WriteFrame(buffer, Id); string dataAsHex = string.Join(string.Empty, buffer.Select((x) => x.ToString("X2"))); Console.WriteLine($"Sending: {dataAsHex}"); Thread.Sleep(1000); } } } }