示例#1
0
 /// <summary> Reads a CAN frame asynchronously, returning the payload and the ID of the received CAN frame. </summary>
 /// <returns> A task that will return a tuple, with the first element being the ID of the received CAN frame and the second being the payload </returns>
 public Task <Tuple <uint, byte[]> > ReadAsync()
 {
     return(Task.Run(() =>
     {
         if (CANFD)
         {
             CANFDFrame Frame = new CANFDFrame();
             read(Socket, ref Frame, Marshal.SizeOf(Frame));
             byte[] Payload = new byte[Frame.DataLength];
             unsafe
             {
                 for (int i = 0; i < Frame.DataLength; i++)
                 {
                     Payload[i] = Frame.Data[i];
                 }
             }
             return new Tuple <uint, byte[]>(Frame.CANID, Payload);
         }
         else
         {
             CANFrame Frame = new CANFrame();
             read(Socket, ref Frame, Marshal.SizeOf(Frame));
             byte[] Payload = new byte[Frame.DataLength];
             unsafe
             {
                 for (int i = 0; i < Frame.DataLength; i++)
                 {
                     Payload[i] = Frame.Data[i];
                 }
             }
             return new Tuple <uint, byte[]>(Frame.CANID, Payload);
         }
     }));
 }
示例#2
0
        /// <summary> Write a payload with specified ID </summary>
        /// <param name="ID"> ID of CAN Frame </param>
        /// <param name="Data"> Payload of CAN Frame. Must be at most 8 bytes. </param>
        public void Write(uint ID, byte[] Data)
        {
            if (!CANFD && Data.Length > 8)
            {
                throw new Exception("CAN Data Length must be no more than 8 bytes for non-Extended frames");
            }
            else if (Data.Length > 64)
            {
                throw new Exception("CAN Data Length must be no more than 64 bytes for Extended frames.");
            }
            int BytesWritten;

            unsafe
            {
                if (CANFD)
                {
                    CANFDFrame Frame = new CANFDFrame();
                    Frame.CANID      = ID | CAN_EFF_FLAG;
                    Frame.DataLength = DLCToLength(LengthToDLC((byte)Data.Length));
                    for (int i = 0; i < Data.Length; i++)
                    {
                        Frame.Data[i] = Data[i];
                    }
                    BytesWritten = write(Socket, ref Frame, Marshal.SizeOf(Frame));
                }
                else
                {
                    CANFrame Frame = new CANFrame();
                    Frame.CANID      = ID;
                    Frame.DataLength = DLCToLength(LengthToDLC((byte)Data.Length));
                    for (int i = 0; i < Data.Length; i++)
                    {
                        Frame.Data[i] = Data[i];
                    }
                    BytesWritten = write(Socket, ref Frame, Marshal.SizeOf(Frame));
                }
            }
            if (BytesWritten < 0)
            {
                throw new Exception("Failed to write CAN frame. Error code: " + Marshal.GetLastWin32Error());
            }
        }
示例#3
0
 private static extern int write(int FileDescriptor, ref CANFDFrame Frame, int Size);