readline arduino, please help

Can somebody please help me a little how to get this working I have 16 relays, and im sending from c# with Serialport1.writeline(“1”); numbers from 1-16. But numbers works just from 0 - 9. I know i can send only one byte, i was playing around with sending bytes, but i just cant get this working, i`m sure its just a little modification needed for someone that know what is doing to fix this, but i really cant figure it out. Please help.

int bufer[2];
   if(Serial.available() >= 2)
   {
     for (int i=0; i <= 2; i++)
     {
       bufer[i]= Serial.read();
     }
  
    }
    prejeto = bufer[1] + bufer[2];
    Serial.println(prejeto);
   switch (prejeto)
    {
      case '0':
      digitalWrite(realy1, HIGH);
      break;
      
      case '1':
      digitalWrite(relay1, LOW);
      break;
      
      case '3':
      digitalWrite(relay2, HIGH);
      break;
      
      case '2':
      digitalWrite(relay2, LOW);
      break;
      
      case '4':
      digitalWrite(relay3, LOW);
      break;
      
      case '5':
      digitalWrite(relay3, HIGH);
      break;
      
      case '6':
      digitalWrite(relay4, LOW);
      break;
      
      case '7':
      digitalWrite(relay4, HIGH);
      break;
      
      case '8':
      digitalWrite(relay5, LOW);
      break;
      
      case '9':
      digitalWrite(relay5, HIGH);
      break;
      
      case '10':
      digitalWrite(relay6, LOW);
      break;
      
      case '11':
      digitalWrite(relay6, HIGH);
      break;
      
      case '12':
      digitalWrite(relay7, LOW);
      break;
      
      case '13':
      digitalWrite(relay7, HIGH);
      break;
     }
}

and im sending from c# with Serialport1.writeline(“1”); numbers from 1-16.

Well, you shouldn’t. You SHOULD be sending something like “<16>”, instead. Those start and end of packet markers are VERY important.

    prejeto = bufer[1] + bufer[2];

Array indices start at 0, not 1.
That should be:

    prejeto = (bufer[0] - '0') * 10 + (bufer[1] - '0');
   if(Serial.available() >= 2)

How many characters in “1”?

      case '10':

Which ONE key did you press to get the ONE character in the single quotes?

If you send packet markers, as I showed, you can read data as fast (or slow, really) as it arrives, and take action only when the whole packet arrives.

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Where is says “Process the packet” inData will contain “1” if you send “<1>” and will contain “13” if you send “<13>”. Then, you can pass inData to atoi() to get an int back, to switch on.

use 0-9, then a-b-c-d-e-f for 10-15.

tr00g33k:
Can somebody please help me a little how to get this working I have 16 relays, and im sending from c# with Serialport1.writeline(“1”);

Here’s a generic “readline” function:

uint16_t readline (char *buffer, uint16_t limit)
{
	char c;
	uint16_t ptr = 0;

	while (1) {

		if (Serial.available()) {

			c = Serial.read();

			if (c == 0x0D) { // cr == end of line
				*(buffer + ptr) = 0; // mark end of line
				break; // return char count
			}

			if (c == 0x08) { // backspace

				if (ptr) { // if not at the beginning

					*(buffer + --ptr) = 0;
					Serial.print ( (char) 0x08); // erase backspaced char
					Serial.print ( (char) 0x20);
					Serial.print ( (char) 0x08);

				} else {
					Serial.print ( (char) 0x07); // ascii bel (beep) if terminal supports it
				}

			} else { // not a backspace, handle it

				if (ptr < (limit - 1)) { // if not at the end of the line

					Serial.print ( (char) c); // echo char to user
					*(buffer + ptr++) = c; // put char into buffer

				} else { // at end of line, can't add any more
					Serial.print ( (char) 0x07); // ascii bel (beep) if terminal supports it
				}
			}
		}
	}

	return ptr;
}

You pass the function a pointer to a buffer and the size of the buffer (the limit so you don’t enter too much). Use it like this:

char buffer [32];
uint16_t char_count;
char_count = readline (buffer, 32);

When it returns, “char_count” contains the number of characters entered and “buffer” contains the data that was input (null terminated).

Hope this helps.