【问题标题】:C# serial port converter read data via ipC#串口转换器通过ip读取数据
【发布时间】:2017-12-28 22:08:41
【问题描述】:

我有一个用于将串行端口转换为 IP 的 tibbo 设备。使用 putty 之类的程序,我可以成功连接设备并且设备正在工作。

我想开发一个小的 C# windows 窗体应用程序来监听这个设备,但我找不到任何方法。应用程序通过 ip 通过 tibbo 串行到 IP 转换器设备从串行端口获取数据。我该怎么做?

【问题讨论】:

    标签: c# tcp serial-port converter


    【解决方案1】:

    安装Tibbo Device Server Toolkit 并将您的 tibbo 设备映射到 COM 端口。如果您在 SerialPort 通信方面需要更多帮助,请阅读本文Serial Port Communication for beginners

    示例代码:

    using System;
    using System.IO.Ports;
    using System.Windows.Forms;
    
    namespace SerialPortExample
    {
      class SerialPortProgram
      {
        // Create the serial port with basic settings
        private SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
    
        [STAThread]
        static void Main(string[] args)
        { 
          // Instatiate this class
          new SerialPortProgram();
        }
    
        private SerialPortProgram()
        {
          Console.WriteLine("Incoming Data:");
    
          // Attach a method to be called when there
          // is data waiting in the port's buffer
          port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
    
          // Begin communications
          port.Open();
    
          // Enter an application loop to keep this thread alive
          Application.Run();
        }
    
        private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
          // Show all the incoming data in the port's buffer
          Console.WriteLine(port.ReadExisting());
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-05
      • 2013-07-25
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多