baidu_35738340 2016-09-19 08:13 采纳率: 0%
浏览 1170

在使用C#开发传感器数据登录软件,这是代码的一部分,运行后没有读取了数据,大家帮我看一下哪里不对。

private void HandleAinuoBSerialData()
{
string cmd = "";
//double V = 0.0;
//double A = 0.0;
//double W = 0.0;
//double WH = 0.0;
byte[] bytesCmd = new byte[8] { 0x7B, 0x00, 0x08, 0x01, 0xF0, 0x00, 0xF9, 0x7D };//request power consumption

        while (sComm4.IsOpen && EnableReceive)
        {
            try
            {
                sComm4.DiscardInBuffer();
                sComm4.DiscardOutBuffer();
                sComm4.Write(bytesCmd, 0, 8);
                byte[] bytesReceive = new byte[12];
                byte CalChecksum = 0;
                for (int i = 0; i < bytesReceive.Length; i++)
                {
                    bytesReceive[i] = (byte)sComm4.ReadByte();
                    CalChecksum ^= (byte)bytesReceive[i];
                    cmd += bytesReceive[i].ToString();
                }
                //string StrBuffer = sComm3.ReadLine();
                // string[] strData = StrBuffer.Split(',');
                //   byte[] ReceiveChecksumBytes = new byte[] { (byte)bytesReceive[bytesReceive.Length - 2], (byte)bytesReceive[bytesReceive.Length - 1] };
                //   string ChecksumStr = Encoding.ASCII.GetString(ReceiveChecksumBytes);
                //   byte ReceiveChecksum = byte.Parse(ChecksumStr, System.Globalization.NumberStyles.HexNumber);
                //   if (ReceiveChecksum == CalChecksum)
                //   {
                //       Log.SetNewData_AinuoB(ref strData);
                //       NumReceived4++;
                //       IsNeedUpdateUI = true;

                //   }

            }

            catch (Exception)
            {
                //MessageBox.Show(e.Message);
            }
            Thread.Sleep(1000);
        }
    }

             private void ToolStripButton_OpenPort_Click(object sender, EventArgs e)
   {
       string statusPort4 = "已关闭";
        if (ToolStripButton_OpenPort.Text == "打开端口")
        {
            try
            {

                bool operatePort4 = false;
            if (cmPort4.Text.StartsWith("COM"))
                {
                    operatePort4 = true;
                    sComm4 = new SerialPort();
                    sComm4.PortName = cmPort4.Text;
                }
                else
                {
                    sComm4 = null;
                }
                if (operatePort4)
                { }
                else
                {
                    MessageBox.Show("端口号无效");
                    return;
                }
            PortUtil.OpenPort(sComm4);
             if (operatePort4 && sComm4.IsOpen)
                {
                    NumReceived4 = 0;
                    updateTimer.Enabled = true;
                    EnableReceive = true;

                    if (serialThread_AinuoB != null)
                    {
                        serialThread_AinuoB.Abort();
                    }
                    serialThread_AinuoB = new Thread(HandleAinuoBSerialData);
                    serialThread_AinuoB.Start();
                    statusPort4 = "已打开 " + Convert.ToString(sComm4.PortName) + ' ' + Convert.ToString(sComm4.BaudRate) + ' ' + Convert.ToString(sComm4.DataBits) + ' ' + Convert.ToString(sComm4.Parity) + ' ' + Convert.ToString(sComm4.StopBits);
                }
  • 写回答

1条回答 默认 最新

  • threenewbee 2016-09-19 15:05
    关注

    用串口调试助手一类的软件调试下。

    评论

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误