cant read the serial port!!!

9 views (last 30 days)
SETH GERSBACH
SETH GERSBACH on 18 May 2022
Edited: Walter Roberson on 18 May 2022
want to read information from serialport but it says it need fopen, but serialport is the fopen,
also this is a gui
When button pressed
app.sp = serialport('COM3', 9600);
while ~feof(app.sp)
spLine = fgetL(app.sp);
spData = sscanf(spLine, '%f V, %i counts, %i ms');
Errors:
Error using feof: Invalid file identifier. Use fopen to generate a valid file identifier.
as well as
Error using serialport (line 116)
Unable to connect to the serialport device at port 'COM3'. Verify that a device
is connected to the port, the port is not in use, and all serialport input arguments
and parameter values are supported by the device
  3 Comments
SETH GERSBACH
SETH GERSBACH on 18 May 2022
The serial port has worked before it pops up randomly, I think I just need to clean them
I was informed that serialport = read
Beyond that the code won’t work because feof wants a read, but serialport does the reading apparently and when adding a read it still won’t work
Geoff Hayes
Geoff Hayes on 18 May 2022
@SETH GERSBACH - you will need to clarify what you mean by and when adding a read it still won’t work.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 18 May 2022
Edited: Walter Roberson on 18 May 2022
feof() is not defined for serialport objects. It was defined for serial() objects.
In order for feof() to be meaningful, serialport objects would need to have an "open" state, with feof set when it is closed. But serialport does not have an open state.
Remember that serial ports are asynchronous, and the serial protocol has no inherent timeout, and no "keepalive", and there is no way to send data that inherently means "end of file" for the serial protocol.
What rs232 does define is the possibility of reading the Carrier Detect pin. However, Carrier Detect dropping is not the same thing as end of file. Carrier Detect was designed around modems, and since the Hayes Smart Modem protocol was created (AT command set), the rule has always been that CD is false in the period before a connection is made, when you are talking to the modem configuring it, and becomes true when the modem frequency / handshakes are completed and the path to transmit data is ready. Lack of CD does not inherently mean that the serial port is EOF.
CD is not pulled true in rs232. When you are working with a non-modem (such as serial over USB), then CD will just be low. It would break the majority of modern uses of COM ports to have low CD automatically mean EOF.
So what can you do? Well, you can https://www.mathworks.com/help/matlab/ref/serialport.getpinstatus.html check the pin status. But unless you are using a modem, expect CD to be false.
rs232 just does not have any meaningful FEOF at the protocol level.
There certainly might be a defined end of file at a higher level. For example if you were running SLIP or PPP then it could make sense for the serial packets to be carrying a RST for a TCP connection being carried. But that involves a software layer receiving the raw bytes and decoding them (and in some cases doing decompression) and making the decoded bytes available... which is not at the serialport level.
Your communications are incorrectly designed if you expect feof on a serialport.

Community Treasure Hunt

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

Start Hunting!