如何在文本框中查看串行端口的读写数据 [英] How can I see reading and writing data for the serial port inside textbox

查看:70
本文介绍了如何在文本框中查看串行端口的读写数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hellow

在我的窗口中形成两台计算机之间的串口程序,我想看两台计算机发送和接收的数据。我已经使用textBox1发送数据和textBox2进行读取,不幸的是读取方法无法接受文本框。

我该怎么做

谢谢



我尝试过的事情:



Hellow
in my window form Serialport program between 2 computer,i want to watch the data sent and received in both computer. i have used textBox1 for send data and textBox2 for the readding , unfortunatley the reading method can not accept textbox .
how can i do that
thanks

What I have tried:

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.Threading;
using System.Threading.Tasks;
using System.IO.Ports;

//https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.readline(v=vs.110).aspx

namespace SerialWinForm
{
    public partial class Form1 : Form
    {
        static SerialPort _serialPort;
        static bool _continue;
        static string globalRead="";
        public Form1()
        {
            InitializeComponent();
        }
        //------------------------------------------------------------------------------------
        private void FormLoad(object sender, EventArgs e)
        {
            //string name;
            //string message;
            //StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
            //Thread readThread = new Thread(Read);
            //// Create a new SerialPort object with default settings.
            _serialPort = new SerialPort();
            // Allow the user to set the appropriate properties.
            _serialPort.PortName = "com1";//SetPortName(_serialPort.PortName);
            _serialPort.BaudRate = 2400;//SetPortBaudRate(_serialPort.BaudRate);
            _serialPort.Parity = Parity.None;//SetPortParity(_serialPort.Parity);
            _serialPort.DataBits = 8;//SetPortDataBits(_serialPort.DataBits);
            _serialPort.StopBits = StopBits.One;//SetPortStopBits(_serialPort.StopBits);
            _serialPort.Handshake = Handshake.None;//SetPortHandshake(_serialPort.Handshake);
            _serialPort.ReadTimeout = 500;
            _serialPort.WriteTimeout = 500;
            _serialPort.Open();
        }////FormLoad
        //----------------------------------
        private void SerialThreadStartClick(object sender, EventArgs e)
        {
            string name;
            string message;
            StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
            Thread readThread = new Thread(Read);
            _continue = true;
            readThread.Start();
            //Console.Write("Name: ");      //name = Console.ReadLine();
            name = textBox0.Text;
            //Console.WriteLine("Type QUIT to exit");
            while (_continue)
            {
                message = textBox1.Text;
                textBox2.Text = globalRead;
                if (stringComparer.Equals("quit", message))
                {
                    _continue = false;
                }
                else
                {
                //    _serialPort.WriteLine(String.Format("<{0}>: {1}", name, message));
                       textBox1.Text = message;
                      _serialPort.WriteLine(message);
                }
            }//while
            readThread.Join();
            _serialPort.Close();
        }
        //-----------------------------------------------------------------------------------
        public static void Read()
        {
            while (_continue)
            {
                try
                {
                    //string message = _serialPort.ReadLine();// string
                    string message = _serialPort.ReadByte().ToString();
                    textBox2.Text = message;//   <-------- Error
                    globalRead = message;
                }//try
                catch (TimeoutException) { }
            }//while
        }//Read
        //----------------------------------
    }

}

推荐答案

除了线程之外你不能访问任何控件是在UI线程上创建的。如果你尝试,你将得到一个跨线程异常。

要访问该控件,你需要调用它: MSDN:Control.Invoke [ ^ ]
You can't access any control except from the thread it was created on - the UI thread. If you try, you will get a Cross Thread Exception.
To access the control, you need to Invoke it: MSDN: Control.Invoke[^]


这篇关于如何在文本框中查看串行端口的读写数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆