Configuring a Serial Port

Configuring a Serial Port
Windows CE 5.0
Send Feedback

The most critical phase in serial communications programming is configuring the port settings with the DCB structure.

Erroneously initializing the DCB structure is a common problem. When a serial communications function does not produce the expected results, the DCB structure might be in err.

A call to the CreateFile function opens a serial port with default port settings. Usually, the application needs to change the defaults. Use the GetCommState function to retrieve the default settings and use the SetCommState function to make port settings.

Also, port configuration involves using the COMMTIMEOUTS structure to set the timeout values for read/write operations. When a timeout occurs, the ReadFile or WriteFile function returns the specific number of characters successfully transferred.

To configure a serial port

  1. Initialize the DCBlength member of the DCB structure to the size of the structure.

    This is required before passing the member as a variable to a function.

  2. Call the GetCommState function to retrieve the default settings for the port opened with the CreateFile function.

    To identify the port, specify in the hPort parameter the handle that CreateFile returns.

  3. Modify DCB members as required.

    The following are DCB structure members.

    MemberDescription
    DCBlengthBefore calling the GetCommState function, set this member to the length of the DCB structure.

    Neglecting to do this can cause a failure or return erroneous data.

    BaudRateSpecifies the device communication rate.

    Assigns an actual baud rate or an index by specifying a CBR_ constant.

    fBinarySpecifies if binary mode is enabled.

    The Microsoft® Win32® API does not support nonbinary mode transfers, so this member must be TRUE.

    Using FALSE does not work.

    fParitySpecifies if parity checking is enabled.

    If this member is TRUE, parity checking is performed and errors are reported

    fOutxCtsFlowTurns the CTS flow control on and off.

    To use RTS/CTS flow control, specify TRUE for this member and RTS_CONTROL_HANDSHAKE for the fRtsControl member.

    fOutxDsrFlowTurns the DSR flow control on and off.

    DSR flow control is rarely used.

    A typical port configuration is to set this member to FALSE, while enabling the DTR line.

    fDtrControlSpecifies the DTR flow control.

    DTR_CONTROL_ENABLE turns on the DTR line during the connection.

    DTR_CONTROL_HANDSHAKE turns on DTR handshaking.

    DTR_CONTROL_DISABLE turns off the DTR line.

    fDsrSensitivitySpecifies if the communications driver is sensitive to the state of the DSR signal.

    If this member is TRUE, the driver ignores any bytes received, unless the DSR modem input line is high.

    fTXContinueOnXoffSpecifies if transmission stops when the input buffer is full and the driver has transmitted the XoffChar character.

    If this member is TRUE, transmission continues after the input buffer has come within XoffLim bytes of being full and the driver has transmitted the XoffChar character to stop receiving bytes.

    If this member is FALSE, transmission does not continue until the input buffer is within XonLim bytes of being empty and the driver has transmitted the XonChar character to resume reception.

    fOutXSpecifies if XON/XOFF flow control is used during transmission.

    If this member is TRUE, transmission stops when the XoffChar character is received and starts again when the XonChar character is received.

    fInXSpecifies if XON/XOFF flow control is used during reception.

    If this member is TRUE, the XoffChar character is sent when the input buffer comes within XoffLim bytes of being full, and the XonChar character is sent when the input buffer comes within XonLim bytes of being empty.

    fErrorCharSpecifies if bytes received with parity errors are replaced with the character specified by the ErrorChar member.

    If this member is TRUE and the fParity member is TRUE, replacement occurs.

    fNullSpecifies if null bytes are discarded.

    If this member is TRUE, null bytes are discarded when received.

    fRtsControlTurns the RTS flow control on and off.

    RTS_CONTROL_ENABLE turns on the RTS line during the connection.

    RTS_CONTROL_HANDSHAKE turns on RTS handshaking.

    RTS_CONTROL_DISABLE turns off the RTS line.

    RTS_CONTROL_TOGGLE Specifies that the RTS line is high if bytes are available for transmission.

    After all buffered bytes have been sent, the RTS line is low.

    fAbortOnErrorSpecifies if read and write operations are terminated when an error occurs.

    If this member is TRUE, the driver terminates read and write operations with an error status when an error occurs.

    The driver does not accept further communications operations until the application has acknowledged the error by calling the ClearCommError function.

    fDummy2Reserved; do not use.
    wReservedNot used; set to zero.
    XonLimSpecifies the maximum number of bytes allowed in the queue.

    The XON character is sent if the number of bytes in the queue falls below this number.

    XoffLimSpecifies the maximum number of bytes accepted in the input buffer before the XOFF character is sent.

    The maximum number of bytes accepted is calculated by subtracting this value from the size, in bytes, of the input buffer.

    ByteSizeSpecifies the bits per byte transmitted and received.
    ParitySpecifies the parity scheme.

    Do not confuse this member with the fParity member, which turns parity on or off.

    Because parity is rarely used, this member can usually be NOPARITY.

    StopBitsSpecifies the number of stop bits.

    ONESTOPBIT is the most common setting.

    XonCharSpecifies the value of the XON character for both transmission and reception.
    XoffCharSpecifies the value of the XOFF character for both transmission and reception.
    ErrorCharSpecifies the value of the character used to replace bytes received with a parity error.
    EofCharSpecifies the value of the character used to signal the end of data.
    EvtCharSpecifies the value of the character used to signal an event.
    WReserved1Reserved, do not use.
  4. Call the SetCommState function to set the port settings.

The following code example shows how to use the GetCommState and SetCommState functions to configure a serial port.

DCB PortDCB;

// Initialize the DCBlength member. 
PortDCB.DCBlength = sizeof (DCB); 

// Get the default port setting information.
GetCommState (hPort, &PortDCB);

// Change the DCB structure settings.
PortDCB.BaudRate = 9600;              // Current baud 
PortDCB.fBinary = TRUE;               // Binary mode; no EOF check 
PortDCB.fParity = TRUE;               // Enable parity checking 
PortDCB.fOutxCtsFlow = FALSE;         // No CTS output flow control 
PortDCB.fOutxDsrFlow = FALSE;         // No DSR output flow control 
PortDCB.fDtrControl = DTR_CONTROL_ENABLE; 
                                      // DTR flow control type 
PortDCB.fDsrSensitivity = FALSE;      // DSR sensitivity 
PortDCB.fTXContinueOnXoff = TRUE;     // XOFF continues Tx 
PortDCB.fOutX = FALSE;                // No XON/XOFF out flow control 
PortDCB.fInX = FALSE;                 // No XON/XOFF in flow control 
PortDCB.fErrorChar = FALSE;           // Disable error replacement 
PortDCB.fNull = FALSE;                // Disable null stripping 
PortDCB.fRtsControl = RTS_CONTROL_ENABLE; 
                                      // RTS flow control 
PortDCB.fAbortOnError = FALSE;        // Do not abort reads/writes on 
                                      // error
PortDCB.ByteSize = 8;                 // Number of bits/byte, 4-8 
PortDCB.Parity = NOPARITY;            // 0-4=no,odd,even,mark,space 
PortDCB.StopBits = ONESTOPBIT;        // 0,1,2 = 1, 1.5, 2 

// Configure the port according to the specifications of the DCB 
// structure.
if (!SetCommState (hPort, &PortDCB))
{
  // Could not configure the serial port.
  dwError = GetLastError ();
  MessageBox (hMainWnd, TEXT("Unable to configure the serial port"), 
              TEXT("Error"), MB_OK);
  return FALSE;
}
See Also

Programming Serial Connections


Send Feedback on this topic to the authors

Feedback FAQs


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 很抱歉,您遇到了这个问题。这个错误提示通常是在安装或配置软件时出现的。这可能是由于许多不同的原因造成的,例如配置文件的错误设置、缺少依赖项、权限问题等。 要解决此问题,您可以尝试以下几种方法: 1. 检查您的配置文件是否正确设置,并确保您已经安装了所需的依赖项。 2. 确保您有足够的权限来访问所需的文件和目录。您可以尝试使用sudo命令来运行程序,以便获得管理员权限。 3. 查看程序的日志文件以了解更多信息。日志文件通常包含有关错误原因的详细信息,帮助您更好地诊断问题。 4. 如果您仍然无法解决问题,请尝试在相关的技术论坛或社区中寻求帮助。您可能会得到更多的建议和指导。 希望这些方法能够帮助您解决问题。如果您需要进一步的帮助,请告诉我更多细节,我会尽力回答您的问题。 ### 回答2: "A problem occurred configuring" 是一个英语短语,指的是在配置过程中出现了问题。它通常用于描述在软件安装、网络设置或设备连接等过程中遇到的困难或错误。 这个问题的具体原因可能有很多种。可能是由于错误的输入、配置文件的损坏、网络连接的故障、设备驱动程序的不兼容性或其他未知的技术问题。 解决这个问题的方法取决于具体的情况。首先,我们应该检查输入是否准确且符合要求,确保所有的配置选项都正确填写。如果是配置文件损坏,在备份的前提下,我们可以尝试使用备份文件进行修复。如果是网络连接问题,我们可以检查线缆连接、路由器设置或尝试重新启动网络设备。如果是设备驱动程序不兼容性,我们可以尝试更新或更换驱动程序。 此外,查找错误信息和日志文件也是解决问题的关键。这些信息通常会提供有关具体问题的详细信息,从而指导我们采取正确的行动。我们可以在互联网上搜索类似的问题和解决方案,或者寻求专业人士的帮助。 总之,在遇到配置问题时,我们应该耐心且仔细地诊断问题,并采取适当的解决方案。通过正确的操作和合理的解决方案,我们通常能够成功解决配置问题并继续进行正常的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值