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);
}
}
}