• 大小: 2.37MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-10-27
  • 语言: C#
  • 标签: 串口通讯  c#源码  

资源简介

C#实现与下位机进行串口通讯,资料里面有相应教程以及代码工程

资源截图

代码片段和文件信息

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 Microsoft.Win32;
using System.Threading;

namespace SerialPort_Assistant
{
    public partial class Form1:Form
    {
        SerialPort serialPort;
        System.Timers.Timer timerSendAuto;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender EventArgs e)
        {
            cbComList.DataSource = GetComList();
            cbBaudRateList.DataSource = GetRautBitsList();
            cbParityList.DataSource = GetParityList();
            cbDataBitsList.DataSource = GetDataBitsList();
            cbStopBitsList.DataSource = GetStopBitsList();
        }
        private void Form1_FormClosing(object sender FormClosingEventArgs e)
        {
            if (serialPort != null)
                if (serialPort.IsOpen)//澶勪簬鎵撳紑鐘舵€?
                {
                    serialPort.Close();
                    serialPort = null;
                }
            if (timerSendAuto != null)
            {
                timerSendAuto.Close();
                timerSendAuto.Dispose();
            }
        }
        //淇″彿鐏?
        private void picComState_Paint(object sender PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Brush brush;
            if (serialPort == null)
                brush = Brushes.Red;
            else if (serialPort.IsOpen)
                brush = Brushes.Green;
            else
                brush = Brushes.Red;
            g.FillEllipse(brush e.ClipRectangle);
        }
        //鎵撳紑銆佸叧闂覆鍙?
        private void btnComOpen_Click(object sender EventArgs e)
        {
            if (serialPort == null)
            {
                serialPort = new SerialPort();
                serialPort.RtsEnable = true;
                serialPort.ReceivedBytesThreshold = 1;
                serialPort.ErrorReceived+=new SerialErrorReceivedEventHandler(serialPort_ErrorReceived);
                serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
            }
            if (serialPort.IsOpen)//澶勪簬鎵撳紑鐘舵€?
            {
                serialPort.Close();
                if (timerSendAuto != null)
                    timerSendAuto.Stop();
            }
            else
            {
                try
                {
                    serialPort.PortName = cbComList.Text.Trim();
                    serialPort.BaudRate = int.Parse(cbBaudRateList.Text);
                    serialPort.Parity = (Parity)Enum.Parse(typeof(Parity) cbParityList.Text);
                    serialPort.DataBits = int.Parse(cbDataBitsList.Text);
                    serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits) cbStopBitsList.Text);
                    serialPo

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

     文件      32768  2012-03-29 21:16  WindowsApplication1\WindowsApplication1\bin\Debug\WindowsApplication1.exe

     文件      36352  2012-03-29 21:16  WindowsApplication1\WindowsApplication1\bin\Debug\WindowsApplication1.pdb

     文件       5632  2005-11-11 22:25  WindowsApplication1\WindowsApplication1\bin\Debug\WindowsApplication1.vshost.exe

     目录          0  2012-03-29 21:16  WindowsApplication1\WindowsApplication1\bin\Debug

     目录          0  2012-03-29 21:10  WindowsApplication1\WindowsApplication1\bin

     文件      10297  2012-03-29 21:16  WindowsApplication1\WindowsApplication1\Form1.cs

     文件      19068  2012-03-29 21:11  WindowsApplication1\WindowsApplication1\Form1.Designer.cs

     文件       5817  2012-03-29 21:11  WindowsApplication1\WindowsApplication1\Form1.resx

     文件        180  2012-03-29 21:11  WindowsApplication1\WindowsApplication1\obj\Debug\SerialPort_Assistant.Form1.resources

     目录          0  2012-03-29 21:10  WindowsApplication1\WindowsApplication1\obj\Debug\TempPE

     文件        842  2012-03-29 21:11  WindowsApplication1\WindowsApplication1\obj\Debug\WindowsApplication1.csproj.GenerateResource.Cache

     文件      32768  2012-03-29 21:16  WindowsApplication1\WindowsApplication1\obj\Debug\WindowsApplication1.exe

     文件      36352  2012-03-29 21:16  WindowsApplication1\WindowsApplication1\obj\Debug\WindowsApplication1.pdb

     文件        180  2012-03-29 21:11  WindowsApplication1\WindowsApplication1\obj\Debug\WindowsApplication1.Properties.Resources.resources

     目录          0  2012-03-29 21:16  WindowsApplication1\WindowsApplication1\obj\Debug

     文件        873  2012-03-29 21:17  WindowsApplication1\WindowsApplication1\obj\WindowsApplication1.csproj.FileListAbsolute.txt

     目录          0  2012-03-29 21:11  WindowsApplication1\WindowsApplication1\obj

     文件        481  2012-03-29 21:12  WindowsApplication1\WindowsApplication1\Program.cs

     文件       1186  2012-03-29 21:10  WindowsApplication1\WindowsApplication1\Properties\AssemblyInfo.cs

     文件       2894  2012-03-29 21:10  WindowsApplication1\WindowsApplication1\Properties\Resources.Designer.cs

     文件       5612  2012-03-29 21:10  WindowsApplication1\WindowsApplication1\Properties\Resources.resx

     文件       1104  2012-03-29 21:10  WindowsApplication1\WindowsApplication1\Properties\Settings.Designer.cs

     文件        249  2012-03-29 21:10  WindowsApplication1\WindowsApplication1\Properties\Settings.settings

     目录          0  2012-03-29 21:10  WindowsApplication1\WindowsApplication1\Properties

     文件       3245  2012-03-29 21:12  WindowsApplication1\WindowsApplication1\WindowsApplication1.csproj

     目录          0  2012-03-29 21:16  WindowsApplication1\WindowsApplication1

     文件        946  2012-03-29 21:10  WindowsApplication1\WindowsApplication1.sln

    ..A..H.     12800  2012-03-29 21:17  WindowsApplication1\WindowsApplication1.suo

     目录          0  2012-03-29 21:10  WindowsApplication1

----------- ---------  ---------- -----  ----

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

评论

共有 条评论