■仕様
・医療機器(このソースは肺機能計)にRs232cのアダプターが接続されたBluetooth接続し、機器に沿ったバイト文字列のコマンドを投げて、値を取得する。
コード
private void btSP350COPD(BluetoothSppConnection connection, String address) {
final int RECEIVE_SIZE = 3000;
byte[] status = new byte[RECEIVE_SIZE + 1]; // 受信バッファ
ReadResultSize resultSize = new ReadResultSize(); // 受信済みサイズ
int btRet = 0;
byte[] byBuff;
String strCommandG = ""; // 属性データ送信用
String strSendAck = ""; //
String strSendCan = ""; //
String strData = "";
String pNO = "";
String pName = "";
String pSex = "";
String pAge = "";
String pSng = "";
String pHeight = "";
String pWeight = "";
boolean bCancelFlag;
int i;
try {
pNO = receiveParam[1]; // '1.検査番号
pName = receiveParam[2]; // '2.氏名
// 漢字名
pSex = receiveParam[4]; // '4.性別
pSng = receiveParam[5]; // '5.生年月日
pAge = receiveParam[6]; // '6.年齢
pHeight = receiveParam[7]; // '7.身長
pWeight = receiveParam[8]; // '8.体重
Log.d(TAG, "肺活量計(SP-350COPD) pNO , pName , pSex , pSng , pAge , pHeight , pWeight" +
pNO + ":::" + pName + ":::" + pSex +
":::" + pSng + ":::" + pAge + ":::" +
pHeight + ":::" + pWeight);
// 出力項目のクリア
clearReceiveValues();
// ASK VB => Chr(6)
char strSendAck_tmp = 0x06; // ACK (受信OK)
strSendAck = String.valueOf(strSendAck_tmp);
// CAN VB => Chr(&h18)
char strSendCan_tmp = 0x18;
strSendCan = String.valueOf(strSendCan_tmp); // CAN (取消)
// ********* マスターモードで仮想シリアルポート接続 *********
btRet = connection.connect(address);
if (btRet == StatusConstants.BT_SUCCESS) {
// ENQ 待ち ENQ コマンド:「(今からデータを送っても)大丈夫?」のような、問い合わせ用のコード
// vb ソース Recv(byStatus, intRecvSize)
btRet = connection.receiveDeviceValue(status, resultSize);
// キャンセル
if (resultSize.get() <= 0) {
// キャンセル
receiveRet = StatusConstants.RET_ERR_CANCEL;
return;
}
Log.d(TAG, "肺活量 SP-350COPD = " + btRet);
// contains での ロジック
String Tmp_i = ByteUtil.bytesToString(status, 0, resultSize.get());
Log.d(TAG, "肺活量 SP-350COPD = Tmp_i ::: " + Tmp_i);
// === 比較用 ENQ ===
char Chr_5 = 0x05;
String Chr_5_str = String.valueOf(Chr_5);
if(Tmp_i.contains(Chr_5_str)) {
i = 1;
Log.d(TAG, "if 文内 ::: i:::" + "値:::" + i);
} else {
i = 0;
Log.d(TAG, "if 文内 else 比較文字列無し ::: i:::" + "値:::" + i);
}
// === 比較用 ENQ ===
if (i < 0) {
// キャンセル
receiveRet = StatusConstants.RET_ERR_CANCEL;
Log.d(TAG, "肺活量計(SP-350COPD) = i 受信キャンセル" + i);
return;
}
// ASK 送信 ASKコマンド:0x06 「OK」「わかりました」のような、肯定する応答(返事)を示すコード
// === コマンド 送信 === 0x06 strSendAck
byBuff = strSendAck.getBytes(AppCharset.SHIFT_JIS);
connection.send(byBuff, byBuff.length);
// 属性問い合わせ待ち
// vb ソース Recv(byStatus, intRecvSize)
btRet = connection.receiveDeviceValue(status, resultSize);
if (btRet < 0) {
// キャンセル
receiveRet = StatusConstants.RET_ERR_CANCEL;
return;
}
String Tmp_i_02 = ByteUtil.bytesToString(status, 0, resultSize.get());
// === 比較用 STX (テキスト開始) ===
char Chr_2 = 0x02;
String Chr_2_str = String.valueOf(Chr_2);
if(Tmp_i_02.contains(Chr_2_str)) {
i = 1;
Log.d(TAG, "if 文内 STX (テキスト開始) ::: i:::" + "値:::" + i);
} else {
i = 0;
Log.d(TAG, "if 文内 STX (テキスト開始) else 比較文字列無し ::: i:::" + "値:::" + i);
}
// === 比較用 STX END ===
if (i < 0) {
// キャンセル
receiveRet = StatusConstants.RET_ERR_CANCEL;
Log.d(TAG, "肺活量計(SP-350COPD) = [Recv] 受信キャンセル (STX無し)" + i);
return;
}
// 属性データ作成
//=== STX
char char_Tmp_01 = 0x02;
strCommandG = String.valueOf(char_Tmp_01);
strCommandG += "0";
//=== 患者ID(13)
strCommandG += ByteUtil.bytesToString(status, 8 - 1, 13);
//strCommandG += ByteUtil.bytesToString(status, 7 - 1, 13).trim();
//=== 検査番号
// Val pNO
String Val_pNO = pNO.replaceAll("[^\\d]", "").trim();
Log.d(TAG, "肺活量計(SP-350COPD) = 変数出力::Val_ pNO " + Val_pNO);
String format_L_pNO = String.format("%9s", Val_pNO);
Log.d(TAG, "肺活量計(SP-350COPD) = 変数出力::format_L_pNO " + format_L_pNO);
strCommandG += format_L_pNO;
//=== 氏名(30) 右側に ( 30 - 氏名 ) パディング
String format_R_pName = String.format("%-30s", pName);
Log.d(TAG, "肺活量計(SP-350COPD) = 変数出力::format_R_pName " + format_R_pName);
strCommandG += format_R_pName;
//=== 生年月日
strCommandG += pSng;
//=== 年齢(3)
// Val pAge
String Val_pAge = pAge.replaceAll("[^\\d]", "").trim();
Log.d(TAG, "肺活量計(SP-350COPD) = 変数出力::Val_pAge " + Val_pAge);
String format_L_pAge = String.format("%3s", Val_pAge);
Log.d(TAG, "肺活量計(SP-350COPD) = 変数出力::format_L_pAge " + format_L_pAge);
strCommandG += format_L_pAge;
Log.d(TAG, "肺活量計(SP-350COPD) = 変数出力::strCommandG " + strCommandG);
//=== 性別
switch (pSex) {
case "1":
strCommandG += "M";
Log.d(TAG, "肺活量計(SP-350COPD)case 1 = 変数出力::pSex " + pSex);
break;
case "2":
strCommandG += "F";
Log.d(TAG, "肺活量計(SP-350COPD)case 2 = 変数出力::pSex " + pSex);
break;
default:
strCommandG += "N";
Log.d(TAG, "肺活量計(SP-350COPD)case default = 変数出力::pSex " + pSex);
break;
} // === END swich
//=== 身長
if (pHeight.trim().length() == 0) {
strCommandG += "000.0";
} else {
strCommandG += pHeight.format("%5s", pHeight).replace(" ", "0");
} // === END if
Log.d(TAG, "肺活量計(SP-350COPD)変数出力::pHeight " + pHeight);
//=== 体重
if (pWeight.trim().length() == 0) {
strCommandG += "000.0";
} else {
strCommandG += pWeight.format("%5s", pWeight).replace(" ", "0");
} // === END if
Log.d(TAG, "肺活量計(SP-350COPD)変数出力::pWeight " + pWeight);
//=== 依頼科(30)
strCommandG += String.format("%-30s", " ");
Log.d(TAG, "肺活量計(SP-350COPD)変数出力::strCommandG " + strCommandG);
//=== 医師名(30)
strCommandG += String.format("%-30s", " ");
Log.d(TAG, "肺活量計(SP-350COPD)変数出力::strCommandG " + strCommandG);
//=== ETX Chr(3) => ETX (テキスト終了) 0x03 電文(データ部または電文全体)の終了位置を示すコード
char char_ETX = 0x03;
String str_Ext = String.valueOf(char_ETX);
strCommandG += str_Ext;
byBuff = strCommandG.getBytes(AppCharset.SHIFT_JIS);
// コマンド送信
connection.send(byBuff, byBuff.length);
//=== ASK 待ち
// vb ソース Recv(byStatus, intRecvSize)
btRet = connection.receiveDeviceValue(status, resultSize);
if (btRet < 0) {
// キャンセル
receiveRet = StatusConstants.RET_ERR_CANCEL;
Log.d(TAG, "肺活量計(SP-350COPD) = [Recv] 受信キャンセル (STX無し)" + i);
return;
}
String Tmp_ii = ByteUtil.bytesToString(status, 0, resultSize.get());
//=== ASK contains Start
char Chr_6 = 0x06;
String Chr_6_str = String.valueOf(Chr_6);
if(Tmp_ii.contains(Chr_6_str)) {
i = 1;
Log.d(TAG, "if 文内 ASK ::: i:::" + "値:::" + i);
} else {
i = 0;
Log.d(TAG, "if 文内 ASK else 比較文字列無し ::: i:::" + "値:::" + i);
}
//=== ASK contains END
if (i < 0) {
// キャンセル
receiveRet = StatusConstants.RET_ERR_CANCEL;
Log.d(TAG, "肺活量計(SP-350COPD) = [Recv] 受信キャンセル (ASK)");
return;
}
//=== ENQ 待ち
btRet = connection.receiveDeviceValue(status, resultSize);
if (btRet < 0) {
// キャンセル
receiveRet = StatusConstants.RET_ERR_CANCEL;
Log.d(TAG, "肺活量計(SP-350COPD) = [Recv] 受信キャンセル (ENQ 待ち)" + i);
return;
}
//=== ENQ contains start
String Tmp_iii = ByteUtil.bytesToString(status, 0, resultSize.get());
char Chr_5_02 = 0x05;
String Chr_5_02_str = String.valueOf(Chr_5_02);
if(Tmp_iii.contains(Chr_5_02_str)) {
i = 1;
Log.d(TAG, "if 文内 ENQ ::: i:::" + "値:::" + i);
} else {
i = 0;
Log.d(TAG, "if 文内 ENQ ::: i:::" + "値:::" + i);
}
if (i < 0) {
// キャンセル ENQ なし
receiveRet = StatusConstants.RET_ERR_CANCEL;
Log.d(TAG, "肺活量計(SP-350COPD) = [Recv] 受信キャンセル (ENQ なし)" + i);
return;
}
//=== ASK送信
// === コマンド 送信 === 0x06
byBuff = strSendAck.getBytes(AppCharset.SHIFT_JIS);
connection.send(byBuff, byBuff.length);
bCancelFlag = false;
int test_count = 0;
boolean Case_Flg = false;
boolean loop_Flg = true; // ループ用 フラグ
while (loop_Flg) {
//=== 値取得 btRet = connection.receiveDeviceValue(status, resultSize); コマンドは最後の RSR , 06 04 06 を返していない
//=== これ => コマンドはちゃんと 返ってるが値がちゃんととれない、connection.receive(status, status.length, resultSize);
// btRet = connection.receive(status, status.length, resultSize);
btRet = connection.receiveDeviceValue(status, resultSize);
// connection.receiveDeviceValue(status, resultSize);
// connection.receive(status, status.length, resultSize);
Log.d(TAG, "肺活量計(SP-350COPD) = while 内 ::: btRet ::: test_count " + btRet + ":::" + test_count);
if (resultSize.get() <= 0) {
bCancelFlag = true;
}
/*
else {
bCancelFlag = false;
}
*/
Log.d(TAG, "肺活量計(SP-350COPD) = while 内 ::: 配列::: status[0]" + status[0]);
switch (status[0]) {
case 1:
// SOH 先頭のヘッダーレコード
if (!bCancelFlag) {
// ASK 送信 strSendAck
byBuff = strSendAck.getBytes(AppCharset.SHIFT_JIS);
connection.send(byBuff, byBuff.length);
} else {
// CAN送信 strSendCan
byBuff = strSendCan.getBytes(AppCharset.SHIFT_JIS);
connection.send(byBuff, byBuff.length);
}
break;
case 2:
// STX 通常レコード
Log.d(TAG, "case 2 : BtSP350COPD_sub1 引数 => resultSize.get()" + resultSize.get());
// ******************** BtSP350COPD_sub1 function ********************
BtSP350COPD_sub1(status, resultSize.get());
// SOH 先頭のヘッダーレコード
if (!bCancelFlag) {
// ASK 送信
byBuff = strSendAck.getBytes(AppCharset.SHIFT_JIS);
connection.send(byBuff, byBuff.length);
} else {
// CAN送信
byBuff = strSendCan.getBytes(AppCharset.SHIFT_JIS);
connection.send(byBuff, byBuff.length);
}
break;
case 0x18 : // 24: // Chr(&H18)
// CAN 分析器 での キャンセル
// CAN送信
byBuff = strSendAck.getBytes(AppCharset.SHIFT_JIS);
connection.send(byBuff, byBuff.length);
bCancelFlag = true;
return;
case 4: // Chr(4)
// EOT 正常終了
// ACK 送信
byBuff = strSendAck.getBytes(AppCharset.SHIFT_JIS);
connection.send(byBuff, byBuff.length);
return;
default:
// 無処理
break;
} // END switch
//===============> break でここで抜けないと、後の for がエラーになる。
// loop_Flg = true; // while 用 ループフラグ true の場合はループを回している。
Log.d(TAG, "肺活量計(SP-350COPD while ループ内::: while loop_Flg :::" + loop_Flg);
test_count++;
Log.d(TAG, "肺活量計(SP-350COPD while ループ内::: test_count ::: 値" + test_count);
} // END while
Log.d(TAG, "肺活量計(SP-350COPD while **************** ループ 抜け *****************" +
" bCancelFlag :::" + bCancelFlag);
if (!bCancelFlag) {
for (int k = 1; k < 8; k++) {
receiveData[k] = receiveData[k].trim();
// && StringUtil.isNumeric(receiveData[k]) == false
Log.d(TAG, "肺活量計(SP-350COPD forループ内::: receiveData[k]" + receiveData[k]);
if (receiveData[k].length() > 0 && StringUtil.isNumeric(receiveData[k]) == false) {
// ブランク 数字 以外はエラー
receiveRet = StatusConstants.RET_ERR_CANCEL;
Log.d(TAG, "肺活量計(SP-350COPD) = [Recv] 異常終了");
}
} // ======= END for
Log.d(TAG, "loop_Flg while 抜け後");
// 肺活量(L)
if (receiveData[1].length() != 0) {
// 10進数 で 0.00 の書式
// receiveData[1] = String.format("%0.00d", receiveData[1]);
Log.d(TAG, "receiveData[1] 取得前");
receiveData[1] = receiveData[1];
Log.d(TAG, "値取得 length() != 0 receiveData[1] " + receiveData[1]);
}
// 一秒量(L)
if (receiveData[3].length() != 0) {
// 10進数 で 0.00
// receiveData[3] = String.format("%0.00d", receiveData[3]);
receiveData[3] = receiveData[3];
Log.d(TAG, "値取得 length() != 0 receiveData[3] " + receiveData[3]);
}
// 予測肺活量(L)
if (receiveData[5].length() != 0) {
// 10進数 で 0.00
// receiveData[5] = String.format("%0.00d", receiveData[5]);
receiveData[5] = receiveData[5];
Log.d(TAG, "値取得 length() != 0 receiveData[5] " + receiveData[5]);
}
// 努力肺活量(L)
if (receiveData[6].length() != 0) {
// 10進数 で 0.00
// receiveData[6] = String.format("%0.00d", receiveData[6]);
receiveData[6] = receiveData[6];
Log.d(TAG, "値取得 length() != 0 receiveData[6] " + receiveData[6]);
}
// 予測一秒量(L)
if (receiveData[7].length() != 0) {
// 10進数 で 0.00
// receiveData[7] = String.format("%0.00d", receiveData[7]);
receiveData[7] = receiveData[7];
Log.d(TAG, "値取得 length() != 0 receiveData[7] " + receiveData[7]);
}
//====== 1秒率 ======
if (receiveData[4].length() != 0) {
// String Temp_receiveData_4 = receiveData[4].substring(0, receiveData[4].length() - 1);
receiveData[4] = receiveData[4];
Log.d(TAG, "値取得 length() != 0 receiveData[4] " + receiveData[4]);
} else {
receiveData[4] = "0";
} // ====== END IF
if (receiveData[8].length() != 0) {
receiveData[8] = receiveData[8];
Log.d(TAG, "値取得 length() != 0 receiveData[8] " + receiveData[8]);
} else {
receiveData[8] = "0";
} // ====== END IF
} else {
// キャンセル
receiveRet = StatusConstants.RET_ERR_CANCEL;
Log.d(TAG, "肺活量計(SP-350COPD) = [Recv] キャンセル 001");
return;
}
// '仮想シリアルポート切断()
connection.close();
// 正常終了
// setReceiveRetWithBtRet(btRet);
} else {
// キャンセル
receiveRet = StatusConstants.RET_ERR_CANCEL;
Log.d(TAG, "肺活量計(SP-350COPD) = [Recv] キャンセル 002");
return;
}
// 正常終了
setReceiveRetWithBtRet(btRet);
} catch (Exception e) {
e.printStackTrace();
receiveRet = StatusConstants.RET_ERR_MEASURE;
} finally {
// 正常終了
setReceiveRetWithBtRet(btRet);
// '仮想シリアルポート切断()
connection.close();
}
}
// ------------------------
/**
* SP 350 (肺機能)値取得 関数
* @param pData
* @param pSize
*/
private void BtSP350COPD_sub1(byte[] pData, int pSize) {
String strData;
if (pSize >= 10) {
String tmp = ByteUtil.bytesToString(pData, 7 - 1, 4);
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub1) = tmp" + tmp);
// switch (ByteUtil.bytesToString(pData, 7 - 1, 4)) {
switch (tmp) {
case "0201":
strData = ByteUtil.bytesToString(pData, 0, pSize);
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub1) = case:0201 strData:::" + strData);
receiveData[5] = BtSP350COPD_sub2(strData, "VC", 2); // 肺活量予測値
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub1)0201 receiveData[5] :::" + receiveData[5]);
receiveData[1] = BtSP350COPD_sub2(strData, "VC", 3); // 肺活量
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub1)0201 receiveData[1] :::" + receiveData[1]);
receiveData[2] = BtSP350COPD_sub2(strData, "VC", 4); // %肺活量
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub1)0201 receiveData[2] :::" + receiveData[2]);
break;
case "0202":
strData = ByteUtil.bytesToString(pData, 0, pSize);
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub1) = case:0202 strData:::" + strData);
receiveData[6] = BtSP350COPD_sub2(strData, "FVC", 3); // 努力肺活量
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub1)0202 receiveData[6] :::" + receiveData[6]);
receiveData[3] = BtSP350COPD_sub2(strData, "FEV1", 3); // 一秒量
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub1)0202 receiveData[3] :::" + receiveData[3]);
receiveData[4] = BtSP350COPD_sub2(strData, "FEV1%G", 3); // 一秒率 FEV1%G
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub1)0202 receiveData[4] :::" + receiveData[4]);
receiveData[7] = BtSP350COPD_sub2(strData, "FEV1", 2); // 一秒量 予測値
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub1)0202 receiveData[7] :::" + receiveData[7]);
receiveData[8] = BtSP350COPD_sub2(strData, "FEV1", 4); // %一秒量
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub1)0202 receiveData[8] :::" + receiveData[8]);
break;
default:
break;
/*
case "0301":
break;
*/
} // ====== END switch
} // ====== END if
} // ===================== END BtSP350COPD_sub1
// ------------------------
private String BtSP350COPD_sub2(String pData, String pKomoku, int pSeq) {
char Chr_9_Temp = 0x09;
String strKey = pKomoku + String.valueOf(Chr_9_Temp);
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub2) = Chr_9_Temp:::" + strKey);
int intPos = 0;
int intPo2 = 0;
int i;
String r_data = "";
String r = "false";
// vb ソース
// === intPos = InStr(pData, strKey)
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub2) = pData:::" + pData);
intPos = pData.indexOf(strKey);
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub2) = intPos:::" + intPos);
if (intPos == 0) {
return r;
} // === END if
for (i = 0; i < pSeq; i++) {
// VB ソース
// === intPo2 = InStr(intPos + 1, pData, Chr(9))
intPo2 = pData.indexOf(0x09, intPos + 1);
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub2) for 内 = intPo2:::" + intPo2);
if (intPo2 == 0) {
return r;
} // === END if
intPos = intPo2;
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub2) for 内 if 内 = intPos:::" + intPos);
} // === END for
// VB ソース
// === intPo2 = InStr(intPos + 1, pData, Chr(9))
intPo2 = pData.indexOf(0x09, intPos + 1);
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub2) for 抜け後 if = intPo2:::" + intPo2);
if (intPo2 == 0) {
return r;
} // === END if
intPos += 1;
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub2)= intPos += 1:::" + intPos);
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub2)= intPos2 += 1:::" + intPo2);
// === VB ソース BtSP350COPD_sub2 = Mid(pData, intPos, in
//BtSP350COPD_sub2 = pData.substring(intPos, intPo2 - intPo2);
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub2)= pData function return 前:::" + pData);
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub2)= intPos, intPo2 - intPos 前 値:::" + (intPos + (intPo2 - intPos)));
r_data = pData.substring(intPos, intPos + (intPo2 - intPos));
Log.d(TAG, "肺活量計 function(BtSP350COPD_sub2)= *** 返り値 *** r_data 前 値:::" + r_data);
return r_data;
} // ========================= BtSP350COPD_sub2 END