• 大小: 133KB
    文件类型: .zip
    金币: 2
    下载: 2 次
    发布日期: 2021-05-13
  • 语言: C#
  • 标签: RS232  C#  

资源简介

1、需要一根Rs232通讯线;2、2台PC机; 之前用c#实现的一个Rs232通讯,最近工厂新增产线,又翻出来看了看。 实现了两台pc机通过rs232通讯线来进行简单的发送、接收数据, 代码简单通俗易懂,适合入门。

资源截图

代码片段和文件信息

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;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SerialPort port1 = new SerialPort();
        string InputData = String.Empty;
        delegate void SetTextCallback(string text);

        private void Port_Select()
        {//获取机器中的串口地址
            string[] ports = SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                comboBox1.Items.Add(port);
            }
        }

        private void Form1_Load_1(object sender EventArgs e)
        {
            Port_Select();
            this.comboBox1.SelectedIndex = 0;
            this.comboBox2.SelectedIndex = 0;
        }

        private void button1_Click(object sender EventArgs e)
        {
            if (button1.Text == “关闭串口“)  //当要关闭串口的时候
            {
                port1.DiscardOutBuffer();
                port1.DiscardInBuffer();
                port1.Close();
                button1.Text = “打开串口“;
                label3.Text = “串口当前状况:未打开“;
                comboBox1.Enabled = true;
                comboBox2.Enabled = true;
            }
            else if (button1.Text == “打开串口“) //当要打开串口的时候
            {
                try
                {
                    port1.PortName = comboBox1.SelectedItem.ToString();
                    port1.BaudRate = Convert.ToInt32(comboBox2.SelectedItem);
                    port1.DataBits = 8;
                    port1.RtsEnable = true;
                    port1.Open();
                    port1.DiscardOutBuffer();
                    port1.DiscardInBuffer();
                    button1.Text = “关闭串口“;
                    comboBox1.Enabled = false;
                    comboBox2.Enabled = false;
                    label3.Text = “串口:“ + comboBox1.SelectedItem.ToString() + “ 波特率:“ + comboBox2.SelectedItem.ToString() + “  数据位:8 “;
                }
                catch
                {
                    button1.Text = “打开串口“;
                    label3.Text = “串口:“ + comboBox1.SelectedItem.ToString() + “打开失败“;
                    MessageBox.Show(“该串口无法打开“);
                }
            }
        }

        private void button2_Click(object sender EventArgs e)
        {//刷新串口列表
            comboBox1.Items.Clear();
            Port_Select();
            this.comboBox1.SelectedIndex = 0;
            this.comboBox2.SelectedIndex = 0;
            comboBox1.Enabled = true;
            comboBox2.Enabled = true;
            port1.Close();
            button1.Text = “打开串口“;
            label3.Text = “串口当前状况:未打开“;

        }

        private void button3_Click(object sender EventArgs e)
        {//发送数据
            byte[] data = En

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-12-06 09:47  串口通讯C#\
     目录           0  2018-12-06 09:47  串口通讯C#\WindowsApplication1\
     目录           0  2018-12-06 09:47  串口通讯C#\WindowsApplication1\.vs\
     目录           0  2018-12-06 09:47  串口通讯C#\WindowsApplication1\.vs\串口通讯\
     目录           0  2018-12-06 09:47  串口通讯C#\WindowsApplication1\.vs\串口通讯\v14\
     文件       12800  2010-08-13 10:15  串口通讯C#\WindowsApplication1\.vs\串口通讯\v14\.suo
     目录           0  2018-12-06 09:47  串口通讯C#\WindowsApplication1\Backup\
     目录           0  2018-12-06 09:47  串口通讯C#\WindowsApplication1\Backup\Backup\
     目录           0  2018-12-06 09:47  串口通讯C#\WindowsApplication1\Backup\Backup\WindowsApplication1\
     文件        8094  2010-08-13 10:00  串口通讯C#\WindowsApplication1\Backup\Backup\WindowsApplication1\Form1.Designer.cs
     文件        3490  2010-08-13 10:15  串口通讯C#\WindowsApplication1\Backup\Backup\WindowsApplication1\Form1.cs
     文件        5814  2010-08-13 10:01  串口通讯C#\WindowsApplication1\Backup\Backup\WindowsApplication1\Form1.resx
     文件         478  2010-08-13 09:43  串口通讯C#\WindowsApplication1\Backup\Backup\WindowsApplication1\Program.cs
     目录           0  2018-12-06 09:47  串口通讯C#\WindowsApplication1\Backup\Backup\WindowsApplication1\Properties\
     文件        1186  2010-08-13 09:43  串口通讯C#\WindowsApplication1\Backup\Backup\WindowsApplication1\Properties\AssemblyInfo.cs
     文件        2894  2010-08-13 09:43  串口通讯C#\WindowsApplication1\Backup\Backup\WindowsApplication1\Properties\Resources.Designer.cs
     文件        5612  2010-08-13 09:43  串口通讯C#\WindowsApplication1\Backup\Backup\WindowsApplication1\Properties\Resources.resx
     文件        1104  2010-08-13 09:43  串口通讯C#\WindowsApplication1\Backup\Backup\WindowsApplication1\Properties\Settings.Designer.cs
     文件         249  2010-08-13 09:43  串口通讯C#\WindowsApplication1\Backup\Backup\WindowsApplication1\Properties\Settings.settings
     文件        3245  2010-08-13 09:46  串口通讯C#\WindowsApplication1\Backup\Backup\WindowsApplication1\串口通讯.csproj
     文件         932  2010-08-13 09:46  串口通讯C#\WindowsApplication1\Backup\Backup\串口通讯.sln
     文件       12800  2010-08-13 10:15  串口通讯C#\WindowsApplication1\Backup\Backup\串口通讯.suo
     文件        4025  2018-01-15 14:14  串口通讯C#\WindowsApplication1\Backup\UpgradeLog.xml
     目录           0  2018-12-06 09:47  串口通讯C#\WindowsApplication1\Backup\WindowsApplication1\
     文件        8094  2010-08-13 10:00  串口通讯C#\WindowsApplication1\Backup\WindowsApplication1\Form1.Designer.cs
     文件        3490  2010-08-13 10:15  串口通讯C#\WindowsApplication1\Backup\WindowsApplication1\Form1.cs
     文件        5814  2018-01-15 14:53  串口通讯C#\WindowsApplication1\Backup\WindowsApplication1\Form1.resx
     文件         478  2010-08-13 09:43  串口通讯C#\WindowsApplication1\Backup\WindowsApplication1\Program.cs
     目录           0  2018-12-06 09:47  串口通讯C#\WindowsApplication1\Backup\WindowsApplication1\Properties\
     文件        1186  2010-08-13 09:43  串口通讯C#\WindowsApplication1\Backup\WindowsApplication1\Properties\AssemblyInfo.cs
     文件        2862  2018-01-15 14:14  串口通讯C#\WindowsApplication1\Backup\WindowsApplication1\Properties\Resources.Designer.cs
............此处省略68个文件信息

评论

共有 条评论