当前位置: 首页>>代码示例>>C#>>正文


C# ICommsSerial类代码示例

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


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

示例13: 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

示例14: 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

示例15: Log_Load

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

            comPort = MainV2.comPort.BaseStream;

            //comPort.ReceivedBytesThreshold = 50;
            //comPort.ReadBufferSize = 1024 * 1024;
            try
            {
                comPort.DtrEnable = false;
                System.Threading.Thread.Sleep(100);
                comPort.DtrEnable = true;
                //comPort.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Error opening comport");
            }

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

                threadrun = true;

                System.Threading.Thread.Sleep(2000);

                try
                {
                    comPort.Write("\n\n\n\n");
                }
                catch {  }

                while (threadrun)
                {
                    try
                    {
                        updateDisplay();

                        System.Threading.Thread.Sleep(10);
                        if (!comPort.IsOpen)
                            break;
                        while (comPort.BytesToRead >= 4)
                        {
                            comPort_DataReceived((object)null, (SerialDataReceivedEventArgs)null);
                        }
                    }
                    catch (Exception ex) { Console.WriteLine("crash in comport reader " + ex.ToString()); } // cant exit unless told to
                }
                Console.WriteLine("Comport thread close");
            });
            t11.Name = "comport reader";
            t11.Start();
            MainV2.threads.Add(t11);

            // doesnt seem to work on mac
            //comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
        }
开发者ID:joshuabenuck,项目名称:ap-experiments,代码行数:59,代码来源:Log.cs


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