从一行c#中的串口接收数据

时间:2014-03-11 06:50:02

标签: c#

我的代码从称重机的串行com端口获取数据。它工作正常,但接收数行的数据。我试图让它在一行中接收数据。

在下面的代码中,我需要使用Visual Studio 2005通过计时器自动从称重桥机器获取重量。它可以工作,但我在几行上接收重量值。

我需要在一行中接收重量并自动更新并将其转换为整数。

using System;
using System.IO.Ports;
using System.Windows.Forms;

namespace CommSample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        void Application_Idle(object sender, EventArgs e)
        {
            label3.Text = serialPort1.IsOpen ? "[Open]" : "[Closed]";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (pollingCheckbox.Checked)
            {
                timer1.Enabled = true;
            }   
            else
            {
                timer1.Enabled = false;
                TransmitCommand();
            }
        }

        private void TransmitCommand()
        {
            if (textBox1.Text.Length > 0)
            {
                if (serialPort1.IsOpen)
                {
                    serialPort1.Write(textBox1.Text + "\r");
                }
            }
        }

        private void ClosePort()
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.DataReceived -= new SerialDataReceivedEventHandler(serialPort1_DataReceived);
                serialPort1.Close();
            }
        }

        private void closePortToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClosePort();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            serialPort1.PortName = Properties.Settings.Default.Port;
            serialPort1.BaudRate = Properties.Settings.Default.Baud;
            serialPort1.DataBits = Properties.Settings.Default.DataBits;
            serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), Properties.Settings.Default.Parity);
            serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), Properties.Settings.Default.StopBits);
            Application.Idle += new EventHandler(Application_Idle);
        }

        private void OpenPort()
        {
            serialPort1.Open();
            serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
        }

        private void openPortToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenPort();
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClosePort();
            using (Form2 form = new Form2())
            {
                if (form.ShowDialog(this) == DialogResult.OK)
                {
                    serialPort1.PortName = Properties.Settings.Default.Port;
                    serialPort1.BaudRate = Properties.Settings.Default.Baud;
                    serialPort1.DataBits = Properties.Settings.Default.DataBits;
                    serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), Properties.Settings.Default.Parity);
                    serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), Properties.Settings.Default.StopBits);
                }
            }
        }

        void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (!InvokeRequired)
            {
                if (e.EventType == SerialData.Chars)
                {
                    string portData = serialPort1.ReadExisting();
                    textBox2.AppendText(portData);
                }
            }
            else
            {
                SerialDataReceivedEventHandler invoker = new SerialDataReceivedEventHandler(serialPort1_DataReceived);
                BeginInvoke(invoker, new object[] { sender, e });
            }
        }

        private void pollingCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            if (timer1.Enabled && !pollingCheckbox.Checked)
            {
                timer1.Enabled = false;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (pollingCheckbox.Checked)
            {
                TransmitCommand();
            }
        }
    }
}

0 个答案:

没有答案