资源简介

开发环境VS2010,C#编写的串口通信程序,利用了serialPort组件,测试过可以使用。

资源截图

代码片段和文件信息

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;

namespace SerialPortTry
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        SerialPort sp = new SerialPort();
        bool cxstate = true;


        private void Form1_Load(object sender EventArgs e)
        {
            
            if (sp.IsOpen)
            {
                sp.Close();
            }
            label7.BackColor = Color.Black;
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
            comboBox3.SelectedIndex = 0;
            comboBox4.SelectedIndex = 0;
            comboBox5.SelectedIndex = 0;
            CheckForIllegalCrossThreadCalls = false;

            
            

        }



        private void button1_Click(object sender EventArgs e)
        {
            sp.PortName = comboBox1.SelectedItem.ToString();
            sp.BaudRate = Convert.ToInt32(comboBox2.SelectedItem);
            sp.DataBits = Convert.ToInt32(comboBox3.SelectedItem);
            //serialPort1.StopBits = Convert.ToSinglecomboBox4.SelectedItem);
            label6.Text = “您选用的串口是:“+sp.PortName;
            if (sp.IsOpen)
            {
                btn_send.Enabled = true;
                button3.Enabled = true;
            }
            else
            {
                btn_send.Enabled = false;
                button3.Enabled = false;
            }
        }

        private void button2_Click(object sender EventArgs e)
        {
            if (sp.IsOpen)
            {
                sp.Close();
                btn_send.Enabled = false;
                button3.Enabled = false;
                label7.BackColor = Color.Black;
                label6.Text = “串口已关闭...“;
                button2.Text = “打开串口“;
            }
            else
            {
                
                sp.Open();
                btn_send.Enabled = true;
                button3.Enabled = true;
                //sp.DtrEnable = true;
                //sp.RtsEnable = true;
                label7.BackColor = Color.Red;
                label6.Text = “串口已打开...“;

                button2.Text = “关闭串口“;
            }
            
        }
        
        


        private void btn_send_Click(object sender EventArgs e)
        {
             sp.WriteLine(textBox1.Text);
             label6.Text = “发送成功“;
        }

        
        private void button3_Click(object sender EventArgs e)
        {
            /*//===================================================查询方式1
            if (button3.Text == “开始接收“)
             {
                 button3.Text = “停止接收“;
                 textBox2.Text = ““;
                 wh

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

     文件      14336  2012-07-04 15:14  SerialPortTry\SerialPortTry\bin\Debug\SerialPortTry.exe

     文件      28160  2012-07-04 15:14  SerialPortTry\SerialPortTry\bin\Debug\SerialPortTry.pdb

     文件      11600  2012-07-04 22:20  SerialPortTry\SerialPortTry\bin\Debug\SerialPortTry.vshost.exe

     文件        490  2010-03-17 22:39  SerialPortTry\SerialPortTry\bin\Debug\SerialPortTry.vshost.exe.manifest

     文件       5358  2012-07-01 11:11  SerialPortTry\SerialPortTry\Form1.cs

     文件      13588  2012-07-01 10:19  SerialPortTry\SerialPortTry\Form1.Designer.cs

     文件       6190  2012-07-01 10:19  SerialPortTry\SerialPortTry\Form1.resx

     文件       4440  2012-06-26 22:55  SerialPortTry\SerialPortTry\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6235  2012-07-04 22:20  SerialPortTry\SerialPortTry\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        268  2012-07-01 10:19  SerialPortTry\SerialPortTry\obj\x86\Debug\GenerateResource.read.1.tlog

     文件        658  2012-07-01 10:19  SerialPortTry\SerialPortTry\obj\x86\Debug\GenerateResource.write.1.tlog

     文件       7897  2012-06-26 23:01  SerialPortTry\SerialPortTry\obj\x86\Debug\ResolveAssemblyReference.cache

     文件        778  2012-07-04 22:20  SerialPortTry\SerialPortTry\obj\x86\Debug\SerialPortTry.csproj.FileListAbsolute.txt

     文件      14336  2012-07-04 15:14  SerialPortTry\SerialPortTry\obj\x86\Debug\SerialPortTry.exe

     文件        180  2012-07-01 10:19  SerialPortTry\SerialPortTry\obj\x86\Debug\SerialPortTry.Form1.resources

     文件      28160  2012-07-04 15:14  SerialPortTry\SerialPortTry\obj\x86\Debug\SerialPortTry.pdb

     文件        180  2012-06-26 11:43  SerialPortTry\SerialPortTry\obj\x86\Debug\SerialPortTry.Properties.Resources.resources

     文件        494  2012-06-26 11:31  SerialPortTry\SerialPortTry\Program.cs

     文件       1406  2012-06-26 11:31  SerialPortTry\SerialPortTry\Properties\AssemblyInfo.cs

     文件       2876  2012-06-26 11:31  SerialPortTry\SerialPortTry\Properties\Resources.Designer.cs

     文件       5612  2012-06-26 11:31  SerialPortTry\SerialPortTry\Properties\Resources.resx

     文件       1098  2012-06-26 11:31  SerialPortTry\SerialPortTry\Properties\Settings.Designer.cs

     文件        249  2012-06-26 11:31  SerialPortTry\SerialPortTry\Properties\Settings.settings

     文件       3685  2012-06-26 11:43  SerialPortTry\SerialPortTry\SerialPortTry.csproj

     文件        881  2012-06-26 11:31  SerialPortTry\SerialPortTry.sln

    ..A..H.     19968  2012-07-04 22:20  SerialPortTry\SerialPortTry.suo

     目录          0  2012-06-26 11:31  SerialPortTry\SerialPortTry\obj\x86\Debug\TempPE

     目录          0  2012-07-04 15:14  SerialPortTry\SerialPortTry\obj\x86\Debug

     目录          0  2012-06-26 11:43  SerialPortTry\SerialPortTry\bin\Debug

     目录          0  2012-06-26 11:31  SerialPortTry\SerialPortTry\obj\x86

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

评论

共有 条评论