Interfacing PCAN USB using C++
-
- Posts: 4
- Joined: Mon 20. Jan 2014, 09:31
Interfacing PCAN USB using C++
Hi,
Can i get a sample C++ code for loading PCANBasic dll, Initializing, sending and receiving CAN message. I am having PCANBasic.dll and PCANBasicCLR.h. In PCAN forum i found one simple C code is a there a C++ code for the same available. The example in the directory is associated with the User interface i am asking for a console app. The requirements will be PCAN USB, BAUD = 500 KBps, Send a message, read a message.
Can i get a sample C++ code for loading PCANBasic dll, Initializing, sending and receiving CAN message. I am having PCANBasic.dll and PCANBasicCLR.h. In PCAN forum i found one simple C code is a there a C++ code for the same available. The example in the directory is associated with the User interface i am asking for a console app. The requirements will be PCAN USB, BAUD = 500 KBps, Send a message, read a message.
Re: Interfacing PCAN USB using C++
Hello,
the code for sending/receiving CAN messages is the same, no matter if you are using a UI or not. Simple examples in C++ can be found in the help file. Look for the section "Example" in the help of the desired function (CAN_* in PCAN-Basic Documentation/Reference/Functions) :
Initialize a channel at 500K:
Send a message:
Read a message:
Put all three together and you have what you need.
NOTE: PCANBasicCLR.h is a header to be used only in managed environments (C++/CLR). For C++ you need to use PCANBasic.h
the code for sending/receiving CAN messages is the same, no matter if you are using a UI or not. Simple examples in C++ can be found in the help file. Look for the section "Example" in the help of the desired function (CAN_* in PCAN-Basic Documentation/Reference/Functions) :
Initialize a channel at 500K:
Code: Select all
TPCANStatus result;
char strMsg[256];
// The Plug & Play Channel (PCAN-USB) is initialized
//
result = CAN_Initialize(PCAN_USBBUS1,PCAN_BAUD_500K);
if(result != PCAN_ERROR_OK)
{
// An error occurred, get a text describing the error and show it
//
CAN_GetErrorText(result, 0, strMsg);
MessageBox(strMsg);
}
else
MessageBox("PCAN-USB (Ch-1) was initialized");
Code: Select all
TPCANMsg msg;
TPCANStatus result;
char strMsg[256];
// A CAN message is configured
//
msg.ID = 0x100;
msg.MSGTYPE = PCAN_MESSAGE_STANDARD;
msg.LEN = 3;
msg.DATA[0] = 1;
msg.DATA[1] = 2;
msg.DATA[2] = 3;
// The message is sent using the PCAN-USB Channel 1
//
result = CAN_Write(PCAN_USBBUS1, &msg);
if(result != PCAN_ERROR_OK)
{
// An error occurred, get a text describing the error and show it
//
CAN_GetErrorText(result, 0, strMsg);
MessageBox(strMsg);
}
else
MessageBox("Message sent successfully");
Code: Select all
TPCANMsg msg;
TPCANTimestamp timestamp;
TPCANStatus result;
char strMsg[256];
do
{
// Check the receive queue for new messages
//
result = CAN_Read(PCAN_USBBUS1,&msg,×tamp);
if(result != PCAN_ERROR_QRCVEMPTY)
{
// Process the received message
//
MessageBox("A message was received");
ProcessMessage(msg)
}
else
{
// An error occurred, get a text describing the error and show it
// and handle the error
//
CAN_GetErrorText(result, 0, strMsg);
MessageBox(strMsg);
// Here can be decided if the loop has to be terminated (eg. the bus
// status is bus-off)
//
HandleReadError(result);
}
// Try to read a message from the receive queue of the PCAN-USB, Channel 1,
// until the queue is empty
//
}while((result & PCAN_ERROR_QRCVEMPTY) != PCAN_ERROR_QRCVEMPTY);
NOTE: PCANBasicCLR.h is a header to be used only in managed environments (C++/CLR). For C++ you need to use PCANBasic.h
Best regards,
Keneth
Keneth
-
- Posts: 4
- Joined: Mon 20. Jan 2014, 09:31
Re: Interfacing PCAN USB using C++
Thank you, That worked fine.
While reading the CAN messages continuously and processing i am getting a lot of delay. When a value change happens in a message the effect comes on my code after some time, why is this happening? Do i need to clear any buffer i didn't see any other API calls other than.Is there any event triggered method other than polling.
An another query is regarding Filtering of messages.If i want to filter multiple messages which are not in order say i have to filter ten messages, is it OK call ten times.
If i need to a filter a particular ID say 0x265, can i give the call like this.
the start and end of the sequence with same id.
While reading the CAN messages continuously and processing i am getting a lot of delay. When a value change happens in a message the effect comes on my code after some time, why is this happening? Do i need to clear any buffer i didn't see any other API calls other than
Code: Select all
PCAN_Read
An another query is regarding Filtering of messages.If i want to filter multiple messages which are not in order say i have to filter ten messages, is it OK call
Code: Select all
g_CAN_FilterMessages
If i need to a filter a particular ID say 0x265, can i give the call like this.
Code: Select all
CANStatus = g_CAN_FilterMessages(g_hChannel, 0x265, 0x265, PCAN_MESSAGE_STANDARD);
- PEAK-Support
- Sales & Support
- Posts: 1646
- Joined: Fri 10. Sep 2010, 19:34
Re: Interfacing PCAN USB using C++
please take a lock in the API description. There you will find all answers to your questions.
We also have a very good documented sample code that alsow shows all types of sending and receiving.
The Docu is part of the PCAN-Basic API package: LINK Please spend some time with study this document (Windows Help File)
We also have a very good documented sample code that alsow shows all types of sending and receiving.
The Docu is part of the PCAN-Basic API package: LINK Please spend some time with study this document (Windows Help File)
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------
-
- Posts: 5
- Joined: Sat 29. Apr 2017, 10:24
Re: Interfacing PCAN USB using C++
Hi
I want to ask what's the header file I should include to use this C++ program. I tried but still getting errors in ProcessMessage function and HandleReadError. also I'm getting link2019 error in visual studio and that related o Can Read. I think I have to include not just PCANBasic.h. I'm using Peak- PCI, but I think I can use the same functions.
I need help please
Thank you
I want to ask what's the header file I should include to use this C++ program. I tried but still getting errors in ProcessMessage function and HandleReadError. also I'm getting link2019 error in visual studio and that related o Can Read. I think I have to include not just PCANBasic.h. I'm using Peak- PCI, but I think I can use the same functions.
I need help please
Thank you
Re: Interfacing PCAN USB using C++
Hello,
if you are using native C, or C++, then you have to use the header file PCANBasic.h
Otherwise, if oyu are using Visual C++/CLR (managed C++), then you need to include the file PCANBasicCLR.h
Note that you have project samples for both cases, so feel free to open them and check which one fits better for you.
if you are using native C, or C++, then you have to use the header file PCANBasic.h
Otherwise, if oyu are using Visual C++/CLR (managed C++), then you need to include the file PCANBasicCLR.h
Note that you have project samples for both cases, so feel free to open them and check which one fits better for you.
Best regards,
Keneth
Keneth
-
- Posts: 5
- Joined: Sat 29. Apr 2017, 10:24
Re: Interfacing PCAN USB using C++
Hi
Thank you for your reply K.Wagner. I'm using native C++. I've already include the header file PCANBasic.h in my program but the problem I'm still have this lnk2019 error (Error LNK2019 unresolved external symbol _CAN_Read@12 referenced in function _main
Error LNK2019 unresolved external symbol _CAN_Initialize@20
)
This is my program:
I tried to search and solve my errors but I couldn't. I've already have a look to the samples but all of them is not a native C++.
Please, I need help.
Thank you
Thank you for your reply K.Wagner. I'm using native C++. I've already include the header file PCANBasic.h in my program but the problem I'm still have this lnk2019 error (Error LNK2019 unresolved external symbol _CAN_Read@12 referenced in function _main
Error LNK2019 unresolved external symbol _CAN_Initialize@20
)
This is my program:
Code: Select all
#include "stdafx.h"
#include "iostream"
#include <stdlib.h>
#include "windows.h"
# include "PCANBasic.h"
//#define _WIN32_WINNT 0x0500
using namespace std;
int ProcessMessage(TPCANMsg msg) {
return 0;
}
int HandleReadError(TPCANStatus result) {
return 0;
}
int main()
{
TPCANMsg msg;
TPCANStatus result;
char strMsg[256];
TPCANTimestamp timestamp;
// The Plug & Play Channel (PCAN-USB) is initialized
//
result = CAN_Initialize(PCAN_USBBUS1, PCAN_BAUD_500K);
if (result != PCAN_ERROR_OK)
{
// An error occurred, get a text describing the error and show it
//
CAN_GetErrorText(result, 0, strMsg);
std::wcout<<(strMsg);
}
else
std::wcout<<("PCAN-USB (Ch-1) was initialized");
// sending the message
// A CAN message is configured
//
msg.ID = 0x701;
msg.MSGTYPE = PCAN_MESSAGE_STANDARD;
msg.LEN = 3;
msg.DATA[0] = 1;
msg.DATA[1] = 2;
msg.DATA[2] = 3;
// receiving the message
do
{
// Check the receive queue for new messages
//
result = CAN_Read(PCAN_USBBUS1, &msg, ×tamp);
if (result != PCAN_ERROR_QRCVEMPTY)
{
// Process the received message
//
std::wcout<<("A message was received");
ProcessMessage(msg);
}
else
{
// An error occurred, get a text describing the error and show it
// and handle the error
//
CAN_GetErrorText(result, 0, strMsg);
std::wcout << (strMsg);
// Here can be decided if the loop has to be terminated (eg. the bus
// status is bus-off)
//
HandleReadError(result);
}
// Try to read a message from the receive queue of the PCAN-USB, Channel 1,
// until the queue is empty
//
} while ((result & PCAN_ERROR_QRCVEMPTY) != PCAN_ERROR_QRCVEMPTY);
return 0;
}
Please, I need help.
Thank you
Re: Interfacing PCAN USB using C++
Hello,
about my last post, I mean the C++/MFC example. This is not a simple native C++ example, though the use of the PCAN-Basic library in this project is the same as in a native C++ application.
Nevertheless, your problem is actually the linkage to the library. Did you use already DLLs within a C++ application? if not, then take a look here for more information: About Dynamic-Link Libraries.
If you deside to use the load-time dynamic linking, tehn you will find the needed *.lib files for each platform within the PCAN-Basic packages you have downloaded (32-Bit: Win32\VC_LIB, 64-Bit: x64\VC_LIB).
about my last post, I mean the C++/MFC example. This is not a simple native C++ example, though the use of the PCAN-Basic library in this project is the same as in a native C++ application.
Nevertheless, your problem is actually the linkage to the library. Did you use already DLLs within a C++ application? if not, then take a look here for more information: About Dynamic-Link Libraries.
If you deside to use the load-time dynamic linking, tehn you will find the needed *.lib files for each platform within the PCAN-Basic packages you have downloaded (32-Bit: Win32\VC_LIB, 64-Bit: x64\VC_LIB).
Best regards,
Keneth
Keneth
-
- Posts: 5
- Joined: Sat 29. Apr 2017, 10:24
Re: Interfacing PCAN USB using C++
Thank you K.Wagner for your help and support.
-
- Posts: 5
- Joined: Sat 29. Apr 2017, 10:24
Re: Interfacing PCAN USB using C++
Hi K.Wagner
I want to ask if you know what's the function of the ProcessMessage and HandleReadError in native c++. Do you know how to write these function in the simple c++.
Thank you
I want to ask if you know what's the function of the ProcessMessage and HandleReadError in native c++. Do you know how to write these function in the simple c++.
Thank you