You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
296 lines
4.6 KiB
C++
296 lines
4.6 KiB
C++
#include "myserialport.h"
|
|
|
|
mySerialPort::mySerialPort()
|
|
{
|
|
|
|
}
|
|
|
|
int mySerialPort::InitStr()
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
int mySerialPort::OpenPort(char * pPort,qint32 baud,
|
|
DataBits bytesize,Parity parity,
|
|
StopBits stopbits)
|
|
{
|
|
bool bRet;
|
|
|
|
setPortName(pPort);
|
|
bRet = setBaudRate(baud);
|
|
if(!bRet)
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
bRet =setDataBits(bytesize);
|
|
if(!bRet)
|
|
{
|
|
return -2;
|
|
}
|
|
|
|
bRet =setParity(parity);
|
|
if(!bRet)
|
|
{
|
|
return -3;
|
|
}
|
|
|
|
bRet =setStopBits(stopbits);
|
|
if(!bRet)
|
|
{
|
|
return -4;
|
|
}
|
|
|
|
bRet =setFlowControl(QSerialPort::NoFlowControl);
|
|
if(!bRet)
|
|
{
|
|
return -5;
|
|
}
|
|
|
|
close(); //先关串口,再打开,可以保证串口不被其它函数占用。
|
|
bRet = open(QIODevice::ReadWrite);
|
|
if(!bRet)
|
|
{
|
|
return -6;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int mySerialPort::InitDevPrint(int iLine)
|
|
{
|
|
char buf[8];
|
|
|
|
buf[0] = 0x1B;
|
|
buf[1] = 0x40;
|
|
WriteToPort(buf,2);
|
|
|
|
buf[0] = 0x1B;
|
|
buf[1] = 0x7A;
|
|
buf[2] = 0x01;
|
|
WriteToPort(buf,3);
|
|
|
|
buf[0] = 0x1B;
|
|
buf[1] = 0x43;
|
|
buf[2] = (unsigned char) (iLine & 0xFF);
|
|
|
|
WriteToPort(buf,3);
|
|
return 0;
|
|
}
|
|
|
|
HRESULT mySerialPort::PRN_CheckPrinter(INT32 &iStatus)
|
|
{
|
|
char buf[8];
|
|
|
|
qint64 iLen;
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
buf[0] = 0x05;
|
|
|
|
clear();
|
|
write(buf,1);
|
|
waitForBytesWritten(100);
|
|
|
|
bool bsucc = waitForReadyRead(50);
|
|
if(!bsucc)
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
iLen = bytesAvailable();
|
|
if(iLen >0)
|
|
{
|
|
read(buf,1);
|
|
|
|
char btemp = buf[0] & 0x08;
|
|
if( 0x0 == btemp)
|
|
{
|
|
iStatus =0;
|
|
}
|
|
else
|
|
{
|
|
iStatus =1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
return -2;
|
|
}
|
|
|
|
void mySerialPort::WriteToPort(char * buf, int iLen)
|
|
{
|
|
int iTime;
|
|
|
|
iTime = 50 + iLen;
|
|
if(iTime >100)
|
|
{
|
|
iTime =100;
|
|
}
|
|
|
|
write(buf,iLen);
|
|
waitForBytesWritten(iTime);
|
|
|
|
// myWritelogArray(array);
|
|
return ;
|
|
}
|
|
|
|
void mySerialPort::ClosePort()
|
|
{
|
|
if(isOpen())
|
|
{
|
|
clear();
|
|
close();
|
|
}
|
|
}
|
|
|
|
int mySerialPort::PrinterData(int mcol,int mline, int ihdj,myCutPaperPara * pPara,QList<myprinterData> & Datalist)
|
|
{
|
|
char FreeBuf[8];
|
|
int iLen;
|
|
int iStartLen = 1;
|
|
char * pData;
|
|
int iPaperLen;
|
|
char tmpbuf[512];
|
|
myprinterData tmpData;
|
|
|
|
strcpy(FreeBuf, "\r\n");
|
|
SetLineWidth(0x0C);
|
|
|
|
for(int i=0; i<Datalist.size(); i++)
|
|
{
|
|
tmpData = Datalist.at(i);
|
|
if(iStartLen < tmpData.line)
|
|
{
|
|
PrintFreeLine(tmpData.line - iStartLen);
|
|
}
|
|
|
|
pData= tmpData.Data;
|
|
iLen = strlen(pData);
|
|
for (int j = (iLen - 1); j>0; j--)
|
|
{
|
|
if (' ' == pData[j])
|
|
{
|
|
pData[j] = 0;
|
|
}
|
|
else
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
iLen = strlen(pData);
|
|
if (iLen>mcol)
|
|
{
|
|
pData[mcol - 1] = 0;
|
|
}
|
|
|
|
strcpy(tmpbuf,pData);
|
|
strcat(tmpbuf, "\r\n");
|
|
iLen = strlen(tmpbuf);
|
|
WriteToPort(tmpbuf,iLen);
|
|
iStartLen = tmpData.line +1;
|
|
}
|
|
|
|
if ((1 == pPara->iCutPaper) && (1 == pPara->iCmdlf))
|
|
{
|
|
iPaperLen = mline - 1;
|
|
}
|
|
else
|
|
{
|
|
iPaperLen = mline;
|
|
}
|
|
|
|
PrintFreeLine(iPaperLen - iStartLen);
|
|
printAdjLine(ihdj);
|
|
|
|
/* iRet = devCurPaper();
|
|
if (iRet < 0)
|
|
{
|
|
return iRet;
|
|
}
|
|
*/
|
|
|
|
return 0;
|
|
}
|
|
|
|
int mySerialPort::PrintFreeLine(int iLen)
|
|
{
|
|
char buf[8];
|
|
|
|
buf[0] = 0x1B;
|
|
buf[1] = 0x61;
|
|
buf[2] = (char) (iLen & 0xFF);
|
|
WriteToPort(buf,3);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int mySerialPort::SetLineWidth(unsigned char cLen)
|
|
{
|
|
char buf[8];
|
|
|
|
buf[0] = 0x1B;
|
|
buf[1] = 0x41;
|
|
buf[2] = cLen;
|
|
|
|
WriteToPort(buf,3);
|
|
|
|
buf[0] = 0x1B;
|
|
buf[1] = 0x32;
|
|
WriteToPort(buf,2);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int mySerialPort::printAdjLine(int ihdj)
|
|
{
|
|
char buf[8];
|
|
buf[0] = 0x1B;
|
|
buf[1] = 0x4A;
|
|
buf[2] = (char)(ihdj & 0xFF);
|
|
|
|
WriteToPort(buf,3);
|
|
return 0;
|
|
}
|
|
|
|
int mySerialPort::devCurPaper(myCutPaperPara * pPara)
|
|
{
|
|
char buf[8];
|
|
int iLen;
|
|
|
|
if (1 != pPara->iCutPaper)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
if (1 == pPara->iCutcmd)
|
|
{
|
|
buf[0] = 0x1B;
|
|
buf[1] = 0x64;
|
|
buf[2] = 0x00;
|
|
iLen = 3;
|
|
}
|
|
else if (2 == pPara->iCutcmd)
|
|
{
|
|
buf[0] = 0x0C;
|
|
iLen = 1;
|
|
}
|
|
else
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
if (1 == pPara->iCmdlf)
|
|
{
|
|
buf[iLen] ='\r';
|
|
buf[iLen +1] ='\n';
|
|
iLen +=2;
|
|
}
|
|
|
|
WriteToPort(buf,iLen);
|
|
return 0;
|
|
}
|
|
|
|
|