• 大小: 61KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-17
  • 语言: C#
  • 标签: Rs232  串口  传输  

资源简介

Rs232 串口 调试信息传输 (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;


namespace RS232
{
    public partial class fclsRS232Tester : Form
    {
        string InputData = String.Empty;
        
        // This delegate enables asynchronous calls for setting
        // the text property on a TextBox control:
        delegate void SetTextCallback(string text);
 
        public fclsRS232Tester()
        {
            InitializeComponent();

            // Nice methods to browse all available ports:
            string[] ports = SerialPort.GetPortNames();

            // Add all port names to the combo box:
            foreach (string port in ports)
            {
                cmbComSelect.Items.Add(port);
            }
            comboBox1.SelectedIndex = 1;
        }

        private void cmbComSelect_SelectionchangeCommitted(object sender EventArgs e)
        { 
            
            if (port.IsOpen) port.Close();
            port.PortName = cmbComSelect.SelectedItem.ToString();  
            port.BaudRate = int.Parse(comboBox1.SelectedItem.ToString());

             

            port.Parity = Parity.None;
            port.DataBits = 8;
            port.StopBits = StopBits.One;

            // try to open the selected port:
            try
            {
                port.Open();
            }
            // give a message if the port is not available:
            catch
            {
                MessageBox.Show(“Serial port “ + port.PortName + “ cannot be opened!“ “RS232 tester“ MessageBoxButtons.OK MessageBoxIcon.Warning);
                cmbComSelect.SelectedText = ““;
                stsStatus.Text = “Select serial port!“;
            }
        }

        private void btnSend_Click(object sender EventArgs e)
        {         
            if (port.IsOpen) port.Write(txtOut.Text);  
            else MessageBox.Show(“Serial port is closed!“ “RS232 tester“ MessageBoxButtons.OK MessageBoxIcon.Error);
            txtOut.Clear();
        }

        private void btnClear_Click(object sender EventArgs e)
        {
            txtIn.Clear();
        }

        private void port_DataReceived_1(object sender SerialDataReceivedEventArgs e)
        { 
            InputData = port.ReadExisting();

            MessageBox.Show(InputData);

            if (InputData != String.Empty)
            {
 //             txtIn.Text = InputData;   // because of different threads this does not work properly !!
                SetText(InputData);
            }
        } 

        private void SetText(string text)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different it returns true.
            if (this.

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

     文件      12800  2009-02-24 13:44  RS232\bin\Debug\RS232.exe

     文件      26112  2009-02-24 13:44  RS232\bin\Debug\RS232.pdb

     文件      11771  2008-07-17 17:08  RS232\bin\Debug\rs232.rar

     文件      14328  2009-03-20 15:55  RS232\bin\Debug\RS232.vshost.exe

     文件      24576  2007-01-22 14:33  RS232\bin\Release\RS232.exe

     文件       5632  2005-09-23 06:56  RS232\bin\Release\RS232.vshost.exe

     文件       1154  2007-11-07 21:50  RS232\ClassDiagram1.cd

     文件       3550  2009-02-24 13:44  RS232\Form1.cs

     文件       9969  2008-07-17 16:56  RS232\Form1.Designer.cs

     文件       6207  2008-07-17 16:56  RS232\Form1.resx

     文件        847  2009-02-24 13:44  RS232\obj\Debug\RS232.csproj.GenerateResource.Cache

     文件      12800  2009-02-24 13:44  RS232\obj\Debug\RS232.exe

     文件        180  2008-11-27 12:08  RS232\obj\Debug\RS232.fclsRS232Tester.resources

     文件      26112  2009-02-24 13:44  RS232\obj\Debug\RS232.pdb

     文件        180  2008-11-27 12:08  RS232\obj\Debug\RS232.Properties.Resources.resources

     文件        264  2008-04-04 23:25  RS232\obj\RS232.csproj.FileList.txt

     文件       1504  2008-07-17 17:08  RS232\obj\RS232.csproj.FileListAbsolute.txt

     文件        482  2007-01-09 11:09  RS232\Program.cs

     文件       1288  2007-11-08 14:05  RS232\Properties\AssemblyInfo.cs

     文件       2834  2008-11-27 12:07  RS232\Properties\Resources.Designer.cs

     文件       5612  2007-01-09 11:08  RS232\Properties\Resources.resx

     文件       1103  2008-11-27 12:07  RS232\Properties\Settings.Designer.cs

     文件        249  2007-01-09 11:08  RS232\Properties\Settings.settings

     文件       3466  2008-11-27 12:07  RS232\RS232.csproj

     文件        168  2008-11-27 12:07  RS232\RS232.csproj.user

     目录          0  2008-04-17 16:28  RS232\obj\Debug\Refactor

     目录          0  2008-11-27 12:08  RS232\obj\Debug\TempPE

     目录          0  2008-04-17 16:28  RS232\obj\Release\TempPE

     目录          0  2008-11-27 12:07  RS232\bin\Debug

     目录          0  2008-04-17 16:28  RS232\bin\Release

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

评论

共有 条评论