How to connect to a net

The free CAN Software API (Application Programming Interface) for Windows®
Post Reply
Thomas1337
Posts: 3
Joined: Mon 28. Apr 2014, 14:41

How to connect to a net

Post by Thomas1337 » Mon 28. Apr 2014, 15:04

Hi,
I am currently trying to connect to a net via an USB PCAN adapter.
I can already receive data with the PCAN View program. But the following code snippet doesnt work.
It says that the PCAN Hardware is used by another PCAN net. If I change the channel to PCAN_USBBUS2,
it says that the handle isnt valid. The other USB channels dont work either.
Oddly, it seems to work when closing PCAN View (using PCAN_USBBUS1). I thought it is possible for a net
to serve more than one client (so that both clients can send and receive CAN messages). Is there anything wrong with the connection?

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Peak.Can.Basic;
namespace CANlayer
{
    class Program
    {
        static void Main(string[] args)
        {            
            var channel =PCANBasic.PCAN_USBBUS1;
            var result=PCANBasic.Initialize(channel, TPCANBaudrate.PCAN_BAUD_125K);
            if (result != TPCANStatus.PCAN_ERROR_OK)
            {               
                var strMsg = new StringBuilder(256);
                PCANBasic.GetErrorText(result, 0, strMsg);
                Console.WriteLine(strMsg.ToString());
            }

            while (!Console.KeyAvailable)
            {
                TPCANMsg msg;
                TPCANStatus rresult = PCANBasic.Read(channel, out msg);

                if (rresult == TPCANStatus.PCAN_ERROR_OK)
                {
                    Console.WriteLine(BitConverter.ToString(msg.DATA));
                }
                else {
                    var strMsg = new StringBuilder(256);
                    PCANBasic.GetErrorText(rresult, 0, strMsg);
                   // Console.WriteLine(strMsg.ToString());
                }

            }
            PCANBasic.Uninitialize(channel);
           
        }
    }
}

K.Wagner
Software Development
Software Development
Posts: 1080
Joined: Wed 22. Sep 2010, 13:36

Re: How to connect to a net

Post by K.Wagner » Mon 28. Apr 2014, 15:13

Hello,

this error indicates that PCAN-View is using a different baudrate than that being issued by your application. I see in your code a baudrate of 125 KB. Please check that the PCAN-View is also using a baudrate of 125 KB.
Best regards,
Keneth

Thomas1337
Posts: 3
Joined: Mon 28. Apr 2014, 14:41

Re: How to connect to a net

Post by Thomas1337 » Mon 28. Apr 2014, 16:31

Thanks for your fast reply. The baud rate is right. Receiving works, when PCAN-View isnt open.
I think it blocks the connection.
I wonder if it is even possible with the Basic API? The documentation states that more than one application
can access one hardware with CanApi2. I am not sure if this is actually the problem. There seem to be
a lot of possibilities to set up a CAN network. I read about repeater, router or even tree topologies.
Well, I just have one PCAN USB adapter connected to an embedded system and to the USB port of my computer.
Currently it is possible to set values on the embedded systems (by sending CAN messages) or to request values,
so that the embedded system will respond with the corresponding value.
I have the requirement to write an application that doesnt interfere with PCAN Explorer or with PCAN View.
These applications should be able to run at the same time, so the new application can be tested with PCAN View/Explorer.
But it seems that only exclusive access to the USB PCAN adapter is possible, is this correct?

K.Wagner
Software Development
Software Development
Posts: 1080
Joined: Wed 22. Sep 2010, 13:36

Re: How to connect to a net

Post by K.Wagner » Mon 28. Apr 2014, 17:11

Thomas1337 wrote:The documentation states that more than one application
can access one hardware with CanApi2.
Please note that PCAN-Basic and CanApi2 are NOT the same API.

You first wrote.
Thomas1337 wrote:If I change the channel to PCAN_USBBUS2,
it says that the handle isnt valid.
And then:
Thomas1337 wrote:Well, I just have one PCAN USB adapter connected to an embedded system and to the USB port of my computer.
If you only have one device, why you try to connect a second channel? A Channel represents each device i.e. each available hardware to connect.
Thomas1337 wrote:I wonder if it is even possible with the Basic API?
A PCAN-View and one PCAN-Basic application do can connect the same Device, as far as they use the same baudrate. Note that they both have to use the same Net too, but this is done intrinsically since PCAN-View and PCAN-Basic use the same Net-Name when connecting the same Hardware (in your case a PCAN-USB device).

Maybe you try to change the connection order. Start your application first, and then connect the PCAN-View to the hardware.
Best regards,
Keneth

Thomas1337
Posts: 3
Joined: Mon 28. Apr 2014, 14:41

Re: How to connect to a net

Post by Thomas1337 » Tue 29. Apr 2014, 14:19

I get following error message when starting the program first, then PCAN-View:

Image

/edit
Seems you are right. It works when clicking on the USB Hardware symbol. I found out (with the program
PCAN nets Configuration) that the PCAN api uses a net which is called PCANLight_USB_16. It doesnt seem to be shown in PCAN-View, of course the other net that I set up doesnt work connecting.


Greetings

K.Wagner
Software Development
Software Development
Posts: 1080
Joined: Wed 22. Sep 2010, 13:36

Re: How to connect to a net

Post by K.Wagner » Tue 29. Apr 2014, 14:58

Yes, the error is OK.

You was connecting a "custom Net". PCAN-Basic, as I wrote, needs to use the same net as the PCAN-View and this happen only when the PCAN-View connect a hardware (directly), not a Net.
K.Wagner wrote:PCAN-View and PCAN-Basic use the same Net-Name when connecting the same Hardware
Best regards,
Keneth

Post Reply