Serial port reading function fread(obj) is giving output empty matrix 1 by 0

1 view (last 30 days)
i have a query regarding reading of serial port data. i am using a usb rs232 cable and has also been detected by matlab instrhwinfo.
actually, i just need to trigger a matlab program when the pin-2 (rx) of rs232 recieves a high voltage. can i do this by just connecting a battery power supply to pin-2(rx) and ground to pin-5(GND) of rs232 without the use of any microcontroller, pic,etc. can or cannot, i have tried this, and did not get an output as 1 or any other output.
when i typed fread(obj), output is empty matrix 1 by 0 and when other reading functions like fscanf, fgetl, get, i got an empty string.
i dont know where am i going wrong?
  2 Comments
SRI
SRI on 22 Mar 2014
Ayush could you send the code so that it is easy to find the Bug inside the code
Ayush
Ayush on 22 Mar 2014
Edited: Ayush on 22 Mar 2014
on command window, first i got the list of serial ports on my computer
instrhwinfo('serial')
obj=serial('COM18')
% in device manager i got to know that rs232 cable is listed as COM18
fopen(obj);
fread(obj) % or get(obj) or fgetl(obj) or fscanf(obj)
output here is: empty matrix 1-by-0
i dont even know that the matlab is detecting the inputs of the rs232 or not
also i think there is no need to set a new baud rate or any other parameter other than default, because i only need to sense any message other than NULL

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 23 Mar 2014
If your baud rate is wrong then the serial port will not be able to properly sense the message.
Have you considered wiring to CD (Carrier Detect) instead of Receive ? Then you can query the pin status; see http://www.mathworks.com/help/matlab/matlab_external/pinstatus.html
  2 Comments
Ayush
Ayush on 23 Mar 2014
I think i am wrong with the input voltage levels of rs232. It requires -3 to - 25 V for signal 1. It would require max232 ic for changing voltage levels. I have tried communicating with real term terminal software and shorted the rx and tx pin, pin indicator was blinking. I think i should go with the voltage levels first.
I am doing this changing voltage levels stuff. But, do i have to set a baud rate compulsorily.
And also, is there a need to short some pins of rs232.
Walter Roberson
Walter Roberson on 23 Mar 2014
I suggest +5V, it should be enough for all modern receivers.
You will not be able to detect characters using the shorting scheme. You might be able to detect BREAK. But Carrier Detect would be a lot easier, and can be done by shorting as well.

Sign in to comment.

Categories

Find more on Simscape Electrical 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!