怎样用c/c++语言实现串并口通信,还有usb  接口等。。
在win98 和win2000/xp 怎样用程序实现

解决方案 »

  1.   

    搜一下以前的帖子和FAQ很多的
      

  2.   

    c
    #include "conio.h"
    inportb(long addr)
    outportb(long addr,char data)
    c++
    _inp(long addr)
    _outp(long addr,char data)
      

  3.   

    用API和串口一样,用CREATEFILE.READFILE,WRITEFILE等
    给你个例子
    extern "C" bool PASCAL Link() //连接到打印机
    {
    m_hCardInterFace = 
    CreateFile("LPT1",
    GENERIC_WRITE,
    NULL,NULL,
    OPEN_EXISTING,
    NULL,
    NULL);
    if(m_hCardInterFace!=INVALID_HANDLE_VALUE)
    {
    m_CommTimeouts.ReadIntervalTimeout = 1000;//读操作两个字节之间的时间(单位:毫秒)
    m_CommTimeouts.ReadTotalTimeoutMultiplier = 1000;//读操作的时间(单位:毫秒)
    m_CommTimeouts.ReadTotalTimeoutConstant = 1000;
    m_CommTimeouts.WriteTotalTimeoutMultiplier = 1000;
    m_CommTimeouts.WriteTotalTimeoutConstant = 1000;
    SetCommTimeouts(m_hCardInterFace, &m_CommTimeouts);
    return true;
    }
    return false;
    }
    extern "C" void PASCAL Unlink() //断开打印机
    {
    CloseHandle(m_hCardInterFace);
    }
    bool SendCommand(CString CommandWord) //向打印机发出命令,并且返回命令状态
    {
    DWORD t1,dwRead;
    COMSTAT t2; CString Command=0x1b;
    Command+=CommandWord;
    Command+=0x0d;
    WriteFile(
    m_hCardInterFace,
    Command.GetBuffer (Command.GetLength ()),
    Command.GetLength (),
    &dwRead,
    NULL);
    if(!ClearCommError(m_hCardInterFace,&t1,&t2))
    {
    switch(t1)
    {
    case CE_OOP:
    TRACE("G CE_OOP\n");
    return false;
    break;
    case CE_PTO://超时错误
    TRACE("G CE_PTO\n");
    return false;
    break;
    case CE_IOE:
    TRACE("G CE_IOE\n");
    return false;
    break;
    }
    }


    return true;
    }
      

  4.   

    实现USB就要看你的USB的驱动了,看他是模拟出串口还是其他的,可以参考SETUPAPI