Correct form of RTS/CTS Flow Control with FTDI's D2XX Interface
I was going through the example code on FTDI's website. After opening the device and setting the flow control to be RTS/CTS they send and receive data in the following manner:
' Set RTS
FT_Status = FT_SetRts(FT_Handle)
If FT_Status <> FT_OK Then
Exit Sub
End If
' Set DTR
FT_Status = FT_SetDtr(FT_Handle)
If FT_Status <> FT_OK Then
Exit Sub
End If
' Write string data to device
FT_Status = FT_Write_String(FT_Handle, TextBox4.Text, Len(TextBox4.Text), BytesWritten)
If FT_Status <> FT_OK Then
Exit Sub
End If
Sleep(100)
' Get number of bytes waiting to be read
FT_Status = FT_GetQueueStatus(FT_Handle, FT_RxQ_Bytes)
If FT_Status <> FT_OK Then
Exit Sub
End If
' Read number of bytes waiting
TempStringData = Space(FT_RxQ_Bytes + 1)
FT_Status = FT_Read_String(FT_Handle, TempStringData, FT_RxQ_Bytes, BytesRead)
If FT_Status <&开发者_如何转开发gt; FT_OK Then
Exit Sub
End If
' Close device
FT_Status = FT_Close(FT_Handle)
If FT_Status <> FT_OK Then
Exit Sub
End If
Basically they set the RTS, DTR, then write data to the device.
Is this correct? If I was sending multiple times to the device, do I need to clear the RTS each time? and then set it high again? or do I just set it high each time I send new data? Why are they setting DTR if the flow control is defined as RTS/CTS?
As far as best practices go, is this the best way to read and write?
The correct way would be to always return the signals to their inactive state. If you do not have data to send immediately clear RTS. DTR is usually used to indicate the presence of terminal equipment on a link so it should always stay in the active state unless there is a failure on your side. I do not agree with the way they implemented their DTR. This also assumes that you are functioning as a terminal. Unless you can not handle the data if receiving and transmitting at the same time you can keep the CTS signal low so that you immediately will receive all data.
My actual practices would be to ignore the control signals and setup interrupts and/or DMA to do the transfers. Unless you are using a very limited CPU there would not be any problem.
The assertion of RTS and DTR have no affect on the communication between the PC and the FTDI chip, which is purely a USB transaction, they are instructions to the FTDI to assert the corresponding lines in the connection between the FTDI chip and the target micro-controller/processor.
They are therefore only relevant if they are implemented in your hardware design.
DTR (data terminal ready) is used in PSTN communication modems to indicate that the terminal (or PC in this case) is ready to communicate. It is also used to 'hang-up' a dial-up connection by de-asserting the line briefly. It is probably being asserted here for completeness in case your target implements DTR handshaking. If it does not, the signalling will be ignored so is benign.
精彩评论