Receiving data over serial port

Hello,

I have a Qt program that send a string of data over to the arduino, based on the data sent the arduino is to set a pin output state. Basically TestIOPCB is called in Qt app which then calls setOutput twice. If it's called twice the Arduino doesn't work properly with the code I have, but if I just send it once it works fine.

Any help will be appreciated.

/**Qt functions below/

void MainWindow::TestIOPCB()
{
    QPixmap stat_OFF (":/res/images/stat_OFF.png");
    QPixmap stat_noGO (":/res/images/stat_noGO.png");
    QPixmap stat_GO (":/res/images/stat_GO.png");
    QPixmap stat_sBy (":/res/images/stat_sBy.png");

    QByteArray testOuts;
            testOuts.resize(4);
            testOuts[0] = JA01;
            testOuts[1] = JA02;
            testOuts[2] = JA03;
            testOuts[3] = JA04;

            QStringList testLabels;
            testLabels << "Label25" << "Laberl45";
            
    char db0 = 0x53;
    char db1 = 0x50;

    setOutput(db0, 'H', db1, JA02);
    ui->label_27->setPixmap(stat_sBy);

    setOutput(db0, 'L', db1, JA02);
       ui->label_28->setPixmap(stat_sBy);

}

void MainWindow::setOutput(char db0, char state, char db1, char pin)
{
    QByteArray setOut;
    setOut.resize(11);
    setOut[0] = '

/**Arduino functions below/

void serialPacket::readSerial()
{
   while (Serial.available() > 0)
    {
		_inChar[_inCnt] = (char)Serial.read();
		_inCnt++;
		delay(10);
	}

   if ((_inChar[0] == '

;            //Start $
    setOut[1] = 0x44;          //Digital
    setOut[2] = 0x57;          //Write
    setOut[3] = '!';              //DATA START
    setOut[4] = db0;            //S
    setOut[5] = state;          //H
    setOut[6] = db1;            //P
    setOut[7] = pin;            //CR for pin 13
    setOut[8] = 0x43;          //C
    setOut[9] = 0x43;          //C
    setOut[10] = '#';          //End #

serial->write(setOut);  //Send string over to arduino

}


__<mark>/************Arduino functions below**********/</mark>__

§DISCOURSE_HOISTED_CODE_1§


) && (_inChar[10] == '#')) //New data packet received
   {
		//Send ACK Reply
		//Calculate checksum
		//Parse Data
	   Serial.println(_inChar);
	   serialPacket::parseInData();
   }
   _inCnt = 0;
}

void serialPacket::parseInData()
{

	inPacket.pCMD[0] = _inChar[1];
	inPacket.pCMD[1] = _inChar[2];
	inPacket.pRes[0] = _inChar[3];
	inPacket.pData[0] = _inChar[4];
	inPacket.pData[1] = _inChar[5];
	inPacket.pData[2] = _inChar[6];
	inPacket.pData[3] = _inChar[7];
	inPacket.pCheck[0] = _inChar[8];
	inPacket.pCheck[1] = _inChar[9];

	switch (inPacket.pCMD[0]){
	case 0x44:	//Digital
		if (inPacket.pCMD[1] == 0x57)
		{
			setOutput(inPacket.pData[0], inPacket.pData[1],
				inPacket.pData[2], inPacket.pData[3]);
		}

		if (inPacket.pCMD[1] == 0x52)
		{



		}
		break;

	}

	void serialPacket::setOutput(char db0, char state, char bd1, char pin)
	{
		if (state == 0x48)
		{
			digitalWrite(pin, HIGH);
			Serial.print("Print from 0x48");
		}
		else if (state == 0x4C)
		{
			digitalWrite(pin, LOW);
			Serial.print("Print from 0x4C");
		}
	}

;            //Start $
    setOut[1] = 0x44;          //Digital
    setOut[2] = 0x57;          //Write
    setOut[3] = '!';              //DATA START
    setOut[4] = db0;            //S
    setOut[5] = state;          //H
    setOut[6] = db1;            //P
    setOut[7] = pin;            //CR for pin 13
    setOut[8] = 0x43;          //C
    setOut[9] = 0x43;          //C
    setOut[10] = '#';          //End #

serial->write(setOut);  //Send string over to arduino

}


__<mark>/************Arduino functions below**********/</mark>__

§DISCOURSE_HOISTED_CODE_1§

The problem is in the code you have NOT posted.

Mark

... and what about explaining what the different sections of your code are intended to do - so that I don't have to waste time working it out (guessing ?) myself ?

It looks like a complicated and not very effective way to read and parse data.

...R

   while (Serial.available() > 0)

If you KNOW that you are sending 11 bytes, why not ignore the data until all 11 are ready to be read?

Then, you wouldn't have to hope that the next character arrives within 10 milliseconds while you ignore the fact that there might already be 11 bytes ready to be read.

PaulS:

   while (Serial.available() > 0)

If you KNOW that you are sending 11 bytes, why not ignore the data until all 11 are ready to be read?

Then, you wouldn't have to hope that the next character arrives within 10 milliseconds while you ignore the fact that there might already be 11 bytes ready to be read.

Thank you very much for your wise response. The below did it for.

while (Serial.available() < 11) {}