Problem receiving serial bytes

I'm trying to send hex values from my computer to the arduino, and having the arduino save those vales.

This data is being sent with the code below:

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim config_values As Byte() = {&HAA, &HFF}
        _serialPort.Write(config_values, 0, 2)
        _serialPort.Close()
    End Sub

On the Arduino I am reading the serial port as such:

byte Register_Values[2];
byte Register_value_in;

void loop() {
 while (Serial.available() >= 2) {
 for (int i = 0; i < 2; i++) {
 Register_value_in = Serial.read();
 Register_Values[i] = Register_value_in;
 }
 }

 Serial.print("Reg 0");
 Serial.println(Register_Values[0]);
 Serial.print("Reg 1");
 Serial.println(Register_Values[1]);

When I add an if statement checking for the hex value the if statement will be ran, so the data is being sent correctly, but for some reason it looks like the data isn't being saved into the array.

Serial data is usually ASCII char, not byte, so '0' is the same as 48. Also, HEX values might use ABCDEF or abcdef, which don't automatically correspond to the byte value directly after '9'.

What about byte 1? With this "while (Serial.available() >= 2)" as soon as the byte count drops to 1 the loop quits, correct?

Here is how I read the serial port with a Due:

void loop()
{
for ( ;; )
  {
    if ( stream2Ptr->available() >= 1 )
    {
      Kernel.Ev_Send(iEvtID_A, EVENT_A);
      Kernel.Sm_Claim(SEM_ID_04, uMT_WAIT); // stop and wait for serial to be received and release of SEMAPHORE token
    }  //  serial available
    Kernel.Tk_Yield();
  }
} // loop



void fReadSerial()
{
  //
  Event_t  eventoutA;
  char OneChar;
  while (1)
  {
    // Event_t  eventoutA;
    Kernel.Ev_Receive(EVENT_A, uMT_ANY, &eventoutA);
    ////
    while ( stream2Ptr->available() )
    {
      OneChar = stream2Ptr->read();
      if ( (OneChar != '\n') )
      {
        //Serial.println(sPi);
        sPi.concat( OneChar );
        // Serial.println(sPi + " char" );
        // Serial.flush();
        digitalWrite( 40, LOW );
      } // if ( (OneChar != '\n') )
      else
      {
        Kernel.Ev_Send(iEvtID_B, EVENT_B);  // trigger fDO_bRcveDone
        // bRcveDone = true;
        // Serial.println( "bRcveDone set true" );
        Serial.flush();
        break;
      } // if ( (chrOne != '\n') ) else
    } // while ( Serial2.available() )
    Kernel.Sm_Release( SEM_ID_04); // release serial avaible trigger in loop
  } //  while (1)
}

Here

if ( stream2Ptr->available() >= 1 )

, I check for a full byte received before trying to read what's actually in the serial port.

When I change the code to this:

void loop() {
	while (Serial.available() >= 2) {
		for (int i = 0; i < 2; i++) {
			Register_value_in = Serial.read();
			if (Register_value_in == 0xAA){
				magAlpha.writeRegister(0, 0xFF);
			}
			if (Register_value_in == 0xFF) {
				magAlpha.writeRegister(1, 0xAA);
			}
			Register_Values[i] = Register_value_in;
		}
	}

	Serial.print("Reg 0");
	Serial.println(Register_Values[0]);
	Serial.print("Reg 1");
	Serial.println(Register_Values[1]);
	Register_0_value = magAlpha.readRegister(0);
	Register_1_value = magAlpha.readRegister(1);
	Serial.print("    Read Register[0] = 0x");
	Serial.println(Register_0_value, HEX); 
	Serial.print("    Read Register[1] = 0x");
	Serial.println(Register_1_value, HEX);
}

I'll get:

Reg 00
Reg 10
Read Register[0] = 0xFF
Read Register[1] = 0xAA

as an output in the serial window. To me that is saying that the data is being received and read correctly, but for some reason it isn't being saved into the array.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Except from what I can tell the data is being received correctly, it just isn't being saved into the array for some reason. The main thing being I'm not using start and end markers in the current design, which I wouldn't think make it not work.

Re-read reply #1

Idahowalker:
Re-read reply #1

Is not the issue. If I put all my code inside the if statement, it works just fine. For some reason the data just got rewritten once the if statement ended. Luckily I can put all my code inside the if statement, and have it work for me.

qwerty77:
Is not the issue. If I put all my code inside the if statement, it works just fine. For some reason the data just got rewritten once the if statement ended. Luckily I can put all my code inside the if statement, and have it work for me.

Post your complete code.

Try this:

byte Register_Values[2];
byte Register_value_in;

void loop() 
{
   byte n = Serial.available();//while (Serial.available() >= 2) {
   if (n == 2)
   {
      for (int i = 0; i < 2; i++) 
      {
         Register_value_in = Serial.read();
         Register_Values[i] = Register_value_in;
      }
      Serial.print("Reg 0");
      Serial.println(Register_Values[0], HEX);
      Serial.print("Reg 1");
      Serial.println(Register_Values[1], HEX);
      while(1);
   }
}