コード例 #1
0
        private void SendWriteCoils(byte[] txBuffer)
        {
            if (serialPort == null)
            {
                if (CrashSerialPort != null)
                {
                    CrashSerialPort(this, null);
                }
                return;
            }

            if (!serialPort.IsOpen)
            {
                if (CrashSerialPort != null)
                {
                    CrashSerialPort(this, null);
                }
                return;
            }

            try
            {
                if (serialPort.BytesToRead != 0)
                {
                    byte[] buff = new byte[serialPort.BytesToRead + 1];
                    serialPort.Read(buff, 0, serialPort.BytesToRead);
                    //serialPort.DiscardInBuffer();
                }
                //Thread.Sleep(2);
                serialPort.Write(txBuffer, 0, txBuffer.Length);
            }
            catch
            {
                if (CrashSerialPort != null)
                {
                    CrashSerialPort(this, null);
                }
                return;
            }

            if (waitSerialData.WaitOne(TimeSpan.FromMilliseconds(Timeout)))
            {
                for (int i = 0; i < waitByteCount; i++)
                {
                    if (serialPort.BytesToRead < 8)
                    {
                        waitSerialData.WaitOne(TimeSpan.FromMilliseconds(10));
                    }
                    else
                    {
                        break;
                    }
                }
            }

            byte[] rxBuffer;

            if (serialPort.BytesToRead == 8)
            {
                rxBuffer = new byte[8];
                try
                {
                    serialPort.Read(rxBuffer, 0, 8);
                }
                catch
                {
                    if (CrashSerialPort != null)
                    {
                        CrashSerialPort(this, null);
                    }
                    return;
                }
            }
            else //Данные так и не пришли
            {
                if (DataSent != null)
                {
                    DataSent(false);
                }
                return;
            }

            bool dataOk = false;

            if (!ModbusCRC.CheckCRC(rxBuffer, 8))
            {
                dataOk = false;
            }
            else
            {
                dataOk = true;
            }

            if (DataSent != null)
            {
                DataSent(dataOk);
            }
        }
コード例 #2
0
        private void SendReadCoils(byte[] txBuffer)
        {
            if (serialPort == null)
            {
                if (CrashSerialPort != null)
                {
                    CrashSerialPort(this, null);
                }
                return;
            }

            if (!serialPort.IsOpen)
            {
                if (CrashSerialPort != null)
                {
                    CrashSerialPort(this, null);
                }
                return;
            }

            try
            {
                if (serialPort.BytesToRead != 0)
                {
                    byte[] buff = new byte[serialPort.BytesToRead + 1];
                    serialPort.Read(buff, 0, serialPort.BytesToRead);
                    //serialPort.DiscardInBuffer();
                }
                //Thread.Sleep(2);
                serialPort.Write(txBuffer, 0, txBuffer.Length);
            }
            catch
            {
                if (CrashSerialPort != null)
                {
                    CrashSerialPort(this, null);
                }
                return;
            }


            if (waitSerialData.WaitOne(TimeSpan.FromMilliseconds(Timeout)))
            {
                for (int i = 0; i < waitByteCount; i++)
                {
                    if (serialPort.BytesToRead < waitByteCount)
                    {
                        waitSerialData.WaitOne(TimeSpan.FromMilliseconds(10));
                    }
                    else
                    {
                        break;
                    }
                }
            }

            bool dataOk = false;

            byte[] rxBuffer = new byte[0];

            if (serialPort.BytesToRead >= waitByteCount)
            {
                int count = serialPort.BytesToRead;
                rxBuffer = new byte[count];

                try
                {
                    serialPort.Read(rxBuffer, 0, count);
                }
                catch
                {
                    if (CrashSerialPort != null)
                    {
                        CrashSerialPort(this, null);
                    }
                    return;
                }
            }
            else //Данные так и не пришли
            {
                if (DataReceived != null)
                {
                    DataReceived(false, new ushort[0]);
                }
                return;
            }

            ushort[] ubuff = new ushort[0];

            if (!ModbusCRC.CheckCRC(rxBuffer, waitByteCount))
            {
                dataOk = false;
                ubuff  = new ushort[0];
            }
            else
            {
                ModbusCRC.RemoveData(rxBuffer, 3, waitWordCount, out ubuff);
                dataOk = true;
            }

            if (DataReceived != null)
            {
                DataReceived(dataOk, ubuff);
            }
        }