1

我正在串行端口上做一个项目..但是在我将串行通信端口更改为组合框选择之后,我似乎无法将任何数据传输出去。这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace XSP
{
    public partial class Form1 : Form
    {
        string RxString;
        byte[] buffer = Encoding.UTF8.GetBytes("RxString");

        public Form1()
        {
            InitializeComponent();

            this.Load += Form1_Load;

            serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
            Console.ReadLine();
        }

        void Form1_Load(object sender, EventArgs e)
        {
            var serialPort1 = SerialPort.GetPortNames();
            cbCommPorts.DataSource = serialPort1;
        }

        public static byte[] ConvertToBinary(string str)
        {
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            return encoding.GetBytes(str);
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {

            if (cbCommPorts.SelectedIndex > -1)
            {
                MessageBox.Show(String.Format("You selected port '{0}'", cbCommPorts.SelectedItem));
                Connect(cbCommPorts.SelectedItem.ToString());
            }
            else
            {
                MessageBox.Show("Please select a port first");
            }

        }

        private void Connect(string portName)
        {
            var serialPort1 = new SerialPort(portName);
            if (!serialPort1.IsOpen)
            {
                serialPort1.BaudRate = 115200;
                serialPort1.Open();

                btnTransmit.Enabled = true;
                btn2.Enabled = true;
                btn3.Enabled = true;
                btnOpen.Enabled = false;
                btnClose.Enabled = true;
            }
        }
        private void btnTransmit_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                string value = "12345";
                serialPort1.Write(value);
            }
            else serialPort1.Close();
        }

        private void txtReceive_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (serialPort1.IsOpen) return;
            char[] buff = new char[1];
            buff[0] = e.KeyChar;
            serialPort1.Write(buff, 0, 1);
            e.Handled = true;
        }

        private void DisplayText(object sender, EventArgs e)
        {
            txtReceive.AppendText(RxString);
        }

        private void serialPort1_DataReceived
            (object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            RxString = serialPort1.ReadExisting();
            this.Invoke(new EventHandler(DisplayText));
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                System.IO.StreamReader sr = new
             System.IO.StreamReader(openFileDialog1.FileName);
                MessageBox.Show(sr.ReadToEnd());
                sr.Close();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
            btnOpen.Enabled = true;
            btnClose.Enabled = false;
            btnTransmit.Enabled = false;
            btn2.Enabled = false;
            btn3.Enabled = false;
        }
    }
}

有人可以帮我指出我的错误在哪里吗?谢谢

4

1 回答 1

0

这里有几个可能的选项,但我们需要更多信息来解决这个问题。首先,请确保您实际上正在发送数据。为此,请尝试在上面发送数据的 if 语句上设置断点。并使用进入代码F10,看看你是否真的在发送任何数据。

如果您正在发送数据,您的代码运行良好并检查接收器应用程序。检查波特率等...

于 2012-12-10T08:37:49.333 回答