matlab读串口数据问题
用matlab设计GUI界面,使他具有读串口数据的功能,请问,应该如何编程,使它能不断连续读串口,而不用人工一直去控制它读!!!总而言之,就是当我只要按一次开始读串口按钮...
用matlab设计GUI界面,使他具有读串口数据的功能,请问,应该如何编程,使它能不断连续读串口,而不用人工一直去控制它读!!!
总而言之,就是当我只要按一次开始读串口按钮,然后它就会自动的一直读数据,再经过处理就可以实时的实现某种功能!!!1 展开
总而言之,就是当我只要按一次开始读串口按钮,然后它就会自动的一直读数据,再经过处理就可以实时的实现某种功能!!!1 展开
展开全部
如果一直读数据,可以使用一个Timer来读串口就可以了。但是,不用也可以,应为串口有一个属性可以当接收缓冲区中有数据的时候自动接收。如下:
comPort=get(handles.pumComPort,'String');
handles.u_sCom=serial(comPort{get(handles.pumComPort,'Value')});
baudRate=get(handles.pumBaudRate,'String');
set(handles.u_sCom,'BaudRate',str2double(baudRate{get(handles.pumBaudRate,'Value')}));
dataBits=get(handles.pumDataBits,'String');
set(handles.u_sCom,'DataBits',str2double(dataBits{get(handles.pumDataBits,'Value')}));
stopBits=get(handles.pumStopBits,'String');
set(handles.u_sCom,'StopBits',str2double(stopBits{get(handles.pumStopBits,'Value')}));
parity=get(handles.pumParity,'String');
if strcmp(parity,'无')==1
set(handles.u_sCom,'Parity','none');
else
if strcmp(parity,'奇校验')==1
set(handles.u_sCom,'Parity','odd');
else
if strcmp(parity,'偶校验')==1
set(handles.u_sCom,'Parity','even');
else
set(handles.u_sCom,'Parity','none');
end
end
end
flowControl=get(handles.pumFlowControl,'String');
if strcmp(flowControl,'无')==1
set(handles.u_sCom,'FlowControl','none');
else
if strcmp(flowControl,'硬件')==1
set(handles.u_sCom,'FlowControl','hardware');
else
if strcmp(flowControl,'软件')==1
set(handles.u_sCom,'FlowControl','software');
else
set(handles.u_sCom,'FlowControl','none');
end
end
end
set(handles.u_sCom,'Timeout',1);
set(handles.u_sCom,'InputBufferSize',1024);
set(handles.u_sCom,'BytesAvailableFcnMode','byte');
set(handles.u_sCom,'BytesAvailableFcnCount',1);
set(handles.u_sCom,'BytesAvailableFcn',{@BytesAvailableFcnCallback,handles});
fopen(handles.u_sCom);
set(handles.txtStatus,'String','打开端口成功');
set(hObject,'Enable','off');
set(handles.btnClosePort,'Enable','On');
guidata(hObject,handles);
set(handles.btnSendData,'Enable','On');
至于全部程序已经发给你了。
comPort=get(handles.pumComPort,'String');
handles.u_sCom=serial(comPort{get(handles.pumComPort,'Value')});
baudRate=get(handles.pumBaudRate,'String');
set(handles.u_sCom,'BaudRate',str2double(baudRate{get(handles.pumBaudRate,'Value')}));
dataBits=get(handles.pumDataBits,'String');
set(handles.u_sCom,'DataBits',str2double(dataBits{get(handles.pumDataBits,'Value')}));
stopBits=get(handles.pumStopBits,'String');
set(handles.u_sCom,'StopBits',str2double(stopBits{get(handles.pumStopBits,'Value')}));
parity=get(handles.pumParity,'String');
if strcmp(parity,'无')==1
set(handles.u_sCom,'Parity','none');
else
if strcmp(parity,'奇校验')==1
set(handles.u_sCom,'Parity','odd');
else
if strcmp(parity,'偶校验')==1
set(handles.u_sCom,'Parity','even');
else
set(handles.u_sCom,'Parity','none');
end
end
end
flowControl=get(handles.pumFlowControl,'String');
if strcmp(flowControl,'无')==1
set(handles.u_sCom,'FlowControl','none');
else
if strcmp(flowControl,'硬件')==1
set(handles.u_sCom,'FlowControl','hardware');
else
if strcmp(flowControl,'软件')==1
set(handles.u_sCom,'FlowControl','software');
else
set(handles.u_sCom,'FlowControl','none');
end
end
end
set(handles.u_sCom,'Timeout',1);
set(handles.u_sCom,'InputBufferSize',1024);
set(handles.u_sCom,'BytesAvailableFcnMode','byte');
set(handles.u_sCom,'BytesAvailableFcnCount',1);
set(handles.u_sCom,'BytesAvailableFcn',{@BytesAvailableFcnCallback,handles});
fopen(handles.u_sCom);
set(handles.txtStatus,'String','打开端口成功');
set(hObject,'Enable','off');
set(handles.btnClosePort,'Enable','On');
guidata(hObject,handles);
set(handles.btnSendData,'Enable','On');
至于全部程序已经发给你了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询