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