#include <windows.h> #include <string> #include <IOSTREAM> using namespace std; typedef struct DataInfo { string Port; //串口端口号 int BawdRate;//波特率 int DataBits; //数据位 char Parity; //校验位 int ReceiveBuffer; //缓冲区 int StopBits;//停止位 }PrintDevice; int WriteData(string meg); int InitPort(PrintDevice &device); void InitializeDevicePar(PrintDevice &device); HANDLE hPort=NULL; //句柄 int main() { PrintDevice device; InitializeDevicePar(device); //初始化打印机参数 InitPort(device); //初始化打印机端口 string s; ////////////////////////////打印头信息开始////////////////////////////////// //设置中文粗体 + 四倍大小, "FS ! n", n = 4 + 8+128, s= "\\x1C\\x21\\x8C"; WriteData(s); s="商店-上海鼎捷\\x0A"; WriteData(s); //打印 //取消中文放大设置 DWORD iBytesLength; char chSetCode[] = "\\x1C\\x21\\x0"; WriteFile(hPort, chSetCode, (DWORD)3L, &iBytesLength, NULL); //////////////////////////打印头信息结束/////////////////////////////////////// //////////////////////////打印商品开始//////////////////////////////////////// s="山东苹果\\t4.00\\t2x\\t8.00元\\x0A"; WriteData(s); //打印 s="福建橘子\\t5.00\\t3x\\t15.00元\\x0A"; WriteData(s); //打印 s="\\x0A"; WriteData(s); //打印 ////////////////////////////打印商品结束////////////////////////////////////// s="小计:23.00元\\x0A"; WriteData(s); //打印 //进纸到切纸位置,再切纸,"GS V m n", n=需要多进的行数 s= "\\x1D\\x56\\x41\\x01"; //m=65 65是全切,66是半切, n=1 0<=n<=255 WriteData(s); //打印 CloseHandle(hPort);//关闭端口 return 0; } void InitializeDevicePar(PrintDevice &device) { device.Port="COM4"; device.BawdRate=9600; device.DataBits=8; device.StopBits=ONESTOPBIT; device.Parity=NOPARITY; //NONE device.ReceiveBuffer=256; } int InitPort(PrintDevice &device) { hPort = CreateFile(device.Port.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if (hPort == INVALID_HANDLE_VALUE) { // 打开端口失败 return false; } else { //设置端口缓冲 SetupComm(hPort, 1024, 1024); // 设定通讯端口超时参数 COMMTIMEOUTS tmouts; tmouts.ReadIntervalTimeout = 100; tmouts.ReadTotalTimeoutMultiplier = 100; tmouts.ReadTotalTimeoutConstant = 100; tmouts.WriteTotalTimeoutConstant = 100; tmouts.WriteTotalTimeoutMultiplier = 100; SetCommTimeouts(hPort, &tmouts); //设定通讯端口通讯参数 DCB dcb; BOOL bol = TRUE; //dcb.DCBlength = sizeof(dcb); bol=GetCommState(hPort, &dcb); dcb.BaudRate = device.BawdRate; dcb.ByteSize =device.DataBits; dcb.StopBits = device.StopBits; dcb.Parity = device.Parity; bol = SetCommState(hPort, &dcb); //配置串口 // 清除通讯端口缓存 PurgeComm(hPort, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT); // 初始化重叠IO对象 OVERLAPPED m_OverlappedRead; OVERLAPPED m_OverlappedWrite; HANDLE m_hStopCommEvent; HANDLE m_hDataReady; memset(&m_OverlappedRead, 0, sizeof(OVERLAPPED)); m_OverlappedRead.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); memset(&m_OverlappedWrite, 0, sizeof(OVERLAPPED)); m_OverlappedWrite.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); // 初始化事件对象 m_hStopCommEvent = CreateEvent(NULL, TRUE, FALSE, NULL); m_hDataReady = CreateEvent(NULL, FALSE, FALSE, NULL); //初始化打印ESC @ DWORD iBytesLength; char chInitCode[] = "\\x0D\\x1B\\x40"; WriteFile(hPort, chInitCode, (DWORD)3L, &iBytesLength, NULL); } return 0; } int WriteData(string meg) { DWORD dwWrite; WriteFile(hPort, meg.c_str(), (DWORD)meg.length(), &dwWrite, NULL); return 0; }
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END