MATLAB-to-MATLAB Serial Communication

7 次查看(过去 30 天)
Bill
Bill 2013-10-7
评论: Bill 2013-10-8
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 个评论
Matt Kindig
Matt Kindig 2013-10-8
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 2013-10-8
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.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2013-10-8
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 个评论
Walter Roberson
Walter Roberson 2013-10-8
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 2013-10-8
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.

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by