understanding bits, bit masks, and bytes in Matlab


Just purchased a StimTracker and using the interface to neuroscan EEG (http://www.cedrus.com/support/stimtracker/tn1469_neuroscan.htm). So seems like I should have 8 event marker/trigger bits with which to send events from Matlab.So using serial.m in Matlab my commands are:

s1 = serial(’/dev/cu.usbserial-AH01NEJ7’, ‘BaudRate’, 115200,‘DataBits’, 8, ‘StopBits’, 1, ‘FlowControl’, ‘none’, ‘Parity’, ‘none’, ‘Terminator’,‘CR’, ‘Timeout’, 400, ‘InputBufferSize’, 16000);


But now I get a little confused. Say I want to send a 10ms pulse, so first thing I need to do is set the pulse duration. According to the cedrus documentation (http://www.cedrus.com/support/stimtracker/tn1450_st_commands.htm) to set the time I need to use a “four byte binary value indicating the number in milliseconds” forming a “32-bit unsigned integer”. So if I use matlab’s decimal-to-binary converter for 10 I get:

ans =

But this is not a four byte binary value. To make this four bytes then I guess I need to add a bunch of 0’s. Also from what I read, bits are read from right to left so the 0’s should be placed on the left of 1010. So then to set the pulse duration to 10ms I would execute the command:


Then say I want to have this 10ms pulse on channel 3 (out of the 8 possible event channels available to me). The documentation states “mh followed by two ‘bit mask’ bytes (a binary value). In the first byte, each bit corresponds to an output line. The second byte is currently ignored”. So if I understand things correctly in the first byte each bit represents the state of each channel - so to have all 8 channels off would be:
Then to turn on channel 3 (and remembering bits are read right to left) would be:
and then taking this and adding a second bit mask byte that is ignored, so will just make it all zeros:
means the Matlab command should ultimately look like this (since read right to left, the ignored byte is supposed to be second, so will add it to the left):


Is my understanding of this correct? If not, could someone else explicitly give their fprint() statements for 1) setting a pulse duration for some time (like 10ms), and 2) sending an event to one of the channels? It would be extremely helpful! (and ultimately I want to use IOPort.m commands since they apparently are more precise, but to begin with I figure I should understand serial.m commands)

Thanks so much!

Hi David

Try sending the decimal equivalent of your bits and see if it will work.
Had the same issue before and was trying to send bits (even with 0x0101) but the decimal part works for me.



We’ve just created a new web page that illustrates how to use the RealTerm terminal emulation program to interact with StimTracker. Of course, please do not hesitate to post any follow up questions after checking out that page.