C#的SerialPort设置

C#的SerialPort设置

本文介绍了C#的SerialPort设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用了的SerialPort 类设置的COM端口的麻烦。有许多的设置,都将置不同于我需要他们时,我称之为的SerialPort 的构造。使用我注意到下面的端口监视器的应用程序:

I'm having trouble in setting up the COM port using the SerialPort class. There are a number of settings that get set differently than I need them to be when I call the SerialPort constructor. Using a port monitor application I have noticed the following:

  1. ReadIntervalTimeout = -1(应0)
  2. ReadTotalTimeoutMultiplier = -1(应为0)
  3. ReadTotalTimeoutConstant = 1000(应为0)
  4. WriteTotalTimeoutMultiplier = 0(OK)
  5. WriteTotalTimeoutConstant = 0(OK)
  6. EoFChar = 26(应为0)
  7. ErrorChar = 63(应为0)
  8. 在休息字符= 63(应为0)
  9. XON限额= 1024(应该是2048)
  10. 在XOFF极限= 1024(应为512)
  11. FlowReplace = 87H的(应为0)
  1. ReadIntervalTimeout = -1 (should be0)
  2. ReadTotalTimeoutMultiplier = -1(should be 0)
  3. ReadTotalTimeoutConstant = 1000(should be 0)
  4. WriteTotalTimeoutMultiplier = 0 (OK)
  5. WriteTotalTimeoutConstant = 0 (OK)
  6. EoFChar = 26 (should be 0)
  7. ErrorChar = 63 (should be 0)
  8. Break char = 63 (should be 0)
  9. XOn Limit = 1024 (should be 2048)
  10. XOff Limit = 1024 (should be 512)
  11. FlowReplace = 0x87 (should be 0)

如何更改这些设置(我使用C#)?

How do I change these settings (I'm using C#)?

推荐答案

这些值不能设置用的SerialPort包装类。它是一个不完整的包装。你可以看看此处用于改变XON和XOFF字符的一个例子。对于剩下的你就必须参阅 DCB文档

Those values cannot be set with the SerialPort wrapper class. It is an incomplete wrapper. You can look here for an example of changing the XON and XOFF characters. For the rest you'll have to refer to the DCB documentation.

更新

在DCB标志值在DCB结构中位字段。

The DCB flag values are bit fields in the DCB structure.

DWORD fBinary  :1;            // Bit 0
DWORD fParity  :1;            // Bit 1
DWORD fOutxCtsFlow  :1;       // Bit 2
DWORD fOutxDsrFlow  :1;       // Bit 3
DWORD fDtrControl  :2;        // Bit 4 - 5
DWORD fDsrSensitivity  :1;    // Bit 6
DWORD fTXContinueOnXoff  :1;  // Bit 7
DWORD fOutX  :1;              // Bit 8
DWORD fInX  :1;               // Bit 9
DWORD fErrorChar  :1;         // Bit 10
DWORD fNull  :1;              // Bit 11
DWORD fRtsControl  :2;        // Bit 12 - 13
DWORD fAbortOnError  :1;      // Bit 14
DWORD fDummy2  :17;           // Bit 15 - 31

而如果你想知道SetDcbFlag如何计算出该位你操纵,这里是(礼貌反射的):

And in case you were wondering how SetDcbFlag figures out which bits you're manipulating, here it is (courtesy of Reflector):

internal void SetDcbFlag(int whichFlag, int setting)
{
    uint mask;
    if ((whichFlag == 4) || (whichFlag == 12))
        mask = 3;
    else if (whichFlag == 15)
        mask = 0x1ffff;
    else
        mask = 1;
    this.dcb.Flags &= ~(mask << whichFlag);
    this.dcb.Flags |= ((uint)setting << whichFlag);
}

您可以看到,对于位#4或#12它采用的是2位掩码,为位#15 17位掩码,并为其余的1位掩码。

You can see that for Bit #4 or #12 it uses a 2-bit mask, for Bit #15 a 17-bit mask, and for the rest a 1-bit mask.

这篇关于C#的SerialPort设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:56