当我发送内容时,为什么我没有从设备(微控制器)接收数据?

时间:2016-02-17 17:33:18

标签: c# visual-studio serial-port microcontroller send

我写了一个简单的串口发送 - 接收c#程序。我能够向设备(微控制器)发送内容但是,我没有从控制器接收任何数据。该设备肯定是有效的,因为我使用2个不同的程序(Realterm& Hyperterminal)来发送东西,我收到的后端数据就好了。此外,我测试了我的代码,以确保它没有任何问题,它也运行良好。 (通过串口连接和发送/接收数据将PC连接到PC进行测试)。我尝试了不同的方法来解决这个问题,但我似乎无法找到解决方案。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Timers;
using System.IO;
using System.Net;
namespace SMCData
{ 

public partial class Form1 : Form
{

    SerialPort _serialPort = new SerialPort();

    public Form1()
    {
        InitializeComponent();
        _serialPort.DataReceived += new SerialDataReceivedEventHandler(DataRecivedHandler);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        foreach (string s in SerialPort.GetPortNames())
        {
            comport.Items.Add(s);
        }
    }

    // settup connection 
    void setport()
    {
        //checking if Baud_Rate_txt is blank
        string temp1 = Baud_Rate_txt.Text;

        if (temp1 == "")
        {
            _serialPort.BaudRate = 9600;
        }
        else
        {
            _serialPort.BaudRate = int.Parse(Baud_Rate_txt.Text);
        }


        /* if (comport.SelectedIndex == -1)
         {
            ErrorBox.Text = " Need Com Port name";
             return;
         } */
        _serialPort.PortName = comport.Text;//comport.SelectedItem.ToString();
        _serialPort.DataBits = 8;
        _serialPort.ReadTimeout = 500;
        _serialPort.WriteTimeout = 500;
        switch (HandshakeCB.Text)
        {
            case "None":
                _serialPort.Handshake = Handshake.None;
                break;
            case "XOnXOff":
                _serialPort.RtsEnable = true;
                _serialPort.DtrEnable = true;
                _serialPort.Handshake = Handshake.XOnXOff;
                break;
        }
        switch (Parity_CB.Text)
        {
            case "None":
                _serialPort.Parity = Parity.None;
                break;
            case "Even":
                _serialPort.Parity = Parity.Even;
                break;
            case "Odd":
                _serialPort.Parity = Parity.Odd;
                break;
        }
        // Looks for user input on the desired Stop Bits of Application
        switch (StopBitsCB.Text)
        {
            case "1":
                _serialPort.StopBits = StopBits.One;
                break;
            case "2":
                _serialPort.StopBits = StopBits.Two;
                break;
            case "None":
                _serialPort.StopBits = StopBits.None;
                break;
        }
        try
        {
            _serialPort.Open();


        }
        catch (UnauthorizedAccessException ex)
        {
            errorbox.Text = ex.Message + System.Environment.NewLine;
        }

    }

    private void DataRecivedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
         int temp2;
     int temp =sp.BytesToRead;
     byte[] array = new byte[temp];
     temp2 = sp.Read(array, 0, temp);
          if (this.DataRecived.InvokeRequired) 
          {
              DataRecived.BeginInvoke((Action)(() =>
                  {
                      DataRecived.Text = DataRecived.Text + Encoding.Default.GetString(array) + System.Environment.NewLine;

                  }));
          }

    }

      private void sendBLN_Click(object sender, EventArgs e)
      {

        byte[] array_out = Encoding.ASCII.GetBytes(SendTXT.Text);
        string command = "myCommand";

        _serialPort.Write(array_out, 0, array_out.Length);

        byte[] array_out2 = new byte[1];
        array_out2 = Encoding.ASCII.GetBytes("\r");

        _serialPort.Write(array_out2, 0, array_out2.Length);
        // _serialPort.Write(string.Format("{0}\r", command));

        // _serialPort.Write(array_out2, 0, array_out2.Length);
        //_serialPort.Write(new byte[] { 13, 10 }, 0, 2);

    }

    private void Refresh_btn_Click(object sender, EventArgs e)
      {
          foreach (string s in SerialPort.GetPortNames())
          {
              comport.Items.Remove(s);
          }

          foreach (string s in SerialPort.GetPortNames())
          {
              comport.Items.Add(s);
          }
      }

      private void Connectbtn_Click(object sender, EventArgs e)
      {
          try
        {
            if (!_serialPort.IsOpen)
                errorbox.Text = errorbox.Text + "Connected..." + System.Environment.NewLine;
            setport();

        }
          catch (Exception ex )
          {
            errorbox.Text = errorbox.Text + ex.Message + System.Environment.NewLine;
        }
      }

    private void button1_Click(object sender, EventArgs e)
    {

    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        _serialPort.Close();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        DataRecived.Text = " ";
        errorbox.Text = " ";

    }

    private void DataRecived_TextChanged(object sender, EventArgs e)
    {

    }

    private void button3_Click(object sender, EventArgs e)
    {

        byte[] array_out2 = new byte[1];
        array_out2 = Encoding.ASCII.GetBytes("\r");

        _serialPort.Write(array_out2, 0, array_out2.Length);

    }
}
}

0 个答案:

没有答案