Peak.Lin.TLINMsgErrors.IdP when Calling Read()

The free LIN software API (Application Programming Interface) for Windows® (only for usage with the PCAN-USB Pro CAN/LIN interface)
Post Reply
colnewman
Posts: 8
Joined: Fri 28. Oct 2016, 19:42

Peak.Lin.TLINMsgErrors.IdP when Calling Read()

Post by colnewman » Thu 3. Nov 2016, 20:07

I've written a wrapper in C# to go around PLinApi.

I keep getting Peak.Lin.TLINMsgErrors.IdParityBit1 | Peak.Lin.TLINMsgErrors.SlaveNOtResponding returned from PLinApi.Read.

Not sure what IdParityBit is.

Code: Select all

public class PeakLIN_Adapter : IAdapter
    {
        private ushort handle = 1;
        byte hClient;
        private const ulong FRAME_FILTER_MASK = 0xFFFFFFFFFFFFFFFF;

        private const TLINHardwareMode HardwareMode = TLINHardwareMode.modMaster;
        private const int BaudRate = 9600;

        public void Initialize()
        {
            
            var errorClient = PLinApi.RegisterClient("LIN API", (IntPtr)0, out hClient);
            if(errorClient == TLINError.errOK)
            {
                if (HardwareAvailable(1))
                {
                    //ClearClientsFromHardware();
                    var errorInit = PLinApi.InitializeHardware(hClient, handle, HardwareMode, BaudRate);
                    if (errorInit == TLINError.errOK)
                    {
                        var errorConnect = PLinApi.ConnectClient(hClient, handle);                       
                        if (errorConnect == TLINError.errOK)
                        {
                            var errorClientFIlter = PLinApi.SetClientFilter(hClient, handle, FRAME_FILTER_MASK);
                            if(errorClientFIlter != TLINError.errOK)
                            {
                                //** fix me throw exception
                            }

                        }
                        else
                        {
                            //** fix me throw exception
                        }
                    }
                    else
                    {
                        //** fix me throw exception
                    }

                }
                else
                {
                    //** fix me throw exception
                }
            }
            else
            {
                //** fix me throw exception
            }


        }

        public void Write(byte id, byte[] data)
        {
            TLINMsg msg = new TLINMsg {FrameId = id, Data = data, ChecksumType = TLINChecksumType.cstAuto, Length = (byte)data.Length};
            Peak.Lin.PLinApi.CalculateChecksum(ref msg);


            PLinApi.Write(hClient, handle, ref msg);

        }

        public bool Query(byte id, out byte[] data, byte NumberOfBytesToFetch)
        {
            

            TLINMsg msgSend = new TLINMsg { FrameId = id, ChecksumType = TLINChecksumType.cstAuto, Length = NumberOfBytesToFetch, Direction = TLINDirection.dirSubscriberAutoLength};
            PLinApi.CalculateChecksum(ref msgSend);
            var errWrite = PLinApi.Write(hClient, handle, ref msgSend);

            if(errWrite != TLINError.errOK)
            {
                //** fix me throw exception
            }

            TLINRcvMsg msgRcv;

            var run = true;
            TLINError errRead;
             
            do
            {
                errRead = PLinApi.Read(hClient, out msgRcv);
                data = msgRcv.Data;
                if (msgRcv.Type != TLINMsgType.mstStandard)
                {
                    continue;
                }
                if(msgRcv.ErrorFlags != 0)
                {
                    Log.Logger.Warning("{Class}.{Method} : Message Error Flag {flag}", nameof(PeakLIN_Adapter), nameof(Query),msgRcv.ErrorFlags);
                    
                    return false;
                }

                if (errRead == TLINError.errRcvQueueEmpty)
                {
                    return false;
                }
                else if(errRead == TLINError.errOK)
                {
                    break;
                }
                else
                {
                    //** fix me throw exception
                }

                
            }
            while (run);

            if(errRead != TLINError.errOK)
            {
                //** fix me throw exception
            }


            data = msgRcv.Data;
            return true;
        }

        private bool IsInitilized()
        {
            int baudRate = 0;
            PLinApi.GetHardwareParam(handle, TLINHardwareParam.hwpBaudrate, out baudRate, 1);

            int mode;

            PLinApi.GetHardwareParam(handle, TLINHardwareParam.hwpMode, out mode, 1);

            if ((TLINHardwareMode)mode == HardwareMode && baudRate == BaudRate)
            {
                return true;
            }
            return false;

        }

        private void ClearClientsFromHardware()
        {
            byte[] Buffer = new byte[255];

            PLinApi.GetHardwareParam(handle, TLINHardwareParam.hwpConnectedClients, Buffer, 255);

            foreach(var blah in Buffer)
            {
                if(blah == 0)
                {
                    continue;
                }
                PLinApi.RemoveClient(blah);
                // PLinApi.GetHardwareParam(handle, TLINHardwareParam.hwpConnectedClients, Buffer, 255);
            }
        }

        private bool HardwareAvailable(ushort target)
        {
            ushort []buff = new ushort[4096];
            ushort wBuffSize = 4096;
            ushort count;

            var error = PLinApi.GetAvailableHardware(buff, wBuffSize, out count);

            if(error != TLINError.errOK)
            {

            }

            var IsAvailable = buff.Any(x => x == target);

            return IsAvailable;

        }


    }
ETA fixed my first problem about initialization I need to call if after I connected, not before. Still have the second problem though.

K.Wagner
Software Development
Software Development
Posts: 732
Joined: Wed 22. Sep 2010, 13:36

Re: Peak.Lin.TLINMsgErrors.IdP when Calling Read()

Post by K.Wagner » Fri 4. Nov 2016, 08:51

Hello,
colnewman wrote:I keep getting Peak.Lin.TLINMsgErrors.IdParityBit1 | Peak.Lin.TLINMsgErrors.SlaveNOtResponding returned from PLinApi.Read.
About IdParityBit1
I cannot see which ID you are using, but I guess you are using as ID a number between 0 and 63. Note that you have to use the ID with Parity. In order to calculate the parity of an ID you need to call the function PLinApi.GetPID() on the Frame ID. This means for example, if you want to sent the ID 0, you actually need to fill the ID member of the frame with 128.

About SlaveNotResponding
Since the ID is not recognized by your slave, it doesn't respond, so you get also the error "Slave not responding". Note that this error is also received when there is no slave at all to communicate with, and also if you use the PCAN-USB Pro FD without power supply (12 volts).
colnewman wrote:ETA fixed my first problem about initialization I need to call if after I connected, not before. Still have the second problem though.
Sorry but I don't understand this sentence.
Best regards,
Keneth

colnewman
Posts: 8
Joined: Fri 28. Oct 2016, 19:42

Re: Peak.Lin.TLINMsgErrors.IdP when Calling Read()

Post by colnewman » Thu 1. Dec 2016, 20:52

K.Wagner wrote:Hello,
colnewman wrote:I keep getting Peak.Lin.TLINMsgErrors.IdParityBit1 | Peak.Lin.TLINMsgErrors.SlaveNOtResponding returned from PLinApi.Read.
About IdParityBit1
I cannot see which ID you are using, but I guess you are using as ID a number between 0 and 63. Note that you have to use the ID with Parity. In order to calculate the parity of an ID you need to call the function PLinApi.GetPID() on the Frame ID. This means for example, if you want to sent the ID 0, you actually need to fill the ID member of the frame with 128.

About SlaveNotResponding
Since the ID is not recognized by your slave, it doesn't respond, so you get also the error "Slave not responding". Note that this error is also received when there is no slave at all to communicate with, and also if you use the PCAN-USB Pro FD without power supply (12 volts).
Sorry for not responding I have just recently gotten back to this project. That worked, Thank you. It also solved another problem I ha,d which was that my publisher messages didn't seem to be working writing to my slave.

K.Wagner wrote:
colnewman wrote:ETA fixed my first problem about initialization I need to call if after I connected, not before. Still have the second problem though.
Sorry but I don't understand this sentence.
Sorry, It would only mean something if you saw my original post before I edited.

Post Reply