Slave publish callback

The free LIN software API (Application Programming Interface) for Windows® (only for usage with the PCAN-USB Pro CAN/LIN interface)
Ales1000
Posts: 45
Joined: Fri 4. Oct 2019, 09:01

Re: Slave publish callback

Post by Ales1000 » Tue 4. Jan 2022, 15:30

Seems, that example is working. Now I don't understand what's wrong. I try to look the code in more detail. I will let you know.

Aleš

Ales1000
Posts: 45
Joined: Fri 4. Oct 2019, 09:01

Re: Slave publish callback

Post by Ales1000 » Wed 5. Jan 2022, 10:22

Hello Marvin,

unfortunately still don't know whats wrong. Can you please tell me? I simplified code, it should be now deeply analyzable.

The output when several message came (received only Bus sleep messages):
OK! Registration of client. success.
OK! Get HWs count success.
OK! Get HW list success.
OK! Connect HW to client success.
OK! Init HW success.
OK! Adding LIN frame success.
OK! Getting connected HWs success.
Connected 1 HWs.
1_HW HANDLE=1
0 0 type=1

OK! Getting received msgs success.
Received 0
Updator finished!
OK! Delete lin channel success.
OK! Delete client success.
ENDED

Code: Select all

int handleRet(DWORD ret, std::string msg) {
	if (ret == 0) cout << "OK! " + msg + " success.\n";
	else cout << "ERROR! " + msg + " failed (err_code=" + toString(ret) + ").\n";
	return ret;
}

std::atomic<int> counter = 1;
std::atomic<bool> quit = false;

std::string getNewData() {
	std::string data = toString(counter);
	std::string remaining = std::string(4 - data.size(), ' ');
	return remaining.append(data);
}

HLINCLIENT cli;
HLINHW hw_pointer;

void simpleUpdator() {
	//Check if connected HWs
	HLINHW arr[5];
	USHORT arr_sz = sizeof(arr) / sizeof(arr[0]);
	if (handleRet(LIN_GetClientParam(cli, clpConnectedHardware, arr, arr_sz), "Getting connected HWs") == errOK) {
		int hw_count = (int)arr[0];
		cout << "Connected " << hw_count << " HWs.\n";
		for (int i = 1; i < hw_count + 1; i++) {
			cout << i << "_HW HANDLE=" << (int)arr[i] << endl;
		}
	}

	while (quit == false) {
		TLINRcvMsg r;
		TLINError err = LIN_Read(cli, &r);
		if (err == errOK)cout << err << " " << (int)r.FrameId << " type=" << (int)r.Type << endl;
		if (err == errOK && r.Type == mstStandard && r.hHw == hw_pointer && r.FrameId == 0x42) {
			std::string new_data = stringToCharArrString(getNewData());
			BYTE arr[2] = { 0 };
			for (size_t i = 0; i < 2; i++)arr[i] = new_data[i];
			DWORD ret = LIN_UpdateByteArray(cli, hw_pointer, 0x02, 0, 2, arr);
			if (ret != 0) std::cout << "ERROR! Updating field failed, returned=\"" << ret << "\"." << endl;
			counter++;
		}
		Sleep(1);
	}

	int rec_msgs = 0;
	if (handleRet(LIN_GetClientParam(cli, clpReceivedMessages, &rec_msgs, sizeof(rec_msgs)), "Getting received msgs") == errOK) {
		cout << "Received " << rec_msgs << endl;
	}

	cout << "Updator finished!" << endl;
}

int main()
{
	LPSTR buff = new CHAR[5]{ 'C','L','I','\n',0 };
	handleRet(LIN_RegisterClient(buff, 0, &cli), "Registration of client.");
	delete[] buff;

	//Connect to channel
	HLINHW *li = nullptr;
	int list_count = 0;
	int ch_count = 0;
	TLINError ret = handleRet(LIN_GetAvailableHardware(NULL, 0, &ch_count), "Get HWs count"); //Get connected HWs
	if (ret == errOK) {
		HLINHW * list = new HLINHW[ch_count];
		ret = handleRet(LIN_GetAvailableHardware(list, ch_count * sizeof(HLINHW), &ch_count), "Get HW list");
		if (ret == errOK) {
			hw_pointer = list[0]; //Channel 1
			ret = handleRet(LIN_ConnectClient(cli, hw_pointer), "Connect HW to client");
			if (ret == errOK) {
				ret = handleRet(LIN_InitializeHardware(cli, hw_pointer, modSlave, 19200), "Init HW");
			}
		}
		delete[] list;
	}

	//Adding message ID=0x02
	std::string doubleHexString = stringToCharArrString(getNewData());
	size_t sz = doubleHexString.size();
	TLINFrameEntry frame;
	frame.FrameId = 0x02;
	frame.Length = (BYTE)sz;
	frame.Direction = dirPublisher;
	for (size_t i = 0; i < sz; i++) frame.InitialData[i] = (int)doubleHexString[i];
	frame.ChecksumType = cstEnhanced;
	frame.Flags = FRAME_FLAG_RESPONSE_ENABLE;
	handleRet(LIN_SetFrameEntry(cli, hw_pointer, &frame), "Adding LIN frame");

	//Adding SLAVE thread
	std::thread t(simpleUpdator);
	getchar();
	quit = true;
	t.join();

	//Delete when SLAVE ends
	handleRet(LIN_DisconnectClient(cli, hw_pointer), "Delete lin channel");
	handleRet(LIN_RemoveClient(cli), "Delete client");
	
	cout << "ENDED" << endl;
}

M.Heidemann
Sales & Support
Sales & Support
Posts: 786
Joined: Fri 20. Sep 2019, 13:31

Re: Slave publish callback

Post by M.Heidemann » Wed 5. Jan 2022, 11:46

Hello Ales,

Because your client-filter is closed.

I don't see the filter being set anywhere, i guess you have forgotten it:
SetClientFilter.png
SetClientFilter.png (103.92 KiB) Viewed 571 times
In the example the filter is automatically set to be open,
see the usage of it here:

Code: Select all

// Set the client filter with the mask.
m_LastLINErr = m_objPLinApi->SetClientFilter(m_hClient, m_hHw, m_lMask);
FilterMask.png
FilterMask.png (5.88 KiB) Viewed 571 times
Set the ClientFilter and see if that resolves it.

Best Regards

Marv

Ales1000
Posts: 45
Joined: Fri 4. Oct 2019, 09:01

Re: Slave publish callback

Post by Ales1000 » Wed 5. Jan 2022, 12:30

Ok, thank you Marvin, this was the problem. I didn't know that filter is closed by default (assumed as in CAN, where it is openned by default).

I also saw, that it is possibility to have READ events as in PCAN-Basic API probably getting clpOnReceiveEventHandle param to LIN_GetClientParam function is obtained handle for receive callback, right? What else is it necessary to manage?

Thank you in advance for your answer.

Aleš

M.Heidemann
Sales & Support
Sales & Support
Posts: 786
Joined: Fri 20. Sep 2019, 13:31

Re: Slave publish callback

Post by M.Heidemann » Wed 5. Jan 2022, 13:30

Hello Ales,

The same approach as in PCANBasic is taken when the ReceiveEvent is used in the PLIN API,
you can check the implementation of it in the PCANBasic examples
as a introduction for it.

Best Regards

Marvin

Ales1000
Posts: 45
Joined: Fri 4. Oct 2019, 09:01

Re: Slave publish callback

Post by Ales1000 » Wed 5. Jan 2022, 14:54

Ok, now the reading using event is also working. It's all from my side.

Thank you Marvin.

Aleš

Locked