0

我有这段代码可以正常工作:

' Declare the local variables that you will use in the code.
Dim hSerialPort, hParallelPort As IntPtr
Dim Success As Boolean
Dim MyDCB As DCB
Dim MyCommTimeouts As COMMTIMEOUTS
Dim BytesWritten, BytesRead As Int32
Dim Buffer() As Byte


        hSerialPort = CreateFile("COM1", GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)

' Retrieve the current control settings.
     Success = GetCommState(hSerialPort, MyDCB)

     MyDCB.BaudRate = 9600
     MyDCB.ByteSize = 8
     MyDCB.Parity = NOPARITY
     MyDCB.StopBits = ONESTOPBIT

' Reconfigure COM1 based on the properties of the modified DCB structure.
     Success = SetCommState(hSerialPort, MyDCB)

     '     

我需要设置硬件控制通量

我该怎么做?

谢谢 !!

4

2 回答 2

0

我假设您指的是基于 RTS/CTS 的流量控制。为此有几个 DCB 参数,fOutxCtsFlow、fOutxDsrFlow、fRtsControl、fDtrControl,以及基于软件的流控制 fInX、fOutX。

只有所有这些设置的正确组合才能按预期工作。在我们的软件中,我们有四个不同的选项,这就是我们的代码设置相关 DCB 值的方式

通常我们设置(在 c++ 中)

   DCBptr->fOutxDsrFlow = 0;
   DCBptr->fDtrControl = DTR_CONTROL_ENABLE;

然后我们决定是否以及使用什么流控制:

 // Flow control settings
  switch(flowControl)
   {case 1:  // HW flow control
      DCBptr->fOutxCtsFlow = 1;
      DCBptr->fRtsControl = RTS_CONTROL_HANDSHAKE;
      DCBptr->fInX = 0;
      DCBptr->fOutX = 0;
      break;
    case 2: // SW (XON/XOFF) flow control
      DCBptr->fOutxCtsFlow = 0;
      DCBptr->fRtsControl = RTS_CONTROL_DISABLE;
      DCBptr->fInX = 1;
      DCBptr->fOutX = 1;
      break;
    case 3: // RTS On Send (RS485 Transceiver Mode) 
      DCBptr->fOutxCtsFlow = 0;
      DCBptr->fRtsControl = RTS_CONTROL_TOGGLE;
      DCBptr->fInX = 0;
      DCBptr->fOutX = 0;
      break;
    default: // no flow control
      DCBptr->fOutxCtsFlow = 0;
      DCBptr->fRtsControl = RTS_CONTROL_DISABLE;
      DCBptr->fInX = 0;
      DCBptr->fOutX = 0;
      break;
   }

希望这会有所帮助,奥利弗

于 2013-09-17T07:45:35.343 回答
0

您可以通过 SerialPort.Handshake 属性设置流量控制;您还可以使用属性 DTREnable、RTSEnable、DSRHolding 和 CTSHolding 直接访问硬件。

于 2013-09-10T22:29:43.833 回答