Writing an array of numbers to serial port

8 views (last 30 days)
Hi,
I have a device connected with the serial port. I intend to transfer an array of decimal numbers but am having some problems. I am able to transfer data successfully only if I use fwrite in a loop (i.e. one by one for each number). Is there a possibility to issue a single fwrite command for the whole array? P.S. I have tried several combinations of codes, but nothing seems to work :(
Following is the code which works:
data_hex = {'08', '0c', '05', '00', '04', '0a', '33', '33', '33', '33', '33', '33', '33', '33', '33', '00'}; % data in hexadecimal format
data_dec = hex2dec(data_hex);
s = serial('COM9');
set(s,'BaudRate',5000000, 'Parity', 'none', 'DataBits', 8, 'StopBits', 1);
fopen(s);
for i=1:16
fwrite(s,data_dec(i,:));
end
fclose(s); delete(s); clear
Ideally, I would like to have something like this:
fwrite(s, data_dec(1:end));
but this does not work. I get an acknowledgement on the serial device once the above 16 bytes are received. Thanks for the help!!!

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!