基本信息
源码名称:C# 串口通信程序 源码下载
源码大小:0.43M
文件格式:.zip
开发语言:C#
更新时间:2016-09-30
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 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)
        {

        }
        
    }
}