Пример #1
0
        public static void Main(string[] args)
        {
            //test SerialPort
            if (Talker.TouchSerialPort ("COM3", 9600)) {
                //create a instance of Talker.
                var talker = new Talker () {
                    PortName = "COM3",
                    BaudRate = 9600,
                    LineBreak = new LineBreak () { //set linebreak type
                        Type = "nt",
                    },
                    Proc = list => {
                        var str = string.Empty;
                        foreach (var i in list) {
                            var ch = (char)i;
                            str += ch;
                        }
                        Console.WriteLine ("echo str: {0}", str);
                    }
                };

                //open talker if it's not opened.
                if (!talker.IsOpen) {
                    talker.Open ();
                }

                //write data to serialport.
                for (int i = 0; i < 1000; i++) {
                    talker.Write (new byte[] { 10, 12, 11, 17, 18 });
                }
                Console.Read ();
            }
        }
Пример #2
0
        public static void Main(string[] args)
        {
            var portName = "COM3";
            Console.Write ("> ");
            portName = Console.ReadLine ();
            Console.WriteLine ("< {0}", portName);

            var talker = new Talker () {
                PortName = portName,
                BaudRate = 9600,
                LineBreak = new LineBreak () {
                    Type = "nt"
                },
                Proc = list => {
                    var str = string.Empty;
                    foreach (var i in list) {
                        var ch = (char)i;
                        str += ch;
                    }
                    Console.WriteLine ("< {0}", str);
                }
            };
            talker.Open ();

            while (true) {
                Console.Write ("> ");
                string line = Console.ReadLine ();
                if (line == ".exit") {
                    if (talker.IsOpen) {
                        talker.Close ();
                    }
                    break;
                }
                var array = Encoding.ASCII.GetBytes (line);
                var length = array.Length;
                var what = new byte[length + 2];
                what [length + 2 - 2] = 13;
                what [length + 2 - 1] = 10;
                array.CopyTo (what, 0);
                talker.Write (what);
            }
        }