MATLAB-to-MATLAB Serial Communication

1 view (last 30 days)
Bill
Bill on 7 Oct 2013
Commented: Bill on 8 Oct 2013
Hello All,
I'm trying to set up serial communication between two MATLAB clients (programs that are compiled differently, one designed to transmit data (SEND client) and one designed to listen for data (RECEIVE client)).
For the SEND client, my code is as follows:
portHandle = serial('COM1', 'baudrate', 9600);
fopen(portHandle);
fprintf(portHandle, '1');
For the RECEIVE client, my code is as follows:
portHandle = serial('COM1', 'baudrate', 9600);
fopen(portHandle);
data = fscanf(portHandle)
However, while I can confirm that things do get sent according to the SEND client's 'Read/Write State,' my receiving client does not seem to receive them, as I receive the error message "Warning: A timeout occurred before the Terminator was reached."
I'm using MATLAB R2010a, Windows 7 x64, and I've confirmed that my COM ports on both the SEND and RECEIVE clients are configured appropriately, i.e. port number, baud rate, etc.
Can someone provide some insight as to what I'm doing wrong? Can MATLAB clients both send and receive serial port information from each other?
Thanks for any support you can provide.
Bill
  5 Comments
Matt Kindig
Matt Kindig on 8 Oct 2013
Also, I know that you get a timeout warning, but have you actually verified that there is no data output? Is 'data' empty after the transmission? What if you did
data = fread(portHandle)
What does this give you for 'data'?
Bill
Bill on 8 Oct 2013
Matt, thanks for your suggestions. I'll have access to the hardware tomorrow, so I'll use PuTTY first thing. As for your question about the timeout warning, I've been using fscan, not fread, so maybe that's part of the problem? I'll be sure to check on that tomorrow, as well.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 8 Oct 2013
I would suggest specifically setting the Terminator property and the BytesAvailableFcnMode. You might possibly also want to configure InputBufferSize. And maybe even flow control.
  3 Comments
Walter Roberson
Walter Roberson on 8 Oct 2013
Serial communications is surprisingly complicated to do well. The model of "A sends a byte over the wire, B reads the byte and passes it on to the program" only works at low speed (up to roughly 4000 bps) and on dedicated systems where there is only one possible program to be served. At higher speeds you start needing to be concerned about on-chip buffering (or more complicated schemes) on the receiver due to the time it takes to service the interrupts. On multi-user systems, the task switching and costs of the interrupt becomes too much and it becomes necessary for the operating system to normally buffer the data and present it to the program in chunks, and it becomes necessary for the program to configure the conditions under which the chunks should be returned to the program.
MATLAB uses the C programming language model of I/O interactions, which expects buffered input streams. Unbuffered (less overhead per character, more total overhead for groups of characters) input is specified as being lower-level in C (e.g., defined by POSIX.1)
You can look at the documentation for BytesAvailabelFcnMode and InputBufferSize and flow control.
Bill
Bill on 8 Oct 2013
Hey Walter, thanks for the information. Tomorrow when I have access to the hardware I'll play with some of those things and see if I can solve the problem. Thanks.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!