简体   繁体   English

如何将字符串分隔为不同类型,可以发出信号?

[英]How to separate a string to separate types, a can signal?

I am working on an API to read CAN signal. 我正在使用API​​读取CAN信号。 This part of the code outputs the received CAN in the form of a string. 这部分代码以字符串形式输出接收到的CAN。

xlCanGetEventString(&xlCanRxEvt) xlCanGetEventString(&xlCanRxEvt)

The CAN signal is in the form of: CAN信号的形式为:

XL_CAN_EV_TAG_RX_OK ch:1 t=9026813952 id:98FF1880 dlc:8 862EC5350C138336 
XL_CAN_EV_TAG_RX_OK ch:1 t=9027108864 id:98FF1980 dlc:8 500111065C018C03 
XL_CAN_EV_TAG_RX_OK ch:1 t=9027411968 id:98FF1A80 dlc:8 0000FFFFFFFFFFFF
XL_CAN_EV_TAG_RX_OK ch:1 t=9027411968 id:98FF1A80 dlc:8 0000FFFFFFFFFFFF 
XL_CAN_EV_TAG_RX_OK ch:1 t=9027657728 id:98FF5180 dlc:5 C000000000

In that signal I need to read the IDs from each line and then display the data only for the IDs I need. 在该信号中,我需要从每一行读取ID,然后仅显示所需ID的数据。 I am currently doing this by using a bunch of if and for statements, and using char arrays to read different part of the line. 我目前正在通过使用一堆if和for语句,并使用char数组读取行的不同部分来执行此操作。

This feels very inefficient to me, is there another way to do it ? 这对我来说效率很低,还有其他方法可以做到吗? To maybe directly extract the Id part or compare the ID part without having to store the whole thread into an array ? 是否可以直接提取ID部分或比较ID部分而不必将整个线程存储到数组中? Or any best or efficient way to do the same ? 还是任何最佳或有效的方法来做到这一点? I don't find a variable storing the data or ID in the typedef struct xlCanRxEvt either. 我也没有在typedef struct xlCanRxEvt中找到存储数据或ID的变量。 Any suggestion will be greatly appreciated. 任何建议将不胜感激。

You are talking about the Vector XL Driver Library. 您正在谈论Vector XL驱动程序库。

You get your XlEvent first from XLCANReceive(). 您首先从XLCANReceive()获取XlEvent。 That XLEvent is a struct, XLEvent是一个结构,

structs_xl_event{  
XLeventTag          tag;  
unsignedchar       chanIndex;  
unsignedshort      transId; 
unsignedshort      portHandle;  
unsignedchar       flags;  
unsignedchar       reserved;  
XLuint64            timeStamp;  
unions_xl_tag_datatagData;
};

And you can access their content like this: 您可以像这样访问他们的内容:

xlEvent[i].tag                 =XL_TRANSMIT_MSG;
xlEvent[i].tagData.msg.id      =0x04;
xlEvent[i].tagData.msg.flags   =0;
xlEvent[i].tagData.msg.data[0]=1;
xlEvent[i].tagData.msg.data[1]=2;
xlEvent[i].tagData.msg.data[2]=3;  
xlEvent[i].tagData.msg.data[3]=4;  
xlEvent[i].tagData.msg.data[4]=5;
xlEvent[i].tagData.msg.data[5]=6;  
xlEvent[i].tagData.msg.data[6]=7;  
xlEvent[i].tagData.msg.data[7]=8;  
xlEvent[i].tagData.msg.dlc     =8;

I think this is enough for you to figure out the more "elegant" way to extract data. 我认为这足以让您找出提取数据的“更优雅”的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM