0

我有欧米茄温度/湿度设备。我需要带串口的数据记录项目。

首先尝试,Excel Vba。

Private Sub CommandButton1_Click()
MSComm21.CommPort = 4
MSComm21.Settings = "9600,N,8,1"
On Local Error GoTo hata
MSComm21.PortOpen = True
Exit Sub
hata:
MsgBox "Port Acılamıyor"
End Sub

Private Sub CommandButton2_Click()
MSComm21.Output = "AT" + vbCr
ifade = MSComm21.Input
MsgBox ifade
End Sub

我获得数据成功,但数据不规则。

[在此处输入图片描述][1]

然后我在 c# 中尝试。我是我不知道 c# 。我是通过研究做到的。但结果与 VBA 相同

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

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

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            cboPort.Items.AddRange(ports);
            cboPort.SelectedIndex = 0;
            btnClose.Enabled=false;
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            btnOpen.Enabled = false;
            btnClose.Enabled = true;
            try
            {
                serialPort1.PortName = cboPort.Text;
                serialPort1.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
 
            try
            {
                if (serialPort1.IsOpen)
                {

                    do
                    {
                        
                        serialPort1.WriteLine("AT" + Environment.NewLine);
                        txtReceive.Text = serialPort1.ReadExisting();


                    } while (condition);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            btnOpen.Enabled = true;
            btnClose.Enabled = false;
            try
            {
                serialPort1.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnReceive_Click(object sender, EventArgs e)
        {
            try
            {
                if (serialPort1.IsOpen)
                {
               

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (serialPort1.IsOpen)
                serialPort1.Close();
        }
    }
}

© 尝试超级终端。数据正常且成功。

© 添加了手动 pdf 图像我的设备 [在此处输入图像描述][2]

请帮助我了解常规数据 [1]:https ://i.stack.imgur.com/v8hwv.png [2]:https ://i.stack.imgur.com/hd1EG.png

4

0 回答 0