English 中文(简体)
Serial Port Buffer on New Thread
原标题:SerialPort Buffer on New Thread

我有一个MySerial Port阶级/目标从表格GUI进入。 在创建MySerialPort标后,我要开放,并不断接收数据。 这些数据将储存和管理于物体数据缓冲。 表格GUI s memoEdit将显示从MySerialPort物体缓冲处收到的代码。

我如何使用[港口]的“新通道”。 阅读法?

using System;
using System.IO.Ports;
using System.Threading;

class MySerialPort
{
   public SerialPort CreatePort(string portName, int portSpeed, int portParity, int portDataSize, int portStopBits)
   {
      // fixed values while testing
      var port = new SerialPort("COM6", 9600, Parity.None, 8, StopBits.One);
      return port;
   }

   public void OpenPort(SerialPort port)
   {
      port.Open();
      new Thread(() => port.ReadExisting).Start();
      while (true)
      {
         // Send to buffer
         // Maybe some break condition
      }
}
最佳回答

观察员模式现在正在运行,我拿着我预计从候机港获得的数据,感谢Henk。

public partial class MyGUI : Form
{
    private MySerialPort MyPort = new MySerialPort();

    public MyGUI()
    {
        InitializeComponent();
        MyPort.OnDataBufferUpdate += DisplayNewData;
    }

    public void DisplayNewData(object sender, EventArgs e)
    {
        if (this.InvokeRequired)
        {
            BeginInvoke(new MethodInvoker(delegate() { DisplayNewData(sender, e); }));
        }
        else
        {
            memoEdit.Text = MyPort.DataBuffer;
        }
    }
}

public class MySerialPort
{
    public MySerialPort()
    {
        // Initialize serial port
        _Port.DataReceived += _Port_DataReceived;
    }

    public delegate void HandleDataBufferUpdate(object sender, EventArgs e);
    public event HandleDataBufferUpdate OnDataBufferUpdate = delegate {};

    private void _Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        // Set _Port.DataBuffer from serial port buffer, then activate event below to signal form
        OnDataBufferUpdate(this, EventArgs.Empty);
    }
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签