qq_24686723
白发22
采纳率0%
2017-03-04 03:12 阅读 1.9k

C++ 串口通讯,为何我数据老是发送错误

 void writechar(char buffer[1024])
{
    COMSTAT cs;
    WaitForSingleObject(hMutex, INFINITE);
    while(1)
    {
    DWORD dwWrittenLen = 0; 
    ClearCommError(hcom,&dwError,&cs);//清除错误


    if(!WriteFile(hcom,buffer,100,&dwWrittenLen,NULL))  
    {  
        printf( "发送数据失败!\n");

    }  

    else
    { printf("往串口发送数据成功!\n" );
    break;
    }

    }

    //CloseHandle(hcom);
};

DWORD WINAPI Write(LPVOID lpParamter)
{
    //读取串口设置
    LPTSTR lpPath =  new char[MAX_PATH];

    int COM4BaudRate;

    int COM4Parity; 

    int COM4ByteSize;

    int COM4StopBits;

    int COM5BaudRate;

    int COM5Parity; 

    int COM5ByteSize;

    int COM5StopBits;

    int COM6BaudRate;

    int COM6Parity; 

    int COM6ByteSize;

    int COM6StopBits;

    int COM7BaudRate;

    int COM7Parity; 

    int COM7ByteSize;

    int COM7StopBits;

    strcpy_s(lpPath, MAX_PATH ,"..\\PortSet.ini");

    COM4BaudRate = GetPrivateProfileInt("COM4", "BaudRate", 0, lpPath);
    COM4Parity = GetPrivateProfileInt("COM4", "Parity", 0, lpPath);
    COM4ByteSize = GetPrivateProfileInt("COM4", "ByteSize", 0, lpPath);
    COM4StopBits = GetPrivateProfileInt("COM4", "StopBits", 0, lpPath);
    COM5BaudRate = GetPrivateProfileInt("COM5", "BaudRate", 0, lpPath);
    COM5Parity = GetPrivateProfileInt("COM5", "Parity", 0, lpPath);
    COM5ByteSize = GetPrivateProfileInt("COM5", "ByteSize", 0, lpPath);
    COM5StopBits = GetPrivateProfileInt("COM5", "StopBits", 0, lpPath);
    COM6BaudRate = GetPrivateProfileInt("COM6", "BaudRate", 0, lpPath);
    COM6Parity = GetPrivateProfileInt("COM6", "Parity", 0, lpPath);
    COM6ByteSize = GetPrivateProfileInt("COM6", "ByteSize", 0, lpPath);
    COM6StopBits = GetPrivateProfileInt("COM6", "StopBits", 0, lpPath);
    COM7BaudRate = GetPrivateProfileInt("COM7", "BaudRate", 0, lpPath);
    COM7Parity = GetPrivateProfileInt("COM7", "Parity", 0, lpPath);
    COM7ByteSize = GetPrivateProfileInt("COM7", "ByteSize", 0, lpPath);
    COM7StopBits = GetPrivateProfileInt("COM7", "StopBits", 0, lpPath);


    delete [] lpPath;

    hcom = CreateFile("COM7",
                       GENERIC_READ | GENERIC_WRITE,
                       0,
                       NULL,
                       OPEN_EXISTING,
                       FILE_ATTRIBUTE_NORMAL,
                       NULL);  
    if (hcom == INVALID_HANDLE_VALUE)  
    {  
        dwError=GetLastError();//处理错误 
        fprintf(stderr, "打开串口失败!\n");  
        exit(0);  
    }


    COMMTIMEOUTS timeouts;
    GetCommTimeouts(hcom, &timeouts);
    timeouts.ReadIntervalTimeout = 0;
    timeouts.ReadTotalTimeoutMultiplier = 0;
    timeouts.ReadTotalTimeoutConstant = 0;
    timeouts.WriteTotalTimeoutMultiplier = 0;
    timeouts.WriteTotalTimeoutConstant = 0;
    if (!SetCommTimeouts(hcom, &timeouts))
    {
        printf("超时设置失败!重新");
        CloseHandle(hcom);
        return 0;
    }


    SetupComm(hcom,1024,1024);  
    DCB dcb;  
    GetCommState(hcom,&dcb);  
    dcb.BaudRate = COM7BaudRate;  //传输速率
    dcb.ByteSize = COM7ByteSize;  //每个字符8位
    dcb.Parity = COM7Parity;  //无校验
    dcb.StopBits = COM7StopBits;  //一个停止位 
    SetCommState(hcom,&dcb);
    if (!SetCommState(hcom, &dcb))    //设置失败的情况
    {
        printf("端口状态设置失败!重新");
        CloseHandle(hcom);
        return 0;
    }

    return 0;
};


这个是成功的;

bool Modbus::openport(char *portname)
{
    Rcom=CreateFile(portname,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0);
    if(Rcom==INVALID_HANDLE_VALUE)
        return FALSE;
    else
        return TRUE;

}

bool Modbus::setupdcb(int com)      //用switch case
{
            //读取串口设置
    LPTSTR lpPath =  new char[MAX_PATH];

    int COM4BaudRate;
    int COM4Parity; 
    int COM4ByteSize;
    int COM4StopBits;

    int COM5BaudRate;
    int COM5Parity; 
    int COM5ByteSize;
    int COM5StopBits;

    int COM6BaudRate;
    int COM6Parity;  
    int COM6ByteSize;
    int COM6StopBits;

    int COM7BaudRate;   
    int COM7Parity;  
    int COM7ByteSize;
    int COM7StopBits;

    strcpy_s(lpPath, MAX_PATH ,"..\\PortSet.ini");

    COM4BaudRate = GetPrivateProfileInt("COM4", "BaudRate", 0, lpPath);
    COM4Parity   = GetPrivateProfileInt("COM4", "Parity", 0, lpPath);
    COM4ByteSize = GetPrivateProfileInt("COM4", "ByteSize", 0, lpPath);
    COM4StopBits = GetPrivateProfileInt("COM4", "StopBits", 0, lpPath);
    COM5BaudRate = GetPrivateProfileInt("COM5", "BaudRate", 0, lpPath);
    COM5Parity   = GetPrivateProfileInt("COM5", "Parity", 0, lpPath);
    COM5ByteSize = GetPrivateProfileInt("COM5", "ByteSize", 0, lpPath);
    COM5StopBits = GetPrivateProfileInt("COM5", "StopBits", 0, lpPath);
    COM6BaudRate = GetPrivateProfileInt("COM6", "BaudRate", 0, lpPath);
    COM6Parity   = GetPrivateProfileInt("COM6", "Parity", 0, lpPath);
    COM6ByteSize = GetPrivateProfileInt("COM6", "ByteSize", 0, lpPath);
    COM6StopBits = GetPrivateProfileInt("COM6", "StopBits", 0, lpPath);
    COM7BaudRate = GetPrivateProfileInt("COM7", "BaudRate", 0, lpPath);
    COM7Parity   = GetPrivateProfileInt("COM7", "Parity", 0, lpPath);
    COM7ByteSize = GetPrivateProfileInt("COM7", "ByteSize", 0, lpPath);
    COM7StopBits = GetPrivateProfileInt("COM7", "StopBits", 0, lpPath);


    delete [] lpPath;

    DCB dcb;                             //在串口通信中都需要用到这个DCB,可以理解为开辟了一块内存块

    memset(&dcb,0,sizeof(dcb));
    if(!GetCommState(Rcom,&dcb))        //获取当前dcb配置
    {
        return FALSE;

    }
    dcb.DCBlength=sizeof(dcb);
    //串口配置
    switch(com) 
    {
    case'5':
    {dcb.BaudRate=COM5BaudRate;                  //波特率
    dcb.Parity=COM5Parity;                //无检验
    dcb.fParity=0;
    dcb.StopBits=COM5StopBits;            //1位停止位
    dcb.ByteSize=COM5ByteSize;
    dcb.fOutxCtsFlow=0;
    dcb.fOutxDsrFlow=0;
    dcb.fDtrControl=DTR_CONTROL_DISABLE;
    dcb.fDsrSensitivity=0;
    dcb.fRtsControl=RTS_CONTROL_DISABLE;
    dcb.fOutX=0;
    dcb.fInX=0;}
    case'7':
    {dcb.BaudRate=COM7BaudRate;                  //波特率
    dcb.Parity=COM7Parity;                //无检验
    dcb.fParity=0;
    dcb.StopBits=COM7StopBits;            //1位停止位
    dcb.ByteSize=COM7ByteSize;
    dcb.fOutxCtsFlow=0;
    dcb.fOutxDsrFlow=0;
    dcb.fDtrControl=DTR_CONTROL_DISABLE;
    dcb.fDsrSensitivity=0;
    dcb.fRtsControl=RTS_CONTROL_DISABLE;
    dcb.fOutX=0;
    dcb.fInX=0;}
    }

    //设置DCB
    if(!SetCommState(Rcom,&dcb))
    {
        printf("端口状态设置失败!重新");
        CloseHandle(Wcom);
        return false;
    }
    else
        return true;

}


//设置前后2个数据之间的发送时间间隔,还有最长间隔时间之类的。。。。。
bool Modbus::setuptimeout(DWORD ReadInterval,DWORD ReadTotalMultiplier,DWORD ReadTotalConstant,DWORD WriteTotalMultiplier,DWORD WriteTotalConstant)
{

    COMMTIMEOUTS timeouts;
    timeouts.ReadIntervalTimeout=ReadInterval;
    timeouts.ReadTotalTimeoutConstant=ReadTotalConstant;
    timeouts.ReadTotalTimeoutMultiplier=ReadTotalMultiplier;
    timeouts.WriteTotalTimeoutConstant=WriteTotalConstant;
    timeouts.WriteTotalTimeoutMultiplier=WriteTotalMultiplier;
    if(!SetCommTimeouts(Rcom,&timeouts))
    {
        printf_s("failed to set timeouts");
        return false;}
    else
        return true;
}

 void Modbus::writechar(char buffer[1024])
{
    COMSTAT cs;
    DWORD BytesSent=0;
    ClearCommError(Wcom,&dwError,&cs);//清除错误
    if(!WriteFile(Wcom,buffer,100,&BytesSent,NULL))
        {
            printf_s("SEND ERROR!~!~!~!~!~");

        }
        else
        {
            printf_s("SEND success!~!~!~!~!~");     
        }


};

这样却不行

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

相关推荐