在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → c# 串口采取数据 示例源码

c# 串口采取数据 示例源码

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.14M
  • 下载次数:52
  • 浏览次数:873
  • 发布时间:2016-12-04
  • 实例类别:C#语言基础
  • 发 布 人:hhaitao
  • 文件格式:.rar
  • 所需积分:2
 相关标签: C# 串口 c

实例介绍

【实例简介】

【实例截图】

【核心代码】


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.IO.Ports;//新增加
using System.Net;//新增加
using System.IO;//新增加
using System.Text.RegularExpressions;//新增加??


namespace AGV
{
   
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            ToolTip a = new ToolTip();
            a.ShowAlways = true;
            a.SetToolTip(this.button1, "要显示的文字");

            comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());//检查有哪些COM口
            
        }
        public struct myStruct//全局变量
        {
          
        }
        private void WriteDataToNet(myStruct info)
        {
            myclass.Dt106 = myclass.Dt106   1;
            myclass.Dt106  ;
            textBox11.Text = myclass.Dt106.ToString();
        }

        public class myclass  //全局变量
        {
            public static int Dt100;//
            public static int Dt101;
            public static int Dt102;
            public static int Dt103;
            public static int Dt104;
            public static int Dt105;
            public static int Dt106;
            public static bool Sm100;
            public static bool Sm101;
            public static bool Sm102;
            public static bool Sm103;
            public static bool Sm104;
            public static bool Sm105;
            public static bool Sm106;
            public static int Tix0;
            public static int Tix1;
            public static int Tix2;
            public static int Tix3;
            public static int Tix4;
            public static int Tix5;
            public static int Tix6;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.Text != "")
                {


                    if (button1.Text == "打开")
                    {
                        serialPort1.PortName = comboBox1.Text; //提取com口号
                        button1.Text = "已打开";
                        serialPort1.Open();  //打开串口
                        button1.BackColor = Color.SpringGreen;
                        //serialPort1.BaudRate = 19200;  //
                        serialPort1.DataBits = 8;  //停止为8
                        comboBox1.BackColor = Color.SpringGreen;

                    }
                    else if (button1.Text == "已打开")
                    {
                        button1.Text = "打开";
                        serialPort1.Close();//关闭COM口
                        button1.BackColor = Color.WhiteSmoke;
                        comboBox1.BackColor = Color.WhiteSmoke;
                    }
                }
                else if (comboBox1.Text == "")
                {
                    MessageBox.Show("未选择COM口");
                }
            }
            catch
            {
                MessageBox.Show("1");
            }
        }
        

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
           
            if (button1.Text == "已打开")
            {
                  if (textBox1.Text != "")
                {


                    string str = textBox1.Text;       //由文本框读入想要发送的数据
                    this.serialPort1.Write(str);     //发送数据   
                }
            }
            else if (button1.Text == "打开")
            {
                MessageBox.Show("COM未打开");
            }
        }

        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Thread.Sleep(1);                                   //10毫秒
            int DataLength = serialPort1.BytesToRead;          //字节长度ReadByte ReadChar
            int DT0 = serialPort1.ReadByte();                  //第一个字符ASILL码
            string DataLength2 = serialPort1.ReadExisting();   //读取所有数字

            char DataLength1 = (char)DT0;                     //将ASILL码转为STRING

            this.textBox2.Invoke
                (

             new MethodInvoker
                 (                                            //MethodInvoker 是不带参数的委托。EventHandler 是可带参数的委托。
             delegate
             {

                 this.textBox2.AppendText(DateTime.Now.ToString()   "  "   DataLength1   DataLength2   "\r\n");//输出到主窗口文本控件 
                 this.textBox3.Text = (DateTime.Now.ToString()   DataLength1   DataLength2);
                 this.textBox4.Text = (DataLength1   DataLength2);


             }
             )
             );
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (button1.Text == "已打开")
            {
            
            if (button5.Text == "访问")
            {
                button5.BackColor = Color.SpringGreen;
                button5.Text = "访问中";
                textBox5.Text = "开始";



            }
            else if (button5.Text == "访问中")
            {
                button5.BackColor = Color.WhiteSmoke;
                button5.Text = "访问";
                textBox5.Text = "结束";
            }
        }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                if (textBox5.Text == "开始")
                {
                    textBox5.Text = "17601";
                }
                if (button5.Text == "访问中")
                {
                    Byte[] BY = new Byte[8]; //建立临时字节数组对象
                    BY[0] = Byte.Parse(this.textBox5.Text.Substring(0, 3));  //由文本框读入想要发送的数据
                    BY[1] = Byte.Parse(this.textBox5.Text.Substring(3, 2));  //
                    this.serialPort1.Write(BY, 0, 8);//发送数据    
                }
                if (textBox5.Text == "开始")
                {
                    textBox5.Text = "17601";
                }
                else if (textBox5.Text == "17601")
                {
                    textBox5.Text = "17602";
                }
                else if (textBox5.Text == "17602")
                {
                    textBox5.Text = "17603";
                }
                else if (textBox5.Text == "17603")
                {
                    textBox5.Text = "17604";
                }
                else if (textBox5.Text == "17604")
                {
                    textBox5.Text = "17605";
                }
                else if (textBox5.Text == "17605")
                {
                    textBox5.Text = "17606";
                }
                else if (textBox5.Text == "17606")
                {
                    textBox5.Text = "17607";
                }
                else if (textBox5.Text == "17607")
                {
                    textBox5.Text = "17608";
                }

                else if (textBox5.Text == "17608")
                {
                    textBox5.Text = "17609";
                }
                else if (textBox5.Text == "17609")
                {
                    textBox5.Text = "17616";
                }
                else if (textBox5.Text == "17616")
                {
                    textBox5.Text = "17617";
                }
                else if (textBox5.Text == "17617")
                {
                    textBox5.Text = "17618";
                }
                else if (textBox5.Text == "17618")
                {
                    textBox5.Text = "17619";
                }
                else if (textBox5.Text == "17619")
                {
                    textBox5.Text = "17620";
                }
                else if (textBox5.Text == "17620")
                {
                    textBox5.Text = "17621";
                }
                else if (textBox5.Text == "17621")
                {
                    textBox5.Text = "17622";
                }
                else if (textBox5.Text == "17622")
                {
                    textBox5.Text = "17623";
                }
                else if (textBox5.Text == "17623")
                {
                    textBox5.Text = "17624";
                }
                else if (textBox5.Text == "17624")
                {
                    textBox5.Text = "17625";
                }
                else if (textBox5.Text == "17625")
                {
                    textBox5.Text = "17601";
                }
            }
            catch
            {
              
            }
           
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string m1,m2;
            m1 = comboBox2.Text;
            m2 = comboBox3.Text;
            textBox1.Text = m1 m2;
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            string m1,m2;
            m1= comboBox2.Text;
            m2=comboBox3.Text;
            textBox1.Text = m1   m2;
        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (comboBox4.Text == "0.5")
            {
                timer1.Interval = 500;
            }

          else   if (comboBox4.Text == "0.7")
            {
                timer1.Interval = 700;
            }
            else if (comboBox4.Text == "0.9")
            {
                timer1.Interval = 900;
            }
            else if (comboBox4.Text == "1.1")
            {
                timer1.Interval = 1100;
            }
            else if (comboBox4.Text == "1.3")
            {
                timer1.Interval = 1300;
            }
            else if (comboBox4.Text == "1.5")
            {
                timer1.Interval = 1500;
            }
            
        }

        

        private void textBox4_TextChanged_1(object sender, EventArgs e)
        {
            if (textBox4.Text != "")
            {
                string d0;
                d0 = this.textBox4.Text.Substring(0, 4);

                if (d0 == "B001")
                {
                    textBox6.Text = textBox4.Text;
                    myclass.Dt100 = 0;
                    myclass.Sm100 = true;
                }
                else if (d0 == "B002")
                {
                    textBox7.Text = textBox4.Text;
                    myclass.Sm101 = true;
                }
                else if (d0 == "B003")
                {
                    textBox8.Text = textBox4.Text;
                    myclass.Sm102 = true;
                }
                else if (d0 == "B004")
                {
                    textBox9.Text = textBox4.Text;
                    myclass.Sm103 = true;
                }
                else if (d0 == "B005")
                {
                    textBox10.Text = textBox4.Text;
                    myclass.Sm104 = true;
                }
            }
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            if (textBox6.Text != "")
            {
                string d0, d1;

                d0 = this.textBox6.Text.Substring(0, 4);
                d1 = this.textBox6.Text.Substring(4, 2);
                if (d0 == "B001")
                {
                    button6.Visible = true;

                }
               
                
                
            }
            else if (textBox6.Text == "")
            {
                button6.Visible = false;

            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
             
            
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
           

            if (myclass.Sm100 == true)
            {
                textBox26.Text = myclass.Dt100.ToString();
                if (textBox4.Text.Substring(0, 4) != "B001")
                {
                    myclass.Dt100 = myclass.Dt100   1;
  
                }
                if (myclass.Dt100 == 20)
                {
                    textBox26.Text = "";
                    textBox6.Text = "";
                    myclass.Sm100 = false;
                    myclass.Dt100 = 0;
                    myclass.Dt101 = 0;
                }
                
                
                 if (textBox6.Text != "")
                 {

                     if (textBox6.Text.Substring(0, 4) == "B001")//是否是A1车
                     {
                         string Agv1 = this.textBox6.Text.Substring(4, 2);//AGV状态     
                         switch (Agv1)
                         {
                             case "02":
                                 myclass.Dt101 = myclass.Dt101   1;
                                 string dt10 = textBox6.Text.Substring(6, 2);//AGV位置
                                 int dt11 = Int32.Parse(dt10);               //SRI转为INT
                                 dt11 = myclass.Dt101   dt11;                //AGV位置
                                 button6.Location = new System.Drawing.Point(dt11, 32);//坐标更改 y.x    
                                 break;
                             case "200":
                                 MessageBox.Show("d0不等于100");
                                 break;

                         }
                     }
                }
            }
        }


        private void button2_Click_1(object sender, EventArgs e)
        {
            //this.Close();//只关闭当前窗口
            //   Application.Exit();//强制所有消息中止
            //  Application.ExitThread();//强制所有消息中止
            System.Environment.Exit(0);//所有的都会退出 
        } 
        
       
    }
}


标签: C# 串口 c

实例下载地址

c# 串口采取数据 示例源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警