多麻烦啊,干脆就用键盘上的某一个键代替算了。
如果用串口测试的话,你需要:
1。一个5V的直流电源(可以从PC电源上取);
2。在9针的串行口上的第5脚上接一根线出来作为信号地;
3。在第3脚上接一根线作为信号线;
4。在信号线上接一个开关作为输入按钮;
5。在按钮和信号地之间接一个1K左右的电阻;
这样就可以测试了,不过,测试的时候最好拿一台准备报废的电脑
以上方法在理论上是可行的,但未经实践考验!本人不对以上言论负责!

解决方案 »

  1.   

    多谢小草兄。
    如你所说,把2,3脚接在开关上,在程序中如何捕获开关按下的消息呢,是否应利用那个叫什么mscomm的控件,它是不是有个接受到信号的事件可以用呢?
    我对串口编程一窍不通,见笑了。
      

  2.   

    我认为如果还有个空闲的串口(除鼠标和键盘使用外的)可以使用的话,你可以采用2/3两个脚进行控制,如下操作:
    关->开:(2收表示转换)
    接收2脚的数据(可以采用3发来控制所谓的开关),接收数据正确,表示变换。
    接收函数如是:(C++版,若你没有学过C++的话,暂...)
    初始化端口:
    bool __fastcall TForm1::InitPort(AnsiString PortName)
    {
     _DCB         myDCB;
     COMMTIMEOUTS  myTMO;
     bool   Result;  // simply define communication handle,if existing and closing
      // renew to create,
      if(hcom != 0/*INVALID_HANDLE_VALUE*/)
         CloseHandle(hcom);  //create communication handle,
      hcom=CreateFile(PortName.c_str(), // port name
                      GENERIC_READ|GENERIC_WRITE,  // RW access mode
                       0, // exclusive mode
                       NULL, // no security attributes
                       OPEN_EXISTING, // open existing port
                       0, // not overlapped
                       0);
      Result = (hcom!=INVALID_HANDLE_VALUE);
      if(!Result)
            MessageDlg("not open port:"+PortName,
                       mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0);
      else
        {
        //set communlcation mask,set of events to be monitored,
        if(!SetCommMask(hcom,EV_RXFLAG))
          {
          if(MessageDlg("not set mask:"+PortName,
                       mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
          {
          CloseHandle(hcom);
          Result=false;
          exit(1);
          }
          else{
           CloseHandle(hcom);
           Result=true;
           exit(1);
           }
          }
        //initializes the communications parameters,
        if(!SetupComm(hcom,4096,4096))//4096 is specifies send and recvieve size,
          {
          if(MessageDlg("not setup communiation:"+PortName,
                       mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
          {
          CloseHandle(hcom);
          Result=false;
          exit(1);
          }
          else{
           CloseHandle(hcom);
           Result=true;
           exit(1);
           }
          }
        //get the communications state and DCB struct,
        if(!GetCommState(hcom,&myDCB))
          {
           if(MessageDlg("not get state:"+PortName,
                       mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
          {
          CloseHandle(hcom);
          Result=false;
          exit(1);
          }
          else{
           CloseHandle(hcom);
           Result=true;
           exit(1);
           }
          }
         //set dcb state,include baudrate/bytebit/stopbit/parity,
         myDCB = GetDCBParater(myDCB);
         // if(!BuildCommDCB("COM1:baud=1200 parity=N data=8 stop=1",&myDCB));
         // { 以上个的函数用法同下 }
        if(!SetCommState(hcom,&myDCB))
          {
          //unsigned long TTT = GetLastError();
          if(MessageDlg("not set state:"+PortName,
                       mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!=mrYes)
          {
          CloseHandle(hcom);
          Result=false;
          exit(1);
          }
          else{
           CloseHandle(hcom);
           Result=true;
           exit(1);
           }
          }
         if(!GetCommTimeouts(hcom,&myTMO))
          {
          if(MessageDlg("not get out time:"+PortName,
                       mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!= mrYes)
          {
          CloseHandle(hcom);
          Result=false;
          exit(1);
          }
          else{
           CloseHandle(hcom);
           Result=true;
           exit(1);
           }
          }
        //set out time function and coeff.
          myTMO.ReadIntervalTimeout = MAXDWORD;
          myTMO.ReadTotalTimeoutMultiplier = 0;
          myTMO.ReadTotalTimeoutConstant = 0;
          myTMO.WriteTotalTimeoutMultiplier =0;
          myTMO.WriteTotalTimeoutConstant =1000;
        if(!SetCommTimeouts(hcom,&myTMO))
          {
          if(MessageDlg("not set out time:"+PortName,
                       mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)!=mrYes)
          {
          CloseHandle(hcom);
          Result=false;
          exit(1);
          }
          else{
           CloseHandle(hcom);
           Result=true;
           exit(1);
           }
          }
      //API_Function: clear send buffer,
         PurgeComm(hcom,PURGE_TXCLEAR);
      //API-Function: clear recvive buffer,
         PurgeComm(hcom,PURGE_RXCLEAR);
        }
      return Result;
    }
    接收数据:
    bool __fastcall TForm1::RecvModemData()
    {
      bool Result;
      AnsiString ReadStr;
      unsigned char  ReadBuffer[100];
      unsigned long  ReadNum;  memset(ReadBuffer,0,sizeof(ReadBuffer));
      Result = ReadFile(hcom,ReadBuffer,sizeof(ReadBuffer),&ReadNum,NULL);
      if(Result)
        {
        //convert char to ascii mode,
         for(int i=0;i<sizeof(ReadBuffer);i++)
           ReadStr = ReadStr + (char)ReadBuffer[i];
        //judgement receive data if or not empty,
         if(sizeof(ReadStr) == 4)
             ReadStr = "0";
         }
      return 1;
    }
    发送数据:从管脚3出去 ,若你采用管脚2来收的话,
    若你发的数据B[100]定义的,而管脚2接收的正确,
    则变位,关->开(或相反),当然,也可以用些变通的方法来实现你的控制。
    bool __fastcall TForm1::SendStrToPort(AnsiString ReadStr)
    {
     bool Result;
     unsigned char B[100];
     int sendnum=100;  WriteFile(hcom,B,SendNum,&SendNum,NULL);
      if(!WriteFile(hcom,B,SendNum,&SendNum,NULL))
        {
        Result = false;
        if(Application->MessageBox("without write modem", NULL, MB_OKCANCEL) != IDOK)
          exit(1);
        }
     return Result;
    }
    以上是采用C++自己写的一个程序。
    在vb中应该有控件mscomm
    funtion myportoperate() as bool
    ' 使用COM1.
       MSComm1.CommPort = 1
    '定义波特率等 9600, no parity, 8 data, and 1 stop bit.
       MSComm1.Settings = "9600,N,8,1"
    '初始化 
      MSComm1.InputLen = 0     
    '打开端口:
       my_MSComm.PortOpen = True
    '发送数据:
       my_MSComm.Output = "你要发送的数据"
    '接收数据:
       Buffer$ = MSComm1.Input
    '关闭端口
       MSComm1.PortOpen = False
    end function   
       
      

  3.   

    上面的vb程序有点乱,我改了一点,如下:
    '在vb中应该有控件mscomm
    '在程序中定义mscomm为my_mscommfuntion myportoperate() as bool
    ' 使用COM1.
      my_MSComm.CommPort = 1
    '定义波特率等 9600, no parity, 8 data, and 1 stop bit.
      my_MSComm.Settings = "9600,N,8,1"
    '初始化 
      my_MSComm.InputLen = 0    
    '打开端口:
      my_MSComm.PortOpen = True
    '发送数据:
      my_MSComm.Output = "你要发送的数据"
    '接收数据:
      Buffer$ = my_MSComm.Input
    '关闭端口
      my_MSComm.PortOpen = False
    end function  
      
      

  4.   

    可以考虑用DSR,DTR这类的信号线。
      

  5.   

    还有CTS,CD线。这些信号发生变化是会触发OnComm事件。呵呵,看看MSDN吧。