Click to See Complete Forum and Search --> : A problem about wrtie to the serial port


tjuzhangrui
October 2nd, 2005, 03:21 AM
When I send out 32bytes from Rs232 serial port on PC, using WinAPI WriteFile(). How long the interval of time will be between two bytes?
I am from China, So I am not 100% fluent, and maybe I should explain my problem more clearly as below pic.

olivthill
October 2nd, 2005, 06:02 PM
Intel processors have two instructions for sending data to ports:

1. OUT for a byte, a word, or a double word
2. OUTS for a string of bytes, words, or double words.

I don't know know which one is used by WriteFile.
Anyway, microprocessor ports are not the same things as ouput ports of UARTs.

The microprocessor have an I/O space, similar to a memory space. When the OUT instruction is executed for a byte, a word, or a double word, then all the bits of the unit are supposed to arrive exactly on the same time at the destination port, in the same way as they would arrive simultaneously at a memory address.

Then, when the bits (8, 16, or 32 of them) are available at a microprocessor's port, a UART (another microchip) fetches them and sends them to the outside world, one bit a time, and usually grouped by packets of 1+8+1 bits.

The UART has its own internal clock. It is not running at the same speed as the CPU clock. You probably already know that the interval of time between the sending of two bits (not two bytes) is called the baud rate. The interval between the sending of two packets of bits cannot be less than a UART clock tick. The interval can be more important. In the case of your question, when 32 bytes are sent, they will be transformed into 32 packets, and the interval of time between each packet could be equal to the interval of time between two consecutive bits in the same packets, because the CPU clock is ticking at a quicker rate than the UART clock.

I said the interval between two bytes could be equal to the baud rate, but this is rarely the case, because of the flow control. There are two types of flow controls: hardware with RTS/CTS (or sometimes DTR/DSR), and software with XON/XOFF. The goal of the flow control is to avoid overflowing the buffers with incoming data, so they are often forced to slow down the transmission rate.

tjuzhangrui
October 2nd, 2005, 11:07 PM
Thank you for the help!
In fact the interval of time between the sending of two bytes is more important for me. I need to know the interval so I can program the mcu with UART to save and clear the buffer within this time.
I look up the MSDN library,but can't find this information. Can I have the advice about where go for it, or maybe I should detect it by Hardware?

Boris K K
October 4th, 2005, 06:28 AM
You can use the SetCommState API function (alone or combined with BuildCommDCB) to specify the baud rate, number of data bits, parity bit, stop bits, hardware (RTS/CTS) or software flow control etc.

I am not sure about the low level details (timings of different electrical lines) but once you know the parameters such as baud rate and data mode, it should be possible to derive timings from the standard. If the receiver acknowledges the bytes as soon as they are received, they will be sent without a delay. If you need time to process each byte on the receiveing side, use RTS/CTS flow control.