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: 54
Joined: Fri 4. Oct 2019, 09:01

Slave publish callback

Post by Ales1000 » Thu 16. Dec 2021, 14:32

Hello,

if I have configured LIN slave. Is there some callback or other function that informs immediately that SLAVE already published message? For example:

PRE: SLAVE has internal counter named cnt = 1 and added frame 0x02 corresponding the counter.
1. MASTER sends frame 0x02 with data 0x0000 to SLAVE.
2. SLAVE fills data to frame 0x02 to 0x0001. Increments counter cnt += 1 and also editting frame to 0x0002.
3. MASTER receives frame 0x02 with data 0x0001.
3. MASTER sends frame 0x02 with data 0x0000 to SLAVE.
4. SLAVE fills data to frame 0x02 to 0x0002. Increments counter cnt += 1 and also editting frame to 0x0003.
...
Thank you in advance.

Ales

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

Re: Slave publish callback

Post by M.Heidemann » Fri 17. Dec 2021, 15:00

Hello Ales,

We do not quite understand what your are proposing here,
but it seems like there are some misconcenptions on how
this would work in the context of the LIN-BUS:

The Master node is subscribed to 0x02 and will
request data from SLAVE, SLAVE will then publish
data to 0x02 which the Master will receive.

The master is not supposed to publish on the same ID on which a subscriber frame is already established.
This does not conform with the LIN-spec, doing so will basically be an error.

As 0 always wins on the LIN-Bus you could the use Master to publish on this
(subscriber)frame and force the data to be set to 0, however the checksum remains
unchanged, justifying it's existence. The Master will be able to notice discrepancies between
Data and checksum and switch to a resolve scheduler which is configured to handle such cases.

As the counter is incremented at SLAVE side in the first place, what's the purpose of breaking the frame by publishing 0?

Are you trying to test the resolve scheduler?

Please let us know.

Best Regards

Marvin
---
Marvin Heidemann
PEAK-Support Team

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

Re: Slave publish callback

Post by Ales1000 » Mon 3. Jan 2022, 09:06

Hello Marvin,

If I understood LIN well, MASTER (subscriber) sends (on the LIN bus) header and SLAVE (publisher) fills data. With this assumption I wrote, what it needs to be done. I try to simplify it from upper (user) layer and try to explain it using your terms:

PRE: SLAVE has internal counter (in my c++ app) named cnt = 1 and added frame 0x02 with data corresponding the counter (0x0001). SLAVE publishes ID=0x02 and MASTER subscribes same ID=0x02 on different channel.
1. MASTER subscribes frame 0x02.
2. SLAVE publishes frame 0x02 with data 0x0001. Increments counter cnt += 1 and also editting frame to 0x0002.
3. MASTER subscribes frame 0x02.
4. SLAVE publishes frame 0x02 with data 0x0002. Increments counter cnt += 1 and also editting frame to 0x0003.
...

Is it somehow possible to increment data of pubsliher depending on subcriber this way? I think, it should be, because it is very often used in cars, f.e. for internal message counters, inverting bits...

Thank you in advance.

Aleš

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

Re: Slave publish callback

Post by M.Heidemann » Mon 3. Jan 2022, 12:55

Hello Ales,

Your assumption is correct.

Your slave will be responsible for updating the frame that the master is subscribed to.
You master requests the frame via lin-write, gets the updated data field form the slave and needs to read the updated frame entry from its own receive Queue.

If i understood correctly, the point of concern is that the counter only increments IF the frame was requested by master? Then you'll need to read the frames from the Slaves RXqueue and see if the frame was requested again and the update the frame-data(once read the message will be "processed" and out of the buffer, if a request is in there you have not yet processed it)

The whole counter logic would be implemented on slave side.

Please see the provided examples for a proper implementation for both Lin-Master and LIN-Slave Nodes.

Best Regards

Marvin
---
Marvin Heidemann
PEAK-Support Team

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

Re: Slave publish callback

Post by Ales1000 » Mon 3. Jan 2022, 13:33

Ok, I have already implemented MASTER and SLAVE successfully, also updating frame is working, but I was not possible to read anything. Please tell me what am I doing wrong (reading code in attachment)? In this case I am not able to read any messages except some (probably) inactivity messages (ID=0).

Thank you in advance.

Aleš
Attachments
LIN_read.PNG
LIN_read.PNG (50.7 KiB) Viewed 6644 times

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

Re: Slave publish callback

Post by M.Heidemann » Mon 3. Jan 2022, 15:25

Hello,

Please note that FrameId is = Frame-ID + Parity -> protected ID, so 0x02 would be 0x42 (In regards of your comparison in line 45).

Furthermore, is this your slave or your master side?

Have you checked if your LIN-communication is functional via PLIN-View Pro yet?

Pleaae let us know.

Best Regards

Marvin
---
Marvin Heidemann
PEAK-Support Team

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

Re: Slave publish callback

Post by Ales1000 » Mon 3. Jan 2022, 15:37

Yeah, you are right, thank you. But still there were no received messages of any subscribed ID.

Of course, this is the SLAVE side. As a MASTER I use for this test PLIN-View Pro to be sure that messages are transmitted. At this moment messages viewed in PLIN-View Pro are "0x0001" which is initial value of the counter. Moreover, I used this previously as data changer, that after Sleep(), frame data was changed inside the slave. This also worked like a charm. There must be problem with calling Read().

Should I fill somehow the buffer inserted into READ() method? How the PCAN knows which channel buffer am I reading? I did'nt fill it because there was not mentioned in documentation.

Thank you in advance for your answer.
Aleš

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

Re: Slave publish callback

Post by M.Heidemann » Mon 3. Jan 2022, 16:43

Hello,

Fo your pupose your code seems mostly correct.

The only thing i do not quite get is how you assign the counter to your message...

Have you forgotten to set the Flag "FRAME_FLAG_RESPONSE_ENABLE" by any chance?

Best Regards

Marvin
---
Marvin Heidemann
PEAK-Support Team

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

Re: Slave publish callback

Post by Ales1000 » Tue 4. Jan 2022, 08:29

Hello Marvin,

here is how I set frame in my LIN test.

In "LIN_Data.PNG" is how the data is obtained. For testing purposes I have global variable which is incremented by SLAVE (dataUpdator). The integer is firstly converted to ASCII string (4 chars) and then converted to string used as BYTE vector (2 chars).

In "LIN_Main.PNG" I firstly call function "setIsLin()" which creates client (LIN_RegisterClient) in this case. Then I call "createLinChannel(int ch_num, bool is_master, bool)" which initialize LIN channel (LIN_InitializeHardware). And then I am adding frame to this HW in "LIN_Add_frame.PNG". It seems that flag FRAME_FLAG_RESPONSE_ENABLE is set.

Thank you in advance for your answer.
Aleš
Attachments
LIN_Data.PNG
LIN_Data.PNG (10.65 KiB) Viewed 6628 times
LIN_Main.PNG
LIN_Main.PNG (15.67 KiB) Viewed 6628 times
LIN_Add_frame.PNG
LIN_Add_frame.PNG (62.04 KiB) Viewed 6628 times

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

Re: Slave publish callback

Post by M.Heidemann » Tue 4. Jan 2022, 13:33

Hello,

While checking your code there are different aspects which we do not quite
understand the purpose of, so we propose the following example.

Please see the example C++ MFC in

plin-api\PLIN-API\Samples\C++_MFC

i added a counter:

Code: Select all

int counter = 0;
i made the following changes to ReadMessages():

Code: Select all

void CPLINAPIExampleDlg::ReadMessages() 
{
	TLINRcvMsg lpMsg;

	// We read at least one time the queue looking for messages.
	// If a message is found, we look again trying to find more.
	// If the queue is empty or an error occurs, we get out from
	// the dowhile statement.
	//	
	do
	{
		m_LastLINErr = m_objPLinApi->Read(m_hClient, &lpMsg);
		// If at least one Frame is received by the LinApi.
		// Check if the received frame is a standard type.
		// If it is not a standard type than ignore it.
		if (lpMsg.Type != mstStandard)
			continue;

		if (m_LastLINErr == errOK)
			if (lpMsg.FrameId == 0x42) {
				if (counter < 255) {
					counter++;
				}
				else {
					counter = 0;
				}
				BYTE data[2] = {0};
				data[0] = (BYTE)counter;
				m_LastLINErr = m_objPLinApi->UpdateByteArray(m_hClient, m_hHw, (BYTE)0x02, (BYTE)0, lpMsg.Length, &data[0]);
			}
		
			ProcessMessage(lpMsg);

	} while (btnRelease.IsWindowEnabled() && (!(m_LastLINErr & errRcvQueueEmpty)));
}
in this example the counter goes up to 255 and is assigned to Byte0, but apart from that
it does exactly what you are asking for:

PLINAPI_EXAMPLE1.png
PLINAPI_EXAMPLE1.png (45.22 KiB) Viewed 6620 times
PLINAPI_EXAMPLE2.png
PLINAPI_EXAMPLE2.png (22.31 KiB) Viewed 6620 times
PLINAPI_EXAMPLE3.png
PLINAPI_EXAMPLE3.png (53.49 KiB) Viewed 6620 times
You could check with this code if the application would also work for you,
if that is the case you should check your codebase and see if it matches the proposed
example.

Please let us know if this was useful to you.

Best Regards

Marvin
---
Marvin Heidemann
PEAK-Support Team

Locked