vc++ mfc 中的串行编程存在一些问题

Some problem with serial programming in vc++ mfc

本文关键字:存在 问题 编程 mfc vc++      更新时间:2023-10-16

当我将嵌入式设备连接到系统时,我正在运行我的程序,该程序将写入我的嵌入式连接端口,并打印对控制台的回复。

当我连接设备并运行此程序时,它不会提供任何输出。

但是当我连接我的设备并使用 PUTTY 先发送一些命令然后运行我的程序时,它正在工作。

也许我开始沟通的方式有问题?

我的源代码是:

#include "stdafx.h"
#include <iostream>
//#include <windows.h>
#include <afx.h>
int main()
{
    using namespace std;
    int i=0;
//  cout << "Hello world!" << endl;

    HANDLE hSerial;
    hSerial = CreateFile("COM5",
    GENERIC_READ | GENERIC_WRITE,
    FILE_SHARE_WRITE | FILE_SHARE_READ,
    0,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    0);
    if(hSerial==INVALID_HANDLE_VALUE)
    {
        if(GetLastError()==ERROR_FILE_NOT_FOUND)
        {
//          TRACE("serial port does not exist for readingn");
        //serial port does not exist. Inform user.
        }
//          TRACE("some other error,serial port does not exist for readingn");
        //some other error occurred. Inform user.
    }
    DCB dcbSerialParams = {0};
    dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
    if (!GetCommState(hSerial, &dcbSerialParams)) 
    {
//                  TRACE("error getting state for readingn");
    //error getting state
    }
    dcbSerialParams.BaudRate=9600;
    dcbSerialParams.ByteSize=8;
    dcbSerialParams.StopBits=ONESTOPBIT;
    dcbSerialParams.Parity=NOPARITY;
    if(!SetCommState(hSerial, &dcbSerialParams))
    {
    //TRACE("error setting state for readingn");
    //error setting serial port state
    }
    COMMTIMEOUTS timeouts={0};
    timeouts.ReadIntervalTimeout=50;
    timeouts.ReadTotalTimeoutConstant=50;
    timeouts.ReadTotalTimeoutMultiplier=10;
    timeouts.WriteTotalTimeoutConstant=50;
    timeouts.WriteTotalTimeoutMultiplier=10;
    if(!SetCommTimeouts(hSerial, &timeouts))
    {
//                  TRACE("some error occured for readingn");
        //error occureed. Inform user
    }       
    int n=100,n1=100;
    char szBuff[100];
    DWORD dwBytesRead = 0;
    char szBuff1[100];
    DWORD dwByteswrote = 0;
    memset(szBuff1,0,100);
    memcpy(szBuff1,"LISTr",5);
    if(!WriteFile(hSerial, szBuff1,5, &dwByteswrote, NULL))
    {
                    cout << "error writing" ;
    }
    cout << szBuff1 << endl;
    cout << dwByteswrote << endl;
    while(1)
    {
        if(!ReadFile(hSerial, szBuff, n1, &dwBytesRead, NULL))
        {
            cout << "error reading";
            break;
        }
        else
        {
            cout << dwBytesRead << endl;
            szBuff[dwBytesRead]='';
            if(dwBytesRead>0)
            {
                cout << (szBuff);
                break;
            }
        }
    }
    cin >> i;
}

试试这个...您可能需要为异常执行代码(例如:如果响应大于 2024)

bool SendModemATCommand(const string &strCommand, int iModemPort, string &strRetValue)
{
    bool bRetValue = false;
    strRetValue = "";
    char cBuffer[2024];
    HANDLE hCom = NULL;   
    char cComPort[64];
    sprintf_s(cComPort,"\\.\COM%d", iModemPort);

    hCom = CreateFile( cComPort,
                    GENERIC_READ | GENERIC_WRITE,
                    0,    // must be opened with exclusive-access
                    NULL, // no security attributes
                    OPEN_EXISTING, // must use OPEN_EXISTING
                    0,    // not overlapped I/O
                    NULL  // hTemplate must be NULL for comm devices
                    );
    if (hCom != INVALID_HANDLE_VALUE) 
    {
        COMMTIMEOUTS comTimeOuts;
        comTimeOuts.ReadIntervalTimeout = MAXDWORD;
        comTimeOuts.ReadTotalTimeoutMultiplier = MAXDWORD;
        comTimeOuts.ReadTotalTimeoutConstant = 0;//MAXDWORD;
        comTimeOuts.WriteTotalTimeoutMultiplier = 0;
        comTimeOuts.WriteTotalTimeoutConstant = 0;
        if(SetCommTimeouts(hCom, &comTimeOuts))
        {
            DCB dcb;
            dcb.DCBlength = sizeof(DCB);
            if(GetCommState(hCom, &dcb))
            {
                DWORD dwBytesWritten = 0;                  
                DWORD dwBytesRead = 0;
                DWORD dwBytesTotal = 0;
                if( WriteFile(hCom, strCommand.c_str(), (int)strCommand.size(), &dwBytesWritten, NULL) )
                {
                    if(dwBytesWritten == strCommand.size())
                    {
                        dwBytesRead = 0;
                        DWORD tickStart = GetTickCount();
                        bool bTimeOut = false;                      
                        while(true)
                        {
                            while(ReadFile(hCom, cBuffer + dwBytesTotal, 1, &dwBytesRead, NULL))
                            {       
                                if(dwBytesRead == 0 && dwBytesTotal != dwBytesWritten)
                                    break;
                                dwBytesTotal += dwBytesRead;                                
                            }
                            if ( dwBytesTotal == 0 )
                            {
                                // timeout
                                if ( GetTickCount() - tickStart > 10000) // 10 Seconds
                                {
                                    bTimeOut = true;
                                    break;                              
                                }
                            }
                            else
                                break;
                        }                   
                        cBuffer[dwBytesTotal] = '';
                        strRetValue = cBuffer;
                        if(bTimeOut)
                            strRetValue = "Timed out:" + strCommand;
                        else
                            bRetValue = true;
                    }
                }
            }
        }
        CloseHandle(hCom);
    }
    return bRetValue;
}

问题很可能出在您的初始化上。

我记得以前遇到过这种类型的麻烦,Com 超时结构特别麻烦。

我建议您从COM5获取零调制解调器电缆到计算机上的另一个端口(如果有),或连接到另一台计算机。 然后使用终端程序打开另一个端口,并看到运行该程序时可以看到"List"命令通过。 如果不是,那么它很可能与您初始化和打开 com 端口的方式有关。

此链接可能很有用。 只需去除 Afx 的内容并特别查看初始化即可。http://www.codeproject.com/KB/system/chaiyasit_t.aspx

另一个建议,您只发送一次列表。 如果设备尚未插入并准备就绪,则不会发生任何操作。 也许它应该继续发送列表命令,直到它得到一个响应。

另外,您是需要"列表\r"还是只需要"列表\r"? 其他两端期待什么?