Linux系统调用PCAN API实现基础功能

  • Post author:
  • Post category:linux




1.环境


系统版本 Ubuntu 22.04


下载链接:https://cn.ubuntu.com/download/desktop


Linux PCAN驱动及PCANVIEW


下载链接:https://www.peak-system.com/fileadmin/media/linux/index.htm

下载后的驱动需要自己编译,在这里不多做讨论。

然后把PCANBasic.h放到创建的项目里保证可以#include “PCANBasic.h”可以找到

然后在项目的CMakeLists.txt中加入lib

target_link_libraries(${PROJECT_NAME} libpcanbasic.so.8)
target_link_libraries(${PROJECT_NAME} libpcanbasic.so)
target_link_libraries(${PROJECT_NAME} libpcanbasic.so.0)



2.编程



1.连接通道

parameter1 硬件通道(如何获得在下文)

parameter2 初始化参数

return 连接状态

TPCANBitrateFD bitrate = (LPSTR)("f_clock_mhz=80, nom_brp=10, nom_tseg1=12, nom_tseg2=3, nom_sjw=1, data_brp=4, data_tseg1=7, data_tseg2=2, data_sjw=1");
TPCANStatus status;
status = CAN_InitializeFD(channelhandle,bitrate);



2.释放通道

parameter1 硬件通道

TPCANStatus status;
status = CAN_Uninitialize(channelhandle);



3.获取通道列表

遍历去连接所有类型的硬件通道,return成的话。就去获得PCAN_DEVICE_ID。然后释放掉通道。如图为我用Qt写的界面。

TPCANStatus status;
    for(uint16_t i=PCAN_PCIBUS1;i<PCAN_PCIBUS1+34;i++)
    {
        status=CAN_InitializeFD(i,bitrate);
        if(status == 0x00)
        {

            status=CAN_GetValue(i,PCAN_DEVICE_ID, (void*)&iBuffer, sizeof(iBuffer));
            QString Device_ID = "Device ID:"+QString::number(iBuffer,16)+"h("+QString::number(i,16)+")";
            m_devicesMap.insert(Device_ID,i);
        }
        CAN_Uninitialize(i);
    }

在这里插入图片描述



3.写入标准帧报文

struct struct_canMessage
{
    uint head;
    uchar data[8] ;
};
TPCANStatus status;
    TPCANMsgFD dataMessage;
    dataMessage.ID = canMessage.head;
    dataMessage.MSGTYPE = PCAN_MESSAGE_STANDARD;
    dataMessage.DLC = sizeof(canMessage.data);
    memcpy(dataMessage.DATA,canMessage.data,sizeof(canMessage.data));
    status = CAN_WriteFD(channelhandle,&dataMessage);



4.读取报文

我把读到的报文存成了QList类型。

QList<QString> CAN_data_list;
    TPCANMsgFD msgFD;
    TPCANTimestampFD timestampFD;
    TPCANStatus result = 0;
    int fdfreamlen = 0;

    do
    {
        result=CAN_ReadFD(PCAN_USBBUS1, &msgFD, &timestampFD);
        if (result == PCAN_ERROR_QRCVEMPTY|| result== PCAN_ERROR_ILLOPERATION)
        {
             break;
        }
        switch (msgFD.DLC)
        {
        case 0:fdfreamlen = 0; break;
        case 1:fdfreamlen = 1; break;
        case 2:fdfreamlen = 2; break;
        case 3:fdfreamlen = 3; break;
        case 4:fdfreamlen = 4; break;
        case 5:fdfreamlen = 5; break;
        case 6:fdfreamlen = 6; break;
        case 7:fdfreamlen = 7; break;
        case 8:fdfreamlen = 8; break;
        case 9:fdfreamlen = 12; break;
        case 10:fdfreamlen = 16; break;
        case 11:fdfreamlen = 20; break;
        case 12:fdfreamlen = 24; break;
        case 13:fdfreamlen = 32; break;
        case 14:fdfreamlen = 48; break;
        case 15:fdfreamlen = 64; break;
        default:break;
        }
        QString Msg_Data;
        QString CAN_msg;
        QString time_str;
        Msg_Data = "     " +QString::number(msgFD.ID,16) + "  [" + QString::number(fdfreamlen)+ "]  ";
        for (int i = 0; i < fdfreamlen; i++)
        {
            if(msgFD.DATA[i]>=0x10)
                CAN_msg += QString::number(msgFD.DATA[i],16) + " ";
            else
                CAN_msg +="0"+ QString::number(msgFD.DATA[i], 16) + " ";
        }
        time_str = QString::number(timestampFD);//时间戳
        Msg_Data += CAN_msg + time_str;


        CAN_data_list.append(Msg_Data);
    }
    while((result & PCAN_ERROR_QRCVEMPTY) != PCAN_ERROR_QRCVEMPTY);
    return CAN_data_list;



版权声明:本文为m0_57124501原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。