RS232通过API设置硬件控制通量

时间:2013-09-10 19:39:55

标签: c# .net vb.net vb6 serial-port

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

' 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)

     '     

我需要设置硬件控制通量

我怎么能这样做?

谢谢!

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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;
   }

希望这有帮助, 奥利弗

相关问题