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");
Thread.Sleep(100);
}
private void QSWbutton_Click(object sender, EventArgs e)
{
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);
if (qswBit == false)
{
tempStatus2 = flagCheck.SendCmd("010201000004");
Thread.Sleep(100);
flagCheck.flushBuff();
QSWbutton.Text = "On";
String result1 = new String(tempStatus2);
basicStatus.Text = result1;
}
else
{
tempStatus1 = flagCheck.SendCmd("010200000003");
Thread.Sleep(100);
flagCheck.flushBuff();
QSWbutton.Text = "Off";
String result = new String(tempStatus1);
basicStatus.Text = result;
}
}
private void Form1_Load(object sender, EventArgs e)
{
char[] status = new char[60];
PortChat ComportInit = new PortChat();
ComportInit.PortInit();
status = ComportInit.SendCmd("00080008");
Thread.Sleep(100);
ComportInit.flushBuff();
bool Qswbit = ComportInit.bitCheck(status[5], 4);
if (Qswbit == true)
{
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];
public void PortInit()
{
_serialPort = new SerialPort();
_serialPort.PortName = "COM4";
_serialPort.BaudRate = 19200;
_serialPort.Parity = Parity.None;
_serialPort.DataBits = 8;
_serialPort.StopBits = StopBits.One;
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
_serialPort.Open();
}
public char[] SendCmd(string command)
{
char[] commandChar = new Char[60];
commandChar = command.ToCharArray();
_serialPort.Write(commandChar, 0, commandChar.Length);
_serialPort.Write("\r");
num_Response = _serialPort.Read(responseChar, 0, 60);
return responseChar;
}
public bool bitCheck(char charInput, int check)
{
int char2int = 0;
int checkResult = 0;
if (((charInput) < '9') && ((charInput) > '0'))
{
char2int = (charInput - 48);
checkResult = (char2int & check);
}
else if (((charInput) > 'A') && ((charInput) < 'F'))
{
char2int = (charInput - 55);
checkResult = (char2int & check);
}
if (checkResult > 0)
return true;
else
return false;
}
public void flushBuff()
{
_serialPort.DiscardInBuffer();
}
}
}