C#串口转换器通过ip读取数据

时间:2017-12-28 13:26:30

标签: c# tcp serial-port converter

我有一个用于将串口转换为IP的tibbo设备。使用像putty这样的程序,我可以成功连接设备并且设备正在运行。

我想开发一些用于收听此设备的小型C#windows表单应用程序,但我找不到任何方法。应用程序通过ip over tibbo serial从串口获取数据到IP转换器设备。我该怎么办?

1 个答案:

答案 0 :(得分:0)

安装Tibbo Device Server Toolkit并将您的tibbo设备映射到COM端口。如果您需要有关SerialPort Communication的更多帮助,请阅读本文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());
    }
  }
}