MATLAB Answers

Error while calling function from another function

조회 수: 1(최근 30일)
Jimmy cho
Jimmy cho 18 Aug 2020
편집: Jimmy cho 18 Aug 2020
Hi guys,
I have implemented successfully those two functions in matlab:
this function returns an array of unsigned values:
function ByteConvertRxBuffer=ByteConvertRxBuffer(rx) %rx is a binary array values that I want to convert it by this function to Byte array values
rx=rx(rx~=' ');
the other function is returning if my input crc is ok or not:
function crcOK=CrcValidationCheck(RxBuffer)
checksum = uint16(0); %#ok<NASGU>
checksum = uint16(CRC_INIT);
i = 1;
while i <= numel(RxBuffer)
checksum = calcCRC(RxBuffer(i), checksum);
i = i + 1;
PacketIndex = PacketIndex + 1; %#ok<NODEF>
if (checksum == 0)
disp(PacketIndex); %this is for displaying the packetNumber that Im currently dealing with...may get more than one packet
crcOK = true; %#ok<NASGU> % your packet number PacketIndex is Valid
crcOK = false;%#ok<NASGU> % your packet number PacketIndex is Valid
the other function that Im calling is:
function crcReg = calcCRC(crcData, crcReg)
CRC16_POLY = 0x8005;
for i = 0 : 7
if bitxor( bitshift( bitand(crcReg, 0x8000), -8), ...
bitand( crcData, 0x0080 ) )
crcReg = bitxor( bitshift(crcReg, 1), CRC16_POLY );
crcReg = bitshift(crcReg, 1);
crcData = bitshift(crcData, 1);
so I've more another function which it's
function Result=Result(buffer)
rx=ByteConvertRxBuffer(buffer); %rx implicitly is valued as unsigned integers of array that's retunred from ByteConvertRxBuffer
Result= CrcValidationCheck(rx);
so I called the function Result in my command window in matlab and I get this:
it shows me this errors:
Error using bitand
Inputs must be signed or unsigned integers of the same class or scalar doubles.
Error in calcCRC (line 5)
bitand( crcData, 0x0080 ) )
Error in CrcValidationCheck (line 8)
checksum = calcCRC(RxBuffer(i), checksum);
First three functions are working seperately fine, this means if I tried to input to one of the first three functions any input it works fine !
but while I call those two functions from another function it doesn't work and I get errors !
Any help please how can I fix those errors? really weird how my functions seperately works fine but once I call to them between eachother they don't work properly.
In order to catch the bug I tried to make the code row in the function CrcValidationCheck
checksum = calcCRC(RxBuffer(i), checksum); as a note by "%"
and all code functions work fine, so the bug maybe while calling calcCRC function? if so how do I fix it?!
thanks alot

  댓글 수: 0

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 18 Aug 2020
What is your crcData? I suspect it is either a double array that is not a scalar or an array of an integer type that is not uint16. Convert the data to uint16 using cast or typecast before calling bitand on it with a uint16 as the second input.

  댓글 수: 1

Jimmy cho
Jimmy cho 18 Aug 2020
crcData is one of every value of the Byte array values that Im getting from function ByteConvertRxBuffer !
in other words crcData is a value of uint8 !
the function ByteConvertRxBuffer returns array of uint8 values ! and I run by a loop on every value of those values calling function crcCalc at every value (see function CrcValidationCheck(RxBuffer) )
my crcData is a uint8 data, and it's a value of each array values of uint8 values called rx (the output of ByteConvertRxBuffer(buffer));

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by