MATLAB Answers

How do I send hex command as mmcorej.CharVector to serial port?

10 views (last 30 days)
Maria M.
Maria M. on 11 Aug 2017
Answered: Maria M. on 16 Aug 2017
Hi All,
I am trying to send a hex message to a light source connected by a usb through the virtual serial port. The source expects an up to 8 byte message, for example 'A9035301A95C' in order to change state. I am trying to communicate with it via Micro Manager adapter for Free Serial Port. Micro Manager command has the following format: setSerialPortCommand(String portLabel, String command, String term) where String command is java.lang.String command.
I tried various forms of the code below, but with no success. I would greatly appreciate any advice on the correct format for the command.
cheers, Maria
function [] = setBrightFieldManual(color,intensity)
global mmc;
comPortScopeLED = 'COM5';
presetColors = {'3000K','4500K','6500K','Red ','Green','Blue '}; % from manual
colorNumbers = {'01','02','03','04','05','06'};
colorMap = containers.Map(presetColors,colorNumbers);
intensityHEX = dec2hex(intensity);
checkSum = dec2hex(2^8-(81+hex2dec(colorMap(color))+intensity));
playPresetColorString = {'A9','04','4D',colorMap(color),intensityHEX,checkSum,'5C'};
setManualControllerLockoutON = {'A9','03','53','01','A9','5C'};
setUSBControl = {'A9','03','27','03','D3','5C'};
setManualControllerLockoutOFF = {'A9','03','53','00','AA','5C'};
getManualControllerLockout = {'A9','02','54','AA','5C'};
setAllShuttersOpen = {'A9','03','55','07','A1','5C'};
mmc.setSerialPortCommand(comPortScopeLED,'A9035301A95C',''); % also tried (comPortScopeLED,'setManualControllerLockoutON','')

Answers (2)

Walter Roberson
Walter Roberson on 11 Aug 2017
h2js = @(hexstring) java.lang.string( char(sscanf(hexstring, '%02x')) )
mmc.setSerialPortCommand(comPortScopeLED, h2js('A9035301A95C'), '');
That is, you need to convert the hex to bytes, and the bytes to java string.
  1 Comment
Maria M.
Maria M. on 14 Aug 2017
Hi Walter,
Thank you very much. The convertation which you suggested works, commands are being sent but device does not recognize them and does not switch the state. I am getting a reply in wrong format: A9034D. It looks like I used the wrong command. setSerialPortCommand sends data as java.lang.String, but I need to send binary data. The correct command for this is writeToSerialPort(String portLabel, CharVector data) but I am again facing the issue of correct convertation. CharVector is a java class from mmcorej package (<>, which I import. I tried the code below and many other variations
setManualControllerLockoutON = {'A9','03','53','01', 'A9','5C'};
hex2jsVector = @(hexstring) mmcorej.CharVector(char(hex2dec(hexstring)));
This code returns "No constructor 'mmcorej.CharVector' with matching signature found."
However, if I try to create an empty object, it works:
CharVecObjEmpty = javaObject('mmcorej.CharVector');
I would really appreciate any suggestions.
The following code works with fwrite command:
setManualControllerLockoutON = hex2dec({'A9','03','53','01', 'A9','5C'});
if exist ('s')
fclose (s)
delete (s)
clear s
s = serial ('COM5','BaudRate',115200);
fopen (s);
fclose (s)
Best, Maria

Sign in to comment.

Maria M.
Maria M. on 16 Aug 2017
Problem solved. The correct command is writeToSerialPort. Here is working example (requires imported mmcorej):
setUSBControl = hex2dec({'A9','03','27','03','D3','5C'});;
ChVsetUSBControl = mmcorej.CharVector();
for k=1:length(setUSBControl)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!