• 大小: 192KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-01
  • 语言: C#
  • 标签: RS232  串口  

资源简介

完整的RS232串口通讯示例

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace RS232串口通讯CSharp
{
    public partial class frmTest : Form
    {
        public frmTest()
        {
            InitializeComponent();
        }

        /// 
        /// SerialPort串行端口
        /// 

        private SerialPort Com_SerialPort = new SerialPort();
        /// 
        /// 是否没有执行完invoke相关操作
        /// 

        private bool IsListening = false;
        /// 
        /// 是否正在关闭串口,执行Application.DoEvents,并阻止再次invoke
        /// 

        private bool IsClosing = false;
        private void button1_Click(object sender EventArgs e)
        {
            //MCJS50.ReadValue(Com_SerialPort);
            //MessageBox.Show(MCJS50.intJs.ToString());

           

        }

        void Com_SerialPort_DataReceived(object sender SerialDataReceivedEventArgs e)
        {
            if (IsClosing) { return; }
            try
            {
                if (Com_SerialPort.BytesToRead <= 0) return;
                Thread.Sleep(50);
                IsListening = true;
                int intBytesToRead = Com_SerialPort.BytesToRead;
                byte[] buffer = new byte[intBytesToRead];
                Com_SerialPort.Read(buffer 0 intBytesToRead);
                if (buffer.Length >= 6)
                {
                    if (buffer[0] == 0xAC && buffer[buffer.Length - 1] == 0x84)
                    {
                        //读计数值
                        string strValue = buffer[5].ToString(“X2“) + “ “ + buffer[6].ToString(“X2“) + “ “ + buffer[7].ToString(“X2“) + “ “ + buffer[8].ToString(“X2“) + “ “ + buffer[9].ToString(“X2“) + “ “ + buffer[10].ToString(“X2“);
                        Int64 intValue = Int64.Parse(buffer[5].ToString() + buffer[6].ToString() + buffer[7].ToString() + buffer[8].ToString() + buffer[9].ToString() + buffer[10].ToString());
                        this.Invoke((EventHandler)(delegate
                        {
                            this.label1.Text = intValue.ToString();
                        }));
                    }
                    else
                    {
                        buffer = null;
                    }
                }
            }
            finally
            {
                IsListening = false;
            }
        }

        private void frmTest_Load(object sender EventArgs e)
        {
            Com_SerialPort = new SerialPort();
            MCJS50.SetSerialPort(Com_SerialPort IsClosing IsListening);
            Com_SerialPort.DataReceived += Com_SerialPort_DataReceived;
        }

        private void button2_Click(object sender EventArgs e)
        {
            MCJS50.ReadValue(Com_SerialPort);
        }
    }

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

    ..A..H.     58880  2016-09-21 16:18  RS232串口通讯CSharp\.vs\RS232串口通讯CSharp\v14\.suo

     文件        157  2016-09-07 16:06  RS232串口通讯CSharp\RS232串口通讯CSharp\App.config

     文件      35840  2016-09-20 10:55  RS232串口通讯CSharp\RS232串口通讯CSharp\bin\Debug\RS232串口通讯CSharp.dll

     文件        157  2016-09-07 16:06  RS232串口通讯CSharp\RS232串口通讯CSharp\bin\Debug\RS232串口通讯CSharp.dll.config

     文件      54784  2016-09-20 10:55  RS232串口通讯CSharp\RS232串口通讯CSharp\bin\Debug\RS232串口通讯CSharp.pdb

     文件      21680  2016-09-19 08:30  RS232串口通讯CSharp\RS232串口通讯CSharp\bin\Debug\RS232串口通讯CSharp.vshost.exe

     文件        157  2016-09-07 16:06  RS232串口通讯CSharp\RS232串口通讯CSharp\bin\Debug\RS232串口通讯CSharp.vshost.exe.config

     文件        490  2015-06-04 12:48  RS232串口通讯CSharp\RS232串口通讯CSharp\bin\Debug\RS232串口通讯CSharp.vshost.exe.manifest

     文件     180904  2013-07-09 11:04  RS232串口通讯CSharp\RS232串口通讯CSharp\bin\Debug\System.Net.Http.dll

     文件     148528  2015-11-02 01:02  RS232串口通讯CSharp\RS232串口通讯CSharp\bin\Debug\System.Net.Http.xml

     文件      17576  2013-07-09 11:34  RS232串口通讯CSharp\RS232串口通讯CSharp\bin\Debug\zh-Hans\System.Net.Http.resources.dll

     文件       3088  2016-09-07 17:28  RS232串口通讯CSharp\RS232串口通讯CSharp\frmTest.cs

     文件       3299  2016-09-07 17:27  RS232串口通讯CSharp\RS232串口通讯CSharp\frmTest.Designer.cs

     文件       5817  2016-09-07 17:27  RS232串口通讯CSharp\RS232串口通讯CSharp\frmTest.resx

     文件       1788  2016-09-07 16:39  RS232串口通讯CSharp\RS232串口通讯CSharp\frmUserVerification.cs

     文件       4930  2016-09-07 16:27  RS232串口通讯CSharp\RS232串口通讯CSharp\frmUserVerification.Designer.cs

     文件       5817  2016-09-07 16:27  RS232串口通讯CSharp\RS232串口通讯CSharp\frmUserVerification.resx

     文件       3203  2016-09-20 10:54  RS232串口通讯CSharp\RS232串口通讯CSharp\MCJS50.cs

     文件      18783  2016-09-08 10:40  RS232串口通讯CSharp\RS232串口通讯CSharp\MCJS50_Rs232.cs

     文件      46603  2016-09-07 18:23  RS232串口通讯CSharp\RS232串口通讯CSharp\MCJS50_Rs232.Designer.cs

     文件       6414  2016-09-07 18:23  RS232串口通讯CSharp\RS232串口通讯CSharp\MCJS50_Rs232.resx

     文件       2410  2016-09-13 17:07  RS232串口通讯CSharp\RS232串口通讯CSharp\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6243  2016-09-20 10:55  RS232串口通讯CSharp\RS232串口通讯CSharp\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件       2423  2016-09-20 10:55  RS232串口通讯CSharp\RS232串口通讯CSharp\obj\Debug\RS232串口通讯CSharp.csproj.FileListAbsolute.txt

     文件        986  2016-09-20 10:55  RS232串口通讯CSharp\RS232串口通讯CSharp\obj\Debug\RS232串口通讯CSharp.csproj.GenerateResource.Cache

     文件      35840  2016-09-20 10:55  RS232串口通讯CSharp\RS232串口通讯CSharp\obj\Debug\RS232串口通讯CSharp.dll

     文件        180  2016-09-20 10:55  RS232串口通讯CSharp\RS232串口通讯CSharp\obj\Debug\RS232串口通讯CSharp.frmTest.resources

     文件        180  2016-09-20 10:55  RS232串口通讯CSharp\RS232串口通讯CSharp\obj\Debug\RS232串口通讯CSharp.frmUserValidating.resources

     文件        180  2016-09-20 10:55  RS232串口通讯CSharp\RS232串口通讯CSharp\obj\Debug\RS232串口通讯CSharp.MCJS50_Rs232.resources

     文件      54784  2016-09-20 10:55  RS232串口通讯CSharp\RS232串口通讯CSharp\obj\Debug\RS232串口通讯CSharp.pdb

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

评论

共有 条评论