0

組み込みデバイスをシステムに接続すると、組み込みが接続されているポートに書き込むプログラムが実行され、コンソールに応答が出力されます。

デバイスを接続してこのプログラムを実行すると、出力が表示されません。

しかし、デバイスを接続し、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 reading\n");
        //serial port does not exist. Inform user.
        }
//          TRACE("some other error,serial port does not exist for reading\n");
        //some other error occurred. Inform user.
    }

    DCB dcbSerialParams = {0};

    dcbSerialParams.DCBlength=sizeof(dcbSerialParams);

    if (!GetCommState(hSerial, &dcbSerialParams)) 
    {
//                  TRACE("error getting state for reading\n");
    //error getting state
    }

    dcbSerialParams.BaudRate=9600;
    dcbSerialParams.ByteSize=8;
    dcbSerialParams.StopBits=ONESTOPBIT;
    dcbSerialParams.Parity=NOPARITY;

    if(!SetCommState(hSerial, &dcbSerialParams))
    {
    //TRACE("error setting state for reading\n");
    //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 reading\n");
        //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,"LIST\r",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]='\0';
            if(dwBytesRead>0)
            {
                cout << (szBuff);

                break;
            }
        }
    }

    cin >> i;
}
4

2 に答える 2

1

これを試してください...おそらく例外のコードを実行する必要があります(例:応答が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;
}
于 2011-05-19T09:14:03.113 に答える
0

ほとんどの場合、問題は初期化にあります。

以前にこの種の問題が発生したことを思い出します。ComTimeouts構造は特に厄介でした。

COM5からマシンの別のポート(ある場合)または別のコンピューターへのヌルモデムケーブルを入手することをお勧めします。次に、ターミナルプログラムを使用して他のポートを開き、プログラムの実行時に「リスト」コマンドが実行されることを確認します。そうでない場合は、COMポートを初期化して開く方法に関連している可能性が非常に高くなります。

このリンクは役に立つかもしれません。Afxのものを取り除き、特に初期化を見てください。 http://www.codeproject.com/KB/system/chaiyasit_t.aspx

もう1つの提案は、リストを1回だけ送信することです。デバイスがまだ接続されて準備ができていない場合、何も起こりません。多分それはそれが応答を得るまでlistコマンドを送り続けるべきです。

また、「リスト\ r \ n」または単に「リスト\r」が必要ですか?他の目的は何を期待していますか?

于 2011-05-19T09:34:43.173 に答える