protected byte[] GetWriteCommand(OmronFinsData arg, byte[] value, bool isBit) { byte[] command = new byte[26 + 8 + value.Length]; Array.Copy(BasicCommand, 0, command, 0, 4); byte[] tmp = BitConverter.GetBytes(command.Length - 8); Array.Reverse(tmp); tmp.CopyTo(command, 4); command[11] = 0x02; command[16] = 0x80; //ICF 信息控制字段 command[17] = 0x00; //RSV 保留字段 command[18] = 0x02; //GCT 网关计数 command[19] = 0x00; //DNA 目标网络地址 00:表示本地网络 0x01~0x7F:表示远程网络 command[20] = 0x13; //DA1 目标节点编号 0x01~0x3E:SYSMAC LINK网络中的节点号 0x01~0x7E:YSMAC NET网络中的节点号 0xFF:广播传输 command[21] = UnitAddress; //DA2 目标单元地址 command[22] = 0x00; //SNA 源网络地址 取值及含义同DNA字段 command[23] = 0x0B; //SA1 源节点编号 取值及含义同DA1字段 command[24] = 0x00; //SA2 源单元地址 取值及含义同DA2字段 command[25] = 0x00; //SID Service ID 取值0x00~0xFF,产生会话的进程的唯一标识 command[26] = 0x01; command[27] = 0x02; //Command Code 内存区域写入 command[28] = isBit ? arg.OmronFinsType.BitCode : arg.OmronFinsType.WordCode; arg.Content.CopyTo(command, 29); command[32] = isBit ? (byte)(value.Length / 256) : (byte)(value.Length / 2 / 256); command[33] = isBit ? (byte)(value.Length % 256) : (byte)(value.Length / 2 % 256); value.CopyTo(command, 34); return(command); }
private OmronFinsData ConvertArg(string address, bool isBit) { address = address.ToUpper(); var data = new OmronFinsData(); switch (address[0]) { case 'D': { data.OmronFinsType = OmronFinsType.DM; break; } case 'C': { data.OmronFinsType = OmronFinsType.CIO; break; } case 'W': { data.OmronFinsType = OmronFinsType.WR; break; } case 'H': { data.OmronFinsType = OmronFinsType.HR; break; } case 'A': { data.OmronFinsType = OmronFinsType.AR; break; } case 'E': { //TODO break; } default: throw new Exception("Address解析异常"); } if (address[0] == 'E') { //TODO } else { if (isBit) { // 位操作 string[] splits = address.Substring(1).Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries); ushort addr = ushort.Parse(splits[0]); data.Content = new byte[3]; data.Content[0] = BitConverter.GetBytes(addr)[1]; data.Content[1] = BitConverter.GetBytes(addr)[0]; if (splits.Length > 1) { data.Content[2] = byte.Parse(splits[1]); if (data.Content[2] > 15) { //输入的位地址只能在0-15之间 throw new Exception("位地址数据异常"); } } } else { // 字操作 ushort addr = ushort.Parse(address.Substring(1)); data.Content = new byte[3]; data.Content[0] = BitConverter.GetBytes(addr)[1]; data.Content[1] = BitConverter.GetBytes(addr)[0]; } } return(data); }