Go Down

Topic: receivind unit8 data in array by arduino (Read 217 times) previous topic - next topic

samarhefina

May 18, 2017, 12:31 pm Last Edit: May 18, 2017, 08:50 pm by samarhefina
i am trying to send unit8 data from matlab to arduino and check the values of the comming data the problem is the data is received one at a time in arduino and whenever i try to store it i fail so any help on how to store unit8data in array using arduino.

 here is the matlab code

Code: [Select]
clc
delete(instrfindall);
s = serial('COM7','BaudRate',9600);
fopen(s);
txdata = ['4';'2';'2';'2';'9'];
%Convert to decimal format
txdata_dec = hex2dec(txdata);
%Write using the UINT8 data format
fwrite(s,txdata_dec,'uint8');
y = fread(s,4, 'uint8')

---------------------
here is arduino code
Code: [Select]
unsigned char buffe[5];

void setup() {
 Serial.begin(9600);
}

void loop() {
 if(Serial.available()) {
   for (int i=0;i<5;i++)
   {
   int s = Serial.read();
   buffe[i]=s;
   Serial.print(s,HEX);

   if(i==5)
   {
   for (int i=0;i<5;i++)
   {
      Serial.print(buffe[i],HEX);
   }
   }
 }


}
}

Budvar10

#1
May 18, 2017, 02:08 pm Last Edit: May 18, 2017, 02:09 pm by Budvar10
The buffe is a pointer to array of bytes. You should definitely use
Code: [Select]
buffe[i] = (unsigned char)s;
and also
Code: [Select]
Serial.print(buffe[i],HEX);
.

BTW: Please edit your post and use code tags for the code to be readable (left most button above edit window).
Arduino clone with ATmega1284P   http://forum.arduino.cc/index.php?topic=277260.0

PaulS

Code: [Select]
fwrite(s,txdata_dec,'uint8');
What IS the value in txtdata_dec? It looks to me like the value is 0x42229. I can't imagine how you think that will fit in a uint8.

Code: [Select]
  if(Serial.available()) {
    for (int i=0;i<5;i++)
    {
    int s = Serial.read();

If there is one or more bytes to read, it is NOT proper to read 5 bytes.
The art of getting good answers lies in asking good questions.

samarhefina

hello Budvar10,
it givees me the same result the problem is i need to check every byte received and whenever i do so it doesnot respond


Code: [Select]
if (( buffe[1] ==1 ) && ( buffe[1] ==1 ) &&   (buffe[2] ==2)&&  (buffe[2] ==2))
{
 Serial.print("abb");
}


but if i check on only one  byte it works i dont know what is the problem

Code: [Select]
if (( buffe[1] ==1 ))
{
 Serial.print("abb");
}

samarhefina

hello PaulS,
i removed the unit_8 and the received data is 42229 and i am receiving every number as int the problem i need to store these values in an array which represent data packet and check every number in it however i cant do so when ever i check single number it works fine but when i check first and second number it doesnot work and i dont know what is wrong.

Robin2

Code: [Select]

txdata = ['4';'2';'2';'2';'9'];
}

If your data starts off as text wouldn't it be simpler all round just to send it as text.

See the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up