数据格式为:1位起始位+8位数据位+1位校验位(当发送地址为1,命令为0)+1停止位
那为大哥给个例子谢谢!
上次的100分已经结帐,可是我还是不明白,咳....

解决方案 »

  1.   

    _inp()函数不行了;它的结果是a byte应该用_inpw,它的结果是a word 
    unsigned short _inpw( unsigned short port );port:Port numberRequired Header:<conio.h>将得到的结果(0x1111111111111)移位分解即可
      

  2.   

    另外下面的程序,在编译时出错。环境VC6.0 
    int PASCAL in_data(int com_port)
    {
    int c;
    do { 
    c = _inp(com_port + 5);
                      if (c & 1) {return _inp(com_port);}  
       }  while (c & 1);
    }
    出错提示:
    warning C4715: 'in_data' : not all control paths return a value
    那为大哥指点一二吧!
      

  3.   

    我也不想呀,可是同事非要用方式三做它的通信(AT89C51单片机),本来我可以用MSCOMM控件,多轻松,可是,咳,没法子了,只有硬着头皮用_inp()和_outp()
    (这两个函数事实上在NT的环境下不支持)。
      

  4.   

    我倒是建议你用createfile和read/writefile来实现串口通信,通用性强而且比较灵活,我就是这样做的,不过我用的是mcs51单片机。
      

  5.   

    呵呵,老大,你学过C吗?你的函数返回的int型,而if (c & 1) {return _inp(com_port);}  _inp可是返回BYTE型的并且包括Head File <conio.h>;那个什么MSCOMM控件有慢又不好使,在win95/98下用_inp()和_outp()就对了;
      

  6.   

    谢谢 nbgyf(小蜜蜂) 
    我还真没学过C,你能帮我该一下吗(不好意思)。
    98440622(都是VC惹的祸) :能给我个用createfile和read/writefile的程序吗?Email:[email protected]
    谢谢!
      

  7.   

    你懂的串口编程么,不是用_inp的,你看看串口编程的东东吧,我是搞它的,如果需要的话给你个代码,[email protected]
      

  8.   

    我觉得用_inp比用Createfile方式更加稳定,因为它更接近底层,
    我个人认为用_inp好些,不过用Createfile方式也行,方便!
    一段Createfile方式的代码
    :com=CreateFile(portname,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
    if(fcom==INVALID_HANDLE_VALUE)
    {
    dwError=GetLastError();
    //return dwError;
    }
    GetCommState(fcom,&dcb);
    switch(portsetting){
    case 0:
    dcb.BaudRate=4800;
    break;
    case 1:
    dcb.BaudRate=9600;
    break;
    case 2:
    dcb.BaudRate=19200;
    break;
    case 3:
    dcb.BaudRate=38400;
    break;
    default:
        dcb.BaudRate=9600;
    break;
    }
    dcb.Parity='n';
    dcb.ByteSize=8;
    dcb.StopBits=1;
    SetCommState(fcom,&dcb);
    GetCommTimeouts(fcom,&cto);
    cto.ReadIntervalTimeout=20;
    cto.ReadTotalTimeoutConstant=20;
    cto.ReadTotalTimeoutMultiplier=200;
    cto.WriteTotalTimeoutConstant=0;
    cto.WriteTotalTimeoutMultiplier=0;
    SetCommTimeouts(fcom,&cto);
    //写端口
    if(WriteFile(fcom,"\x1b\x40\x1c\x26",4,&dw,NULL)==0){//写失败
    CloseHandle(fcom);
    ret=Get_Status(NULL,portname,portsetting);
    if(ret)
    return -2;
    else return -1;
    }
    if(WriteFile(fcom,printdata,strlen(printdata),&dw,NULL)==0){//写失败
    CloseHandle(fcom);
    ret=Get_Status(NULL,portname,portsetting);
    if(ret)
    return -2;
    else return -1;
    }
    if(dw!=strlen(printdata)){
    CloseHandle(fcom);
    return -3;
      

  9.   

    另外下面的程序,在编译时出错。环境VC6.0 
    int PASCAL in_data(int com_port)
    {
    int c;
    do { 
    c = _inp(com_port + 5);
                      if (c & 1) {return _inp(com_port);}  
       }  while (c & 1);
        return 1;
    }
    加一个返回值就不会有WARING了,
    还有_inp不能在NT和2000下使用,并且这个不是用来编串口程序的!
      

  10.   

    非常感谢楼上的弟兄们。
    但是如果用Createfile能对8250A(串口芯片)的寄存器设置吗,例如如果你和单片机用方式三通信,就需要把COM1(1016/0X3F8)+3(LCR)
      

  11.   

    非常感谢楼上的弟兄们。
    但是如果用Createfile能对8250A(串口芯片)的寄存器设置吗,例如如果你和单片机用方式三通信,就需要把COM1(1016/0X3F8)+3(LCR寄存器)的Bits 3, 4 ,5位进行设置(当发送地址是设为:101;其它为:111)。
    另外用_inp()接收数据时,当你的发送数据大于127(小于255)时,得到是一个负数,因为这时候最高位为1,这也让我很麻烦,需要在程序中判断,如果小于0,则加2,不知有什么简单的方法?
    另外在编译DLL的时候出现:
    Creating library Release/com.lib and object Release/com.exp
    LINK : warning LNK4089: all references to "SHELL32.dll" discarded by /OPT:REF
    LINK : warning LNK4089: all references to "comdlg32.dll" discarded by /OPT:REFcom.dll - 0 error(s), 2 warning(s)
    在DEBUG下不会出现。
      

  12.   

    _inp()在NT OS以后就不可用了,我以前也是用它,可是WIN2K等下面不能用,现在还不得都改!建议还是尽早改为其它,除非你的程序要在DOS下运行,而且以后不考虑迁移。