/// <summary> /// 에러 이벤트 전달 /// </summary> /// <param name="id"></param> /// <param name="code"></param> /// <param name="value"></param> /// <returns></returns> public int ErrorEventSender(string id, string code, string value) { string eventErrorLog = ""; DataRow dw; DateTime Logfatchtime = DateTime.Now; dw = mErrorListTable.NewRow(); dw["Error Time"] = Logfatchtime.ToString("yyyy-MM-dd HH:mm:ss"); dw["COM"] = mLineName; dw["BMS ID"] = id; dw["Error Code"] = code; dw["Error Value"] = value; string fileDay = DateTime.Now.ToString("yyyyMMdd"); IniFileHandle errorLog = new IniFileHandle(Properties.Settings.Default.BackupDir + "\\ErrorLog" + "\\" + fileDay + ".Log"); eventErrorLog += (eventErrorLog.Length == 0 ? "" : "\r\n") + string.Format("{0}\t\t{1}\t\t{2}\t\t{3}\t\t{4,-40}\t\t{5,-20}", Logfatchtime.ToString("yyyy-MM-dd HH:mm:ss"), dw["Error Time"], dw["COM"], dw["BMS ID"], dw["Error Code"], dw["Error Value"] ); errorLog.FileWrite(eventErrorLog); // 에러조회도 포트별 아이디별로 조회할 수 있게 추가함 2015.09.22 if (code != "COM Error" && code != "COM Error Cancel") { //string path = Properties.Settings.Default.BackupDir + "\\ErrorLog" + "\\" + mLineName + "\\" + id + "\\" + fileDay + ".Log"; IniFileHandle errorLog2 = new IniFileHandle(Properties.Settings.Default.BackupDir + "\\ErrorLog" + "\\" + mLineName + "\\" + id + "\\" + fileDay + ".Log"); errorLog2.FileWrite(eventErrorLog); } // 에러 발생 이벤트 전달 ErrorMessageEventArgs errorInfo = new ErrorMessageEventArgs(dw); OnErrorMessageChanged(errorInfo); return(0); }
/// <summary> /// 모드버스 에러 읽기 /// </summary> public int getError(SerialPort master, string bmsId, byte slaveId) { string eventErrorLog = ""; DataRow row = null; row = readError(master, bmsId, slaveId); if (row != null) { string fileDay = DateTime.Now.ToString("yyyyMMdd"); IniFileHandle errorLog = new IniFileHandle(Properties.Settings.Default.BackupDir + "\\ErrorLog" + "\\" + fileDay + ".Log"); DateTime Logfatchtime = DateTime.Now; eventErrorLog += (eventErrorLog.Length == 0 ? "" : "\r\n") + string.Format("{0}\t\t{1}\t\t{2}\t\t{3}\t\t{4,-40}\t\t{5,-20}", Logfatchtime.ToString("yyyy-MM-dd HH:mm:ss"), row["Error Time"], row["COM"], row["BMS ID"], row["Error Code"], row["Error Value"] ); errorLog.FileWrite(eventErrorLog); // 에러조회도 포트별 아이디별로 조회할 수 있게 추가함 2015.09.22 IniFileHandle errorLog2 = new IniFileHandle(Properties.Settings.Default.BackupDir + "\\ErrorLog" + "\\" + row["COM"] + "\\" + row["BMS ID"] + "\\" + fileDay + ".Log"); errorLog2.FileWrite(eventErrorLog); // 에러 발생 이벤트 전달 ErrorMessageEventArgs errorInfo = new ErrorMessageEventArgs(row); OnErrorMessageChanged(errorInfo); //mParent.Update(row); } //else //{ // Console.WriteLine("null"); //} return(0); }
/// <summary> /// 실시간 데이터 읽기 /// </summary> /// public int ModbusSerialAsciiMasterReadRegisters(SerialPort master, string bmsId, byte slaveId, int rowIndex) { // 에러 발생 int err = 0; // 읽기 주소 ushort startAddress = 0x8000; // 읽을 사이즈 ushort numRegisters = 0x32; // 레지스터 offset int regOffset = 16; // voltage offset byte readType = 4; // 그래프 출력 변수 Vars curValue = new Vars(); //if (master == null || master.Transport == null || mComport.IsOpen != true) // return 0; string eventLog = ""; int errorCheck = 0; DateTime Logfatchtime; byte[] buildPacket = new byte[7]; // read protocol size string recvBuffer = ""; ushort[] registers = new ushort[50]; byte[] checkBuffer = null; bool success = false; try { //3A30323034383030303030333234380D0A //3A 3032 3034 3830 3030 3030 3332 3438 0D 0A Utils util = new Utils(); modbus modbusUtil = new modbus(); // 전송 패킷 생성 modbusUtil.BuildModbusAsciiMessage(slaveId, readType, startAddress, numRegisters, ref buildPacket); // 헥사 스트링으로 변환 string convert = util.ByteArrayToHexString(buildPacket); // 공백문자제거 convert = convert.Replace(" ", ""); // STX, CR/LF 추가 string sendMessage = string.Format(":{0}\r\n", convert); // 전송 success = modbusUtil.SendFc4(master, sendMessage, ref recvBuffer); if (success) { checkBuffer = util.HexStringToByteArray(recvBuffer.Substring(1, recvBuffer.Length - 1)); if (!modbusUtil.CheckLRCResponse(checkBuffer)) { return(-2); } } else { // 이벤트 전달 //TimeoutEventArgs timeoutPort = new TimeoutEventArgs(this.mLineName); //OnTimeoutChanged(timeoutPort); return(-1); } //Return requested register values: for (int i = 0; i < (checkBuffer.Length - 4) / 2; i++) { registers[i] = checkBuffer[2 * i + 3]; registers[i] <<= 8; registers[i] += checkBuffer[2 * i + 4]; } // BMS 리스트 테이블에 수신한 ID 찾기 DataRow foundRow = mCommTable.Rows.Find(string.Format("0x{0:D2}", slaveId)); DataRow foundErrRow = mErrorTable.Rows.Find(string.Format("0x{0:D2}", slaveId)); // 있다면 업데이트 if (foundRow != null) { int index = mCommTable.Rows.IndexOf(foundRow); // 전압 값 int voltage = registers[regOffset++]; if (voltage > 0xF000) { voltage -= 65536; } curValue.Data[0] = (voltage / 10.0); //curValue.Data[0] = (registers[regOffset++] / 10.0); foundRow["VOLT"] = string.Format("{0:F1}", curValue.Data[0]); // 에러체크 errorCheck = setDataTableErrorSet(foundRow["ID"].ToString(), "VOLT", curValue.Data[0]); if (foundErrRow["VOLT"].ToString() != errorCheck.ToString()) { // 에러가 발생되었다면 이벤트를 보냄 if (errorCheck == 1) { ErrorEventSender(bmsId, "Voltage Error(SW)", curValue.Data[0].ToString()); } else { ErrorEventSender(bmsId, "Voltage Error Cancel(SW)", curValue.Data[0].ToString()); } foundErrRow["VOLT"] = errorCheck; errorCheck = 0; } regOffset += 3; // 전류 값 int current = registers[regOffset++]; if (current > 0xF000) { current -= 65536; } curValue.Data[1] = current; foundRow["CURRENT"] = string.Format("{0}", curValue.Data[1]); errorCheck = setDataTableErrorSet(foundRow["ID"].ToString(), "CURRENT", curValue.Data[1]); if (foundErrRow["CURRENT"].ToString() != errorCheck.ToString()) { // 에러가 발생되었다면 이벤트를 보냄 if (errorCheck == 1) { ErrorEventSender(bmsId, "Current Error(SW)", curValue.Data[1].ToString()); } else { ErrorEventSender(bmsId, "Current Error Cancel(SW)", curValue.Data[1].ToString()); } foundErrRow["CURRENT"] = errorCheck; errorCheck = 0; } regOffset += 3; // 온도 값 for (int i = 2; i < 8; i++) { int temperature = registers[regOffset++]; if (temperature > 0xF000) { temperature -= 65536; } curValue.Data[i] = temperature / 10.0; foundRow[i + 2] = string.Format("{0:F1}", curValue.Data[i]); // 온도 에러 체크 설정상태 errorCheck = setDataTableErrorSet(foundRow["ID"].ToString(), ("TEMP" + (i - 1)), curValue.Data[i]); if (foundErrRow[i + 2].ToString() != errorCheck.ToString()) { // 에러가 발생되었다면 이벤트를 보냄 if (errorCheck == 1) { ErrorEventSender(bmsId, "Temp" + (i - 1) + " Error(SW)", curValue.Data[i].ToString()); } else { ErrorEventSender(bmsId, "Temp" + (i - 1) + " Error Cancel(SW)", curValue.Data[i].ToString()); } foundErrRow[i + 2] = errorCheck; errorCheck = 0; } regOffset += 3; } // 접점 상태 읽기 ( 4포트 ) int[] contact = new int[4]; for (int i = 0; i < 4; i++) { // 접점 읽기 contact[i] = 0x01 & (registers[regOffset] >> i); foundRow[i + 10] = contact[i]; // 에러 읽기 errorCheck = 0x01 & (registers[regOffset] >> i + 8); if (foundErrRow[i + 10].ToString() != errorCheck.ToString()) { DataRow errorName = mDataTable.Rows.Find(string.Format("0x{0:D2}", slaveId)); // 에러가 발생되었다면 이벤트를 보냄 //if (errorCheck == 1) // ErrorEventSender(bmsId, errorName[i + 3] + " Error(SW)", contact[i].ToString()); //else // ErrorEventSender(bmsId, errorName[i + 3] + " Error Cancel(SW)", contact[i].ToString()); foundErrRow[i + 10] = errorCheck; errorCheck = 0; } } string fileDay = DateTime.Now.ToString("yyyyMMdd"); IniFileHandle log = new IniFileHandle(Properties.Settings.Default.BackupDir + "\\Log" + "\\" + mLineName + "\\" + foundRow["Name"].ToString() + "\\" + fileDay + ".Log"); Logfatchtime = DateTime.Now; eventLog += (eventLog.Length == 0 ? "" : "\r\n") + string.Format("{0}\t\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\t{11}\t{12}", Logfatchtime.ToString("yyyy-MM-dd HH:mm:ss"), foundRow["VOLT"].ToString(), foundRow["CURRENT"].ToString(), foundRow["TEMP1"].ToString(), foundRow["TEMP2"].ToString(), foundRow["TEMP3"].ToString(), foundRow["TEMP4"].ToString(), foundRow["TEMP5"].ToString(), foundRow["TEMP6"].ToString(), foundRow["Contact1"].ToString(), foundRow["Contact2"].ToString(), foundRow["Contact3"].ToString(), foundRow["Contact4"].ToString()); log.FileWrite(eventLog); curValue.Datetime = DateTime.Now; mGraphData[rowIndex].Add(curValue); if (mGraphData[rowIndex].Count > 20) { mGraphData[rowIndex].RemoveAt(0); } } } catch (Exception e) { Console.WriteLine(e.Message); } return(0); }