• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# ICommsSerial类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中ICommsSerial的典型用法代码示例。如果您正苦于以下问题:C# ICommsSerial类的具体用法?C# ICommsSerial怎么用?C# ICommsSerial使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



ICommsSerial类属于命名空间,在下文中一共展示了ICommsSerial类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: doCommand

        public string doCommand(ICommsSerial comPort, string cmd, int level = 0)
        {
            if (!comPort.IsOpen)
                return "";

            comPort.ReadTimeout = 1000;
            // setup to known state
            comPort.Write("\r\n");
            // alow some time to gather thoughts
            Sleep(100);
            // ignore all existing data
            comPort.DiscardInBuffer();
            lbl_status.Text = "Doing Command " + cmd;
            log.Info("Doing Command " + cmd);
            // write command
            comPort.Write(cmd + "\r\n");
            // read echoed line or existing data
            string temp;
            try
            {
                temp = Serial_ReadLine(comPort);
            }
            catch { temp = comPort.ReadExisting(); }
            log.Info("cmd " + cmd + " echo " + temp);
            // delay for command
            Sleep(500);
            // get responce
            string ans = "";
            while (comPort.BytesToRead > 0)
            {
                try
                {
                    ans = ans + Serial_ReadLine(comPort) + "\n";
                }
                catch { ans = ans + comPort.ReadExisting() + "\n"; }
                Sleep(50);

                if (ans.Length > 500)
                {
                    break;
                }
            }

            log.Info("responce " + level + " " + ans.Replace('\0', ' '));

            Regex pattern = new Regex(@"^\[([0-9+])\]\s+",RegexOptions.Multiline);

            if (pattern.IsMatch(ans))
            {
                Match mat = pattern.Match(ans);

                ans = pattern.Replace(ans,"");
            }

            // try again
            if (ans == "" && level == 0)
                return doCommand(comPort, cmd, 1);

            return ans;
        }
开发者ID:GCBrentA,项目名称:MissionPlanner,代码行数:60,代码来源:3DRradio.cs


示例2: upload

		/// <summary>
		/// Upload the specified image_data.
		/// </summary>
		/// <param name='image_data'>
		/// Image_data to be uploaded.
		/// </param>
		public void upload (ICommsSerial on_port, IHex image_data, bool use_mavlink = false)
		{
			progress (0);
			
			port = on_port;
			
			try {
                connect_and_sync();
				upload_and_verify (image_data);
				cmdReboot ();
			} catch {
				if (port.IsOpen)
					port.Close ();
				throw;
			}
		}
开发者ID:mileslee1987,项目名称:MissionPlanner,代码行数:22,代码来源:Uploader.cs


示例3: Upload

        public static void Upload(string firmwarebin, ICommsSerial comPort)
        {
            using (var fs = new FileStream(firmwarebin, FileMode.Open))
            {
                var len = (int)fs.Length;
                len = (len % 128) == 0 ? len / 128 : (len / 128) + 1;
                var startlen = len;

                int a = 1;
                while (len > 0)
                {
                    if (LogEvent != null)
                        LogEvent("Uploading block " + a + "/" + startlen);

                    SendBlock(fs, comPort, a);
                    // responce ACK
                    var ack = comPort.ReadByte();
                    while (ack == 'C')
                        ack = comPort.ReadByte();

                    if (ack == ACK)
                    {
                        len--;
                        a++;

                        if (ProgressEvent != null)
                            ProgressEvent(len / startlen);
                    }
                    else if (ack == NAK)
                    {
                        CustomMessageBox.Show("Corrupted packet. Please power cycle and try again.\r\n", "Warning",
                            MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        len = 0;
                    }
                }
            }

            // boot
            comPort.Write("b");
        }
开发者ID:ArduPilot,项目名称:MissionPlanner,代码行数:40,代码来源:XModem.cs


示例4: SendBlock

        public static void SendBlock(FileStream fs, ICommsSerial Serial, int bNumber)
        {
            byte[] packet = new byte[133];
            byte[] bits = new byte[128];
            UInt16 CRC = 0;

            for (int i = 0; i < bits.Length; i++) { bits[i] = 0x26; }

            packet[0] = SOH;
            packet[1] = (byte)(bNumber % 256);
            packet[2] = (byte)(255 - (bNumber % 256));

            var bytesRead = fs.Read(bits, 0, bits.Length);

            if (bytesRead == bits.Length)
            {
                CRC = CRC_calc(bits, 128);
                System.Buffer.BlockCopy(bits, 0, packet, 3, 128);
                packet[131] = (byte)(CRC >> 8);
                packet[132] = (byte)(CRC);
                Serial.Write(packet, 0, packet.Length);
            }
            else if (bytesRead > 0)
            {
                CRC = CRC_calc(bits, 128);
                System.Buffer.BlockCopy(bits, 0, packet, 3, 128);
                packet[131] = (byte)(CRC >> 8);
                packet[132] = (byte)(CRC);
                Serial.Write(packet, 0, packet.Length);
                Serial.Write("" + EOT);
                CustomMessageBox.Show("Firmware upgraded successfully.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (bytesRead == 0)
            {
                Serial.Write("" + EOT);
                CustomMessageBox.Show("Firmware upgraded successfully.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
开发者ID:AndersonRayner,项目名称:MissionPlanner,代码行数:38,代码来源:XModem.cs


示例5: doCommand

        public string doCommand(ICommsSerial comPort, string cmd, int level = 0)
        {
            if (!comPort.IsOpen)
                return "";

            comPort.ReadTimeout = 1000;
            comPort.DiscardInBuffer();
            // setup to known state
            comPort.Write("\r\n");
            try
            {
                var temp1 = Serial_ReadLine(comPort);
            }
            catch 
            {
                try
                {
                    comPort.ReadExisting();
                }
                catch { return ""; }
            }
            Sleep(100);
            comPort.DiscardInBuffer();
            lbl_status.Text = "Doing Command " + cmd;
            log.Info("Doing Command " + cmd);
            // write command
            comPort.Write(cmd + "\r\n");
            // read echoed line or existing data
            string temp;
            try
            {
                temp = Serial_ReadLine(comPort);
            }
            catch { temp = comPort.ReadExisting(); }
            log.Info("cmd " + cmd + " echo " + temp);
            // get response
            string ans = "";
            DateTime deadline = DateTime.Now.AddMilliseconds(200);
            while (comPort.BytesToRead > 0 || DateTime.Now < deadline)
            {
                try
                {
                    ans = ans + Serial_ReadLine(comPort) + "\n";
                }
                catch { ans = ans + comPort.ReadExisting() + "\n"; }
                Sleep(50);

                if (ans.Length > 1024)
                {
                    break;
                }
            }

            log.Info("response " + level + " " + ans.Replace('\0', ' '));

            Regex pattern = new Regex(@"^\[([0-9+])\]\s+", RegexOptions.Multiline);

            if (pattern.IsMatch(ans))
            {
                Match mat = pattern.Match(ans);

                ans = pattern.Replace(ans, "");
            }

            // try again
            if (ans == "" && level == 0)
                return doCommand(comPort, cmd, 1);

            return ans;
        }
开发者ID:RabidCicada,项目名称:senior-design,代码行数:70,代码来源:3DRradio.cs


示例6: turnon_off

        public void turnon_off(ICommsSerial port, byte clas, byte subclass, byte every_xsamples)
        {
            byte[] datastruct1 = { clas, subclass, 0, every_xsamples, 0, every_xsamples, 0, 0 };

            var packet = generate(0x6, 0x1, datastruct1);

            port.Write(packet, 0, packet.Length);
        }
开发者ID:nitbot,项目名称:MissionPlanner,代码行数:8,代码来源:ubx_m8p.cs


示例7: SetupM8P

        public void SetupM8P(ICommsSerial port, PointLatLngAlt basepos, int surveyindur = 0, double surveyinacc = 0)
        {
            port.BaseStream.Flush();

            // port config - 115200 - uart1
            var packet = generate(0x6, 0x00, new byte[] { 0x01, 0x00, 0x00, 0x00, 0xD0, 0x08, 0x00, 0x00, 0x00, 0xC2,
                0x01, 0x00, 0x23, 0x00, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00 });
            port.Write(packet, 0, packet.Length);
            System.Threading.Thread.Sleep(300);

            // port config - usb
            packet = generate(0x6, 0x00, new byte[] { 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x23, 0x00, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00 });
            port.Write(packet, 0, packet.Length);
            System.Threading.Thread.Sleep(300);

            // set rate to 1hz
            packet = generate(0x6, 0x8, new byte[] { 0xE8, 0x03, 0x01, 0x00, 0x01, 0x00 });
            port.Write(packet, 0, packet.Length);
            System.Threading.Thread.Sleep(200);

            // set navmode to stationary
            packet = generate(0x6, 0x24, new byte[] { 0xFF ,0xFF ,0x02 ,0x03 ,0x00 ,0x00 ,0x00 ,0x00 ,0x10 ,0x27 ,0x00 ,0x00 ,0x05 ,0x00
                ,0xFA ,0x00 ,0xFA ,0x00 ,0x64 ,0x00 ,0x2C ,0x01 ,0x00 ,0x00 ,0x00 ,0x00 ,0x10 ,0x27 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00
                ,0x00 ,0x00 });
            port.Write(packet, 0, packet.Length);
            System.Threading.Thread.Sleep(200);

            // turn off all nmea
            for (int a = 0; a <= 0xf; a++)
            {
                if (a == 0xb || a == 0xc || a == 0xe)
                    continue;
                turnon_off(port, 0xf0, (byte)a, 0);
            }

            // mon-ver
            poll_msg(port, 0xa, 0x4);

            // surveyin msg - for feedback
            turnon_off(port, 0x01, 0x3b, 1);

            // pvt msg - for feedback
            turnon_off(port, 0x01, 0x07, 1);

            // 1005 - 5s
            turnon_off(port, 0xf5, 0x05, 5);

            // 1077 - 1s
            turnon_off(port, 0xf5, 0x4d, 1);

            // 1087 - 1s
            turnon_off(port, 0xf5, 0x57, 1);

            // rxm-raw/rawx - 1s
            turnon_off(port, 0x02, 0x15, 1);
            turnon_off(port, 0x02, 0x10, 1);

            // rxm-sfrb/sfrb - 5s
            turnon_off(port, 0x02, 0x13, 5);
            turnon_off(port, 0x02, 0x11, 5);


            System.Threading.Thread.Sleep(100);
            System.Threading.Thread.Sleep(100);

            if (basepos == PointLatLngAlt.Zero)
            {
                // survey in config - 60s and < 2m
                packet = generate(0x6, 0x71, new ubx_cfg_tmode3((uint)surveyindur, surveyinacc));
                port.Write(packet, 0, packet.Length);
            }
            else
            {
                byte[] data = new ubx_cfg_tmode3(basepos.Lat, basepos.Lng, basepos.Alt);
                packet = generate(0x6, 0x71, data);
                port.Write(packet, 0, packet.Length);
            }

            System.Threading.Thread.Sleep(100);
        }
开发者ID:ArduPilot,项目名称:MissionPlanner,代码行数:81,代码来源:ubx_m8p.cs


示例8: CloseStream

 private void CloseStream(ICommsSerial basestream)
 {
     try
     {
         if (basestream.IsOpen)
             basestream.Close();
     }
     catch { }
 }
开发者ID:lmhonorio,项目名称:TrimaresRouter,代码行数:9,代码来源:MsgRouter.cs


示例9: getTelemPortWithRadio

        void getTelemPortWithRadio(ref ICommsSerial comPort)
        {
            // try telem1

            comPort = new MAVLinkSerialPort(MainV2.comPort, (int)MAVLink.SERIAL_CONTROL_DEV.TELEM1);

            comPort.ReadTimeout = 4000;

            comPort.Open();

            if (doConnect(comPort))
            {
                return;
            }

            comPort.Close();

            // try telem2

            comPort = new MAVLinkSerialPort(MainV2.comPort, (int)MAVLink.SERIAL_CONTROL_DEV.TELEM2);

            comPort.ReadTimeout = 4000;

            comPort.Open();

            if (doConnect(comPort))
            {
                return;
            }

            comPort.Close();
        }
开发者ID:nitbot,项目名称:MissionPlanner,代码行数:32,代码来源:Sikradio.cs


示例10: upload_xmodem

        bool upload_xmodem(ICommsSerial comPort)
        {
            // try xmodem mode
            // xmodem - short cts to ground
            try
            {
                uploader_LogEvent("Trying XModem Mode");
                //comPort.BaudRate = 57600;
                comPort.BaudRate = MainV2.comPort.BaseStream.BaudRate;
                comPort.ReadTimeout = 1000;

                Thread.Sleep(2000);
                var tempd = comPort.ReadExisting();
                Console.WriteLine(tempd);
                comPort.Write("U");
                Thread.Sleep(1000);
                var resp1 = Serial_ReadLine(comPort); // echo
                var resp2 = Serial_ReadLine(comPort); // echo 2
                var tempd2 = comPort.ReadExisting(); // posibly bootloader info / use to sync
                // identify
                comPort.Write("i");
                // responce is rfd900....
                var resp3 = Serial_ReadLine(comPort); //echo
                var resp4 = Serial_ReadLine(comPort); // newline
                var resp5 = Serial_ReadLine(comPort); // bootloader info
                uploader_LogEvent(resp5);
                if (resp5.Contains("RFD900"))
                {
                    // start upload
                    comPort.Write("u");
                    var resp6 = Serial_ReadLine(comPort); // echo
                    var resp7 = Serial_ReadLine(comPort); // Ready
                    if (resp7.Contains("Ready"))
                    {
                        comPort.ReadTimeout = 3500;
                        // responce is C
                        var isC = comPort.ReadByte();
                        var temp = comPort.ReadExisting();
                        if (isC == 'C')
                        {
                            XModem.LogEvent += uploader_LogEvent;
                            XModem.ProgressEvent += uploader_ProgressEvent;
                            // start file send
                            XModem.Upload(@"SiK900x.bin",
                                comPort);
                            XModem.LogEvent -= uploader_LogEvent;
                            XModem.ProgressEvent -= uploader_ProgressEvent;
                            return true;
                        }
                    }
                }
            }
            catch (Exception ex2)
            {
                log.Error(ex2);
            }

            return false;
        }
开发者ID:nitbot,项目名称:MissionPlanner,代码行数:59,代码来源:Sikradio.cs


示例11: poll_msg

        public void poll_msg(ICommsSerial port, byte clas, byte subclass)
        {
            byte[] datastruct1 = { };

            var packet = generate(clas, subclass, datastruct1);

            port.Write(packet, 0, packet.Length);

            System.Threading.Thread.Sleep(10);
        }
开发者ID:ArduPilot,项目名称:MissionPlanner,代码行数:10,代码来源:ubx_m8p.cs


示例12: SetupM8P

        public void SetupM8P(ICommsSerial port)
        {
            // port config - 115200 - uart1
            var packet = generate(0x6, 0x00, new byte[] { 0x01, 0x00, 0x00, 0x00, 0xD0, 0x08, 0x00, 0x00, 0x00, 0xC2,
                0x01, 0x00, 0x23, 0x00, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00 });
            port.Write(packet, 0, packet.Length);
            System.Threading.Thread.Sleep(100);

            // set rate to 1hz
            packet = generate(0x6, 0x8, new byte[] { 0xE8, 0x03, 0x01, 0x00, 0x01, 0x00 });
            port.Write(packet, 0, packet.Length);
            System.Threading.Thread.Sleep(100);

            // surveyin msg - for feedback
            packet = generate(0x6, 0x1, new byte[] { 0x01, 0x3b, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00 });
            port.Write(packet, 0, packet.Length);

            // pvt msg - for feedback
            packet = generate(0x6, 0x1, new byte[] { 0x01, 0x7, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00 });
            port.Write(packet, 0, packet.Length);

            // 1005 - 5s
            packet = generate(0x6, 0x1, new byte[] { 0xF5, 0x05, 0x00, 0x05, 0x00, 0x05, 0x00, 0x00 });
            port.Write(packet, 0, packet.Length);

            // 1077 - 1s
            packet = generate(0x6, 0x1, new byte[] {0xF5, 0x4D, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00 });
            port.Write(packet, 0, packet.Length);

            // 1087 - 1s
            packet = generate(0x6, 0x1, new byte[] { 0xF5, 0x57, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00 });
            port.Write(packet, 0, packet.Length);

            System.Threading.Thread.Sleep(100);
            System.Threading.Thread.Sleep(100);

            // survey in config - 60s and < 2m
            packet = generate(0x6, 0x71, new byte[] {0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3C, 0x00,
                        0x00, 0x00, 0x20, 0x4E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });
            port.Write(packet, 0, packet.Length);
            System.Threading.Thread.Sleep(100);
        }
开发者ID:nitbot,项目名称:MissionPlanner,代码行数:43,代码来源:ubx_m8p.cs


示例13: doConnect

        public bool doConnect(ICommsSerial comPort)
        {
            try
            {
                // clear buffer
                comPort.DiscardInBuffer();
                // setup a known enviroment
                comPort.Write("\r\n");
                // wait
                Sleep(1100);
                // send config string
                comPort.Write("+++");
                // wait
                Sleep(1100);
                // check for config responce "OK"
                log.Info("Connect btr " + comPort.BytesToRead + " baud " + comPort.BaudRate);
                string conn = comPort.ReadExisting();
                log.Info("Connect first responce " + conn.Replace('\0', ' ') + " " + conn.Length);
                if (conn.Contains("OK"))
                {
                    //return true;
                }
                else
                {
                    // cleanup incase we are already in cmd mode
                    comPort.Write("\r\n");
                }

                doCommand(comPort, "AT&T");

                string version = doCommand(comPort, "ATI");

                log.Info("Connect Version: " + version.Trim() + "\n");

                Regex regex = new Regex(@"SiK\s+(.*)\s+on\s+(.*)");

                if (regex.IsMatch(version))
                {
                    return true;
                }

                return false;
            }
            catch { return false; }
        }
开发者ID:LeoTosti,项目名称:x-drone,代码行数:45,代码来源:3DRradio.cs


示例14: Sleep

        void Sleep(int mstimeout, ICommsSerial comPort = null)
        {
            DateTime endtime = DateTime.Now.AddMilliseconds(mstimeout);

            while (DateTime.Now < endtime)
            {
                System.Threading.Thread.Sleep(1);
                Application.DoEvents();

                // prime the mavlinkserial loop with data.
                if (comPort != null) { 
                    int test = comPort.BytesToRead;
                    test++;
                }
            }
        }
开发者ID:RabidCicada,项目名称:senior-design,代码行数:16,代码来源:3DRradio.cs


示例15: doConnect

        public bool doConnect(ICommsSerial comPort)
        {
            try
            {
                Console.WriteLine("doConnect");

                // setup a known enviroment
                comPort.Write("ATO\r\n");
                // wait
                Sleep(1100, comPort);
                comPort.DiscardInBuffer();
                // send config string
                comPort.Write("+++");
                Sleep(1100,comPort);
                // check for config response "OK"
                log.Info("Connect btr " + comPort.BytesToRead + " baud " + comPort.BaudRate);
                // allow time for data/response
                

                byte[] buffer = new byte[20];
                int len = comPort.Read(buffer, 0, buffer.Length);
                string conn = ASCIIEncoding.ASCII.GetString(buffer, 0, len);
                log.Info("Connect first response " + conn.Replace('\0', ' ') + " " + conn.Length);
                if (conn.Contains("OK"))
                {
                    //return true;
                }
                else
                {
                    // cleanup incase we are already in cmd mode
                    comPort.Write("\r\n");
                }

                doCommand(comPort, "AT&T");

                string version = doCommand(comPort, "ATI");

                log.Info("Connect Version: " + version.Trim() + "\n");

                Regex regex = new Regex(@"SiK\s+(.*)\s+on\s+(.*)");

                if (regex.IsMatch(version))
                {
                    return true;
                }

                return false;
            }
            catch { return false; }
        }
开发者ID:RabidCicada,项目名称:senior-design,代码行数:50,代码来源:3DRradio.cs


示例16: Serial_ReadLine

        string Serial_ReadLine(ICommsSerial comPort)
        {
            StringBuilder sb = new StringBuilder();
            DateTime Deadline = DateTime.Now.AddMilliseconds(comPort.ReadTimeout);

            while (DateTime.Now < Deadline)
            {
                if (comPort.BytesToRead > 0)
                {
                    byte data = (byte)comPort.ReadByte();
                    sb.Append((char)data);
                    if (data == '\n')
                        break;
                }
            }

            return sb.ToString();
        }
开发者ID:LeoTosti,项目名称:x-drone,代码行数:18,代码来源:3DRradio.cs


示例17: doCommand

        public string doCommand(ICommsSerial comPort, string cmd, bool multiLineResponce = false, int level = 0)
        {
            if (!comPort.IsOpen)
                return "";

            comPort.DiscardInBuffer();

            lbl_status.Text = "Doing Command " + cmd;
            log.Info("Doing Command " + cmd);

            comPort.Write(cmd + "\r\n");

            comPort.ReadTimeout = 1000;

            // command echo
            var cmdecho = Serial_ReadLine(comPort);

            if (cmdecho.Contains(cmd))
            {
                var value = "";

                if (multiLineResponce)
                {
                    DateTime deadline = DateTime.Now.AddMilliseconds(1000);
                    while (comPort.BytesToRead > 0 || DateTime.Now < deadline)
                    {
                        try
                        {
                            value = value + Serial_ReadLine(comPort);
                        }
                        catch
                        {
                            value = value + comPort.ReadExisting();
                        }
                    }
                }
                else
                {
                    value = Serial_ReadLine(comPort);

                    if (value == "" && level == 0)
                    {
                        return doCommand(comPort, cmd, multiLineResponce, 1);
                    }
                }

                log.Info(value.Replace('\0', ' '));

                return value;
            }

            comPort.DiscardInBuffer();

            // try again
            if (level == 0)
                return doCommand(comPort, cmd, multiLineResponce, 1);

            return "";
        }
开发者ID:Errowily,项目名称:MissionPlanner,代码行数:59,代码来源:Sikradio.cs


示例18: Scan

        static public void Scan(bool connect = false)
        {
            foundport = false;
            portinterface = null;
            lock (runlock)
            {
                run = 0;
                running = 0;
            }
            CommsSerialScan.connect = connect;

            List<MAVLinkInterface> scanports = new List<MAVLinkInterface>();

            string[] portlist = SerialPort.GetPortNames();

            foreach (string port in portlist)
            {
                scanports.Add(new MAVLinkInterface() { BaseStream = new SerialPort() { PortName = port, BaudRate = bauds[0] } });
            }

            foreach (MAVLinkInterface inter in scanports)
            {
                System.Threading.ThreadPool.QueueUserWorkItem(doread, inter);
            }
        }
开发者ID:kkouer,项目名称:PcGcs,代码行数:25,代码来源:CommsSerialScan.cs


示例19: Terminal_Load

        private void Terminal_Load(object sender, EventArgs e)
        {
            try
            {
                MainV2.comPort.giveComport = true;

                comPort = MainV2.comPort.BaseStream;

                if (comPort.IsOpen)
                    comPort.Close();

                comPort.ReadBufferSize = 1024 * 1024;

                comPort.PortName = MainV2.comPortName;

                comPort.BaudRate = int.Parse(MainV2._connectionControl.CMB_baudrate.Text);

                comPort.Open();

                comPort.toggleDTR();

                comPort.DiscardInBuffer();

                Console.WriteLine("Terminal_Load");

                System.Threading.Thread t11 = new System.Threading.Thread(delegate()
                {
                    threadrun = true;

                    Console.WriteLine("Terminal thread start");

                    // 10 sec
                        waitandsleep(10000);

                    Console.WriteLine("Terminal thread 1");

                    // 100 ms
                        readandsleep(100);

                    Console.WriteLine("Terminal thread 2");

                    try
                    {
                        if (!inlogview)
                            comPort.Write("\n\n\n");

                        // 1 secs
                        if (!inlogview)
                            readandsleep(1000);

                        if (!inlogview)
                            comPort.Write("\r\r\r?\r");
                    }
                    catch (Exception ex) { Console.WriteLine("Terminal thread 3 " + ex.ToString()); threadrun = false; return; }

                    Console.WriteLine("Terminal thread 3");

                    while (threadrun)
                    {
                        try
                        {
                            System.Threading.Thread.Sleep(10);
                            if (inlogview)
                                continue;
                            if (!comPort.IsOpen)
                                break;
                            if (comPort.BytesToRead > 0)
                            {
                                comPort_DataReceived((object)null, (SerialDataReceivedEventArgs)null);
                            }
                        }
                        catch (Exception ex) { Console.WriteLine("Terminal thread 4 " + ex.ToString()); }
                    }

                    threadrun = false;
                    try
                    {
                        comPort.DtrEnable = false;
                    }
                    catch { }
                    try
                    {
                        Console.WriteLine("term thread close");
                        comPort.Close();
                    }
                    catch { }

                    Console.WriteLine("Comport thread close");
                });
                t11.IsBackground = true;
                t11.Name = "Terminal serial thread";
                t11.Start();

                // doesnt seem to work on mac
                //comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);

                TXT_terminal.AppendText("Opened com port\r\n");
                inputStartPos = TXT_terminal.SelectionStart;
            }
            catch (Exception) { TXT_terminal.AppendText("Cant open serial port\r\n"); return; }
//.........这里部分代码省略.........
开发者ID:hzeera,项目名称:MissionPlanner,代码行数:101,代码来源:Terminal.cs


示例20: Log_Load

        private void Log_Load(object sender, EventArgs e)
        {
            status = serialstatus.Connecting;

            comPort = GCSViews.Terminal.comPort;

            try
            {
                Console.WriteLine("Log_load " + comPort.IsOpen);

                if (!comPort.IsOpen)
                    comPort.Open();

                //Console.WriteLine("Log dtr");

                //comPort.toggleDTR();

                Console.WriteLine("Log discard");

                comPort.DiscardInBuffer();

                Console.WriteLine("Log w&sleep");

                try
                {
                    // try provoke a response
                    comPort.Write("\n\n?\r\n\n");
                }
                catch
                {
                }

                // 10 sec
                waitandsleep(10000);
            }
            catch (Exception ex)
            {
                log.Error("Error opening comport", ex);
                CustomMessageBox.Show("Error opening comport");
                return;
            }

            var t11 = new System.Threading.Thread(delegate()
            {
                var start = DateTime.Now;

                threadrun = true;

                if (comPort.IsOpen)
                    readandsleep(100);

                try
                {
                    if (comPort.IsOpen)
                        comPort.Write("\n\n\n\nexit\r\nlogs\r\n"); // more in "connecting"
                }
                catch
                {
                }

                while (threadrun)
                {
                    try
                    {
                        updateDisplay();

                        System.Threading.Thread.Sleep(1);
                        if (!comPort.IsOpen)
                            break;
                        while (comPort.BytesToRead >= 4)
                        {
                            comPort_DataReceived((object) null, (SerialDataReceivedEventArgs) null);
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("crash in comport reader " + ex);
                    } // cant exit unless told to
                }
                log.Info("Comport thread close");
            }) {Name = "comport reader", IsBackground = true};
            t11.Start();

            // doesnt seem to work on mac
            //comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
        }
开发者ID:duyisu,项目名称:MissionPlanner,代码行数:86,代码来源:LogDownload.cs



注:本文中的ICommsSerial类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# ICommunicationContext类代码示例发布时间:2022-05-24
下一篇:
C# ICommonServices类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap