开发者

Please help with this serial communication program

开发者 https://www.devze.com 2023-01-25 06:51 出处:网络
What is wrong with this? I am trying to communicate to a TReX motor controller.I need to send the following data \"DA 1F 1F\" or \"0xDA 0x1F 0x1F\"

What is wrong with this? I am trying to communicate to a TReX motor controller. I need to send the following data "DA 1F 1F" or "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.Pari开发者_C百科ty = Parity.None;
        _serialPort.StopBits = StopBits.One;        
        _serialPort.Write("Byte[DA 1F 1F]");
        _serialPort.Close();
    }
}


  1. Create Com port instance with needed parameters.
  2. Open Com port
  3. Write command to it
  4. Close it

        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();
    }
    
0

精彩评论

暂无评论...
验证码 换一张
取 消