G++ CAN Simulation Program
Building the Runtime Environment
Change the current directory to the directory that contains the source code, and then run make.
When the build completes normally, the executable file mpx_canfd_simsample is generated.
Operation Specifications
| Item | Description |
|---|---|
| CH1 | Operates in simulation mode. Sends CAN frames using the slot transmission function and direct transmission function (for the CAN frame contents, see Preset Frames). Also calls MPXGetLog every 100 ms to acquire logs and displays them on the console. |
| CH2 | Not used |
Preset Frames
The contents of each preset frame are as follows.
| Frame Type | Slot | ID | DL | Data (Hex) | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Format | CAN ID | D1 | D2 | D3 | D4 | D5 | ... | D20 | D21 | ... | D64 | |||
| Periodic transmit frame (transmit interval: 100 ms) (*1) | 0 | Standard ID | 300H | 20 | 10H | 11H | 12H | 13H | 14H | ... | 23H | ... | ||
| Periodic transmit frame (transmit interval: 200 ms) (*1) | 1 | Standard ID | 400H | 20 | 20H | 21H | 22H | 23H | 24H | ... | 33H | ... | ||
| Direct transmit frame (*2) | - | Standard ID | 500H | 64 | 00H | 00H | 00H | 00H | 30H | ... | 30H | 30H | ... | 30H |
*1: The values of D1 to D20 are incremented by 1 on each transmission.
*2: D1 to D4 are set to the monitor-cycle counter value, and D5 and later are fixed at 30H. This frame is transmitted every 5 monitor cycles.
Log Display Details
| Item | Description |
|---|---|
| Time | Displays the timestamp. |
| Protocol | Displays the CAN protocol (CAN or CAN FD). |
| Dir. | Displays the log direction. Rx: receive log / Tx: transmit log |
| ID | Displays the ID format (see below for meaning) and CAN ID (hexadecimal). Std: standard ID / Ext: extended ID |
| DL | Displays the data length (0 to 64). |
| Data | Displays data (D1 to D64) in hexadecimal. Data bytes are separated by spaces. |
Sample Source Code
Source: MPXCtrl_CANFD_SimSample/mpx_canfd_simsample.c
#include "MPXCtrlFree.h"
#include "stdio.h"
#include "unistd.h"
#include <chrono>
#include <deque>
#include <iostream>
#include <pthread.h>
#include <sstream>
#include <stdint.h>
#include <string.h>
#include <thread>
// グローバル
uint32_t serial;
StMPXCANSlot MPXCANSlot[2];
// 表示文字列
std::deque<std::string> print_que;
void DispMonitor(void);
pthread_mutex_t que_mutex;
std::string strbuf;
void *thread_routine(void *data) {
while (1) {
usleep(1000 * 5); // wait1ms
pthread_mutex_lock(&que_mutex);
if (!print_que.empty()) {
strbuf = print_que.front();
print_que.pop_front(); // 先頭要素を削除
std::cout << strbuf; // バッファを表示
}
pthread_mutex_unlock(&que_mutex);
pthread_testcancel();
}
return 0;
}
int main() {
ER ret;
uint32_t MSec;
uint16_t USec;
pthread_t print_thread;
printf("CAN FD Simulation Sample Start\n");
/* ADLバージョン取得 */
StMPXAPIVersion apiVersion;
MPXGetAPIVersion(&apiVersion);
printf("API Version : [%s]\n", apiVersion.APIVersion);
/* MicroPeckerXオープン */
StMPXDeviceInfo devices[4];
uint8_t count;
ret = MPXOpen(devices, 4, &count);
if (ret != E_OK) {
printf("MPXOpen : error[%d]\n", ret);
return 1;
} else if (count == 0) {
printf("MPXOpen : no devices\n");
return 1;
} else {
/* シリアル番号 */
serial = devices[0].Serial;
/* LED消灯 */
MPXSetLED(serial, MPX_LED_OFF, MPX_LED_OFF, MPX_LED_OFF, MPX_LED_OFF, MPX_LED_OFF);
}
for (int i = 0; i < count; i++) {
printf("Serial %d: [%d]\n", i, devices[i].Serial);
}
/* CAN FD通信パラメータ設定(CH1) */
StMPXCANParam param1;
memset(¶m1, '\0', sizeof(param1));
param1.Mode = MPX_CAN_MODE_SIM; /* シミュレーションモード */
param1.ArbitrationBaudrate = MPX_CAN_PARAM_ABR_500K;
param1.ArbitrationSamplepoint = MPX_CAN_PARAM_SP_80P;
param1.DataBaudrate = MPX_CAN_PARAM_DBR_2M;
param1.DataSamplepoint = MPX_CAN_PARAM_SP_80P;
param1.EnableTerminate = MPX_CAN_TERMINATE_ENABLE; // 終端有効
ret = MPXCANSetParam(serial, 1, ¶m1);
if (ret != E_OK) {
printf("\n MPXCANSetParam 1 : error[%d]\n", ret);
return -1;
}
/* CAN FD通信パラメータ設定(CH2) */
StMPXCANParam param2;
memset(¶m2, '\0', sizeof(param2));
param2 = param1;
param2.Mode = MPX_LIN_MODE_NONE; /* 動作なし */
// モード以外はCH1と同じ
ret = MPXCANSetParam(serial, 2, ¶m2);
if (ret != E_OK) {
printf("\n MPXCANSetParam 2 : error[%d]\n", ret);
return -1;
}
/* シミュレーションデータ設定(スロット送信機能) */
for (int i = 0; i < 2; i++) {
MPXCANSlot[i].SlotNo = i;
MPXCANSlot[i].FrameType.Enabled = MPX_CAN_SLOT_ENABLE; /* スロットの有効化 */
MPXCANSlot[i].FrameType.Option.Protocol = MPX_CAN_PROTOCOL_CANFD;
MPXCANSlot[i].FrameType.Option.BRS = MPX_CAN_BRS_ENABLE;
MPXCANSlot[i].Frame.ID.IDE = MPX_CAN_IDE_STD;
MPXCANSlot[i].Frame.ID.RTR = MPX_CAN_RTR_DATA;
MPXCANSlot[i].msSendCycle = 100U;
MPXCANSlot[i].SendCounter = 0; /* 0は無制限を示す */
MPXCANSlot[i].Frame.DL = 20;
for (int j = 0; j < 20; j++) {
MPXCANSlot[i].Frame.Data[j] = (i + 1) * 0x10 + j;
MPXCANSlot[i].Increment[j] = MPX_CAN_SIM_INC_TRUE;
}
}
/* フレーム個別設定 */
MPXCANSlot[0].Frame.ID.ID = 0x300U;
MPXCANSlot[1].Frame.ID.ID = 0x400U;
MPXCANSlot[1].msSendCycle = 200U;
MPXCANSlot[0].FrameType.FrameType = MPX_CAN_FRAME_TYPE_PERIODIC;
MPXCANSlot[1].FrameType.FrameType = MPX_CAN_FRAME_TYPE_PERIODIC;
/* スロット設定(CH1) */
ret = MPXCANSetSlot(serial, 1, &MPXCANSlot[0], 2);
if (ret != E_OK) {
printf("\n MPXCANSetSlot 1 : error[%d]\n", ret);
return -1;
}
printf("\n Monitor Start ...\n");
/* モニタリング開始 */
ret = MPXMonitorStart(serial, MPX_SYNC_MASTER);
if (ret != E_OK) {
printf("\n Monitor Start : error[%d]\n", ret);
return -1;
}
int pth_arg;
// 表示スレッド起動
pthread_mutex_init(&que_mutex, NULL);
if (pthread_create(&print_thread, NULL, thread_routine, (void *)&pth_arg) != 0) {
perror("pthread_create\n");
return 1;
}
/* モニタリング */
for (int i = 0; i < 100; i++) {
/* usleep (コメントアウト中):
ソフトウェア制御では周期送信間隔を正確に維持できない
(スケジューリングにより設定したSleep時間より多くの時間を待たされる) */
// usleep(50)
std::this_thread::sleep_for(std::chrono::milliseconds(100));
if (i % 5 == 0) {
/* ダイレクト送信機能 */
StMPXCANDirect directFrame;
/* 送信データ設定(CAN-FDデータ) */
directFrame.FrameType.Option.Protocol = MPX_CAN_PROTOCOL_CANFD;
directFrame.FrameType.Option.BRS = MPX_CAN_BRS_ENABLE;
directFrame.Frame.ID.IDE = MPX_CAN_IDE_STD;
directFrame.Frame.ID.RTR = MPX_CAN_RTR_DATA;
directFrame.Frame.ID.ID = 0x500;
directFrame.Frame.DL = 64;
/* 先頭4byteにカウンタ値を代入(CPUエンディアン依存) */
memcpy(&directFrame.Frame.Data[0], &i, 4);
for (int i = 4; i < 64; i++) {
directFrame.Frame.Data[i] = 0x30;
}
MPXCANDirectSend(serial, 1, &directFrame);
}
DispMonitor();
}
ret = MPXMonitorStop(serial, &MSec, &USec);
if (ret != E_OK) {
printf("MPXMonitorStop : error[%d]\n", ret);
return -1;
}
printf("\n Monitor Stop ...\n");
pthread_mutex_lock(&que_mutex);
print_que.clear();
pthread_mutex_unlock(&que_mutex);
usleep(100 * 1000); // wait100ms
pthread_cancel(print_thread);
pthread_join(print_thread, NULL);
// 表示用キューを削除
if (!print_que.empty())
print_que.clear();
MPXClose();
printf("MPXClose\n");
printf("CAN FD Simulation Sample End\n");
return 0;
}
void DispMonitor(void) {
/* モニタログ取得 */
uint16_t Count;
uint8_t BufferOver;
ER ret;
StMPXLog *MPXLog;
std::stringstream dispTmp;
ret = MPXGetLog(serial, 1, &MPXLog, &Count, &BufferOver);
if (ret != E_OK) {
printf("MPXGetLog : error[%d]\n", ret);
return;
}
/* ログ表示 */
for (int i = 0; i < (int)Count; i++) {
// 空ログ無視
if (MPXLog[i].Protocol == MPX_LOG_TYPE_EMPTY) {
continue;
}
/* エラーチェック */
if (MPXLog[i].Misc.CAN.Error == MPX_CAN_ERRFLG_FALSE) {
/* 時間 */
dispTmp << "Time : " << std::dec << (int)MPXLog[i].mSec << "[ms] " << (int)MPXLog[i].uSec << "[us] , ";
/* プロトコル */
dispTmp << "Protocol : ";
if (MPXLog[i].Protocol == MPX_LOG_TYPE_CANFD) {
dispTmp << "CAN FD , ";
} else if (MPXLog[i].Protocol == MPX_LOG_TYPE_CANFD_NOBRS) {
dispTmp << "CAN FD (NO BRS) , ";
} else {
dispTmp << "CAN , ";
}
/* 方向 */
dispTmp << "Dir. : ";
if (MPXLog[i].Dir == MPX_LOG_DIR_RX) {
dispTmp << "Rx , ";
} else {
dispTmp << "Tx , ";
}
/* IDフォーマット */
if (MPXLog[i].Misc.CAN.IDE == MPX_CAN_IDE_STD) {
dispTmp << "ID : Std ";
} else {
dispTmp << "ID : Ext ";
}
/* ID */
dispTmp << std::hex << (int)MPXLog[i].Misc.CAN.ID << " , ";
/* DL */
dispTmp << "DL : " << std::dec << (int)MPXLog[i].Misc.CAN.DL << " , ";
/* Data */
dispTmp << "Data : ";
for (int j = 0; j < MPXLog[i].Misc.CAN.DL; j++) {
dispTmp << std::hex << (int)MPXLog[i].Data[j] << " ";
}
dispTmp << "\n";
} else {
if (MPXLog[i].Misc.CAN.ErrorInfo & MPX_CAN_ERR_BUS) {
dispTmp << "ERROR : Bus Err.\n";
} else if (MPXLog[i].Misc.CAN.ErrorInfo & MPX_CAN_ERR_WARN) {
dispTmp << "ERROR : Err. Warning\n";
} else if (MPXLog[i].Misc.CAN.ErrorInfo & MPX_CAN_ERR_PASV) {
dispTmp << "ERROR : Err. Passive\n";
} else if (MPXLog[i].Misc.CAN.ErrorInfo & MPX_CAN_ERR_BUSOFF) {
dispTmp << "ERROR : Bus Off Entry\n";
} else if (MPXLog[i].Misc.CAN.ErrorInfo & MPX_CAN_ERR_BUSWAKE) {
dispTmp << "ERROR : Bus Off Recovery\n";
} else if (MPXLog[i].Misc.CAN.ErrorInfo & MPX_CAN_ERR_OVERLOAD) {
dispTmp << "ERROR : Overload Frame Err.\n";
} else if (MPXLog[i].Misc.CAN.ErrorInfo & MPX_CAN_ERR_BUSLOCK) {
dispTmp << "ERROR : Channel Bus Lock\n";
} else if (MPXLog[i].Misc.CAN.ErrorInfo & MPX_CAN_ERR_ARBLOST) {
dispTmp << "ERROR : Arbitration Lost\n";
} else if (MPXLog[i].Misc.CAN.ErrorInfo & MPX_CAN_ERR_STAFF) {
dispTmp << "ERROR : Stuff Err.\n";
} else if (MPXLog[i].Misc.CAN.ErrorInfo & MPX_CAN_ERR_FORM) {
dispTmp << "ERROR : Form Err.\n";
} else if (MPXLog[i].Misc.CAN.ErrorInfo & MPX_CAN_ERR_ACK) {
dispTmp << "ERROR : ACK Err.\n";
} else if (MPXLog[i].Misc.CAN.ErrorInfo & MPX_CAN_ERR_CRC) {
dispTmp << "ERROR : CRC Err.\n";
} else if (MPXLog[i].Misc.CAN.ErrorInfo & MPX_CAN_ERR_RECEIVE) {
dispTmp << "ERROR : Recessive Bit Err.\n";
} else if (MPXLog[i].Misc.CAN.ErrorInfo & MPX_CAN_ERR_DOMINANT) {
dispTmp << "ERROR : Dominant Err.\n";
} else if (MPXLog[i].Misc.CAN.ErrorInfo & MPX_CAN_ERR_DELIMITER) {
dispTmp << "ERROR : ACK Delimiter Err.\n";
}
}
}
pthread_mutex_lock(&que_mutex);
print_que.push_back(dispTmp.str());
pthread_mutex_unlock(&que_mutex);
}