You are on page 1of 2

Để giao tiếp giữa matlab và proteus qua cổng com cần lưu ý các vấn đề:

LẬP TRÌNH CỔNG COM TRÊN MATLAB:

Đoạn code viết trong tên_gui_opening_function.

delete(instrfind( {'Port'},{'COM3'})) % xoa cong com phat hien tren may...


clear a;
global a;
a=serial('COM3'); % khai báo cổng com
fopen(a);

Để cài đặt thông số cho cổng COM:


s=serial(comPort);
set(s,'DataBits',8);
set(s,'StopBits',1);
set(s,'BaudRate',9600);
set(s,'Parity','none');

KẾT NỐI ARDUINO-MATHLAB:

Cài thư viện :arduinoio……..install_arduino.

Dùng các lệnh thông thường :


A=arduino(‘com2’).
a.pinMode(13,’output’);
a.digitalWrite(13,1);

Để xuất dữ liệu ra cổng COM từ matlab dùng lệnh:


 Fprintf( a, string): xuất dữ liệu dạng chuỗi.
 Fprintf(a,’ %d ’, dec): xuất dạng số.
Để lưu data từ cổng COM trên máy tính :
 Fscanf(a,’ %d ’);
Lưu giá trị liên tục ta dùng mảng trong matlab:

global s;
i=0;
while (i<100)
i=i+1;
adc(i)=fscanf(d,'%d');
drawnow;
axes(handles.axes1);
plot(adc,'r.-','LineWidth',5);
grid on;
axis([0 500 0 100]);
pause(0.05);
end

Để lấy giá trị edit textbox ta dùng lệnh:


Get(handles.edittext,’String’).
Để xuất dữ liệu lên dùng lệnh:
Set(handles.edittext,’String’,c);

Lưu ý 2 hàm số str2num và num2str để biến trở lại.

LẬP TRÌNH ARDUINO TỪ CỔNG COM:


Xuất data ra serial:
Serial.print (data,DEC): kieu so.
Nhận dữ liệu về :
Char kytu = serial.read();
String chuoi = chuoi+kytu;
Chuyển dữ liệu từ string sang số dùng lệnh:
Int num = chuoi.toInt();

You might also like