⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 win_qextserialport.cpp

📁 linux qt环境下的串口多线程功能的实现
💻 CPP
📖 第 1 页 / 共 3 页
字号:
        }        else {            retVal=((int)Win_BytesRead);        }    }    UNLOCK_MUTEX();    return retVal;}/*!\fn Q_LONG Win_QextSerialPort::writeBlock(const char *data, uint len)Writes a block of data to the serial port.  This function will write len bytes from the buffer pointed to by data to the serial port.  Return value is the number of bytes actually written, or -1 on error.  This function will have no effect if the serial port associated with the class is not currently open.*/Q_LONG Win_QextSerialPort::writeBlock(const char *data, #ifdef QTVER_PRE_30                                      uint len) #else                                      unsigned long len)#endif{    LOCK_MUTEX();    int retVal=0;    if (portOpen) {        DWORD Win_BytesWritten;        if (!WriteFile(Win_Handle, (void*)data, (DWORD)len, &Win_BytesWritten, NULL)) {            lastErr=E_WRITE_FAILED;            retVal=-1;        }        else {            retVal=((int)Win_BytesWritten);        }    }    UNLOCK_MUTEX();    flush();    return retVal;}/*!\fn int Win_QextSerialPort::getch()Returns a single character from the serial port, or -1 on error.  This function has no effect ifthe serial port associated with the class is not currently open.*/int Win_QextSerialPort::getch() {    LOCK_MUTEX();    int retVal=-1;    if (portOpen) {        int readChar=-1;        COMSTAT Win_ComStat;        DWORD Win_BytesRead=0;        DWORD Win_ErrorMask=0;        ClearCommError(Win_Handle, &Win_ErrorMask, &Win_ComStat);        if (Win_ComStat.cbInQue) {            if (!ReadFile(Win_Handle, (void*)&readChar, 1, &Win_BytesRead, NULL)                 || Win_BytesRead==0) {                retVal=-1;                lastErr=E_READ_FAILED;            }            else {                /*Windows returns (unsigned short)-1 when it means 0xFF for some reason*/                if (readChar==-1) {                    readChar=0xFF;                }                retVal=readChar;            }        }    }        UNLOCK_MUTEX();    return retVal;}    /*!\fn int Win_QextSerialPort::putch(int ch)Writes a single character to the serial port.  Return value is the byte written, or -1 on error.  This function has no effect if the serial port associated with the class is not currently open.*/int Win_QextSerialPort::putch(int ch) {    LOCK_MUTEX();    int retVal=0;    if (portOpen) {        DWORD Win_BytesWritten;        if (!WriteFile(Win_Handle, (void*)&ch, 1, &Win_BytesWritten, NULL)) {            retVal=-1;            lastErr=E_WRITE_FAILED;        }        else {            retVal=ch;        }    }    UNLOCK_MUTEX();    flush();    return retVal;}/*!\fn int Win_QextSerialPort::ungetch(int)This function is included to implement the full QIODevice interface, and currently has no purpose within this class.  This function is meaningless on an unbuffered device and currentlyonly prints a warning message to that effect.*/int Win_QextSerialPort::ungetch(int) {    /*meaningless on unbuffered sequential device - return error and print a warning*/    TTY_WARNING("Win_QextSerialPort: ungetch() called on an unbuffered sequential device - operation is meaningless");    return -1;}/*!\fn void Win_QextSerialPort::setFlowControl(FlowType flow)Sets the flow control used by the port.  Possible values of flow are:\verbatim    FLOW_OFF            No flow control    FLOW_HARDWARE       Hardware (RTS/CTS) flow control    FLOW_XONXOFF        Software (XON/XOFF) flow control\endverbatim*/void Win_QextSerialPort::setFlowControl(FlowType flow) {    LOCK_MUTEX();    if (Settings.FlowControl!=flow) {        Settings.FlowControl=flow;    }    if (portOpen) {        switch(flow) {            /*no flow control*/            case FLOW_OFF:                Win_CommConfig.dcb.fOutxCtsFlow=FALSE;                Win_CommConfig.dcb.fRtsControl=RTS_CONTROL_DISABLE;                Win_CommConfig.dcb.fInX=FALSE;                Win_CommConfig.dcb.fOutX=FALSE;                SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));                break;            /*software (XON/XOFF) flow control*/            case FLOW_XONXOFF:                Win_CommConfig.dcb.fOutxCtsFlow=FALSE;                Win_CommConfig.dcb.fRtsControl=RTS_CONTROL_DISABLE;                Win_CommConfig.dcb.fInX=TRUE;                Win_CommConfig.dcb.fOutX=TRUE;                SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));                break;            case FLOW_HARDWARE:                Win_CommConfig.dcb.fOutxCtsFlow=TRUE;                Win_CommConfig.dcb.fRtsControl=RTS_CONTROL_HANDSHAKE;                Win_CommConfig.dcb.fInX=FALSE;                Win_CommConfig.dcb.fOutX=FALSE;                SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));                break;        }    }    UNLOCK_MUTEX();}/*!\fn void Win_QextSerialPort::setParity(ParityType parity)Sets the parity associated with the serial port.  The possible values of parity are:\verbatim    PAR_SPACE       Space Parity    PAR_MARK        Mark Parity    PAR_NONE        No Parity    PAR_EVEN        Even Parity    PAR_ODD         Odd Parity\endverbatim*/void Win_QextSerialPort::setParity(ParityType parity) {    LOCK_MUTEX();    if (Settings.Parity!=parity) {        Settings.Parity=parity;    }    if (portOpen) {        Win_CommConfig.dcb.Parity=(unsigned char)parity;        switch (parity) {            /*space parity*/            case PAR_SPACE:                if (Settings.DataBits==DATA_8) {                    TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning: Space parity with 8 data bits is not supported by POSIX systems.");                }                Win_CommConfig.dcb.fParity=TRUE;                break;            /*mark parity - WINDOWS ONLY*/            case PAR_MARK:                TTY_PORTABILITY_WARNING("Win_QextSerialPort Portability Warning:  Mark parity is not supported by POSIX systems");                Win_CommConfig.dcb.fParity=TRUE;                break;                    /*no parity*/            case PAR_NONE:                Win_CommConfig.dcb.fParity=FALSE;                break;            /*even parity*/            case PAR_EVEN:                Win_CommConfig.dcb.fParity=TRUE;                break;            /*odd parity*/            case PAR_ODD:                Win_CommConfig.dcb.fParity=TRUE;                break;        }        SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));    }    UNLOCK_MUTEX();}    /*!\fn void Win_QextSerialPort::setDataBits(DataBitsType dataBits)Sets the number of data bits used by the serial port.  Possible values of dataBits are:\verbatim    DATA_5      5 data bits    DATA_6      6 data bits    DATA_7      7 data bits    DATA_8      8 data bits\endverbatim\note This function is subject to the following restrictions:\par    5 data bits cannot be used with 2 stop bits.\par    1.5 stop bits can only be used with 5 data bits.\par    8 data bits cannot be used with space parity on POSIX systems.*/void Win_QextSerialPort::setDataBits(DataBitsType dataBits) {    LOCK_MUTEX();    if (Settings.DataBits!=dataBits) {        if ((Settings.StopBits==STOP_2 && dataBits==DATA_5) ||             (Settings.StopBits==STOP_1_5 && dataBits!=DATA_5)) {        }        else {            Settings.DataBits=dataBits;        }    }    if (portOpen) {        switch(dataBits) {            /*5 data bits*/            case DATA_5:                if (Settings.StopBits==STOP_2) {                    TTY_WARNING("Win_QextSerialPort: 5 Data bits cannot be used with 2 stop bits.");                }                else {                    Win_CommConfig.dcb.ByteSize=5;                    SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));                }                break;            /*6 data bits*/            case DATA_6:                if (Settings.StopBits==STOP_1_5) {                    TTY_WARNING("Win_QextSerialPort: 6 Data bits cannot be used with 1.5 stop bits.");                }                else {                    Win_CommConfig.dcb.ByteSize=6;                    SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));                }                break;            /*7 data bits*/            case DATA_7:                if (Settings.StopBits==STOP_1_5) {                    TTY_WARNING("Win_QextSerialPort: 7 Data bits cannot be used with 1.5 stop bits.");                }                else {                    Win_CommConfig.dcb.ByteSize=7;                    SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));                }                break;            /*8 data bits*/            case DATA_8:                if (Settings.StopBits==STOP_1_5) {                    TTY_WARNING("Win_QextSerialPort: 8 Data bits cannot be used with 1.5 stop bits.");                }                else {                    Win_CommConfig.dcb.ByteSize=8;                    SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));                }                break;        }    }    UNLOCK_MUTEX();}/*!\fn void Win_QextSerialPort::setStopBits(StopBitsType stopBits)Sets the number of stop bits used by the serial port.  Possible values of stopBits are:\verbatim    STOP_1      1 stop bit    STOP_1_5    1.5 stop bits    STOP_2      2 stop bits\endverbatim\note This function is subject to the following restrictions:\par    2 stop bits cannot be used with 5 data bits.\par    1.5 stop bits cannot be used with 6 or more data bits.\par    POSIX does not support 1.5 stop bits.*/void Win_QextSerialPort::setStopBits(StopBitsType stopBits) {    LOCK_MUTEX();    if (Settings.StopBits!=stopBits) {        if ((Settings.DataBits==DATA_5 && stopBits==STOP_2) ||             (stopBits==STOP_1_5 && Settings.DataBits!=DATA_5)) {        }        else {            Settings.StopBits=stopBits;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -