Пример #1
0
        private byte[] createRespondMessage()
        {
            int numberOfPoints = 0;
            int bytesToSend    = 0;
            int startAddress   = 0;

            numberOfPoints = (_messageReceived[4] << 8) | _messageReceived[5];
            bytesToSend    = 5 + numberOfPoints * 2;
            byte[] respondMessage = new byte[bytesToSend];
            respondMessage[0] = _slaveAddress;
            respondMessage[1] = 3;
            respondMessage[2] = Convert.ToByte(2 * numberOfPoints);
            startAddress      = (_messageReceived[2] << 8) | _messageReceived[3];
            lock (loc_aenvoyer)
            {
                int j = 0;
                for (int i = 0; i < numberOfPoints; i++)
                {
                    respondMessage[i + j + 3] = Convert.ToByte((Form1.aenvoyer[startAddress + i] >> 8) & 0xff);
                    respondMessage[i + j + 4] = Convert.ToByte(Form1.aenvoyer[startAddress + i] & 0xff);
                    j++;
                }
            }
            byte[] crcCalculation = CRCStuff.calculateCRC(ref respondMessage, bytesToSend - 2);
            respondMessage[bytesToSend - 2] = crcCalculation[0];
            respondMessage[bytesToSend - 1] = crcCalculation[1];
            return(respondMessage);
        }
Пример #2
0
 void serial1_DataReceived(object sender, SerialDataReceivedEventArgs e)
 {
     while (serial1.BytesToRead >= 8)
     {
         serial1.Read(_messageReceived, 0, 8);
         //addLog(createLogStr(ref _messageReceived), LogType.RX);
         if (_messageReceived[0] == _slaveAddress && _messageReceived[1] == 3)
         {
             if (CRCStuff.checkCRC(ref _messageReceived, 8))
             {
                 DateTime test = Form1.date_tcpip.AddMinutes(15);
                 if (test > DateTime.Now)
                 {
                     byte[] messageToSend = createRespondMessage();
                     System.Threading.Thread.Sleep(_slaveDelay);
                     serial1.Write(messageToSend, 0, messageToSend.Length);
                     if (reponse_modbus1 != null)
                     {
                         reponse_modbus1(DateTime.Now);
                     }
                 }
             }
             else
             {
                 if (logging_info_evt != null)
                 {
                     logging_info_evt("Modbus, CRC erreur.");
                 }
                 // on vide le buffer
                 if (serial1.BytesToRead > 0)
                 {
                     byte[] buf_1 = new byte[serial1.BytesToRead];
                     serial1.Read(buf_1, 0, serial1.BytesToRead);
                 }
             }
         }
         else
         {
             if (logging_info_evt != null)
             {
                 logging_info_evt("Modbus, adresse erreur.");
             }
             // on vide le buffer
             if (serial1.BytesToRead > 0)
             {
                 byte[] buf_1 = new byte[serial1.BytesToRead];
                 serial1.Read(buf_1, 0, serial1.BytesToRead);
             }
         }
     }
 }