• 大小: 0.20M
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2024-04-30
  • 语言: 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.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.IO;
using System.Collections;

namespace RS232Comm
{
    public partial class MainForm : Form
    {
        protected Boolean stop = false;    
        protected Boolean conState = false;
        private StreamReader sRead;
        public MainForm()
        {
            InitializeComponent();
        }
        string strRecieve;
        bool bAccept = false;
        SerialPort sp = new SerialPort();
 
        public static string strPortName = ““;
        public static string strBaudRate = ““;
        public static string strDataBits = ““;
        public static string strStopBits = ““;

        private void btnSetSP_Click(object sender EventArgs e)//串口设置
        {
            timer1.Enabled = false;
            sp.Close();
            ComSet dlg = new ComSet();

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                sp.PortName = strPortName;
                sp.BaudRate = int.Parse(strBaudRate);
                sp.DataBits = int.Parse(strDataBits);
                sp.StopBits = (StopBits)int.Parse(strStopBits);
                sp.ReadTimeout = 500;
            }
        }

        private void btnSwitchSP_Click(object sender EventArgs e)//打开串口
        {
            if (btnSwitchSP.Text == “打开串口“)
            {
                if (strPortName != ““ && strBaudRate != ““ && strDataBits != ““ && strStopBits != ““)
                {
                    try
                    {
                        if (sp.IsOpen)
                        {
                            sp.Close();
                            sp.Open(); //打开串口    
                        }
                        else
                        {
                            sp.Open();//打开串口    
                        }
                        btnSwitchSP.Text = “关闭串口“;
                        groupBox1.Enabled = true;
                        groupBox2.Enabled = true;
                        this.toolStripStatusLabel1.Text = “端口号:“ + sp.PortName + “ 丨“;
                        this.toolStripStatusLabel2.Text = “波特率:“ + sp.BaudRate + “ 丨“;
                        this.toolStripStatusLabel3.Text = “数据位:“ + sp.DataBits + “ 丨“;
                        this.toolStripStatusLabel4.Text = “停止位:“ + sp.StopBits + “ 丨“;
                        this.toolStripStatusLabel5.Text = ““;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(“错误:“ + ex.Message“C#串口通信“);
                    }
                }
                else
                {
                    MessageBox.Show(“请先设置串口!““RS232串口通信“);
                }
            }
            else
            {
             

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件     219648  2014-08-09 19:46  C#SerialPort\bin\Debug\RS232Comm.exe

     文件      46592  2014-08-09 19:46  C#SerialPort\bin\Debug\RS232Comm.pdb

     文件      11600  2014-12-13 11:36  C#SerialPort\bin\Debug\RS232Comm.vshost.exe

     文件        490  2010-03-17 22:39  C#SerialPort\bin\Debug\RS232Comm.vshost.exe.manifest

     文件      23040  2014-08-09 13:49  C#SerialPort\bin\Debug\串口通信.exe

     文件      69120  2014-08-09 13:49  C#SerialPort\bin\Debug\串口通信.pdb

     文件      98798  2014-01-02 12:58  C#SerialPort\bipt.ico

     文件       9118  2014-08-09 19:49  C#SerialPort\MainForm.cs

     文件      14721  2014-08-09 19:46  C#SerialPort\MainForm.Designer.cs

     文件     154906  2014-08-09 19:46  C#SerialPort\MainForm.resx

     文件       2164  2014-08-09 12:42  C#SerialPort\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7367  2014-12-13 11:36  C#SerialPort\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        384  2012-07-10 17:08  C#SerialPort\obj\x86\Debug\GenerateResource.read.1.tlog

     文件        940  2012-07-10 17:08  C#SerialPort\obj\x86\Debug\GenerateResource.write.1.tlog

     文件       8614  2012-07-12 12:10  C#SerialPort\obj\x86\Debug\ResolveAssemblyReference.cache

     文件        180  2014-08-09 19:46  C#SerialPort\obj\x86\Debug\RS232Comm.ComSet.resources

     文件       2596  2014-12-13 11:36  C#SerialPort\obj\x86\Debug\RS232Comm.csproj.FileListAbsolute.txt

     文件     219648  2014-08-09 19:46  C#SerialPort\obj\x86\Debug\RS232Comm.exe

     文件      99364  2014-08-09 19:46  C#SerialPort\obj\x86\Debug\RS232Comm.MainForm.resources

     文件      46592  2014-08-09 19:46  C#SerialPort\obj\x86\Debug\RS232Comm.pdb

     文件        180  2014-08-09 19:46  C#SerialPort\obj\x86\Debug\RS232Comm.Properties.Resources.resources

     文件       4608  2014-08-09 13:57  C#SerialPort\obj\x86\Debug\TempPE\Properties.Resources.Designer.cs.dll

     文件        180  2014-08-09 13:49  C#SerialPort\obj\x86\Debug\串口通信.ComSet.resources

     文件       6568  2014-08-09 13:54  C#SerialPort\obj\x86\Debug\串口通信.csproj.FileListAbsolute.txt

     文件       1091  2014-01-03 19:14  C#SerialPort\obj\x86\Debug\串口通信.csproj.GenerateResource.Cache

     文件       9948  2014-01-02 11:13  C#SerialPort\obj\x86\Debug\串口通信.csprojResolveAssemblyReference.cache

     文件        180  2014-08-09 13:49  C#SerialPort\obj\x86\Debug\串口通信.Form1.resources

     文件        180  2014-08-09 13:49  C#SerialPort\obj\x86\Debug\串口通信.Form2.resources

     文件        180  2014-08-09 13:49  C#SerialPort\obj\x86\Debug\串口通信.Properties.Resources.resources

     文件        493  2014-08-09 13:57  C#SerialPort\Program.cs

............此处省略25个文件信息

评论

共有 条评论