Arduino Freezing During Serial Communication [Laser Spirograph Project]

Hey guys, I'm working on a laser spirograph that uses two small low voltage dc motors. I'm driving these two motors with transistors and using PWM to control the speed. My problem is that when I try to send the desired speeds to my arduino through serial it freezes everything after I send it a few commands, including my computer (MacBook Pro OSX 10.8.3). The freezing happens whether I use the arduino serial monitor or the serial python library. I'm also working on a GUI for my mac that will allow me to control the motor speeds using popen("echo a100 > /dev/tty.usbserial-A6006kDo", "r"); and that will always lock up everything right away. The way I'm sending the speed data is "a100" ( analogWrite(pinA, 100); ).
Here is my code:

int firstByte;

int pinA = 10;
int pinB = 9;

int dataValueA = 0;
int dataValueB = 0;

void setup()
{
  Serial.begin(9600);
}

void readIn()
{
  if (Serial.available() > 0)
  {
    firstByte = Serial.read();
    
    if (firstByte == 'a' && Serial.available() == 3)
    {
      dataValueA = Serial.parseInt();
    }
    
    if (firstByte == 'b' && Serial.available() ==3)
    {
       dataValueB = Serial.parseInt();
    }
    
    if (firstByte == 'o' && Serial.available() == 0)
    {
      dataValueA = 0;
      dataValueB = 0;
    }
  }
}

void loop()
{
  readIn();
  analogWrite(pinA, dataValueA);
  analogWrite(pinB, dataValueB);
  delay(20); //seems to help with the freezing 
}

How can I prevent my arduino and laptop from locking up?

Determine if its a Hardware or Software problem: Disconnect everything from the Arduino. Does it still lock up?

delay(20); //seems to help with the freezing

By locking everything up for 1/50th of a second?

if (Serial.available() > 0)
  {
    firstByte = Serial.read();
    
    if (firstByte == 'a' && Serial.available() == 3)

Remember, serial is slow, so first time you read an 'a', it is very unlikely there is even a single other character ready to be read, let alone three.

Also note that most Arduinos will reset when the serial port is opened.

AWOL:

delay(20); //seems to help with the freezing

By locking everything up for 1/50th of a second?

if (Serial.available() > 0)

{
    firstByte = Serial.read();
   
    if (firstByte == ‘a’ && Serial.available() == 3)



Remember, serial is slow, so first time you read an 'a', it is very unlikely there is even a single other character ready to be read, let alone three.

So should my if statement be if (Serial.available() > 4) to ensure that all bytes are waiting to be read?

PeterH:
Also note that most Arduinos will reset when the serial port is opened.

Is there anyway to keep that from happening?

You could do that (there's no guarantee the first character will be an 'a'), or you could write a simple state machine.

Arrch:
Determine if its a Hardware or Software problem: Disconnect everything from the Arduino. Does it still lock up?

It doesn't seem to lock up if I disconnect everything, although if I echo in something through the terminal it doesn't trigger the Serial.println() that I set up to test it, but it is triggered when I used python or the arduino serial monitor.
I've heard that DC motors (which is what I'm using) can sometimes send some voltage back if they're being PWM'd. I'm using a Toshiba ULN2803APG transistor array with Vcc hooked up to the arduino's 5V pin and GND is hooked up to an external 5V power supply's GND. I'm using this IC to sink the current of the DC motors so they have their GND hooked up to the IC and positive voltage is hooked up to my external 5V supply. I'm pretty new to all this but, I'm thinking a sticking a diode somewhere might help with any weird voltages coming from the DC motors.

raphre:
Is there anyway to keep that from happening?

The reset is triggered by the serial port DTR line going low and pulling the Arduino reset pin down. You can prevent the Arduino from resetting when that happens by connecting a capacitor between reset and ground. It might be better to consider whether you actually need to open and close the port repeatedly - it's not good practice.