G++版 LINモニタプログラム
動作環境構築
カレントディレクトリをソースコードと同じディレクトリにして make を実行してください。
ビルドが正常に完了すると、実行ファイル mpx_lin_monsample が生成されます。
動作仕様
| 項目 | 説明 |
|---|---|
| CH1 | モニタモードで動作します。 100msごとにMPXGetLogを呼び出してログを取得し、コンソールに表示します。 |
| CH2 | 未使用 |
ログ表示部詳細
| 項目 | 説明 |
|---|---|
| Time | タイムスタンプを表示します。 |
| Dir. | ログ方向を表示します。 Rx:受信ログ Tx:送信ログ |
| Type | ログ種別を表示します。 |
| ID | LIN ID(16進数)を表示します。 |
| SBL | SyncBreak幅を表示します。 |
| DL | データ長(0~64)を表示します。 |
| Data | データ(D1~D64)を16進数で表示します。データの区切り文字はスペースです。 |
| CS | チェックサムを16進数で表示します。 |
| WPL | ウェイクアップパルス幅を表示します。 |
サンプルソースコード
対応元: MPXCtrl_LIN_MonSample/mpx_lin_monsample.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;
// 表示文字列
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("LIN Monitor 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);
}
/* LIN通信パラメータ設定 */
StMPXLINParam param;
memset(¶m, '\0', sizeof(param));
param.Mode = MPX_LIN_MODE_MONITOR; /* モニタモード */
param.Revision = MPX_LIN_REV_21;
param.Baudrate = 19200;
param.SyncBreak = 13;
param.Delimiter = 1;
param.HeaderSpace = 1;
param.ResponseSpace = 1;
param.ByteSpace = 1;
param.WakeupWidth = 250;
param.WakeupWait = 0;
param.SleepTime = 4000;
for (int i = 0x00; i < 0x40; i++) {
/* DL */
if (i < 0x20) {
param.IDSetting[i].DL = 2;
} else if (i < 0x30) {
param.IDSetting[i].DL = 4;
} else {
param.IDSetting[i].DL = 8;
}
/* CheckSum */
param.IDSetting[i].CheckSumType = MPX_LIN_PARAM_CS_CLASSIC;
}
ret = MPXLINSetParam(serial, ¶m);
if (ret != E_OK)
{
printf("\n MPXLINSetParam : 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 < 200; i++) {
/* usleep (コメントアウト中):
ソフトウェア制御では周期送信間隔を正確に維持できない
(スケジューリングにより設定したSleep時間より多くの時間を待たされる) */
// usleep(50)
std::this_thread::sleep_for(std::chrono::milliseconds(100));
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("LIN Monitor 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.LIN.ErrorCode == MPX_LIN_ERR_NONE) {
/* 時間 */
dispTmp << "Time : " << std::dec << (int)MPXLog[i].mSec << "[ms] " << (int)MPXLog[i].uSec << "[us] , ";
if (MPXLog[i].Misc.LIN.Type == MPX_LIN_LOG_TYPE_HEADER) {
/* 方向 */
dispTmp << "Dir. : ";
if (MPXLog[i].Dir == MPX_LOG_DIR_RX)
{
dispTmp << "Rx , ";
}
else
{
dispTmp << "Tx , ";
}
/* ヘッダー */
dispTmp << "Type : Header , ";
/* ID */
dispTmp << "ID : " << std::hex << (int)MPXLog[i].Misc.LIN.ID << " , ";
/* SyncBreak幅 */
dispTmp << "SBL : " << std::dec << (int)MPXLog[i].Misc.LIN.SBLen << "[bit]";
} else if (MPXLog[i].Misc.LIN.Type == MPX_LIN_LOG_TYPE_RESPONSE) {
/* 方向 */
dispTmp << "Dir. : ";
if (MPXLog[i].Dir == MPX_LOG_DIR_RX)
{
dispTmp << "Rx , ";
}
else
{
dispTmp << "Tx , ";
}
/* レスポンス */
dispTmp << "Type : Response , ";
/* ID */
dispTmp << "ID : " << std::hex << (int)MPXLog[i].Misc.LIN.ID << " , ";
/* DL */
dispTmp << "DL : " << std::dec << (int)MPXLog[i].Misc.LIN.DL << " , ";
/* Data */
dispTmp << "Data : ";
for (int j = 0; j < MPXLog[i].Misc.LIN.DL; j++) {
dispTmp << std::hex << (int)MPXLog[i].Data[j] << " ";
}
dispTmp << ", ";
/* Checksum */
dispTmp << "CS : " << std::hex << (int)MPXLog[i].Data[MPXLog[i].Misc.LIN.DL];
} else if (MPXLog[i].Misc.LIN.Type == MPX_LIN_LOG_TYPE_WAKEUP) {
/* 方向 */
dispTmp << "Dir. : ";
if (MPXLog[i].Dir == MPX_LOG_DIR_RX)
{
dispTmp << "Rx , ";
}
else
{
dispTmp << "Tx , ";
}
/* ウェイクアップ */
dispTmp << "Type : Wakeup , ";
/* ウェイクアップパルス幅 */
dispTmp << "WPL : " << std::dec << (int)MPXLog[i].Misc.LIN.WakeupWidth << "[ms]";
} else if (MPXLog[i].Misc.LIN.Type == MPX_LIN_LOG_TYPE_SLEEP) {
/* 方向 */
dispTmp << "Dir. : ";
if (MPXLog[i].Dir == MPX_LOG_DIR_RX)
{
dispTmp << "Rx , ";
}
else
{
dispTmp << "Tx , ";
}
/* スリープ */
dispTmp << "Type : Sleep , ";
/* ID */
dispTmp << "ID : " << std::hex << (int)MPXLog[i].Misc.LIN.ID << " , ";
/* DL */
dispTmp << "DL : " << std::dec << (int)MPXLog[i].Misc.LIN.DL << " , ";
/* Data */
dispTmp << "Data : ";
for (int j = 0; j < MPXLog[i].Misc.LIN.DL; j++) {
dispTmp << std::hex << (int)MPXLog[i].Data[j] << " ";
}
dispTmp << ", ";
/* Checksum */
dispTmp << "CS : " << std::hex << (int)MPXLog[i].Data[MPXLog[i].Misc.LIN.DL];
} else if (MPXLog[i].Misc.LIN.Type == MPX_LIN_LOG_TYPE_BUS) {
/* バスエラー */
dispTmp << "Type : Bus Error";
} else if (MPXLog[i].Misc.LIN.Type == MPX_LIN_LOG_TYPE_BUSSLEEP) {
/* バススリープ */
dispTmp << "Type : Bus Sleep";
} else {
/* その他 */
dispTmp << "Type : Ext.";
}
dispTmp << "\n";
}
else
{
if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_SBL) {
dispTmp << "ERROR : SynchBreak Too Long\n";
} else if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_SBS) {
dispTmp << "ERROR : SynchBreak Too Short\n";
} else if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_PARITY) {
dispTmp << "ERROR : Parity Err.\n";
} else if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_SHORT) {
dispTmp << "ERROR : Short Msg.\n";
} else if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_CHKSUM) {
dispTmp << "ERROR : CheckSum Err.\n";
} else if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_NORES) {
dispTmp << "ERROR : Slave Not Responding\n";
} else if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_BIT) {
dispTmp << "ERROR : Bit Err.\n";
} else if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_FRAMING) {
dispTmp << "ERROR : Framing Err.\n";
} else if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_SFIELD) {
dispTmp << "ERROR : SynchField Err.\n";
} else if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_HTIMEOUT) {
dispTmp << "ERROR : Header Timeout\n";
} else if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_UART) {
dispTmp << "ERROR : UART Err.\n";
}
}
}
pthread_mutex_lock(&que_mutex);
print_que.push_back(dispTmp.str());
pthread_mutex_unlock(&que_mutex);
}