首页 新闻 搜索 专区 学院

求串口通信的代码程序

0
悬赏园豆:200 [已解决问题] 解决于 2009-12-31 15:50

要有发送数据的程序和接收数据的程序,有的发给我。

reacgae的主页 reacgae | 初学一级 | 园豆:0
提问于:2009-12-31 10:07
< >
分享
最佳答案
0

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.Threading;

namespace Read
{
    public partial class Form1 : Form
    {
        public SerialPort serialPort2;

        delegate void HandleInterfaceUpdateDelegate(string text);  //委托,此为重点
        HandleInterfaceUpdateDelegate interfaceUpdateHandle;


        public Form1()
        {
            InitializeComponent();
            InitCOM("COM4");
        }
       
        /// <summary>
        /// 串口接收通信配置方法
        /// </summary>
        /// <param name="PortName"></param>
        public void InitCOM(string PortName)
        {
            interfaceUpdateHandle = new HandleInterfaceUpdateDelegate(UpdateTextBox);  //实例化委托对象
            serialPort2 = new SerialPort(PortName, 9600, Parity.None, 8, StopBits.One);
            serialPort2.DataReceived += new SerialDataReceivedEventHandler(serialPort2_DataReceived);//DataReceived事件委托
            serialPort2.ReceivedBytesThreshold = 1;
            serialPort2.RtsEnable = true;
            OpenPort();
            label1.Text = label1.Text+serialPort2.PortName;
            label2.Text = label2.Text + serialPort2.BaudRate.ToString();
            label3.Text = label3.Text + serialPort2.DataBits.ToString();
            label4.Text = label4.Text + serialPort2.StopBits.ToString();
            label5.Text = label5.Text + serialPort2.Parity.ToString();


            comboBox1.Text = serialPort2.PortName;
            comboBox2.Text = serialPort2.BaudRate.ToString();
            comboBox3.Text = serialPort2.DataBits.ToString();
            comboBox4.Text = serialPort2.StopBits.ToString();
            comboBox5.Text = serialPort2.Parity.ToString();

        }

        /// <summary>
        /// 数据显示方法
        /// </summary>
        /// <param name="text"></param>
        private void UpdateTextBox(string text)
        {
            textBox1.Text = text;
        }
       
        /// <summary>
        /// 数据接收事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void serialPort2_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Thread.Sleep(2000);
            byte[] readBuffer = new byte[serialPort2.ReadBufferSize];
            serialPort2.Read(readBuffer, 0, readBuffer.Length);
            this.Invoke(interfaceUpdateHandle, new string[] { Encoding.UTF8.GetString(readBuffer, 0, 100) });
        }
       
        //public static string ByteArrayToHexString(byte[] data)//字节数组转为十六进制字符串
        //{
        //    StringBuilder sb = new StringBuilder(data.Length * 3);
        //    foreach (byte b in data)
        //        sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
        //    return sb.ToString().ToUpper();
        //}

        //打开串口的方法
        public void OpenPort()
        {
            try
            {
                serialPort2.Open();
            }
            catch { }
            if (serialPort2.IsOpen)
            {
                Console.WriteLine("the port is opened!");
            }
            else
            {
                Console.WriteLine("failure to open the port!");
            }
        }

        //关闭串口的方法
        public void ClosePort()
        {
            serialPort2.Close();
            if (!serialPort2.IsOpen)
            {
                Console.WriteLine("the port is already closed!");
            }
        }

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender">事件参数</param>
        /// <param name="e">窗体</param>
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SendCommand(textBox2.Text);

 

                textBox1.Text = "";
                MessageBox.Show("数据发送成功!", "系统提示");
            }
            catch
            {
                MessageBox.Show("发送失败!");
            }
        }


        //向串口发送数据
        public void SendCommand(string CommandString)
        {
            byte[] WriteBuffer = Encoding.ASCII.GetBytes(CommandString);
            serialPort2.Write(WriteBuffer, 0, WriteBuffer.Length);
        }
    }
}

 

 

 

 

 

 

 

 

 

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.Threading;

namespace Send
{
    public partial class Form1 : Form
    {
        public SerialPort serialPort2;

        public Form1()
        {
            InitializeComponent();
            InitCOM("COM4");
        }

        /// <summary>
        /// 串口发送通信配置方法
        /// </summary>
        /// <param name="PortName"></param>
        public void InitCOM(string PortName)
        {
            serialPort2 = new SerialPort(PortName,9600,Parity.None,8,StopBits.One);
            OpenPort();

            comboBox1.Text = serialPort2.PortName;
            comboBox2.Text = serialPort2.BaudRate.ToString();
            comboBox3.Text = serialPort2.DataBits.ToString();
            comboBox4.Text = serialPort2.StopBits.ToString();
            comboBox5.Text = serialPort2.Parity.ToString();
        }

        /// <summary>
        /// 发送按钮事件
        /// </summary>
        /// <param name="sender">事件参数</param>
        /// <param name="e">按钮</param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                 SendCommand(textBox1.Text);

 

                textBox1.Text = "";
                MessageBox.Show("数据发送成功!", "系统提示");
            }
            catch
            {
                MessageBox.Show("发送失败!");
            }
        }

        //打开串口的方法
        public void OpenPort()
        {
            try
            {
                serialPort2.Open();
            }
            catch { }
            if (serialPort2.IsOpen)
            {
                Console.WriteLine("the port is opened!");
            }
            else
            {
                Console.WriteLine("failure to open the port!");
            }
        }
 
        //关闭串口的方法
        public void ClosePort()
        {
            serialPort2.Close();

            if (!serialPort2.IsOpen)
            {
                Console.WriteLine("the port is already closed!");
            }
        }

        //向串口发送数据
        public void SendCommand(string CommandString)
        {
            byte[] WriteBuffer = Encoding.ASCII.GetBytes(CommandString);
            serialPort2.Write(WriteBuffer, 0, WriteBuffer.Length);
        }

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender">事件参数</param>
        /// <param name="e">窗体</param>
        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

收获园豆:200
大草包 | 小虾三级 |园豆:1745 | 2009-12-31 10:10
这个例子我要了!~
reacgae | 园豆:0 (初学一级) | 2009-12-31 13:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册