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;
}
}