Beispiel #1
0
        private void ReadRaw()
        {
            try
            {
                TPCANStatus result;

                do
                {
                    TpcanMsg readCanMsg;
                    result = PcanBasicDllWrapper.Read(this.MPcanHandle, out readCanMsg, out _);
                    if (result == TPCANStatus.PCAN_ERROR_OK)
                    {
                        this.ReadCanMessageEvent.OnReached(new ReadCanMessageEventArgs(readCanMsg.Id, readCanMsg.Data));
                    }
                    else
                    {
                        Thread.Sleep(1);
                    }
                }while (true);
            }
            catch (Exception ex)
            {
                this.Logger.LogError(ex);
                throw;
            }
        }
Beispiel #2
0
        /// <summary>
        ///     Does this instance.
        /// </summary>
        /// <returns>Return the handle.</returns>
        /// <exception cref="System.Exception">Throw an exception.</exception>
        public ushort Do()
        {
            try
            {
                lock (LockInit)
                {
                    var resInit = PcanBasicDllWrapper.Initialize(this.MPcanHandle, this.Baudrate, TPCANType.PCAN_TYPE_ISA, Convert.ToUInt32("0100", 16), Convert.ToUInt16("3"));

                    // this.Logger.LogDebug($"Init tes {resInit}");
                    if (resInit == TPCANStatus.PCAN_ERROR_INITIALIZE)
                    {
                        return(this.MPcanHandle);
                    }

                    if (resInit != TPCANStatus.PCAN_ERROR_OK)
                    {
                        // Ein Fehler ist aufgetreten. Die Rückgabewert wird in Text umgewandelt und angezeigt.
                        var strMsg = new StringBuilder(256);
                        PcanBasicDllWrapper.GetErrorText(resInit, 0, strMsg);

                        // this.Logger.LogDebug("In init :" + strMsg.ToString());
                        throw new Exception($"CAN-init not successfull handle {this.MPcanHandle}");
                    }

                    // this.Logger.LogTrace("CAN is already initaized.");
                    return(this.MPcanHandle);
                }
            }
            catch (Exception e)
            {
                this.Logger.LogError(e);
                throw;
            }
        }
Beispiel #3
0
        /// <summary>
        ///     Remotes the request for channel value.
        /// </summary>
        /// <param name="node">The node value.</param>
        public void RemoteRequestForChannelValue(uint node)
        {
            try
            {
                this.OpenCanDongle();

                // ClearMessageBuffer(node);
                var canMsg = new TpcanMsg
                {
                    Id      = node,
                    Msgtype = TpcanMessageType.PCAN_MESSAGE_RTR,
                    Data    = new byte[8],
                    Len     = 2              // todo mb: die längenangabe ist aufgrund eines Fehlverhaltens im alten Knoten notwendig
                };

                // Console.WriteLine("canMsg.ID: RemoteRequest" + canMsg.ID);
                var result = PcanBasicDllWrapper.Write(this.MPcanHandle, ref canMsg);

                if (result != TPCANStatus.PCAN_ERROR_OK)
                {
                    throw new Exception("TpCanStatus");
                }
            }
            catch (Exception e)
            {
                this.Logger.LogError(e);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Resets this instance.
        /// </summary>
        public void Reset()
        {
            try
            {
                PcanBasicDllWrapper.Uninitialize(this.MPcanHandle);

                this.Do(); // handle neu schreiben??!! todo mb. evtl das reset impliziert?
            }
            catch (Exception e)
            {
                this.Logger.LogError(e);
                throw;
            }
        }
Beispiel #5
0
 /// <summary>
 ///     Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 /// </summary>
 public void Dispose()
 {
     try
     {
         lock (LockDispose)
         {
             PcanBasicDllWrapper.Uninitialize(PcanBasicDllWrapper.PCAN_NONEBUS);
         }
     }
     catch (Exception e)
     {
         this.Logger.LogError(e);
         throw;
     }
 }
Beispiel #6
0
        ///// <summary>
        /////     Clears the message buffer.
        ///// </summary>
        ///// <param name="type">The type of message.</param>
        // public void ClearMessageBuffer(uint id, TpcanMessageType type = TpcanMessageType.PCAN_MESSAGE_STANDARD)
        // {
        // TpcanMsg readCanMsg;
        // readCanMsg.Id = 0x1B;
        // readCanMsg.Len = 8;

        // readCanMsg.Msgtype = type;

        // while (TPCANStatus.PCAN_ERROR_QRCVEMPTY
        // != PcanBasicDllWrapper.Read(this.MPcanHandle, out readCanMsg, out _))
        // {
        // // Console.WriteLine($"clear {readCanMsg.Id}");
        // this.Logger.LogTrace("read empty" + readCanMsg.Id);
        // }
        // }



        /// <summary>
        ///     Writes the can.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="data">The data info.</param>
        /// <exception cref="Exception">TPCANSTATUS not ok.</exception>
        public void WriteCan(uint id, IReadOnlyList <byte> data, TpcanMessageType tpcanMessageType = TpcanMessageType.PCAN_MESSAGE_STANDARD)
        {
            try
            {
                lock (LockWrite)
                {
                    this.Logger.LogBegin(this.GetType());

                    this.OpenCanDongle();

                    if (data.Count > 8)
                    {
                        throw new Exception("Could not write more then 8 bytes.");
                    }

                    var canMsg = new TpcanMsg();
                    canMsg.Id   = id;
                    canMsg.Data = new byte[8];
                    canMsg.Len  = (byte)data.Count;
                    for (var i = 0; i < canMsg.Len; i++)
                    {
                        canMsg.Data[i] = data[i];
                    }

                    canMsg.Msgtype = tpcanMessageType;//TpcanMessageType.PCAN_MESSAGE_STANDARD;

                    var result = PcanBasicDllWrapper.Write(this.MPcanHandle, ref canMsg);

                    if (result == TPCANStatus.PCAN_ERROR_BUSOFF)
                    {
                        this.PreparePeakCan.Reset();
                    }

                    if (result != TPCANStatus.PCAN_ERROR_OK)
                    {
                        throw new Exception("TpCanStatus.");
                    }
                }
            }
            catch (Exception ex)
            {
                this.Logger.LogError(ex);
                throw;
            }
        }