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

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.

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

답변 (0개)

카테고리

도움말 센터File Exchange에서 C Shared Library Integration에 대해 자세히 알아보기

질문:

2016년 7월 28일

댓글:

2016년 7월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by