串口不工作但在超级终端工作

时间:2016-10-23 17:38:18

标签: c# serial-port hyper-v

我使用C#实现应用程序串行端口并使用它发送数据。 但是不要工作。 我发送“A”字符,我的硬件无法正常工作 但我通过超级终端应用程序或SimpleTerm发送并工作。

我的代码申请:

if (_port.IsOpen)
            {
                _port.Close();
            }
            _port.PortName = cmport.Text;
            _port.Parity = (Parity)Enum.Parse(typeof(Parity), cmparity.Text);
            _port.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cmstopbit.Text);
            _port.DataBits = Convert.ToInt32(cmdatabit.Text);
            _port.BaudRate = Convert.ToInt32(cmboundrate.Text);
            _port.Handshake = Handshake.None;
            if (!_port.IsOpen)
                    {
                        _port.Open();
                    }

                    _port.Write(textBoxX1.Text.Trim());

2 个答案:

答案 0 :(得分:1)

您的串口是如何配置的?

查看此网站:https://code.msdn.microsoft.com/windowsdesktop/SerialPort-brief-Example-ac0d5004

void ** malloc_array2d(size_t m, size_t n){

  /* pointer to array of pointers */
  void ** pointer;
  size_t i;
  /* allocate pointer array of length m */
  pointer = malloc(m*sizeof(void));
  if(pointer == NULL){
    return NULL;
  }
  /* allocate storage for m*n entries */
  pointer[0] = malloc(m*n*sizeof(void));
  if (pointer[0] == NULL) {
    free(pointer);
    return NULL;
  }
  /* set the pointers */
  for (i = 1; i < m; i++) {
    pointer[i] = pointer[0] + i*n;
  }

return pointer;
}

你的textBoxX1.Text.Trim()是否包含结束字符? (_terminator):)

编辑: 将数据发送到com服务的整个项目。 http://www.codeproject.com/Articles/678025/Serial-Comms-in-Csharp-for-Beginners

我真的认为您应该将\ r \ n添加到要发送给COM的文本中,这将告诉端口com该行已完成并将读取它。

试试这个:

using System; 
using System.IO.Ports; 
using System.Text; 

namespace SerialPortExample 
{ 
    /// <summary> 
    /// Interfaces with a serial port. There should only be one instance 
    /// of this class for each serial port to be used. 
    /// </summary> 
    public class SerialPortInterface 
    { 
        private SerialPort _serialPort = new SerialPort(); 
        private int _baudRate = 9600; 
        private int _dataBits = 8; 
        private Handshake _handshake = Handshake.None; 
        private Parity _parity = Parity.None; 
        private string _portName = "COM1"; 
        private StopBits _stopBits = StopBits.One; 


    /// <summary> 
    /// Holds data received until we get a terminator. 
    /// </summary> 
    private string tString = string.Empty; 
    /// <summary> 
    /// End of transmition byte in this case EOT (ASCII 4). 
    /// </summary> 
    private byte _terminator = 0x4; 

    public int BaudRate { get { return _baudRate; } set { _baudRate = value; } } 
    public int DataBits { get { return _dataBits; } set { _dataBits = value; } } 
    public Handshake Handshake { get { return _handshake; } set { _handshake = value; } } 
    public Parity Parity { get { return _parity; } set { _parity = value; } } 
    public string PortName { get { return _portName; } set { _portName = value; } } 
    public bool Open() 
    { 
        try 
        { 
            _serialPort.BaudRate = _baudRate; 
            _serialPort.DataBits = _dataBits; 
            _serialPort.Handshake = _handshake; 
            _serialPort.Parity = _parity; 
            _serialPort.PortName = _portName; 
            _serialPort.StopBits = _stopBits; 
            _serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived); 
        } 
        catch { return false; } 
        return true; 
    } 

    void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) 
    { 
        //Initialize a buffer to hold the received data 
        byte[] buffer = new byte[_serialPort.ReadBufferSize]; 

        //There is no accurate method for checking how many bytes are read 
        //unless you check the return from the Read method 
        int bytesRead = _serialPort.Read(buffer, 0, buffer.Length); 

        //For the example assume the data we are received is ASCII data. 
        tString += Encoding.ASCII.GetString(buffer, 0, bytesRead); 
        //Check if string contains the terminator  
        if (tString.IndexOf((char)_terminator) > -1) 
        { 
            //If tString does contain terminator we cannot assume that it is the last character received 
            string workingString = tString.Substring(0, tString.IndexOf((char)_terminator)); 
            //Remove the data up to the terminator from tString 
            tString = tString.Substring(tString.IndexOf((char)_terminator)); 
            //Do something with workingString 
            Console.WriteLine(workingString); 
        } 
    } 

} 
}

答案 1 :(得分:1)

当您使用超级终端时,它将添加回车符。 你必须在你的文字之后添加\ r \ n。 示例:A \ r