internal static byte[] FinsCmd(RorW rw, PlcMemory mr, MemoryType mt, short ch, short offset, short cnt) { byte[] buffer = new byte[0x22]; buffer[0] = 70; buffer[1] = 0x49; buffer[2] = 0x4e; buffer[3] = 0x53; buffer[4] = 0; buffer[5] = 0; if (rw == RorW.Read) { buffer[6] = 0; buffer[7] = 0x1a; } else if (mt == MemoryType.Word) { buffer[6] = (byte)(((cnt * 2) + 0x1a) / 0x100); buffer[7] = (byte)(((cnt * 2) + 0x1a) % 0x100); } else { buffer[6] = 0; buffer[7] = 0x1b; } buffer[8] = 0; buffer[9] = 0; buffer[10] = 0; buffer[11] = 2; buffer[12] = 0; buffer[13] = 0; buffer[14] = 0; buffer[15] = 0; buffer[0x10] = 0x80; buffer[0x11] = 0; buffer[0x12] = 2; buffer[0x13] = 0; buffer[20] = BasicClass.plcNode; buffer[0x15] = 0; buffer[0x16] = 0; buffer[0x17] = BasicClass.pcNode; buffer[0x18] = 0; buffer[0x19] = 0xff; if (rw == RorW.Read) { buffer[0x1a] = 1; buffer[0x1b] = 1; } else { buffer[0x1a] = 1; buffer[0x1b] = 2; } buffer[0x1c] = GetMemoryCode(mr, mt); buffer[0x1d] = (byte)(ch / 0x100); buffer[30] = (byte)(ch % 0x100); buffer[0x1f] = (byte)offset; buffer[0x20] = (byte)(cnt / 0x100); buffer[0x21] = (byte)(cnt % 0x100); return(buffer); }
/// <summary> /// Fins读写指令生成 /// </summary> /// <param name="rw">读写类型</param> /// <param name="mr">寄存器类型</param> /// <param name="mt">地址类型</param> /// <param name="ch">起始地址</param> /// <param name="offset">位地址:00-15,字地址则为00</param> /// <param name="cnt">地址个数,按位读写只能是1</param> /// <returns></returns> internal byte[] FinsCmdUDP(RorW rw, PlcMemory mr, MemoryType mt, short ch, short offset, short cnt, ref int iLength) { int index = 0; byte[] array = new byte[256]; //UDP FINS header array[index++] = 0x80; //ICF - Display frame information array[index++] = 0x00; //RSV - Reserved by system array[index++] = 0x02; //GCT - Permissible number of gateways array[index++] = 0x00; //DNA - Destination network address array[index++] = plcNode; //DA1 - Destination node address (PLC node) array[index++] = 0x00; //DA2 - Destination unit address array[index++] = 0x00; //SNA - Source network address array[index++] = pcNode; //SA1 - Source node address (PC node) array[index++] = 0x00; //SA2 - Source unit address array[index++] = 0x00; //SID - Service ID //FINS Command (read: 0101 / write: 0102) array[index++] = 0x01; //MRC - Main request code array[index++] = (rw == RorW.Read) ? (byte)0x01: (byte)0x02; //SRC - Sub-request code //FINS Parameter/data Command parameters and send data.The data length depends on the MRC and SRC array[index++] = GetMemoryCode(mr, mt); //I/O Memory area code (DM - 0x82 ) array[index++] = (byte)(ch / 256); //Beginning address (High byte) array[index++] = (byte)(ch % 256); //Beginning address (Low byte) array[index++] = (byte)offset; //Bit address (00 - 15 / 00) array[index++] = (byte)(cnt / 256); //NO. of items (High byte) array[index++] = (byte)(cnt % 256); //NO. of items (low byte) iLength = index; return(array); }
/// <summary> /// Fins读写指令生成 /// </summary> /// <param name="rw">读写类型</param> /// <param name="mr">寄存器类型</param> /// <param name="mt">地址类型</param> /// <param name="ch">起始地址</param> /// <param name="offset">位地址:00-15,字地址则为00</param> /// <param name="cnt">地址个数,按位读写只能是1</param> /// <returns></returns> internal static byte[] FinsCmd(RorW rw, PlcMemory mr, MemoryType mt, short ch, short offset, short cnt) { //byte[] array; //if (rw == RorW.Read) // array = new byte[34]; //else // array = new byte[(int)(cnt * 2 + 33 + 1)];//长度是如何确定的在fins协议174页 byte[] array = new byte[34]; //写指令还有后面的写入数组需要拼接在一起! //TCP FINS header array[0] = 0x46; //F array[1] = 0x49; //I array[2] = 0x4E; //N array[3] = 0x53; //S array[4] = 0; //cmd length array[5] = 0; //指令长度从下面字节开始计算array[8] if (rw == RorW.Read) { array[6] = 0; array[7] = 0x1A;//26 } else { //写数据的时候一个字占两个字节,而一个位只占一个字节 if (mt == MemoryType.Word) { array[6] = (byte)((cnt * 2 + 26) / 256); array[7] = (byte)((cnt * 2 + 26) % 256); } else { array[6] = 0; array[7] = 0x1B; } } array[8] = 0;//frame command array[9] = 0; array[10] = 0; array[11] = 0x02; array[12] = 0;//err array[13] = 0; array[14] = 0; array[15] = 0; //command frame header array[16] = 0x80; //ICF array[17] = 0x00; //RSV array[18] = 0x02; //GCT, less than 8 network layers array[19] = 0x00; //DNA, local network array[20] = BasicClass.plcNode; //DA1 array[21] = 0x00; //DA2, CPU unit array[22] = 0x00; //SNA, local network array[23] = BasicClass.pcNode; //SA1 array[24] = 0x00; //SA2, CPU unit array[25] = 0xFF; //TODO:array[25] = Convert.ToByte(21);//SID//?????----------------------------------00-FF任意值 //指令码 if (rw == RorW.Read) { array[26] = 0x01;//cmdCode--0101 array[27] = 0x01; } else { array[26] = 0x01;//write---0102 array[27] = 0x02; } //地址 //array[28] = (byte)mr; array[28] = GetMemoryCode(mr, mt); array[29] = (byte)(ch / 256); array[30] = (byte)(ch % 256); array[31] = (byte)offset; array[32] = (byte)(cnt / 256); array[33] = (byte)(cnt % 256); return(array); }