Chinaunix首页 | 论坛 | 博客
  • 博客访问: 263195
  • 博文数量: 71
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 920
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-16 13:07
个人简介

我喜欢蓝天,也喜欢雨天。

文章分类

全部博文(71)

文章存档

2014年(14)

2013年(57)

分类: C/C++

2013-08-20 15:47:44

1.环境 Windows7 vs2010
2.语言 c++
3.设备 钱箱与打印机的LPT1口相连
4.控制指令  27 112 7(十进制)
5.代码

点击(此处)折叠或打开

  1. CString sPort;
  2.     //sPort.Format(_T("\\\\.\\LPT1"));//两种方法都行
  3.     sPort.Format(_T("LPT1"));
  4. #if 1
  5.     //设置为重叠 异步IO
  6.     HANDLE hPort = CreateFile(sPort,GENERIC_READ | GENERIC_WRITE,0,NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);//创建文件
  7.     if (hPort == INVALID_HANDLE_VALUE || hPort == (HANDLE)ERROR_INVALID_HANDLE)
  8.     {
  9.         CString lsreturn = L"open fail";
  10.         ExecutePrint(rect,LogFont,pDC,lsreturn.GetBuffer(0),lsreturn.GetLength(),DT_CENTER | DT_BOTTOM | DT_SINGLELINE);
  11.         return false;
  12.     }
  13.     char lco[4];
  14.     
  15.     lco[0] = 27;
  16.     lco[1] = 112;
  17.     lco[2] = 7;
  18.     lco[3] = '\0';

  19.     DWORD writebyte = 0;
  20.     DWORD writecount = 4;

  21.     OVERLAPPED* pOverlapped = new OVERLAPPED;
  22.     ZeroMemory(pOverlapped, sizeof(OVERLAPPED));
  23.     pOverlapped->Offset = 0;
  24.     pOverlapped->OffsetHigh = 0;
  25.     pOverlapped->hEvent = 0;
  26.     //pOverlapped->hEvent = (HANDLE) this;
  27.     int lret = 0;
  28.     lret = WriteFile(hPort,lco,writecount,&writebyte,pOverlapped);//向文件中直接写命令
  29.     if (lret == 0)
  30.     {
  31.         int err = GetLastError();
  32.         if (GetLastError() == ERROR_IO_PENDING)//重叠IO进行中
  33.         {
  34.             GetOverlappedResult(hPort,pOverlapped,&writebyte,true);
  35.         }
  36.         else//其他的错误
  37.         {
  38.             delete pOverlapped;
  39.             CloseHandle(hPort);
  40.             CString lsreturn = L"Failed to write";
  41.             ExecutePrint(rect,LogFont,pDC,lsreturn.GetBuffer(0),lsreturn.GetLength(),DT_CENTER | DT_BOTTOM | DT_SINGLELINE);
  42.             return false;
  43.         }
  44.     }
  45. #endif

  46. #if 0
  47.     //同步IO 2种方法都可以
  48.     HANDLE hPort = CreateFile(sPort,GENERIC_READ | GENERIC_WRITE,0,NULL, OPEN_EXISTING, 0, NULL);
  49.     if (hPort == INVALID_HANDLE_VALUE || hPort == (HANDLE)ERROR_INVALID_HANDLE)
  50.     {
  51.         CString lsreturn = L"open fail";
  52.         ExecutePrint(rect,LogFont,pDC,lsreturn.GetBuffer(0),lsreturn.GetLength(),DT_CENTER | DT_BOTTOM | DT_SINGLELINE);
  53.         return false;
  54.     }
  55.     char lco[4];
  56.     lco[0] = 27;
  57.     lco[1] = 112;
  58.     lco[2] = 7;
  59.     lco[3] = '\0';

  60.     DWORD writebyte = 0;
  61.     DWORD writecount = 4;
  62.     int lret = 0;
  63.     lret = WriteFile(hPort,lco,writecount,&writebyte,NULL);
  64.     if (lret == 0)
  65.     {
  66.         int err = GetLastError();
  67.         CloseHandle(hPort);
  68.         CString lsreturn = L"Failed to write";
  69.         ExecutePrint(rect,LogFont,pDC,lsreturn.GetBuffer(0),lsreturn.GetLength(),DT_CENTER | DT_BOTTOM | DT_SINGLELINE);
  70.         return false;
  71.     }
  72. #endif
  73.     CloseHandle(hPort);

打开LPT1文件
向文件中写入命令
关闭文件

另外,在网上还查到一些别的指令,如 27 112 0 60 225(十进制的)。但是中间的那个0会被看成空字符,指令会被截断,不知道用什么方法解决。
阅读(1656) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~