qq_32191349
qq_32191349
采纳率0%
2019-05-27 10:35

新手求助!!c++串口通讯,缓冲区设置失败,是结构体指针设置错误吗?求指导。

//头文件
```class IDC_DLL_CLASS CComDevice
{
//串口类方法
public:
CComDevice();
virtual ~CComDevice();
//打开串口
int Open(char * , LPDCB);
//关闭串口
int Close();
//读串口收到的数据(一次)
int Read(char *, DWORD);
//读串口收到的数据(同步)
int WaitRead(char *, DWORD, DWORD, DWORD);
//向串口写数据(同步)
int Write(char *, DWORD, DWORD);

//串口类属性

public:
DWORD m_error_code;//最后一次操作错误码(windows定义)
DCB m_state;//串口配置
HANDLE m_handle; //串口句柄
COMMTIMEOUTS m_Timeouts;//读写超时时间
};


//串口打开
int CComDevice::Open(char * Port , LPDCB lpDCB)
{

    //操作错误码复位
    this->m_error_code = 0;
    //判断串口是否已经打开
    if (this->m_handle != NULL)
    {
        printf("串口打开失败");
        return -1;

    }
    //打开串口文件
    this->m_handle = ::CreateFileA(Port, GENERIC_READ | GENERIC_WRITE,
        0,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
        NULL);
        printf("打开串口文件");
        printf("\n");

    if (this->m_handle == INVALID_HANDLE_VALUE)
    {
        this->m_error_code = ::GetLastError();//获取错误码
        this->m_handle = NULL;//串口句柄置空
        printf("系统错误\n");
        return -2;

    }
    try
    {
        //设置串口读写缓冲区大小

        if (!SetupComm(this->m_handle, 2048, 2048))
            printf("\n");
            printf("设置串口读写缓冲区大小错误");  //配置失败
            throw(FALSE);

        //终止串口所有异步读写操作并清空读写缓冲区
        if (!PurgeComm(this->m_handle, PURGE_TXABORT | PURGE_RXABORT |
            PURGE_TXCLEAR | PURGE_RXCLEAR))
            printf("\n");
            printf("终止串口所有异步读写操作并清空读写缓冲区错误");
            throw(FALSE);

        //获取串口默认配置
        if (!GetCommState(this->m_handle, &this->m_state))
            printf("\n");
            printf("获取串口默认配置错误");
            throw(FALSE);

        this->m_state.DCBlength = sizeof(DCB);   //DCB长度
        this->m_state.BaudRate = lpDCB->BaudRate;  //波特率
        this->m_state.ByteSize = lpDCB->ByteSize;   //通信字节位数
        this->m_state.Parity = lpDCB->Parity;//奇偶校验位
        this->m_state.StopBits = lpDCB->StopBits;  //停止位

        this->m_state.fOutxDsrFlow = lpDCB->fOutxDsrFlow;
        this->m_state.fDtrControl = lpDCB->fDtrControl;

        this->m_state.fOutxCtsFlow = lpDCB->fOutxCtsFlow;
        this->m_state.fRtsControl = lpDCB->fRtsControl;

        this->m_state.fInX = lpDCB->fInX;
        this->m_state.fOutX = lpDCB->fInX;
        this->m_state.XonChar = lpDCB->XonChar;
        this->m_state.XoffChar = lpDCB->XoffChar;
        this->m_state.XonLim = lpDCB->XonLim;//指定在XON字符发送之前接收缓冲区中空缓冲区可允许的最小字节数
        this->m_state.XoffLim = lpDCB->XoffLim;//指定在XOFF字符发送之前接收缓冲区中空缓冲区可允许的最小字节数

        this->m_state.fBinary = lpDCB->fBinary;
        this->m_state.fParity = lpDCB->fParity;

        //设置串口制订配置
        if (!SetCommState(this->m_handle, &(this->m_state)))
            printf("\n");
            printf("配置错误");
            throw(FALSE);
    }
    catch (BOOL)
    {
        this->m_error_code = ::GetLastError();//获取错误码
        ::CloseHandle(this->m_handle);//关闭串口    
        this->m_handle = NULL;//串口句柄置空  
        printf("\n串口错误");    //------>程序运行到这里卡住
        return -1;

    }
    printf("打开串口成功");
    return 0;

}

//main函数打开串口
int _tmain(int argc, _TCHAR* argv[])
{
CComDevice CardReader;
DCB DCB_COM;
DCB_COM.DCBlength = 50;
DCB_COM.BaudRate = CBR_9600; // 设置波特率9600
DCB_COM.fBinary = TRUE; // 设置二进制模式,此处必须设置TRUE
DCB_COM.fParity = TRUE; // 支持奇偶校验
DCB_COM.fOutxCtsFlow = FALSE; // No CTS output flow control
DCB_COM.fOutxDsrFlow = FALSE; // No DSR output flow control
DCB_COM.fDtrControl = DTR_CONTROL_DISABLE; // No DTR flow control
DCB_COM.XonChar = 0;
DCB_COM.XoffChar = 0;
DCB_COM.XonLim = 100;
DCB_COM.XoffLim = 100;
//DCB_COM.fDsrSensitivity = FALSE; // DSR sensitivity
//DCB_COM.fTXContinueOnXoff = TRUE; // XOFF continues Tx
DCB_COM.fOutX = FALSE; // No XON/XOFF out flow control
DCB_COM.fInX = FALSE; // No XON/XOFF in flow control
//DCB_COM.fErrorChar = FALSE; // Disable error replacement
//DCB_COM.fNull = FALSE; // Disable null stripping
DCB_COM.fRtsControl = RTS_CONTROL_DISABLE; //No RTS flow control
//DCB_COM.fAbortOnError = FALSE; // 当串口发生错误,并不终止串口读写
DCB_COM.ByteSize = 8; // 数据位,范围:4-8
DCB_COM.Parity = NOPARITY; // 校验模式
DCB_COM.StopBits = 0; // 1位停止位
int a = CardReader.Open("COM5", &DCB_COM);
cout << "\na:" << a << endl;
system("pause");
return 0;
}
`

![图片说明](https://img-ask.csdn.net/upload/201905/27/1558924537_857227.png)
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • VisualEleven Eleven 2年前

    SetupComm调用失败,看看GetLastError()的错误码是什么不就清楚了~

    点赞 评论 复制链接分享

为你推荐