send data by serial port
2 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Geoff Hayes
on 19 Nov 2014
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 Comments
More Answers (0)
See Also
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!