public static byte[] WriteAllDO(int addr, int ionum, bool openclose) { byte[] src = new byte[10 + (ionum - 1) / 8]; int index = 0; src[index++] = (byte)addr; src[index++] = 0x0f; src[index++] = 0x00; src[index++] = 0x00; src[index++] = 0x00; src[index++] = (byte)ionum; src[index++] = (byte)((ionum + 7) / 8); src[index++] = (byte)((openclose) ? 0xff : 0x00); if (ionum > 8) { src[index++] = (byte)((openclose) ? 0xff : 0x00); } if (ionum > 16) { src[index++] = (byte)((openclose) ? 0xff : 0x00); } if (ionum > 24) { src[index++] = (byte)((openclose) ? 0xff : 0x00); } ushort crc = CMBRTU.CalculateCrc(src, index); src[index++] = (byte)(crc & 0xff); src[index++] = (byte)(crc >> 8); return(src); }
public static byte[] WriteDO(int addr, int io, bool openclose) { byte[] src = new byte[8]; src[0] = (byte)addr; src[1] = 0x05; src[2] = 0x00; src[3] = (byte)io; src[4] = (byte)((openclose) ? 0xff : 0x00); src[5] = 0x00; ushort crc = CMBRTU.CalculateCrc(src, 6); src[6] = (byte)(crc & 0xff); src[7] = (byte)(crc >> 8); return(src); }
public static byte[] ReadAIInfo(int addr, int regstart, int regnum) { byte[] src = new byte[8]; src[0] = (byte)addr; src[1] = 0x04; src[2] = (byte)(regstart >> 8); src[3] = (byte)(regstart & 0xff); src[4] = (byte)(regnum >> 8); src[5] = (byte)(regnum & 0xff); ushort crc = CMBRTU.CalculateCrc(src, 6); src[6] = (byte)(crc & 0xff); src[7] = (byte)(crc >> 8); return(src); }
public static byte[] ReadDI(int addr, int dinum) { byte[] src = new byte[8]; src[0] = (byte)addr; src[1] = 0x02; src[2] = 0x00; src[3] = 0x00; src[4] = 0x00; src[5] = (byte)dinum; ushort crc = CMBRTU.CalculateCrc(src, 6); src[6] = (byte)(crc & 0xff); src[7] = (byte)(crc >> 8); return(src); }
//分析从继电器收到的信息 private byte[] AnalysisRcv(byte[] src, int len) { if (len < 6) { return(null); } if (src[0] != 254) { return(null); } switch (src[1]) { case 0x01: if (CMBRTU.CalculateCrc(src, src[2] + 5) == 0x00) { byte[] dst = new byte[src[2]]; for (int i = 0; i < src[2]; i++) { dst[i] = src[3 + i]; } return(dst); } break; case 0x02: if (CMBRTU.CalculateCrc(src, src[2] + 5) == 0x00) { byte[] dst = new byte[src[2]]; for (int i = 0; i < src[2]; i++) { dst[i] = src[3 + i]; } return(dst); } break; case 0x04: if (CMBRTU.CalculateCrc(src, src[2] + 5) == 0x00) { byte[] dst = new byte[src[2]]; for (int i = 0; i < src[2]; i++) { dst[i] = src[3 + i]; } return(dst); } break; case 0x05: if (CMBRTU.CalculateCrc(src, 8) == 0x00) { byte[] dst = new byte[1]; dst[0] = src[4]; return(dst); } break; case 0x0f: if (CMBRTU.CalculateCrc(src, 8) == 0x00) { byte[] dst = new byte[1]; dst[0] = 1; return(dst); } break; } return(null); }