Main Site Documentation

Is there a write buffer for the serial port?


#1

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

MO=1;JV=123;BG;

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


#2

Hi,
According to
https://www.ghielectronics.com/community/forum/topic?id=14346&page=2
Post #18
at least for the Cerb family

256 for TX
1024 for RX
Cheers
Roland


#3

Sorry, I should have mentioned I’m working with the Spider and the Raptor just in case the answer is hardware dependent. I wanted to know a little more about this before actually trying it on the hardware since a garbled command could (would, according to Murphy’s Law) send my motor spinning off into outer space.

Thanks for the help so far.


#4

@ Gene - These are the following buffers for EMX (Spider), G120 (Cobra II), and G400 (Raptor):

4 KiB for the TX on all;

4 KiB for RX on G400

and 16 KiB for RX on EMX and G120.


#5

@ Aron - thanks for the information. It is a little different than previous responses so I’m glad to have the straight scoop.


#6

@ Aron

Sorry if forgot to ask if the numbers you gave are per serial port or total for all serial ports.

Thanks - Gene


#7

Per port


#8

Thanks