I am a C# Software Developer and I am having some issues when trying to combine XCP and CAN features in the same port in PCAN-USB Pro FD device. I use the non-free PCAN-API 4 libraries which, theoretically, support this combination.
What I am doing is:
TXCPResult result = XCPApi.InitializeCanChannel(out _XcpChannel, PCANBasic.PCAN_USBBUS1, TPCANBaudrate.PCAN_BAUD_500K); --> Returns "TXCPResult.XCP_ERR_OK"
StatusErrors status = CanApi4.RegisterClient(PcanDevice.Usb, "Port" + 16, 0, out byte client); --> Returns "StatusErrors.Ok"
status = CanApi4.ConnectToHardware(PcanDevice.Usb, client, _comm_parameters, ref _nethandle); --> Returns "StatusErrors.HardwareInUse"
_comm_parameters = "hw_handle=16, f_clock=40000000, nom_brp=5, nom_tseg1=11, nom_tseg2=4, nom_sjw=1" --> which sould mean using USBBUS1 with 500K baudrate speed.
If I try to execute these instructions in reverse order, the CanApi4 instructions return "StatusErrors.Ok" but the XCPApi instruction returns "TXCPResult.XCP_ERROR_INVALID_HANDLE".
Maybe I am doing something wrong (or maybe I am not doing something ). I would appreciate a lot if someone can give me some support so I can solve this issue.
Thanks a lot in advance!
Note that the bitrates used in XCP when initializing it thought the function
Code: Select all
Try using a frequency of 24 MHz for your "_comm_parameters " bitrate string, for instance:
Code: Select all
f_clock=24000000, nom_brp=3, nom_tseg1=13, nom_tseg2=2, nom_sjw=1
Following your recommendations, I have used the Bit Rate Calculation Tool and I have used the following "_comm_parameters" combinations:
- "hw_handle=16, f_clock=24000000, nom_brp=3, nom_tseg1=13, nom_tseg2=2, nom_sjw=1".
- "hw_handle=16, f_clock=24000000, nom_brp=3, nom_tseg1=13, nom_tseg2=2, nom_sjw=2"
- "hw_handle=16, f_clock=80000000, nom_brp=10, nom_tseg1=13, nom_tseg2=2, nom_sjw=2"
- "hw_handle=16, f_clock=8000000, nom_brp=1, nom_tseg1=13, nom_tseg2=2, nom_sjw=1"
I have tried all these "_comm_parameters" combinations with the same results commented in the first post. This is:
TXCPResult result = XCPApi.InitializeCanChannel(out _XcpChannel, PCANBasic.PCAN_USBBUS1, TPCANBaudrate.PCAN_BAUD_500K); --> Returns: TXCPResult.XCP_ERR_OK
StatusErrors status = CanApi4.RegisterClient(PcanDevice.Usb, "Port" + 16, 0, out byte client); --> Returns: StatusErrors.Ok
status = CanApi4.ConnectToHardware(PcanDevice.Usb, client, _comm_parameters, ref _nethandle); --> Returns: StatusErrors.HardwareInUse
All _comm_parameters commented above have the same Nominal Sample Point (87,5%) and Nominal Bit Duration (16).
Thank you for your attention!
I have to admit, I concentrated on the bitrate and missed to say something important regarding the function CanApi4.ConnectToHardware. This function always tries to create a net for the conenction so that, indifferently from the bitrate used, it will fail if the hardware is already connected by any other application.
Trying to connect first your application with a call to CanApi4.ConnectToHardware as you posted will also not work, because the created net is not compatible with those created by PCAN-Basic (API used for CAN communication in XCP). For this reason, the call to XCPApi.InitializeCanChannel will also fail, as you already noted.
I see two ways to achieve what you want:
- Connect first the XCP application, and then use CanApi4.ConnectToNet instead of CanApi4.ConnectToHardware, to connect to the net created by the XCP application. You can see the name by using the PCAN Status Display Tool, or
- connect first your CanApi4 application, but give as parameter the name for the net that will be created. Be sure the net name is the same as that used by the XCP connection. You can also pass the bitrate as BTR0BTR1 for simplicity, for instance: "name=NetName,hw_handle=16, btr0btr1=0x1C" (500 kBit/s, samp. 87,5%, as used by PCAN-Basic).
Wether if I use option a) or b) I need PCAN Status Tool software to obtain XCP's created netname. I have tried several random netnames with no success (I also tried "NetName" ).
Maybe I am wrong, but I am not able to download this software in the web site. I can find several references to this program, but I have not seen any download link.
I tried to retrieve this netname created by XCP using PCAN-View but it is only possible to see that the channel is already used without additional info.
Thanks a lot for your fast and useful answers.
You are using CanApi4.dll which is part of the professional API, PCAN-Developer 4, that is tied to a license (not for free). This package contains the mentioned tool. Please send us an email to support[at]peak-system.com with your license number and making mention to this topic. Otherwise we will not be able to help on this anymore.
Thanks for your understanding.
At the moment I wrote the original post, I haven’t installed all the package included in the flash memory stick brought with the non-free license. I had only downloaded the API from this website. That’s why I wasn’t able to find PCAN Status Tool on my computer.
With PCAN Status Tool I was able to see netNames and its naming rules which are related to the USB port involved.
Just to let you know, related to baudrates, I found out that communication parameters (prescalers, tseg1, tseg2, sjw...) of the baudrates included by default in PCAN-View were the ones that worked when trying to combine CAN (non-FD) with XCP. For non-FD communication, I used them instead of the ones shown in Bit Rate Calculation Tool.
These communication parameters, along with the netname knowledge, allowed me to solve my initialization issues.
Thanks a lot for your information and guidelines!
glad to read that the problem is solved.
Regarding the Bitrate values in PCAN-View: yes, you are right. PCAN-View has default bitrates that pass for all devices, also those not being FD capable (SJA1000 register). This is mostly for backward compatibility. I missed to tell you this.
Regarding the Bitrate Tool: this tool does calculate the bitrate in 3 different modes, "CAN Bitrate", "CAN-FD Bitrate" and "SJA1000 Bitrate". The calculation algorithms are different, so in several cases the SJW value doesn't fit when calculating a SJA1000 value with other frequency than 8MHz. For example, here are the values for a 500 kBits as SJA1000 Bitrate (left) and as CAN bitrate at 24 Mhz (right):
For this reason the bitrate returned by the tool is not working in your case. Changing the SJW to 1 solves the problem. A workaround is to calculate the SJA1000 values and multiply the SJA1000 Prescaler by 3 (frequency of 24MHz / 8). For example, the prescaler of a SJA1000 250 kBit bitrate (011Ch) is 2. When using 24 MHz, it would be needed to change it to 6.
But I admit, the tool should include these standard values too to avoid misunderstandings. We will check the tool for this. Thanks for bringing this to our attention.
This topic will be closed. Feel free to open another one if you need additional help.