Serial communication with microcontroller: receiving garbage values

Hi everyone,
I am trying to establish a communication between MATLAB and a microcontroller using the serial port. Every 1 second I send a string encoded with a variable name and the microcontroller should respond by giving the value stored in it.
I tested the C code of the microntroller using putty software and it sends the correct data. But when I use MATLAB I dont get any values. If I change the C microcontroller code to respond to just one character input from the string in MATLAB then I receive the value once in every 3 attempts. But for more than one character string input I dont get anything.
I tested the C code to check the received buffer with itself and transmit the received string. In this case only the first character is what I had sent and the remaining are garbage values.
Any suggestions what I might be doing wrong? My MATLAB code is as follows:
clc
clear all
s = serial('COM8','baudrate',115200);
set(s, 'InputBufferSize', 13);
fopen(s);
while(1)
pause(1);
disp('tx and rx');
//commented// fprintf(s,'ckpp');
fwrite(s,'ckpp');
b=fread(s);
//commented// b=fscanf(s);
end
fclose(s);
Thanks!

2 Comments

MATLAB does not permit the use of // commenting, so you must be using some language other than MATLAB.
For any data rate over 9600, you should be setting hardware flow control, unless perhaps you are using a serial to USB convertor. Serial to USB convertors have their own challenges, however.

Sign in to comment.

Answers (0)

Categories

Asked:

on 28 Jul 2016

Commented:

on 28 Jul 2016

Community Treasure Hunt

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

Start Hunting!