[COLOR=#0000FF]#include[/color] [COLOR=#A31515]"main.h"[/color]
[COLOR=#0000FF]#include[/color] [COLOR=#A31515]"stdio.h"[/color]
[COLOR=#0000FF]static[/color] [COLOR=#0000FF]int[/color] cPort = 1;
[COLOR=#0000FF]static[/color] [COLOR=#0000FF]int[/color] bRate = 9600;
[COLOR=#0000FF]static[/color] [COLOR=#0000FF]int[/color] pType = 0;
[COLOR=#0000FF]static[/color] [COLOR=#0000FF]int[/color] dBits = 8;
[COLOR=#0000FF]static[/color] [COLOR=#0000FF]int[/color] sBits = 1;
[COLOR=#0000FF]void[/color] DLL_EXPORT SetComPort([COLOR=#0000FF]int[/color] comPort, [COLOR=#0000FF]int[/color] baudRate, [COLOR=#0000FF]int[/color] parityType, [COLOR=#0000FF]int[/color] dataBits, [COLOR=#0000FF]int[/color] stopBits)
{
cPort = comPort;
bRate = baudRate;
pType = parityType;
dBits = dataBits;
sBits = stopBits;
}
[COLOR=#0000FF]void[/color] DLL_EXPORT PrintQRCode([COLOR=#0000FF]char[/color]* data, [COLOR=#0000FF]int[/color] fandc)
{
[COLOR=#0000FF]char[/color] comFile[32] = [COLOR=#A31515]"\\\\.\\COM"[/color];
[COLOR=#0000FF]char[/color] scratch[10];
sprintf(scratch, [COLOR=#A31515]"%d"[/color], cPort);
strcat(comFile, scratch);
HANDLE hComm;
hComm = CreateFile(
comFile,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0
);
DCB dcb = {0};
[COLOR=#0000FF]if[/color] (GetCommState(hComm, &dcb))
{
dcb.BaudRate = bRate;
dcb.ByteSize = dBits;
[COLOR=#0000FF]switch[/color] (pType)
{
[COLOR=#0000FF]case[/color] 2:
dcb.Parity = EVENPARITY;
[COLOR=#0000FF]break[/color];
[COLOR=#0000FF]case[/color] 1:
dcb.Parity = ODDPARITY;
[COLOR=#0000FF]break[/color];
[COLOR=#0000FF]case[/color] 0:
dcb.Parity = NOPARITY;
[COLOR=#0000FF]break[/color];
[COLOR=#0000FF]default[/color]:
dcb.Parity = NOPARITY;
}
dcb.StopBits = sBits - 1;
}
OVERLAPPED osWrite = {0};
DWORD dwWritten;
DWORD dwRes;
BOOL fRes;
byte storeQRCode[] = {
0x1D, 0x28, 0x6B, (byte)(strlen(data) + 3), 0x00, 0x31, 0x50, 0x30
};
byte modSizeCenterPrintCode[] = {
0x1D, 0x28, 0x6B, 0x04, 0x00, 0x31, 0x41, 0x32, 0x00,
0x1D, 0x28, 0x6B, 0x03, 0x00, 0x31, 0x43, 0x07,
0x1b, 0x61, 0x01,
0x1D, 0x28, 0x6B, 0x03, 0x00, 0x31, 0x51, 0x30
};
byte feedAndCut[] = { 27, 64, 0xA, 0x1d, 0x56, 66, 30 };
byte dataToPrint[ 8 + strlen(data) + 28 + 7 ];
memcpy(dataToPrint, storeQRCode, 8);
memcpy(dataToPrint + 8, data, strlen(data));
memcpy(dataToPrint + 8 + strlen(data), modSizeCenterPrintCode, 28);
[COLOR=#0000FF]if[/color] (fandc == 1)
memcpy(dataToPrint + 8 + strlen(data) + 28, feedAndCut, 7);
osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
[COLOR=#0000FF]if[/color] (!WriteFile(hComm, dataToPrint, 8 + strlen(data) + 28, &dwWritten, &osWrite))
{
[COLOR=#0000FF]if[/color] (GetLastError() != ERROR_IO_PENDING)
{
fRes = FALSE;
}
[COLOR=#0000FF]else[/color]
{
dwRes = WaitForSingleObject(osWrite.hEvent, INFINITE);
[COLOR=#0000FF]switch[/color] (dwRes)
{
[COLOR=#0000FF]case[/color] WAIT_OBJECT_0:
[COLOR=#0000FF]if[/color] (!GetOverlappedResult(hComm, &osWrite, &dwWritten, FALSE))
fRes = FALSE;
[COLOR=#0000FF]else[/color]
fRes = TRUE;
[COLOR=#0000FF]break[/color];
[COLOR=#0000FF]default[/color]:
fRes = FALSE;
[COLOR=#0000FF]break[/color];
}
}
}
[COLOR=#0000FF]else[/color]
fRes = TRUE;
}
[COLOR=#0000FF]extern[/color] [COLOR=#A31515]"C"[/color] DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
[COLOR=#0000FF]switch[/color] (fdwReason)
{
[COLOR=#0000FF]case[/color] DLL_PROCESS_ATTACH:
[COLOR=#008000]// attach to process[/color]
[COLOR=#008000]// return FALSE to fail DLL load[/color]
[COLOR=#0000FF]break[/color];
[COLOR=#0000FF]case[/color] DLL_PROCESS_DETACH:
[COLOR=#008000]// detach from process[/color]
[COLOR=#0000FF]break[/color];
[COLOR=#0000FF]case[/color] DLL_THREAD_ATTACH:
[COLOR=#008000]// attach to thread[/color]
[COLOR=#0000FF]break[/color];
[COLOR=#0000FF]case[/color] DLL_THREAD_DETACH:
[COLOR=#008000]// detach from thread[/color]
[COLOR=#0000FF]break[/color];
}
[COLOR=#0000FF]return[/color] TRUE; [COLOR=#008000]// succesful[/color]
}