/// <summary> /// 通过TCP/IP写入byte[]类型数据到对应PLC中的DB块的对应地址里 /// </summary> /// <param name="dataType"></param> /// <param name="db"></param> /// <param name="startByteAdr"></param> /// <param name="value"></param> /// <returns></returns> public ErrorCode WriteBytes(DataType dataType, int db, int startByteAdr, byte[] value) { byte[] bReceive = new byte[513]; int varCount = 0; try { /*// -----add by wanxiaona20150909 * string data = string.Empty; * for (int i = 0; i < value.Length; i++) * { * data += string.Format("[{0}]", value[i]); * } * CLOGException.Trace(0, "CommunicationLib.CTcpClientAccessTimer.WriteBytes ", string.Format("IP:{0} db:{1} startByteAdr:{2} DATA:{3}", IP, db, startByteAdr, data)); * // -----end by wanxiaona20150909*/ varCount = value.Length; // first create the header int packageSize = 35 + value.Length; Types.ByteArray package = new Types.ByteArray(packageSize); package.Add(new byte[] { 3, 0, 0 }); package.Add((byte)packageSize); package.Add(new byte[] { 2, 0xf0, 0x80, 0x32, 1, 0, 0 }); package.Add(Types.Word.ToByteArray((ushort)(varCount - 1))); package.Add(new byte[] { 0, 0x0e }); package.Add(Types.Word.ToByteArray((ushort)(varCount + 4))); package.Add(new byte[] { 0x05, 0x01, 0x12, 0x0a, 0x10, 0x02 }); package.Add(Types.Word.ToByteArray((ushort)varCount)); package.Add(Types.Word.ToByteArray((ushort)(db))); package.Add((byte)dataType); package.Add((byte)0); package.Add(Types.Word.ToByteArray((ushort)(startByteAdr * 8))); package.Add(new byte[] { 0, 4 }); package.Add(Types.Word.ToByteArray((ushort)(varCount * 8))); // now join the header and the data package.Add(value); _mSocket.Send(package.array, package.array.Length, SocketFlags.None); _mSocket.Receive(bReceive, 512, SocketFlags.None); if (bReceive[21] != 0xff) { CLOGException.Trace("CommunicationLib.CTcpClientAccessTimer.WriteBytes ", IP + " ErrorCode.WrongNumberReceivedBytes"); return(ErrorCode.WrongNumberReceivedBytes); } return(ErrorCode.NoError); } catch (Exception ex) { LastErrorCode = ErrorCode.WriteData; LastErrorString = ""; CLOGException.Trace("函数CommunicationLib.CTcpClientAccessTimer.WriteBytes 异常," + IP, CBaseMethods.MyBase.GetExceptionInfo(ex)); return(LastErrorCode); } }
/// <summary> /// 通过TCP/IP从对应PLC中的DB块的对应地址中读取byte[]类型数据 /// </summary> /// <param name="dataType"></param> /// <param name="DB"></param> /// <param name="startByteAdr"></param> /// <param name="count"></param> /// <returns></returns> public byte[] ReadBytes(DataType dataType, int DB, int startByteAdr, int count) { byte[] bytes = new byte[count]; try { // first create the header const int packageSize = 31; Types.ByteArray package = new Types.ByteArray(packageSize); package.Add(new byte[] { 0x03, 0x00, 0x00 }); package.Add((byte)packageSize); package.Add(new byte[] { 0x02, 0xf0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x04, 0x01, 0x12, 0x0a, 0x10 }); // package.Add(0x02); // datenart switch (dataType) { case DataType.Timer: case DataType.Counter: package.Add((byte)dataType); break; default: package.Add(0x02); break; } package.Add(Types.Word.ToByteArray((ushort)(count))); package.Add(Types.Word.ToByteArray((ushort)(DB))); package.Add((byte)dataType); package.Add((byte)0); switch (dataType) { case DataType.Timer: case DataType.Counter: package.Add(Types.Word.ToByteArray((ushort)(startByteAdr))); break; default: package.Add(Types.Word.ToByteArray((ushort)((startByteAdr) * 8))); break; } _mSocket.Send(package.array, package.array.Length, SocketFlags.None); byte[] bReceive = new byte[512]; _mSocket.Receive(bReceive, 512, SocketFlags.None); if (bReceive[21] != 0xff) { CLOGException.Trace("CommunicationLib.CTcpClientAccessTimer.ReadBytes ", IP + " ErrorCode.WrongNumberReceivedBytes"); return(null); } /*// -----add by wanxiaona20150910 * string data = string.Empty; * byte[] temp = new byte[count]; * for (int i = 0; i < bReceive.Length && i < count + 25; i++) * { * data += string.Format("[{0}]", bReceive[i]); * if (i< count) * { * temp[i] = bReceive[i + 25]; * } * } * CLOGException.Trace(0, "CommunicationLib.CTcpClientAccessTimer.ReadBytes ", string.Format("IP:{0} DB:{1} bReceive:{2}", IP, DB, data)); * data = string.Empty; * Int16[] intemp = Types.Int.ToArray(temp); * for (int i = 0; i < intemp.Length; i++) * { * data += string.Format("[{0}]", intemp[i]); * } * CLOGException.Trace(0, "CommunicationLib.CTcpClientAccessTimer.ReadBytes ", string.Format("IP:{0} DB:{1} bReceiveint:{2}", IP, DB, data)); * // -----end by wanxiaona20150910*/ for (int cnt = 0; cnt < count; cnt++) { bytes[cnt] = bReceive[cnt + 25]; } return(bytes); } catch (SocketException ex) { m_isConnected = false; LastErrorCode = ErrorCode.WriteData; LastErrorString = ex.Message; CLOGException.Trace("函数CommunicationLib.CTcpClientAccessTimer.ReadBytes SocketException异常," + IP, CBaseMethods.MyBase.GetExceptionInfo(ex)); bytes = null; return(null); } catch (Exception ex) { LastErrorCode = ErrorCode.WriteData; LastErrorString = ex.Message; CLOGException.Trace("函数CommunicationLib.CTcpClientAccessTimer.ReadBytes Exception异常," + IP, CBaseMethods.MyBase.GetExceptionInfo(ex)); bytes = null; return(null); } }