Sample C++ code for Zaber devices

892 views Asked by At

It looks like the serial protocol for Zaber devices is pretty simple to implement, but is there any sample code in C++ available?

2

There are 2 answers

0
Don Kirkby On BEST ANSWER

If you are writing managed C++, you can use our .NET library DLL just like you would in a C# or Visual Basic project. You can find more information with the Zaber Console source code. The examples are all in C# or Visual Basic, but you can do the same things in managed C++.

If you're not writing managed C++, you'll have to write directly to the serial port. We have some example C code that will show you how to convert the commands into a byte stream. This snippet prepares the six bytes to write to the serial port.

txBuffer[0] = deviceNum;
txBuffer[1] = command;
// Position 2 is LSB; Position 5 is MSB
txBuffer[2] = ( data        & 0x000000FF);
txBuffer[3] = ((data >>  8) & 0x000000FF);
txBuffer[4] = ((data >> 16) & 0x000000FF);
txBuffer[5] = ((data >> 24) & 0x000000FF);

This snippet processes six bytes received from the serial port.

deviceNum = rxBuffer[0];
command = rxBuffer[1];
// Position 2 is LSB; Position 5 is MSB
data = ( rxBuffer[2]        & 0x000000FF)
     + ((rxBuffer[3] <<  8) & 0x0000FF00)
     + ((rxBuffer[4] << 16) & 0x00FF0000)
     + ((rxBuffer[5] << 24) & 0xFF000000);

How you connect to the serial port will probably be different from our example, but the C++ compiler documentation should be able to help you there. Download the example code to see more details, such as a timer to reset the protocol if it gets out of synch.

For more description of the serial communication protocol, see the user manual.

You can also write Zaber Console scripts using C++, although we haven't yet created a C++ script template.

1
Ken Tindell On

Watch out for a bear trap with the Zaber protocol: if the microcontroller loses sync with the Zaber motor it can get stuck reading the back half of one packet and the front half of the next (you should use a timer to detect inter-packet timing gap to reset the protocol state machine)