using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AuroraHeightSensor.Keyence { public partial class HeightSensorLKG85 { private bool ReceiveValue(string strCommand, ref string strReceiveValue) { string strFullCmd = string.Format("{0}\r", strCommand); strReceiveValue = WriteAndReadLine(strFullCmd); if (strReceiveValue == null) { Log.E("ReceiveValue return value is null"); return false; } return CheckError(strCommand, strReceiveValue) == e_HS_ERROR_CODE.ERROR_NO; } private bool SendCommand(string strCommand) { string strFullCmd = string.Format("{0}\r", strCommand); var strReceiveValue = WriteAndReadLine(strFullCmd); if (strReceiveValue == null) { Log.E("SendCommand return value is null"); return false; } return CheckError(strCommand, strReceiveValue) == e_HS_ERROR_CODE.ERROR_NO; } private e_HS_ERROR_CODE CheckError(string strCmd, string strReceiveValue) { if (strReceiveValue.Substring(0, 2) != "ER") { // check 1 : not 'ER' => check returned 2 word if (strCmd.Substring(0, 2) != strReceiveValue.Substring(0, 2)) { Log.E("Sent code and received code are different"); return e_HS_ERROR_CODE.ERROR_DIFFERENT_MAIN_COMMAND; } return e_HS_ERROR_CODE.ERROR_NO; } var splitedStr = strReceiveValue.Split(','); if (splitedStr.Length < 3) { Log.E("Received codes are not 3 string."); return e_HS_ERROR_CODE.ERROR_DIFFERENT_RETURN_COUNT; } string sReceiveMainCommand = splitedStr[1]; if (sReceiveMainCommand != strCmd.Substring(0, 2)) { Log.E("Sent code and received code are different on Error code : " + sReceiveMainCommand); return e_HS_ERROR_CODE.ERROR_DIFFERENT_MAIN_COMMAND; } string sErrorCode = splitedStr[2]; if (!int.TryParse(sErrorCode, out int iErrorCode)) { Log.E("Parse received Error code : " + sErrorCode); return e_HS_ERROR_CODE.ERROR_COMMAND; } // check 3 : case of 'ER' e_HS_ERROR_CODE errorCode = (e_HS_ERROR_CODE)iErrorCode; Log.E("Return Value Error : " + errorCode.ToString()); return errorCode; } private bool CheckRange(int channel, int minChannel, int maxChannel) { if ((channel >= minChannel) && (channel <= maxChannel)) { return true; } return false; } // 측정값 (8자리) // TODO : -FFFFFFF 와 +FFFFFFF 의 값에 대해서 확인 필요함. 아래 if 와 else if 문 // BOOL CHeightSensorCom::cmdGetMeasureValue(int iCH,double& dValue) 의 data get private bool GetFormatMeasureData(string strValue, ref double doubleValue) { if (strValue == "-FFFFFFF") { doubleValue = 999999.9; } else if (strValue == "+FFFFFFF") { doubleValue = 999999.9; } else { if (!double.TryParse(strValue, out doubleValue)) { return false; } } return true; } private char GetSign(double value) { return value >= 0 ? '+' : '-'; } private static string GetMeasureDoubleFormat(double measure, int formatLength) { double absMeasure = Math.Abs(measure); string strMeasure = absMeasure.ToString(); //Console.WriteLine(strMeasure + ", string length : " + strMeasure.Length + ", format length : " + formatLength); if (strMeasure.Length == formatLength) { // same format return strMeasure; } string modifyStrMeasure = string.Empty; int index = strMeasure.IndexOf("."); //Console.WriteLine(strMeasure + ", index of : " + index); if (strMeasure.Length > formatLength) { if (index < 0) { //Console.WriteLine("없는 경우로 판단, 이럴경우, 자릿수 만큼 F 로 채움"); for (int i = 0; i < formatLength; i++) { modifyStrMeasure += "F"; } } else { if (index > formatLength) { for (int i = 0; i < formatLength; i++) { modifyStrMeasure += "F"; } } else if (index == formatLength || (index == formatLength - 1)) { string format = string.Empty; for (int i = 0; i < formatLength; i++) { format += "0"; } modifyStrMeasure = (Math.Round(absMeasure, 0)).ToString(format); } else { modifyStrMeasure = (Math.Round(absMeasure, formatLength - 1 - index)).ToString(); } } } else { if (index < 0) { string format = string.Empty; for (int i = 0; i < formatLength; i++) { format += "0"; } modifyStrMeasure = (Math.Round(absMeasure, 0)).ToString(format); } else { for (int i = 0; i < formatLength - strMeasure.Length; i++) { modifyStrMeasure += "0"; } modifyStrMeasure += strMeasure; } } return modifyStrMeasure; } } }