how do you control two serial devices separately on same software serial line?

I am trying to control two dc motors using two pololu simple high-power motor controller (http://www.pololu.com/catalog/product/1377) with their serial ttl interface. I have the code to control one of them, but how do i control both of them separately at the same time ? I am connecting them to the arduino duemilanove.

#include <NewSoftSerial.h>
#define rxPin 10 // pin 10 connects to SMC TX
#define txPin 9 // pin 9 connects to SMC RX
#define resetPin 11 // pin 11 connects to SMC nRST
#define errPin 12 // pin 12 connects to SMC ERR
NewSoftSerial mySerial = NewSoftSerial(rxPin, txPin);
// some variable IDs
#define ERROR_STATUS 0
#define LIMIT_STATUS 3
#define TARGET_SPEED 1000
#define INPUT_VOLTAGE 23
#define TEMPERATURE 24
// some motor limit IDs
#define FORWARD_ACCELERATION 0
#define REVERSE_ACCELERATION 0
#define DECELERATION 0
// read a serial byte (returns -1 if nothing received after 500 milliseconds)
int readByte()
{
unsigned long time = millis();
while (!mySerial.available())
{
if (millis() - time > 500)
return -1;
}
return mySerial.read();
}
// required to allow motors to move
// must be called when controller restarts and after any error
void exitSafeStart()
{
mySerial.print(0x83, BYTE);
}
// speed should be a number from -3200 to 3200
void setMotorSpeed(int speed)
{
if (speed < 0)
{
mySerial.print(0x86, BYTE); // motor reverse command
speed = -speed; // make speed positive
}
else
{
mySerial.print(0x85, BYTE); // motor forward command
}
mySerial.print((unsigned char)(speed & 0x1F), BYTE);
mySerial.print((unsigned char)(speed >> 5), BYTE);
}
unsigned char setMotorLimit(unsigned char limitID, unsigned int limitValue)
{
mySerial.print(0xA2, BYTE);
mySerial.print(limitID, BYTE);
mySerial.print((unsigned char)(limitValue & 0x7F), BYTE);
mySerial.print((unsigned char)(limitValue >> 7), BYTE);
return readByte();
}
// returns the specified variable as an unsigned integer.
// if the requested variable is signed, the value returned by this function
// should be typecast as an int.
unsigned int getVariable(unsigned char variableID)
{
mySerial.print(0xA1, BYTE);
mySerial.print(variableID, BYTE);
return readByte() + 256 * readByte();
}
void setup()
{
Serial.begin(9600); // for debugging (optional)
mySerial.begin(38400);
// briefly reset SMC when Arduino starts up (optional)
pinMode(resetPin, OUTPUT);
digitalWrite(resetPin, LOW); // reset SMC
delay(1); // wait 1 ms
pinMode(resetPin, INPUT); // let SMC run again
// must wait at least 1 ms after reset before transmitting
delay(5);
// this lets us read the state of the SMC ERR pin (optional)
pinMode(errPin, INPUT);
mySerial.print(0xAA, BYTE); // send baud-indicator byte
setMotorLimit(FORWARD_ACCELERATION, 4);
setMotorLimit(REVERSE_ACCELERATION, 10);
setMotorLimit(DECELERATION, 20);
// clear the safe-start violation and let the motor run
exitSafeStart();
}
void loop()
{
setMotorSpeed(3200); // full-speed forward
// signed variables must be cast to ints:
Serial.println((int)getVariable(TARGET_SPEED));
delay(1000);
setMotorSpeed(-3200); // full-speed reverse
Serial.println((int)getVariable(TARGET_SPEED));
delay(1000);
// print input voltage (in millivolts) to the serial monitor
Serial.print("VIN = ");
Serial.print(getVariable(INPUT_VOLTAGE));
Serial.println(" mV");
// if an error is stopping the motor, print the error status variable
// and try to re-enable the motor
if (digitalRead(errPin) == HIGH)
{
Serial.print("Error Status: ");
Serial.println(getVariable(ERROR_STATUS));
// once all other errors have been fixed,
// this lets the motors run again
exitSafeStart();
}
}

Are the pololu simple high-power motor controllers designed/programmed to be operated off of a single serial source?

How do you control two serial devices separately on same software serial line?

There must be a protocol to address them. This can be in software (like I2C bus) or you have to make a hardware switch that can be controlled by a digitalWrite().

==> http://www.pololu.com/docs/pdf/0J44/simple_motor_controllers.pdf - page 61 describes the SW protocol .

That said - looking at your code I think there is enough room for a second SoftSerial port, or is that not an option?

You have to use the Pololu protocol as described in the manual and assign separate device numbers to each of the devices you want to control separately.

Pololu Protocol

This protocol is compatible with the serial protocol used by our other serial motor and servo controllers. As such, you can daisy-chain a Simple Motor Controller on a single serial line along with our other serial controllers (including additional Simple Motor Controllers) and, using this protocol, send commands specifically to the desired controller without confusing the other devices on the line.

To use the Pololu protocol, you must transmit 0xAA (170 in decimal) as the first (command) byte, followed by a Device Number data byte. The default Device Number for the Simple Motor Controller is 0x0D (13 in decimal), but this is a configuration parameter you can change. Any controller on the line whose device number matches the specified device number accepts the command that follows; all other Pololu devices ignore the command.

This is the first time that I have ever used serial devices. This code that I have came from the manufacturer, and for the most part, I dont know what most of the serial commands mean, most of the simple code I under stand. is this just a way of giving a name to the devices?

alphamike88: This is the first time that I have ever used serial devices. This code that I have came from the manufacturer, and for the most part, I dont know what most of the serial commands mean, most of the simple code I under stand. is this just a way of giving a name to the devices?

Sounds like a learning opportunity to learn! Read the manual (linked above) to see what each command does. Once you understand what the software is doing you can change it to do whatever you want. Read about the Pololu protocol and how to assign a device number to each of your controllers. Change the program to address each device by its number.

okay, i've been reading the manual and i'm not sure what "mySerial.print(0x85, BYTE);" means. what does BYTE indicate?

Look at the Arduino reference manual. You'll find it in the "Help" menu of the Arduino IDE:

0x85 is an integer constant in hexadecimal digits.

BYTE tells Serial.print() to send the integer value as an 8-bit binary byte (a single character) rather than spelling it out in decimal digits (the default treatment for printing integers):

Serial.print(78) gives "78" Serial.print('N') gives "N" Serial.print(78, BYTE) gives "N" Serial.print(78, BIN) gives "1001110" Serial.print(78, OCT) gives "116" Serial.print(78, DEC) gives "78" Serial.print(78, HEX) gives "4E"