none
怎么知道哪个COM口对应GSM? RRS feed

  • 问题

  • 我编写在Mobile手机上运行的程序。

    我要使用CreateFile打开GSM对应的COM口读写,可是怎么知道哪个口是对应GSM呢?
    另外,打开COM口,写入“AT\r\n”之后,怎么没有反应,不是应该返回AT或者OK的吗?
    2010年3月13日 13:04

答案

全部回复

  • There are my codes below, any mistakes?

     wchar_t Buff[36];
     char CharBuff[256];
     COMMTIMEOUTS ctos;
     DCB dcb;
      wsprintf(Buff, L"COM%d:", 4);//my GSM is COM4
      HANDLE hFile = CreateFile(Buff, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
      if(hFile != INVALID_HANDLE_VALUE)
      {
       if(!GetCommState(hFile, &dcb))
       {
        outputMsgIntoFile("GetCommState failed, error code is 0x%X\n", GetLastError());
        CloseHandle(hFile);
        return;
       }
       //PrintDCB(&dcb);
       dcb.BaudRate = CBR_9600;
       dcb.ByteSize = 8;
       dcb.Parity = NOPARITY;
       dcb.fParity = FALSE;
       dcb.StopBits = ONESTOPBIT;
       dcb.fBinary = TRUE;
       dcb.fDtrControl = 0; 
       dcb.fRtsControl = 0;
       dcb.fOutX = 0;
       dcb.fInX = 0;
       dcb.fTXContinueOnXoff = 0;
       SetupComm(hFile, 512, 512);
       if(!SetCommState(hFile, &dcb))
       {
        outputMsgIntoFile("SetCommState failed, error code is 0x%X\n", GetLastError());
        CloseHandle(hFile);
        return;
       }

       outputMsgIntoFile("COM opened.\n");
       ctos.ReadIntervalTimeout = MAXDWORD;
       ctos.ReadTotalTimeoutMultiplier = 0;
       ctos.ReadTotalTimeoutConstant = 0;
       ctos.WriteTotalTimeoutConstant = 5;
       ctos.WriteTotalTimeoutMultiplier = 5;
       if(!SetCommTimeouts(hFile, &ctos))
       {
        outputMsgIntoFile("SetCommTimeouts failed, error code is 0x%X\n", GetLastError());
        CloseHandle(hFile);
        return;
       }
       outputMsgIntoFile("SetCommTimeouts done");
       sprintf(CharBuff, "AT\r\n");
       DWORD dwWritten;
       if(!WriteFile(hFile, &CharBuff[0], strlen(CharBuff), &dwWritten, NULL))
       {
        outputMsgIntoFile("WriteFile failed, error code is 0x%X\n", GetLastError());
        CloseHandle(hFile);
        return;
       }
       outputMsgIntoFile("WriteFile OK");
       DWORD dwRead;
       memset(&CharBuff[0], 0, 256);

       Sleep(15000);

       DWORD dwErrorFlags;
       COMSTAT ComStat ;
       
       ClearCommError(hFile, &dwErrorFlags, &ComStat );
       if(ComStat.cbInQue)
       {
        if(!ReadFile(hFile, &CharBuff[0], (ComStat.cbInQue > 200) ? 200 : ComStat.cbInQue, &dwRead, NULL))
        {
         outputMsgIntoFile("ReadFile failed, error code is 0x%X\n", GetLastError());
         CloseHandle(hFile);
         return;
        }
        outputMsgIntoFile("ReadFile OK, got:%s", CharBuff);
       }
       else
       {
        outputMsgIntoFile("no data");
       }
       CloseHandle(hFile);
      }
      else
      {
       outputMsgIntoFile("Can not opened");
      }

    2010年3月13日 13:39
  • 你好,

    可以参考英文论坛的这个帖子,里面给出了一些建议:

    connect to internal modem

    Starting With a GSM Modem Application


    施炯
    2011年3月23日 3:53
    版主