调试问题 [英] Debugging problem

查看:126
本文介绍了调试问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,伙计们

新手在这里.我有关于调试的问题.当我逐步调试时,我的代码运行良好,但在编译时却显示了不同的结果.任何人都可以告诉我造成这种问题的原因是什么.非常感谢!

这是一个表格应用程序,它允许我与具有串行端口的微控制器进行通信.
我的想法是先启动串行端口,设置端口号,波特率,奇偶校验,停止位.然后通过发送命令检查电气单元的初始状态(ON或OFF),然后检查微控制器的响应.知道初始状态后,我可以单击按钮,该按钮还会发送命令以打开或关闭电气单元.

例如,我将电气单元设置为ON,因此,我想在运行程序时看到"ON"状态.当我逐步调试代码时,我从控制器获得了正确的响应,并且电气单元的状态为"ON",这是正确的.但是,如果我只是编译代码或运行* .exe文件,结果将有所不同,它显示为"OFF".我怀疑通讯部分可能存在问题,但我找不到它.

让我知道是否有任何不清楚的地方,我会继续对其进行修改.
这是很长的阅读,所以谢谢您的时间!

这是代码.

Hello, guys

Newbie here. I have question about debugging. My code runs well when I debugging step by step, but show me the different result when I compile it. Anyone could tell me what could be the reason cause this kind of problem. Thanks a lot!

This is a form application that allow me to communicate with a micro controller with serial port.
My thought is to initiate the serial port first, set port number, baud rate, parity, stop bit. Then checking the initial status of an electrical unit, ON or OFF, by sending the command and then check the response from micro controller. After I know the initial status, I can click button, which also sending the command to turn ON or OFF the electrical unit.

For example, I''ve set the electrical unit ON, so, I suppose to see the "ON" status when I run the program. When I debug the code step by step, I got the right response from the controller and the status of the electrical unit is "ON", which is right. But if I just compile the code or run the *.exe file, the result is different, it show "OFF". I suspect there might be some problem with communication part, but I can''t find it.

Let me know if there is anything not clear, I will keep modify it.
It''s quite a long reading, so thank you for you time!

Here is the code.

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 SerialComm;
using System.Threading;


namespace BasicGUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {

            InitializeComponent();
        
        }

        private void SHUTbutton_Click(object sender, EventArgs e)
        {
            PortChat sendcommand = new PortChat();
            sendcommand.SendCmd("010101000003"); // Open Shutter
            Thread.Sleep(100);
        }

        private void QSWbutton_Click(object sender, EventArgs e)
        {
            //check Qsw status, if Qsw is on, Click turn it OFF, otherwise turn it on
            char[]  tempStatus = new char[60];
            char[] tempStatus1 = new char[60];
            char[] tempStatus2 = new char[60];
            PortChat flagCheck = new PortChat();
            
            tempStatus = flagCheck.SendCmd("00080008");
            
            Thread.Sleep(100);
            flagCheck.flushBuff();
            bool qswBit = flagCheck.bitCheck(tempStatus[5],4);
            //byte qswFlag = (byte)(tempStatus[5] - '0');
            
            //int status = (qswFlag & 0x04);
            if (qswBit == false) // Qsw is OFF
            {
                //PortChat sendcommand1 = new PortChat();
                tempStatus2 = flagCheck.SendCmd("010201000004"); // Turn ON Qswitch 

                Thread.Sleep(100);
                flagCheck.flushBuff();
                QSWbutton.Text = "On";
                String result1 = new String(tempStatus2);

                basicStatus.Text = result1;
               
            }
            else
            {
                //PortChat sendcommand = new PortChat();
                tempStatus1 = flagCheck.SendCmd("010200000003"); // Turn OFF Qswitch 

                Thread.Sleep(100);
                flagCheck.flushBuff();
                QSWbutton.Text = "Off";
                String result = new String(tempStatus1);
                basicStatus.Text = result;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Initialize the COMport
            char[] status = new char[60];

            PortChat ComportInit = new PortChat();
            ComportInit.PortInit();


            //  status check, check the Qsw and Shutter status

            //PortChat CheckStatus = new PortChat();
            status = ComportInit.SendCmd("00080008"); //

            Thread.Sleep(100);
            ComportInit.flushBuff();
            //check Q switch bit status

            bool Qswbit = ComportInit.bitCheck(status[5], 4);
            //byte tempStatus = (byte)(status[5] - '0');
            //int tempStatus = (int)('a' - '0');
            //Byte basicStatus = (byte)(tempStatus);

            //char test = 'f';
            //byte test1 = (byte)(test);
            //int QswStatus = (tempStatus & 0x04); //QswStatus

            if (Qswbit == true) // check Qsw status 0100
            {
                QSWbutton.Text = "ON";

            }
            else
            {
                QSWbutton.Text = "OFF";
            }

            String Basicstatus = new String(status);
            basicStatus.Text = Basicstatus;
        }

            }


}




这是交流部分




Here is the communication part

using System;
using System.IO.Ports;
using System.Threading;
namespace SerialComm
{

    public class PortChat
    {
        static SerialPort _serialPort;
        private int num_Response;
        private char[] responseChar = new Char[60];
        // setup baudrate, parity, stopbit
        public void PortInit()
        {
            // Create a new SerialPort object with default settings.
            _serialPort = new SerialPort();
            // Allow the user to set the appropriate properties.
            _serialPort.PortName = "COM4";
            // Console.WriteLine("Serial Port set to COM4");
            _serialPort.BaudRate = 19200;
            // Console.WriteLine("BaudRate set to 19200");
            //_serialPort.Parity = 0;
            _serialPort.Parity = Parity.None;
            //Console.WriteLine("None Parity");
            _serialPort.DataBits = 8;
            // Console.WriteLine("8 data bits");
            //_serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits),"One");
            _serialPort.StopBits = StopBits.One;
            // Console.WriteLine("One stop bit");

            // Set the read/write timeouts
            _serialPort.ReadTimeout = 500;
            _serialPort.WriteTimeout = 500;
            _serialPort.Open();
        }

        //send command and return the response form controller
        public char[] SendCmd(string command)
        {
            //string command;
            char[] commandChar = new Char[60];
            // StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
            // Thread readThread = new Thread(Read);

            //_continue = true;
            // readThread.Start();
            commandChar = command.ToCharArray();
            //commandChar[i] = Console.Read;
            // }
            // System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

            _serialPort.Write(commandChar, 0, commandChar.Length);
            _serialPort.Write("\r"); //send the cartridge return character after the command

            num_Response = _serialPort.Read(responseChar, 0, 60);
           // _serialPort.DiscardInBuffer();
            return responseChar;

            // Console.WriteLine("Length of the response {0}", num_Response);
            //Write the response
            //int i = 0;
            //for (i = 0; i < num_Response; i++) ;
            //     Console.Write(feedbackChar[i]);
            //  Console.WriteLine();
            // }

        }
        // check the bit is 1 or 0
        public bool bitCheck(char charInput, int check)
        {
            int char2int = 0;
            int checkResult = 0;
            if (((charInput) < '9') && ((charInput) > '0')) //charIn is number 0 ~ 9
            {
                char2int = (charInput - 48);
                //Console.WriteLine(char2int);
                checkResult = (char2int & check);
                //Console.WriteLine(checkResult);
            }
            else if (((charInput) > 'A') && ((charInput) < 'F')) //charIn is letter A ~ F
            {
                char2int = (charInput - 55);
                //Console.WriteLine(char2int);
                checkResult = (char2int & check);
                //Console.WriteLine(checkResult);
            }
            if (checkResult > 0)
                return true;  // this bit is 1
            else
                return false; // this bit is 0
        }

        public void flushBuff()
        {
            _serialPort.DiscardInBuffer();
        }

        //public void GetResponse()
        //{
        //    char[] responseChar = new Char[60];
        //    int num_Response;
        //    int i = 0;
        //    num_Response = _serialPort.Read(responseChar, 0, 60); //read response from buffer
        //    for (i = 0; i < num_Response; i++)
        //    {
        //    }

        //}
        //  readThread.Join();
        // _serialPort.Close();
    }
}
//public static void Read()
//{
//    while (_continue)
//    {
//        try
//        {
//            string message = _serialPort.ReadLine();
//            Console.WriteLine(message);
//        }
//        catch (TimeoutException) { }
//    }
//}
//}

推荐答案

我的猜测是,因为您正在调试,所以当您逐步执行时,您为串行端口另一端的单元提供了足够的时间来完成其任务,但是在编译过程中,它没有有足够的时间.

例如,您写入串行端口并告诉它打开.然后,立即检查其状态. (或者至少是它的样子.

但是,在这两者之间,只有一个cpu周期,而在设备上,则可能需要更多时间.

您可能要尝试的一件事是在
之后立即打个睡眠电话
My guess would be that because you were debugging, when you stepped through, you provided enough time for the unit at the other end of the serial port to complete it''s tasks, but during compile, it doesn''t have enough time.

For instance, you write to the serial port and tell it to turn on. Then, right away, you check it''s status. (or at least that''s what it looks like.

But, in between the two, is only a single cpu cycle, while on the unit, it might take more time.

One thing you might try is to put a sleep call right after

_serialPort.Write("\r"); //send the cartridge return character after the command



在您的SendCmd例程中.

从更高的位置开始,然后慢慢地减少时间,直到您始终为单元提供足够的时间来响应为止.



in your SendCmd routine.

Start higher, and then slowly cut the time until you are consistently providing enough time for the unit to respond.


不看代码很难给您答案.
尝试通过构建一次来清洁您的解决方案->干净的解决方案.
It is difficult to give you an answer without looking at your code.
Trying cleaning your solution once by going to build -> clean solution.


这篇关于调试问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆