public void HandleClassification(Classification classification)
 {
     if (classification.isSpam)
     {
         device.Write(new byte[] { ((byte)1) });
     }
     else
     {
         device.Write(new byte[] { ((byte)2) });
     }
 }
Exemple #2
0
        public bool SendFc16(byte address, ushort start, ushort registers, short[] values)
        {
            //Ensure port is open:
            if (sp.IsOpen)
            {
                //Clear in/out buffers:
                sp.DiscardOutBuffer();
                sp.DiscardInBuffer();
                //Message is 1 addr + 1 fcn + 2 start + 2 reg + 1 count + 2 * reg vals + 2 CRC
                byte[] message = new byte[9 + 2 * registers];
                //Function 16 response is fixed at 8 bytes
                byte[] response = new byte[8];

                //Add bytecount to message:
                message[6] = (byte)(registers * 2);
                //Put write values into message prior to sending:
                for (int i = 0; i < registers; i++)
                {
                    message[7 + 2 * i] = (byte)(values[i] >> 8);
                    message[8 + 2 * i] = (byte)(values[i]);
                }
                //Build outgoing message:
                BuildMessage(address, (byte)16, start, registers, ref message);

                //Send Modbus message to Serial Port:
                try
                {
                    sp.Write(message, 0, message.Length);
                    GetResponse(ref response);
                }
                catch (Exception err)
                {
                    modbusStatus = "Error in write event: " + err.Message;
                    return(false);
                }
                //Evaluate message:
                if (CheckResponse(response))
                {
                    modbusStatus = "Write successful";
                    return(true);
                }
                else
                {
                    modbusStatus = "CRC error";
                    return(false);
                }
            }
            else
            {
                modbusStatus = "Serial port not open";
                return(false);
            }
        }
Exemple #3
0
        public void SendCommand(MTRFMode mode, MTRFAction action, byte channel, MTRFCommand command,
                                MTRFRepeatCount repeatCount = MTRFRepeatCount.NoRepeat, MTRFDataFormat format = MTRFDataFormat.NoData,
                                byte[] data = null, UInt32 target = 0)
        {
            var cmd = BuildCommand(mode, action, repeatCount, channel, command, format, data, target);

            device.Write(cmd);
        }
 public void Write(byte[] data)
 {
     if (monitor != null)
     {
         monitor('>', data, data.Length);
     }
     serialDevice.Write(data);
 }
Exemple #5
0
        /// <summary>
        /// Send a command to the module
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="data"></param>
        private void SendCommand(FingerprintCommand cmd, byte[] data)
        {
            Console.WriteLine($"Command {cmd.ToString()}");
            FingerPrintProtocol fp = new FingerPrintProtocol(cmd, data);

            _newPacket = false;
            _wiating   = true;
            _inBuff.Clear();
            _sps.Write(fp.GetStructuredPacket());
        }
Exemple #6
0
 public void sendCommand(string command)
 {
     if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
     {
         serialPort.Write(Encoding.UTF8.GetBytes(command));
     }
     else
     {
         serialPortWin.Write(command);
     }
 }
Exemple #7
0
 /// <summary>
 /// 向串口发送数据
 /// </summary>
 /// <param name="data"></param>
 public void SendData(byte[] data)
 {
     try
     {
         SerialDevice.DiscardInBuffer();
         SerialDevice.Write(data, 0, data.Length);
     }
     catch (Exception ex)
     {
         ExceptionProcess(ex);
     }
 }
Exemple #8
0
        protected override async Task <byte[]> SendRequest(byte[] request, int reqLen)
        {
            //double slient_interval = 1000 * 5 * ((double)1 / (double)config.BaudRate);
            byte[] response = null;

            m_semaphore_connection.Wait();

            if (m_serialPort != null && m_serialPort.IsOpen())
            {
                try
                {
                    m_serialPort.DiscardInBuffer();
                    m_serialPort.DiscardOutBuffer();
                    Task.Delay(m_silent).Wait();
                    m_serialPort.Write(request, 0, reqLen);
                    response = ReadResponse();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Something wrong with the connection, disposing...");
                    Console.WriteLine(e.Message);
                    m_serialPort.Dispose();
                    m_serialPort = null;
                    Console.WriteLine("Connection lost, reconnecting...");
                    await ConnectSlave();
                }
            }
            else
            {
                Console.WriteLine("Connection lost, reconnecting...");
                await ConnectSlave();
            }

            m_semaphore_connection.Release();

            return(response);
        }
Exemple #9
0
        public void sendCommand(string command)
        {
            try
            {
                if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    serialPort.Write(Encoding.UTF8.GetBytes(command));
                }
                else
                {
                    serialPortWin.Write(command);
                }

                if (this.LogWrites)
                {
                    TestLogger.Log($"[SERIALW] {command}");
                }
            }
            catch (Exception ex)
            {
                TestLogger.Log($"Error writing to serial port. {ex.ToString()}");
                throw;
            }
        }