在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 串口通信程序 源码下载

C# 串口通信程序 源码下载

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.43M
  • 下载次数:69
  • 浏览次数:501
  • 发布时间:2016-09-30
  • 实例类别:C#语言基础
  • 发 布 人:910887181
  • 文件格式:.zip
  • 所需积分:3
 相关标签: 串口 通信

实例介绍

【实例简介】

【实例截图】

【核心代码】


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Net.Sockets;


namespace WindowsApplication1
{
    public partial class Form1 : Form

    {
        public char[] bufchar;
        public Form1()
        {
            InitializeComponent();
            //textBox1.Text = "hello";

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            if (mycom.IsOpen)
            {

                mycom.Close();
                button1.Text="打开串口";   
            }
            else 
            {

                mycom.PortName = PortNameBox.Text;
                mycom.BaudRate = Convert.ToInt32(BpsBox.Text);  // 字符转成数值,如"100"转成100


                switch (StpBox.SelectedIndex)
                {
                    case 0:
                        {
                            mycom.StopBits = StopBits.One;
                        }
                        break;
                    case 1:
                        {
                            mycom.StopBits = System.IO.Ports.StopBits.Two;
                        }
                        break;
                }

            
                switch (ParBox.SelectedIndex)
                {
                    case 0:
                        {
                            mycom.Parity = System.IO.Ports.Parity.None;
                        }
                        break;
                    case 1:
                        {
                            mycom.Parity = System.IO.Ports.Parity.Odd;
                        }
                        break;
                    case 2:
                        {
                            mycom.Parity = System.IO.Ports.Parity.Even;
                        }
                        break;
                    case 3:
                        {
                            mycom.Parity = System.IO.Ports.Parity.Mark;
                        }
                        break;
                    case 4:
                        {
                            mycom.Parity = System.IO.Ports.Parity.Space;
                        }
                        break;
                }

                

                try
                {
                    mycom.Open();
                    button1.Text = "关闭串口";
                }
                catch
                {
                    MessageBox.Show( "端口没有正确打开!","请注意");
                }

            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            string test;

            test = textBox2.Text;
            if (mycom.IsOpen)
            {
                
                
                if (sendhex.Checked ==false )
                {
                mycom.Write(test);
                }
                else
                {

                    byte[] send_bytebuf;
                    send_bytebuf = send_byte(textBox2.Text);
                    mycom.Write(send_bytebuf, 0, (textBox2.Text.Length / 2));                    
                }
            }
            else
            {
            
            //输出提示
             MessageBox.Show("端口没有正确打开!");
            }

        }

        
        /// 字节数组转16进制字符串 
        /// 
        public static string byteToHexStr(byte[] bytes) 
        { 
        string returnStr = ""; 
        if (bytes != null) 
        { 
        for (int i = 0; i < bytes.Length; i  ) 
        { 
        returnStr  = bytes[i].ToString("X2"); 
        } 
        } 
        return returnStr; 
        } 

        private void mycom_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {

            int bytes = mycom.BytesToRead;
            int intputs;
            byte[] inputbuf=new byte[bytes];
            char[] incharbuf = new char[bytes];
            string instrbuf;

            if (revhex.Checked == true)
            {
                intputs = mycom.Read(inputbuf, 0, bytes);
                instrbuf = byteToHexStr(inputbuf);
                System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
                textBox1.Text = textBox1.Text   instrbuf;
            }
            else
            {

                intputs = mycom.Read(incharbuf, 0, bytes);
                instrbuf = new string(incharbuf);
                System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
                textBox1.Text = textBox1.Text   instrbuf;
            
            }

         }



        private void Form1_Load(object sender, EventArgs e)
        {
            uint i;
            string Comnotempstr;

            for (i = 1; i < 254; i  )
            {
                Comnotempstr = "COM"   Convert.ToString(i);  //数字转成字符;
                PortNameBox.Items.Add(Comnotempstr);
            }


            PortNameBox.SelectedIndex = 3;

            BpsBox.Items.Add("1200");
            BpsBox.Items.Add("2400");
            BpsBox.Items.Add("4800");
            BpsBox.Items.Add("9600");
            BpsBox.Items.Add("19200");
            BpsBox.Items.Add("38400");
            BpsBox.Items.Add("57600");
            BpsBox.Items.Add("115200");
            BpsBox.SelectedIndex = 3;

            StpBox.Items.Add("1位");
            StpBox.Items.Add("2位");
            StpBox.SelectedIndex = 0;

            ParBox.Items.Clear();
            ParBox.Items.AddRange(Enum.GetNames(typeof(System.IO.Ports.Parity)));  //自动填加奇偶校验位
            ParBox.SelectedIndex = 0;



        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }


        private static byte[] send_byte(string hexString) /// 字符串转16进制字节数组 
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString  = " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i  )
                try    
                {
                    returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); 
                }
                catch
                {
                    MessageBox.Show("HEX数据" Convert.ToString(i 1)  "字节为非法的hex数据", "请注意");
                }
            return returnBytes;
        }

        private void ClrSendbuf_Click(object sender, EventArgs e)
        {
            textBox2.Text = "";
        }

        private void PortNameBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
           // MessageBox.Show("再见!");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }
        
    }
}


标签: 串口 通信

实例下载地址

C# 串口通信程序 源码下载

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

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

网友评论

发表评论

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

查看所有0条评论>>

小贴士

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

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

关于好例子网

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

;
报警