有如下程序,为MSDN中的示例代码,可是为什么运行时显示:
GetCommState failed with error 2, msdn中说明为:2 (ERROR_FILE_NOT_FOUND)  :   The system cannot find the file specified
可是我的电脑有串口呀,把"COM1"改为"COM2"也不行,代码如下:
#include "stdafx.h"#include <windows.h>
#include <tchar.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
   DCB dcb;
   HANDLE hCom;
   BOOL fSuccess;
   TCHAR *pcCommPort = TEXT("COM1");   hCom = CreateFile( "COM1",
                    GENERIC_READ | GENERIC_WRITE,
                    0,    // must be opened with exclusive-access
                    NULL, // default 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) 
   {
       // Handle the error.
       printf ("CreateFile failed with error %d.\n", GetLastError());
       return (1);
   }   // Build on the current configuration, and skip setting the size
   // of the input and output buffers with SetupComm.  // SecureZeroMemory(&dcb, sizeof(DCB));
   dcb.DCBlength = sizeof(DCB);
   fSuccess = GetCommState(hCom, &dcb);   if (!fSuccess) 
   {
      // Handle the error.
      printf ("GetCommState failed with error %d.\n", GetLastError());
      return (2);
   }   // Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.   dcb.BaudRate = CBR_57600;     // set the baud rate
   dcb.ByteSize = 8;             // data size, xmit, and rcv
   dcb.Parity = NOPARITY;        // no parity bit
   dcb.StopBits = ONESTOPBIT;    // one stop bit   fSuccess = SetCommState(hCom, &dcb);   if (!fSuccess) 
   {
      // Handle the error.
      printf ("SetCommState failed with error %d.\n", GetLastError());
      return (3);
   }   _tprintf (TEXT("Serial port %s successfully reconfigured.\n"), pcCommPort);
   return (0);
}

解决方案 »

  1.   

    或许你电脑上的串口不是COM1 or COM2.
      

  2.   

    COM1 COM2 or COM3 是系统怎么定义出来的?
      

  3.   

    按下面方法,你看看你的电脑设置:
    【我的电脑】->【右键】->【管理】->【设备管理】->右边设备树内找到:
    【端口(COM和LPT)】若无,表明你机器上没有串口(这个很正常,现在新买的个人电脑一般都没有);
    若有,看看他的儿子节点,后面的括号内就是COM号,比对下COM号是否对。例如,我的电脑上是这样的:
    Prolific USB-to-Serial Bridge(COM4)这个意思是:我的机器上有一个串口口,他是COM4,看名字知道,这个串口是我用USB转换线转换出来的。
      

  4.   


    CreateFile 失败后,调用 GetLastError 获得错误信息。
      

  5.   

    下面方法,你看看你的电脑设置:
    【我的电脑】->【右键】->【管理】->【设备管理】->右边设备树内找到:
    【端口(COM和LPT)】找到是com1还是com2,com3,一般是com1//初始化
    CString initcom;
    initcom="com1";
    hComm=NULL;
    initcom=CommPort;
    CommPort="";
    GetDlgItemText(IDC_COMBO,CommPort);
    hComm=CreateFile(CommPort,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,
    FILE_FLAG_OVERLAPPED,NULL);
    if(hComm==INVALID_HANDLE_VALUE)
    {
    CommPort=initcom;
    AfxMessageBox("Create COM file error!",MB_ICONHAND);
    return ;
    }
    AfxMessageBox("Create COM file success!",MB_ICONINFORMATION);
    SetCommMask(hComm,EV_RXCHAR|EV_TXEMPTY);
    SetupComm(hComm,1500,1500);
    PurgeComm(hComm,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
    //define the timeout struction
    COMMTIMEOUTS CommTimeOuts;
    CommTimeOuts.ReadIntervalTimeout=MAXDWORD;
    CommTimeOuts.ReadTotalTimeoutConstant=0;
    CommTimeOuts.ReadTotalTimeoutMultiplier=0;
    CommTimeOuts.WriteTotalTimeoutConstant=0;
    CommTimeOuts.WriteTotalTimeoutMultiplier=0;//5000;
    SetCommTimeouts(hComm,&CommTimeOuts);

    DWORD BaudRate;
    BaudRate=9600;
    DCB dcb;
    GetCommState(hComm,&dcb);
    dcb.BaudRate=BaudRate;
    dcb.ByteSize=7;
    dcb.Parity=2;
    dcb.StopBits=ONESTOPBIT;
    dcb.fBinary=TRUE;
    dcb.fParity=FALSE;
    SetCommState(hComm,&dcb);//读数据
    char* CCommandDlg::ReadComm(HANDLE hcom)
    {
    DWORD lpRead;
    OVERLAPPED osRead={0};
    char str[1024];
    char strcom[1024];
    memset(strcom,0,1024);
    memset(str,0,1024);
    if(hcom!=NULL && hcom!=INVALID_HANDLE_VALUE)
    {
    if(ReadFile(hcom,strcom,1024,&lpRead,&osRead))
    {
    for(int i=0;i<1024;i++)
    str[i]=strcom[i];
    return str;
    }
    else 
    {
    // MessageBox("fail");
    return "";
    }
    }
    return "";
    }
    //写数据
    void CCommandDlg::WriteDataToComm(char *buf, HANDLE hcom)
    {
    DWORD lpWriten;
    OVERLAPPED osWrite={0}; CString str=""; if(hcom!=NULL && hcom!=INVALID_HANDLE_VALUE)
    {
    if(!WriteFile(hcom,buf,strlen(buf),&lpWriten,&osWrite))
    { str="Send Data To ";
    str+=CommPort;
    str+=':';
    str+=buf;
    str+="\r\n";
    ShowEdit(str);
    return ;
    }
    }
    }
      

  6.   

    OVERLAPPED   osWrite={0}; CString   str= " "; if(hcom!=NULL   &&   hcom!=INVALID_HANDLE_VALUE)