no errors PYserial but it wont work

I have been trying to send a serial data to my Arduino Uno but it wont work, I am using PYserial and there are no error messages but it dose not activate my sketch on the arduino what have I done wrong? Edit: Every time I execute ser.write('10') the rx light flashes.

Arduino code

int SerialValue = 0;
 
void setup(){
 
  pinMode(13, OUTPUT);
  Serial.begin(9600);
 
}

void loop(){
  SerialValue = Serial.read();
    if(SerialValue == 10)
    {
      digitalWrite(13, HIGH);
      delay(10000);
      digitalWrite(13, LOW);
      delay(1000);      
    }
}

Python input

foo@bar-PC ~ $ sudo python
[sudo] password for foo: 
Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
>>> ser = serial.Serial('/dev/ttyACM0', 9600)
>>> ser.write('10')
2

Any ideas why it returns 2 when I send 10?

bananasdoom:
Any ideas why it returns 2 when I send 10?

No ideas on that, unless it's telling you how many bytes it sent.

>>> ser.write('10')

I don't know about python, but I would guess that you are sending the text string '10'.

  SerialValue = Serial.read();
    if(SerialValue == 10)

But your sketch is looking for a numeric result.

So any hints on how to fix my sketch or is it really broken? As far as I can tell my sketch should be looking for a "numeric result", or am I missing something?

No ideas on that, unless it's telling you how many bytes it sent.

That's certainly easy enough to test. Send 1, 10, 100, and 1000. If you get 1, 2, 3, and 4, then the value is the number of bytes written.

As far as I can tell my sketch should be looking for a "numeric result", or am I missing something?

You sketch is. Your PC is not sending a numeric value, though. It is sending a string.

The Arduino reads one character at a time from the serial port. You need to include something in what you send that tells the Arduino that the end of a packet has arrived, so that it can read and store a complete packet, and parse that packet only when the packet is complete.

If you change you python script to send '<10>', then you can use this code to read the packet.

#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 "10". You can use atoi(inData) to convert the string to a numeric value, or you can use

if(strcmp(inData, "10") == 0)
{
   // Got "10"
}

Hi,

I would suggest not to say (in Python)

ser.write('10')

because it sends a string of two bytes but to say

ser.write(chr(10))

That is a character = one byte (the line feed character btw.)

Werner