• 大小: 78KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-18
  • 语言: C#
  • 标签:

资源简介

C#串口通信实例(RS232SerialPort)

资源截图

代码片段和文件信息

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

namespace SCKJSerialPort
{
    public partial class CODDemo : Form
    {
        private static COD cod = new COD();
       
        public CODDemo()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender EventArgs e)
        {
            cod.Open();
        }

        private void btnSet_Click(object sender EventArgs e)
        {
            string senddata = this.txtContent.Text;
            byte[] para = System.Text.Encoding.Default.GetBytes(senddata);
            int functioncode=int.Parse(this.txtCommand.Text);
            cod.Address = this.txtA1.Text + “.“ + this.txtA2.Text + “.“ + this.txtA3.Text + “.“ + this.txtA4.Text;
            lbllrc.Text = Protocol.CreateLrc(cod.Address functioncode ref para).ToString();
            cod.SetCommand(functioncode ref para);

            Thread.Sleep(1000);//此处故意设置一段系统等候时间,以让其备运转处理

            //----------------------------------------------------------------------//


            byte[] temp = null;
            cod.ReceiveData(ref temp);
            string returnreceivedata = ““;
            string returnaddress = ““;
            int returnfunctioncode = 0;
            int returnlrc = 0;
            if (temp != null)
            {

                returnaddress = Protocol.GetAddress(temp);
                returnreceivedata = System.Text.Encoding.Default.GetString(Protocol.GetData(temp));
                returnfunctioncode = Protocol.GetFunctionCode(temp);
                returnlrc = Protocol.GetLrc(temp);

                this.txt3.Text += “地址:“ + returnaddress.ToString() + “\r\n“;
                this.txt3.Text += “功能号:“ + returnfunctioncode.ToString() + “\r\n“;
                this.txt3.Text += “发送数据:“ + returnreceivedata + “\r\n“;
                this.txt3.Text += “lrc数据校验:“ + returnlrc.ToString() + “\r\n“;
            }
        }

        private void GetData()
        {
            byte[] temp = null;
            cod.ReceiveData(ref temp);
            string receivedata = ““;
            if (temp != null)
            {
                receivedata = System.Text.Encoding.Default.GetString(Protocol.GetData(temp));
                this.txt3.Text = receivedata;
            }    
        }

        private void btnSend_Click(object sender EventArgs e)
        {
            string senddata = this.txt1.Text;
            cod.SendData(senddata);  
        }

        private void btnReceive_Click(object sender EventArgs e)
        {
            GetData(); 
        }

    }
}

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

     目录          0  2008-04-07 14:06  RS232SerialPort

     目录          0  2008-04-09 08:27  RS232SerialPort\ConsoleApplication1

     目录          0  2008-04-07 14:05  RS232SerialPort\ConsoleApplication1\bin

     目录          0  2008-04-09 02:06  RS232SerialPort\ConsoleApplication1\bin\Debug

     文件      40960  2008-04-09 08:27  RS232SerialPort\ConsoleApplication1\bin\Debug\SCKJSerialPort.exe

     文件      67072  2008-04-09 08:27  RS232SerialPort\ConsoleApplication1\bin\Debug\SCKJSerialPort.pdb

     文件       5632  2005-12-08 14:51  RS232SerialPort\ConsoleApplication1\bin\Debug\SCKJSerialPort.vshost.exe

     目录          0  2008-04-09 03:10  RS232SerialPort\ConsoleApplication1\Class

     文件       6394  2008-04-09 03:10  RS232SerialPort\ConsoleApplication1\Class\basePort.cs

     文件       2214  2008-04-09 03:01  RS232SerialPort\ConsoleApplication1\Class\InheritPort.cs

     文件       1042  2008-04-09 02:06  RS232SerialPort\ConsoleApplication1\Class\Interface.cs

     文件        494  2008-04-09 02:19  RS232SerialPort\ConsoleApplication1\Class\Program.cs

     文件      10717  2008-04-09 02:53  RS232SerialPort\ConsoleApplication1\Class\Protocol.cs

     文件       2886  2008-04-09 08:27  RS232SerialPort\ConsoleApplication1\CODDemo.cs

     文件       9619  2008-04-09 03:03  RS232SerialPort\ConsoleApplication1\CODDemo.Designer.cs

     文件       5814  2008-04-09 03:03  RS232SerialPort\ConsoleApplication1\CODDemo.resx

     文件       3026  2008-04-09 02:19  RS232SerialPort\ConsoleApplication1\ConsoleApplication1.csproj

     文件        168  2008-04-09 02:12  RS232SerialPort\ConsoleApplication1\ConsoleApplication1.csproj.user

     文件      14644  2008-04-09 02:15  RS232SerialPort\ConsoleApplication1\Debugger.cs

     文件      20628  2008-04-09 02:15  RS232SerialPort\ConsoleApplication1\Debugger.Designer.cs

     文件       6011  2008-03-22 16:11  RS232SerialPort\ConsoleApplication1\Debugger.resx

     目录          0  2008-04-01 15:59  RS232SerialPort\ConsoleApplication1\obj

     文件        312  2008-04-09 08:47  RS232SerialPort\ConsoleApplication1\obj\ConsoleApplication1.csproj.FileList.txt

     目录          0  2008-04-09 08:27  RS232SerialPort\ConsoleApplication1\obj\Debug

     文件       1116  2008-04-09 03:03  RS232SerialPort\ConsoleApplication1\obj\Debug\ConsoleApplication1.csproj.GenerateResource.Cache

     目录          0  2008-04-02 09:51  RS232SerialPort\ConsoleApplication1\obj\Debug\Refactor

     文件       3836  2008-04-06 16:36  RS232SerialPort\ConsoleApplication1\obj\Debug\ResolveAssemblyReference.cache

     文件        180  2008-04-09 03:03  RS232SerialPort\ConsoleApplication1\obj\Debug\SCKJSerialPort.CODDemo.resources

     文件        180  2008-04-09 02:15  RS232SerialPort\ConsoleApplication1\obj\Debug\SCKJSerialPort.Debugger.resources

     文件      40960  2008-04-09 08:27  RS232SerialPort\ConsoleApplication1\obj\Debug\SCKJSerialPort.exe

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

评论

共有 条评论

相关资源