private byte[] createRespondMessage() { int numberOfPoints = 0; int bytesToSend = 0; int startAddress = 0; numberOfPoints = (readData[4] << 8) | readData[5]; bytesToSend = 2 * numberOfPoints + 5; byte[] respondMessage = new byte[bytesToSend]; respondMessage[0] = _slaveAddress; respondMessage[1] = 3; respondMessage[2] = Convert.ToByte(2 * numberOfPoints); startAddress = (readData[2] << 8) | readData[3]; int j = 0; for (int i = 0; i < numberOfPoints; i++) { respondMessage[i + j + 3] = Convert.ToByte((AO[startAddress + i] >> 8) & 0xff); respondMessage[i + j + 4] = Convert.ToByte(AO[startAddress + i] & 0xff); j++; } byte[] crcCalculation = CRCStuff.calculateCRC(ref respondMessage, bytesToSend - 2); respondMessage[bytesToSend - 2] = crcCalculation[0]; respondMessage[bytesToSend - 1] = crcCalculation[1]; return(respondMessage); }
private void raiseAppSerialDataEvent(byte[] buf) { Buffer.BlockCopy(buf, 0, readData, 0, buf.Length); //проверка прочитанного сообщения на принадлежность к типу Modbus-запроса if (readData[0] == _slaveAddress && readData[1] == 0x03) { if (CRCStuff.checkCRC(ref readData, 8)) { byte[] messageToSend = createRespondMessage(); System.Threading.Thread.Sleep(_slaveDelay); StreamWriter(messageToSend, 0, messageToSend.Length); //addLog(createLogStr(ref messageToSend), LogType.TX, worker); } else { //addLog("", LogType.CRC_ERR, worker); } } }