send data by serial port

조회 수: 1 (최근 30일)
dao
dao 2014년 11월 19일
답변: Geoff Hayes 2014년 11월 19일
I want send a cell data with elements are chars through visual COM.
In trans:
for i=1:length(f)% f is a cell array
YourCell = {f{i}};
string = YourCell{1};
fprintf(s,'%c',string);
end
In receive:
function BytesAvailable_Callback(obj,event)
global s;
myData = fgets(s)
But I get error:
  • Error using fgetsInvalid file identifier. Use fopen to generate a valid file identifier. *
  • Error in GUIDE_RECEIVE>BytesAvailable_Callback (line 170)myData = fgets(s) *
So, How can I fix it ? Thank for your help

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 11월 19일
Dao - how and where in your GUI have you initialized s? Based on the error message and the fact that s is declared as a global variable, it probably has not been initialized properly and so is empty. For example, the following code generates the same error message
s = [];
fgets(s);
Find the code where you initialize s and make sure that you specify that s is global. Something like
function opensSerialPort()
global s;
s = fopen(...);
Now try the re-running your GUI to see what happens.
---------------
As an aside, you may want to experiment with the input obj to your BytesAvailable_Callback. As obj is the serial port object that caused the event to occur, then you may be able to just do
function BytesAvailable_Callback(obj,event)
myData = fgets(obj);
I haven't tested this out, but it may be worth trying because it would simplify the code (and avoid troublesome global variables).

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by