C#的SerialPort设置SerialPort

由网友(丶神经质先生\")分享简介:我在使用了的SerialPort 类设置的COM端口的麻烦。有许多的设置,都将置不同于我需要他们时,我称之为的SerialPort 的构造。使用我注意到下面的端口监视器的应用程序:I'm having trouble in setting up the COM port using the SerialPort cl...

我在使用了的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:

ReadIntervalTimeout = -1(应 0) ReadTotalTimeoutMultiplier = -1 (应为0) ReadTotalTimeoutConstant = 1000 (应为0) WriteTotalTimeoutMultiplier = 0(OK) WriteTotalTimeoutConstant = 0(OK) EoFChar = 26(应为0) ErrorChar = 63(应为0) 在休息字符= 63(应为0) XON限额= 1024(应该是2048) 在XOFF极限= 1024(应为512) FlowReplace = 87H的(应为0) ReadIntervalTimeout = -1 (should be 0) ReadTotalTimeoutMultiplier = -1 (should be 0) ReadTotalTimeoutConstant = 1000 (should be 0) WriteTotalTimeoutMultiplier = 0 (OK) WriteTotalTimeoutConstant = 0 (OK) EoFChar = 26 (should be 0) ErrorChar = 63 (should be 0) Break char = 63 (should be 0) XOn Limit = 1024 (should be 2048) XOff Limit = 1024 (should be 512) 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.

阅读全文

相关推荐

最新文章