سؤال

عندما أقوم بتوصيل جهازي المضمن بنظامي، أقوم بتشغيل برنامجي الذي ستكتب إلى المنفذ الخاص بي المضمن هو الاتصال ويطبع الرد على وحدة التحكم.

عندما أقوم بتوصيل جهازي وتشغيل هذا البرنامج، لا يعطي أي إخراج.

ولكن عندما أقوم بتوصيل جهازي واستخدم المعجون لإرسال بعض الأوامر أولا ثم قم بتشغيل برنامجي يعمل.

ربما هناك مشكلة في الطريقة التي بدأت فيها الاتصال؟

شفرة المصدر الخاصة بي هي: giveacodicetagpre.

هل كانت مفيدة؟

المحلول

Try this... you will probably need to do the code for exceptions (ex: if the response is bigger than 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] = '\0';
                        strRetValue = cBuffer;

                        if(bTimeOut)
                            strRetValue = "Timed out:" + strCommand;
                        else
                            bRetValue = true;
                    }
                }
            }
        }
        CloseHandle(hCom);
    }

    return bRetValue;
}

نصائح أخرى

Most likely the problem is with your initialization.

I recall having this type of trouble before and Com Timeouts structure was particularly troublesome.

I suggest you get a null modem cable from COM5 to another port on the machine (if you have one), or to another computer. Then use a terminal program to open up the other port and see you can see the "List" command coming through when you run the program. If not then it's very likely to be related to the way you are initializing & opening the com port.

This link may prove useful. Just strip out the Afx stuff and look particularly at the initialization. http://www.codeproject.com/KB/system/chaiyasit_t.aspx

One other suggestion, you only send List once. If the device is not already plugged in and ready, nothing will happen. Maybe it should keep sending the list command until it gets a Response.

Also, do you need "List\r\n" or just "List\r"? What is the other ends expecting?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top