Professional CAN and CAN FD Development Package for Windows®
-
Krishna.skt
- Posts: 21
- Joined: Mon 6. Apr 2020, 15:33
Post
by Krishna.skt » Mon 1. Feb 2021, 17:35
Hello there,
I have just started using PCAN Developer 4 and was going through its examples. Is there any way to wait for some time while transmitting the number of messages to the connected client, as "Wait" instruction in PCAN Explorer 6?
Code: Select all
// Sends 5 standard messages over a client with handle 1.
//
StatusErrors status;
CommonObjectInfo[] toSend = new CommonObjectInfo[5];
UInt32 bytesWritten = 0;
for (int i = 0; i < 5; i++)
{
toSend[i] = new DataFrame();
DataFrame frame = toSend[i] as DataFrame;
frame.Timestamp = 0; // Message will be sent as soon as possible.
frame.Tag = 0; // Not used in this example.
frame.ClientHandle = 1; // Client with handle 1
frame.NetHandle = 2; // Net with handle 2
frame.MessageType = MessageTypes.Standard; // Standard 11-bit message
frame.ID = (uint)(0x100 + i); // CAN ID 0x100 to 0x104
frame.DLC = 8; // 8 data bytes
for (int j = 0; j < 8; j++)
frame.Data[j] = (byte)((i * 16) + j);
}
status = CanApi4.Write(PcanDevice.Usb, toSend, ref bytesWritten);
if (status != StatusErrors.Ok)
{
CanApi4.GetStatusText(status, out string errorText);
Console.WriteLine(String.Format("CanApi4.Write failed. Error: 0x{0,8:X} ({1})\n",
status, errorText));
}
I used this method to write the CAN messages but could not find a way to wait some time after transmitting each message. The transmitting process takes place at such a speed that my client could not respond. That is why I need to wait after transmitting each message. I also tried incrementing the Timestamp value, but still, it's not working. Any help would be highly appreciated.
Best regards,
Krishna
-
PEAK-Support
- Sales & Support

- Posts: 1646
- Joined: Fri 10. Sep 2010, 19:34
Post
by PEAK-Support » Tue 2. Feb 2021, 12:46
Keep in mind that CAN is no real time system - it need some time until the CAN Frame is on the BUS , ACK and received by the other nodes.
If some CAN node then answer, it also take some time to send, ACK and the will be pushed into the incomming queue of your Clinet.
Waiting the whole time in a main loop is the wrong way. Yoe need to setup a state machine and use seperate (event driven) threads for reading,
so that you do not block your main application.
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------
-
Krishna.skt
- Posts: 21
- Joined: Mon 6. Apr 2020, 15:33
Post
by Krishna.skt » Tue 2. Feb 2021, 19:17
Hello Wilhelm,
Thank you for your response. I got your point but I am in a situation where I need to transmit lots of messages to my client and every time I need to wait a few milliseconds before transmitting the next message. While working with PCAN Explorer I used wait instruction and it was working perfectly fine, but this time messages to the client are transmitting at a very high speed, because of which my client is not responding as I needed.
So, I just wanted to know if there is any way to wait between the messages while transmitting, following the same method attached in my last post.
Looking forward to your reply.
Best regards
Krishna
-
PEAK-Support
- Sales & Support

- Posts: 1646
- Joined: Fri 10. Sep 2010, 19:34
Post
by PEAK-Support » Wed 3. Feb 2021, 09:45
If you do not care that your main loop stoks, you simply loop in a ReadCAN until your needed CAN-Msg was received or a timeout (max. delay that you will accept ) is reached.
To prevent a nearly 100% CPU load, simply add a "sleep(1)" in your loop.
Here a pseudo C code sample...
Wait for the ID "MyNeeded_CAN_ID" with a timeout aoff 100ms
Code: Select all
StartTickCounter=GetTickCount();
received = false;
do{
// CAN Read calling and store all Information inside the CANArray
i=g_CAN_Read(CANChannel, &CANRecvMsg, &CANTimestamp);
if(i==PCAN_ERROR_OK){ // A CAN Message was read
//Maybe better check here also the Msg.Type to be sure the Message is a real CAN Frame...
// and check if it is the ID you are waitung for
if(CANRecvMsg.ID== MyNeeded_CAN_ID) // yes - thats what we wait for...
{
received=true;
// do what ever you need to do ...
CANArray[0]=CANRecvMsg.LEN;
CANArray[1]=CANRecvMsg.MSGTYPE;
for(i=0;i<CANRecvMsg.LEN;i++)
CANArray[2+i]=CANRecvMsg.DATA[i];
}
else
{
//break;
}
}else // If no CAN Frame received - we wait for 1ms
Sleep(1); //wait 1 ms ..if possible
TickCounter=GetTickCount();
}
while( ((TickCounter-StartTickCounter)<100 && !received);
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------