请帮助这个串行通信程序

时间:2010-11-17 12:26:21

标签: c# serial-port

这有什么问题?我正在尝试与TReX电机控制器通信。我需要发送以下数据“DA 1F 1F”或“0xDA 0x1F 0x1F”

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

public class PortChat
{
    static SerialPort _serialPort;
    public static void Main()
    {

        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
        // Create a new SerialPort object with default settings.
        _serialPort = new SerialPort();
        _serialPort.PortName = "COM3";
        _serialPort.Open();
        _serialPort.BaudRate = 19200;
        _serialPort.DataBits = 8;
        _serialPort.Parity = Parity.None;
        _serialPort.StopBits = StopBits.One;        
        _serialPort.Write("Byte[DA 1F 1F]");
        _serialPort.Close();
    }
}

1 个答案:

答案 0 :(得分:2)

  1. 使用所需参数创建Com端口实例。
  2. 打开网络端口
  3. 向它写命令
  4. 关闭它

        static SerialPort _serialPort;
        public static void Main()
        {
            _serialPort = new SerialPort();
            _serialPort.PortName = "COM3";
            _serialPort.BaudRate = 19200;
            _serialPort.DataBits = 8;
            _serialPort.Parity = Parity.None;
            _serialPort.StopBits = StopBits.One;
    
    
    
        _serialPort.Open();
    
    
        byte[] command = new byte[] { 0xDA, 0x1F, 0x1F };
        _serialPort.Write(command, 0, command.Length);
    
    
        _serialPort.Close();
    }