private void Log_Write(string data, enCmdType type, bool Error) { if (_dtLog.Rows.Count > 0) { if (Fnc.obj2String(_dtLog.Rows[0]["Log"]).Equals(data.Trim())) { _dtLog.Rows[0]["Time"] = DateTime.Now.ToString("HH:mm:ss"); return; } } DataRow dr = _dtLog.NewRow(); dr["Time"] = DateTime.Now.ToString("HH:mm:ss"); dr["Log"] = data; dr["Type"] = type.ToString(); dr["Err"] = Error; _dtLog.Rows.InsertAt(dr, 0); }
private void Data_Proc(byte[] data) { try { Console.WriteLine("[Data수신]" + Fnc.ByteArray2HexString(data, " ")); //헤더 체크 if (!Fnc.bytesEqual(data, 0, _bytHeader, 0, _bytHeader.Length)) { Log_Write("헤더 불일치", enCmdType.None, true); return; } string log = string.Empty; enCmdType cmd = enCmdType.None; //20~21 명령 타입 switch (data[20]) { case 0x54: //read command cmd = enCmdType.Read; break; case 0x58: //write command cmd = enCmdType.Write; break; case 0xBD: byte[] bytData = new byte[] { 0x01, 0xff }; Server.Send(bytData); break; default: break; } if (cmd == enCmdType.None) { return; } //22-23데이터타입 enDataType dType = enDataType.Block; if (data[22] != 0x14) { dType = enDataType.Unit; } //24-25 예약 //26-27 변수 개수 int cnt = fnc.ByteToInt(data, 26, 2); int idx = 28; int ridx = 30; DataRow dr; string add = string.Empty; byte[] send; int value; int vLen; //log = string.Format("[{0}]", cmd); if (dType == enDataType.Block) { vLen = fnc.ByteToInt(data, idx, 2); cnt = fnc.ByteToInt(data, idx + 2 + vLen, 2); cnt = Multiple > 1 ? cnt / Multiple : cnt; } if (cmd == enCmdType.Read) { if (dType == enDataType.Unit) { send = new byte[32 + cnt * 4]; } else { send = new byte[32 + cnt * 2]; } } else { send = new byte[30]; } for (int i = 0; i < cnt; i++) { //처음이거나 처리단위가 유닛이면 if (i == 0 || dType == enDataType.Unit) { //28-29 변수명 길이 vLen = fnc.ByteToInt(data, idx, 2); idx += 2; //30~ 변수명 add = fnc.BytesToAscii(data, idx, vLen); add = fnc.Address_SetAddType(add, Multiple); idx += vLen; } else { //블록단위(연속처리) add = fnc.Address_NetGet(add); } if (_dtAdd.Select(string.Format("Address = '{0}'", add)).Length > 0) { dr = _dtAdd.Select(string.Format("Address = '{0}'", add))[0]; } else { dr = _dtAdd.NewRow(); dr["Address"] = add; dr["Value"] = 0; _dtAdd.Rows.Add(dr); } if (cmd == enCmdType.Read) { if (dType == enDataType.Unit) { //data size fnc.ByteSetIntValue(send, ridx, 2, 2); ridx += 2; } //datavalue fnc.ByteSetIntValue(send, ridx, 2, Fnc.obj2int(dr["Value"])); ridx += 2; //if (i == 0 && dType == enDataType.Block & cnt > 1) // log += string.Format("[{0}", add); //else if (i == 0 || dType == enDataType.Unit) // log += string.Format("[{0}] ", add); log += string.Format("[{0}]{1} ", add, dr["Value"]); } else if (cmd == enCmdType.Write) { //값 길이 vLen = fnc.ByteToInt(data, idx, 2); idx += 2; //값 value = fnc.ByteToInt(data, idx, vLen); value = Multiple > 1 ? value / Multiple : value; log += string.Format("[{0}]{1}=>{2} ", add, dr["Value"], value); dr["Value"] = value; idx += 2; } } //if (cmd == enCmdType.Read && dType == enDataType.Block && cnt > 1) //{ // log += string.Format("~{0}] ", add); //} _bytHeader.CopyTo(send, 0); //plc info 10~11 send[11] = 1; //cpu info [A0]XGK [A4]XGI [A8]XGR send[12] = data[12]; //source of frame send[13] = 0x11; //invoke id send[14] = data[14]; send[15] = data[15]; //length fnc.ByteSetIntValue(send, 16, 2, send.Length); //response type if (cmd == enCmdType.Read) { send[20] = 0x55; //data type send[22] = data[22]; //data cnt fnc.ByteSetIntValue(send, 28, 2, cnt); //data size //fnc.ByteSetIntValue(send, 30, 2, 2); } else if (cmd == enCmdType.Write) { send[20] = (byte)(dType == enDataType.Block ? 0x59 : 0x58); send[20] = 0x59; //data type send[22] = data[22]; //data cnt fnc.ByteSetIntValue(send, 28, 2, cnt); } Log_Write(log, cmd, false); Server.Send(send); } catch (Exception ex) { ProcException(ex, "Data_Proc"); } }
private void Data_Proc(byte[] data) { enCmdType cmd = enCmdType.None; switch (data[0]) { case 0x01: //read command cmd = enCmdType.Read; break; case 0x03: //write command cmd = enCmdType.Write; break; case 0xBD: byte[] bytData = new byte[] { 0x01, 0xff }; Server.Send(bytData); this.Fp_InsertRow(bytData, true); break; default: break; } if (cmd == enCmdType.None) { return; } string strAdd = data[5].ToString("X4") + data[4].ToString("X2"); int intAdd = int.Parse(strAdd, System.Globalization.NumberStyles.HexNumber); DataRow dr; if (dtAdd.Select(string.Format("Address = {0}", intAdd)).Length > 0) { dr = dtAdd.Select(string.Format("Address = {0}", intAdd))[0]; } else { dr = dtAdd.NewRow(); dr["Address"] = intAdd; dr["Value"] = 0; dtAdd.Rows.Add(dr); } string strV; int intV; if (cmd == enCmdType.Read) { intV = (int)dr["Value"]; strV = intV.ToString("X4"); byte[] byt = new byte[] { 0x81, 0x00, 0x00, 0x00 }; byt[3] = byte.Parse(strV.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); byt[2] = byte.Parse(strV.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); Server.Send(byt); this.Fp_InsertRow(byt, true); } else { strV = data[13].ToString("X4") + data[12].ToString("X2"); intV = int.Parse(strV, System.Globalization.NumberStyles.HexNumber); dr["Value"] = intV; byte[] byt = new byte[] { 0x81, 0x00 }; Server.Send(byt); this.Fp_InsertRow(byt, true); } }