How SEND and READ a hexadecimal code in matlab and instrument via port usb

25 views (last 30 days)
Hello
I really need need your help on this please. The goal is to send a command code in hexadecimal 8-bit and receive the answer as 8-bit Matlab. For now, I can send information in the following map out verification oscilloscope and I see the same .By against my problem is transmitting the data to matlab. I tried everything with fread or fscanf but it still shows me error; such as:
Warning: The specified Amount of data returned 'Was not Within The Timeout period.
So I do not know if SOMEBODY can help me on this problem please because I have already made several attempts, but this time to no avail.
sorry for my english.
s = serial('COM5');
set(s,'BaudRate',9600,'DataBits', 8, 'Parity', 'none','StopBits', 1, 'FlowControl', 'none','Terminator','CR');
set(s,'Timeout',1);
% Connect to instrument object, obj1.
fopen(s);
txdata = ['10';'02';'00';'00';'10';'03']; %hexadecimal code
%Convert to decimal format
txdata_dec = hex2dec(txdata);
pause(2)
%read data format UINT8
fwrite (obj2, txdata_dec, 'uint8');
Until then everything works, but at the reception, there is error.
I did:
out=fscanf(s)
ou
out = fread(s,8,'uint8');
but not reponse.
window command
get(s)
ByteOrder = littleEndian
BytesAvailable = 0
BytesAvailableFcn =
BytesAvailableFcnCount = 48
BytesAvailableFcnMode = terminator
BytesToOutput = 0
ErrorFcn =
InputBufferSize = 512
Name = Serial-COM5
ObjectVisibility = on
OutputBufferSize = 512
OutputEmptyFcn =
RecordDetail = compact
RecordMode = overwrite
RecordName = record.txt
RecordStatus = off
Status = open
Tag =
Timeout = 1
TimerFcn =
TimerPeriod = 1
TransferStatus = idle
Type = serial
UserData = []
ValuesReceived = 0
ValuesSent = 6
SERIAL specific properties:
BaudRate = 9600
BreakInterruptFcn =
DataBits = 8
DataTerminalReady = on
FlowControl = none
Parity = none
PinStatus = [1x1 struct]
PinStatusFcn =
Port = COM5
ReadAsyncMode = continuous
RequestToSend = off
StopBits = 1
Terminator = CR
thanks
  3 Comments
Walter Roberson
Walter Roberson on 9 May 2021
Terminator = CR
You are using a binary protocol. It is likely that either there is no terminator or that the terminator is not carriage return. You might need to switch to non-terminator reading, such as BytesAvailableFcn that does the fread()

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!