Exemplo n.º 1
0
        private static void Main(string[] args)
        {
            var ports             = SerialPortDevice.GetPortNames();
            SerialPortDevice port = new SerialPortDevice(null, "COM3", BitRate.B9600);

            //var port = new System.IO.Ports.SerialPort("/dev/ttyUSB0", 9600);

            /*
             * FE 05 00 00 FF 00 98 35
             * FE 05 00 01 FF 00 C9 F5*/

            //port.PortName = "/dev/ttyUSB0";
            //port.BaudRate = 9600;

            port.Open();
            var data = new byte[]
            {
                0xFE, 0x05, 0x00, 0x01, 0xFF, 0x00, 0xc9, 0xf5
            };

            //FE 05 00 01 FF 00 C9 F5
            port.Write(data);

            data = new byte[]
            {
                0xFE, 0x05, 0x00, 0x00, 0x00, 0x00, 0xD9, 0xC5
            };
            port.Write(data);

            port.Close();
        }
Exemplo n.º 2
0
        public DAM(ILogger logger, string comPath, byte address = 0xfe)
        {
            if (string.IsNullOrEmpty(comPath))
            {
                throw new ArgumentException("message", nameof(comPath));
            }

            Logger       = logger;
            ComPath      = comPath;
            this.address = address;

            var ports = SerialPortDevice.GetPortNames();

            device = new SerialPortDevice(logger, comPath, BitRate.B9600);

            for (int i = 0; i < this.RelayPortsCount; i++)
            {
                RelayPort[i] = false;
            }

            for (int i = 0; i < this.OptocouplerPortsCount; i++)
            {
                OptocouplerPort[i] = false;
            }
            for (int i = 0; i < this.AnalogInputCount; i++)
            {
                AnalogInput[i] = 0;
            }
            device.Open();
        }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            var value = SerialPortDevice.Test(5);

            System.Diagnostics.Debug.WriteLine(value);


            var portNames = SerialPortDevice.GetAllDevicesPath();

            foreach (var item in portNames)
            {
                System.Diagnostics.Debug.WriteLine(item);
            }
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);


            var serialport = new SerialPortDevice("/dev/ttyS3", 115200);
            int RecvCount  = 0;
            int SendCount  = 0;

            serialport.Received += (s, e) =>
            {
                RecvCount += e.Length;
            };
            try
            {
                serialport.Open();
            }
            catch (System.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                return;
            }

            Task.Run(() =>
            {
                while (true)
                {
                    System.Diagnostics.Debug.WriteLine($"Count:{SendCount} {RecvCount}");


                    serialport.Send(new byte[] { 0x01, 0x02 });
                    SendCount += 2;
                    Thread.Sleep(50);
                }
            });
        }
Exemplo n.º 4
0
        public virtual bool Close(int port)
        {
            var command       = MakeOpenCloseCommand(port != -1, port, false);
            var openByAnother = false;

            if (device.IsOpened)
            {
                openByAnother = true;
            }
            else
            {
                device.Open();
            }

            //   atutResetEvet.Reset();
            // Log(command, "关闭");
            device.Write(command);

            if (!openByAnother)
            {
                device.Close();
            }
            return(true);
        }
 public bool Open(string name, int baudrate, int flags = 0)
 {
     serialPortDevice           = new SerialPortDevice(name, baudrate, flags);
     serialPortDevice.Received += SerialPortDevice_Received;
     return(serialPortDevice.Open());
 }