I’m trying to find a simple way to write a series of commands to a RS232 servo motor controller but I’d like to find out if there is a write buffer for the serial port and if so, how long it is. The command consists of a bunch of little strings, separated by semicolons with a few variables in the middle. For example, to set the motor velocity to 123 counts per second, I’d send
I’d like to avoid GC so I’d like stay away from strings. StringBuilder would be OK but the most efficient thing to my simple mind is to define a bunch of constant byte arrays and then send them out with successive port.write commands. Something like
private static byte ElmoMotorOn = UTF8Encoding.UTF8.GetBytes("MO=1;"), ElmoMotorOff = UTF8Encoding.UTF8.GetBytes("MO=0;"), ElmoJogVelocity = UTF8Encoding.UTF8.GetBytes("JV="), ElmoBegin = UTF8Encoding.UTF8.GetBytes("BG;"), semicolon = UTF8Encoding.UTF8.GetBytes(";"); TXBytes = UTF8Encoding.UTF8.GetBytes(jogVelocityCPS.ToString()); ElmoPort.Write(ElmoMotorOn, 0, ElmoMotorOn.Length); ElmoPort.Write(ElmoJogVelocity, 0, ElmoJogVelocity.Length); ElmoPort.write(TXBytes, 0, TXBytes.Length); ElmoPort.Write(semicolon, 0, semicolon.Length); ElmoPort.Write(ElmoBegin, 0, ElmoBegin.Length);
Seems like this would work if .Net buffers up the successive .Writes. My longest command is probably less than 20 or 30 characters so if there is any buffer at all, I might be OK.
Suggestions about simpler or more robust or just plain better approaches would also be appreciated.
Thanks - Gene