Reputation: 29
I'm reading data from serial port through MATLAB. The serial port is connected by an XBee module.
I have sucessully read the data and can also send the data correctly. Here's the code; it's quite simple:
s = serial('COM4', 'BaudRate', 9600, 'Terminator', 'CR', 'StopBit', 1, 'Parity', 'None');
fopen(s);
while(1)
while(s.BytesAvailable==0)
end
fprintf(s,'1');
fscanf(s)
s.BytesAvailable
end
So, as you can see at the first stage of the main loop, I'm waiting until data is available in the input buffer. Once the code detects data, immediately a character is sent. However the execution is not fast as I expected. Using an oscilloscope, probes on Xbee DIN and DOUT, I measure 34 ms between when the data is sent to the PC and when the data comes from the PC.
For my application, 34 ms is a critical time.
How can I fix this?
Upvotes: 1
Views: 2624
Reputation: 1091
Why do you use USRT (universal serial receiver/transmitter) at 9600 Baud rate?
Your USRT setup, "'BaudRate', 9600, 'StopBit', 1" means one byte (8 bit) of data is transferred by 10 bits (1 start bit, 8 data bit and 1 stop bit) on the wire whose speed is 9600 bit per second, so 960 bytes per second is the maximum speed of data.
It is about one byte per ms (millisecond).
XBee use 5 byte for the header, so its header overhead is about 5 ms.
34 ms is not so bad if you use 25-28 bytes of data. If you use only few bytes of data, you may have another problem.
To improve on this problem, you should use a higher rate for USRT.
If you use the following setup, transmission of your data may be achieved within 3 ms - 4 ms.
s = serial('COM4', 'BaudRate', 115200, 'Terminator', 'CR', 'StopBit', 1, 'Parity', 'None');
I would just change the Baud rate from 9600 to 115200 in your code.
Upvotes: 2