
// jsprinterDLLDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "jsprinterDLL.h"
#include "jsprinterDLLDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CjsprinterDLLDlg 对话框



CjsprinterDLLDlg::CjsprinterDLLDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CjsprinterDLLDlg::IDD, pParent)
	, m_baudrate(0)
	, m_ComPort(_T(""))
	, m_LptPort(_T(""))
	, m_portselect(0)
	, SendTemp(_T(""))
	, m_strfunc(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CjsprinterDLLDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_IPADDRESS1, m_ipctr);
	DDX_CBString(pDX, IDC_COMBCOM, m_ComPort);
	DDX_CBString(pDX, IDC_COMBLPT, m_LptPort);
	DDX_Radio(pDX, IDC_RDLPT, m_portselect);
	DDX_Text(pDX, IDC_EDTTXT, SendTemp);
	DDX_CBString(pDX, IDC_CMBFUNCLIST, m_strfunc);
}

BEGIN_MESSAGE_MAP(CjsprinterDLLDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BtnPrintTxt, &CjsprinterDLLDlg::OnBnClickedBtnprinttxt)
	ON_BN_CLICKED(IDC_BtnPrintHex, &CjsprinterDLLDlg::OnBnClickedBtnprinthex)
	ON_BN_CLICKED(IDC_BtnRead, &CjsprinterDLLDlg::OnBnClickedBtnread)
	ON_BN_CLICKED(IDC_BtnCall, &CjsprinterDLLDlg::OnBnClickedBtncall)
END_MESSAGE_MAP()


void CjsprinterDLLDlg::InitComb()
{
	CComboBox *pcombBaudrate = (CComboBox *)GetDlgItem(IDC_COMBCOM);
	pcombBaudrate->AddString(_T("COM1"));
	pcombBaudrate->AddString(_T("COM2"));
	pcombBaudrate->AddString(_T("COM3"));
	pcombBaudrate->AddString(_T("COM4"));
	pcombBaudrate->AddString(_T("COM5"));
	pcombBaudrate->AddString(_T("COM6"));
	pcombBaudrate->AddString(_T("COM7"));
	pcombBaudrate->AddString(_T("COM8"));
	pcombBaudrate->AddString(_T("COM9"));
	pcombBaudrate->AddString(_T("COM10"));
	pcombBaudrate->AddString(_T("COM11"));
	pcombBaudrate->AddString(_T("COM12"));
	pcombBaudrate->AddString(_T("COM13"));
	pcombBaudrate->SetCurSel(0);

	CComboBox *pcombComPort = (CComboBox *)GetDlgItem(IDC_COMBBaudRate);
	pcombComPort->AddString(_T("9600"));
	pcombComPort->AddString(_T("19200"));
	pcombComPort->AddString(_T("38400"));
	pcombComPort->AddString(_T("115200"));
	pcombComPort->SetCurSel(0);

	CComboBox *pcombLPT = (CComboBox *)GetDlgItem(IDC_COMBLPT);
	pcombLPT->AddString(_T("LPT1"));
	pcombLPT->AddString(_T("LPT2"));
	pcombLPT->AddString(_T("LPT3"));
	pcombLPT->AddString(_T("LPT4"));
	pcombLPT->AddString(_T("LPT5"));
	pcombLPT->AddString(_T("LPT6"));
	pcombLPT->AddString(_T("LPT7"));
	pcombLPT->AddString(_T("LPT8"));
	pcombLPT->AddString(_T("LPT9"));
	pcombLPT->AddString(_T("LPT10"));
	pcombLPT->AddString(_T("LPT11"));
	pcombLPT->AddString(_T("LPT12"));
	pcombLPT->AddString(_T("LPT13"));
	pcombLPT->SetCurSel(0);

	UpdateData(FALSE);
}


// CjsprinterDLLDlg 消息处理程序

BOOL CjsprinterDLLDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO:  在此添加额外的初始化代码

	InitComb();

	hDllInst = ::LoadLibrary(_T("JsPrinterDll.dll"));
	if (hDllInst)
	{
		uniOpenCom =            (puniOpenCom)GetProcAddress(hDllInst, "uniOpenCom");
		uniOpenUsbByVidPid =    (puniOpenUsbByVidPid)GetProcAddress(hDllInst, "uniOpenUsbByVidPid");
		uniOpenUsb =            (puniOpenUsb)GetProcAddress(hDllInst, "uniOpenUsb");
		uniConnectNetPortByIp = (puniConnectNetPortByIp)GetProcAddress(hDllInst, "uniConnectNetPortByIp");
		uniOpenLpt =            (puniOpenLpt)GetProcAddress(hDllInst, "uniOpenLpt");
		uniClose =              (puniClose)GetProcAddress(hDllInst, "uniClose");
		uniRead =               (puniRead)GetProcAddress(hDllInst, "uniRead");
		uniWrite =              (puniWrite)GetProcAddress(hDllInst, "uniWrite");
		uniInitNetSev =         (puniInitNetSev)GetProcAddress(hDllInst, "uniInitNetSev");
		uniCloseNetServ =       (puniCloseNetServ)GetProcAddress(hDllInst, "uniCloseNetServ");

		uniPrintImg1d76 =       (puniPrintImg1d76)GetProcAddress(hDllInst, "uniPrintImg1d76");
		uniDownLogo =           (puniDownLogo)GetProcAddress(hDllInst, "uniDownLogo");
		uniprintBmpInFLASH =    (puniprintBmpInFLASH)GetProcAddress(hDllInst, "uniprintBmpInFLASH");
		uniPrintImg1b2a =       (puniPrintImg1b2a)GetProcAddress(hDllInst, "uniPrintImg1b2a");
		LoadFuncNames();
	}
	else
	{
		AfxMessageBox(_T("加载动态库失败JsPrinterDll.dll，无法继续调用函数"));
	}

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CjsprinterDLLDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CjsprinterDLLDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CjsprinterDLLDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CjsprinterDLLDlg::LoadFuncNames()
{
	CComboBox* pcombfunc = (CComboBox*)GetDlgItem(IDC_CMBFUNCLIST);
	int nindex = pcombfunc->InsertString(0,_T("uniPrintImg1d76"));
	nindex = pcombfunc->InsertString(nindex+1, _T("uniDownLogo"));
	nindex = pcombfunc->InsertString(nindex + 1, _T("uniprintBmpInFLASH"));
	nindex = pcombfunc->InsertString(0, _T("uniPrintImg1b2a"));
	UpdateData(FALSE);
}

int CjsprinterDLLDlg::uniOpen()
{
	UpdateData(TRUE);
	int fs = 0;
	if (m_portselect == 0)
	{
		if (hDllInst)
		{
			int fs = 0;
			char Combuff[100] = { 0 };
			UpdateData(TRUE);
			int nLength = m_LptPort.GetLength();
			int nBytes = WideCharToMultiByte(CP_ACP, 0, m_LptPort, nLength, NULL, 0, NULL, NULL);
			WideCharToMultiByte(CP_OEMCP, 0, m_LptPort, nLength, Combuff, nBytes, NULL, NULL);

			fs = uniOpenLpt(Combuff);
			return fs;
		}
	}
	else if (m_portselect == 1)
	{
		if (hDllInst)
		{
			int nbaundrate = 0;
			char Combuff[100] = { 0 };

			int nLength = m_ComPort.GetLength();
			m_baudrate = GetDlgItemInt(IDC_COMBBaudRate);
			int nBytes = WideCharToMultiByte(CP_ACP, 0, m_ComPort, nLength, NULL, 0, NULL, NULL);
			WideCharToMultiByte(CP_OEMCP, 0, m_ComPort, nLength, Combuff, nBytes, NULL, NULL);


			fs = uniOpenCom(Combuff, m_baudrate);
			return fs;
		}
	}
	else if (m_portselect == 2)
	{
		if (hDllInst)
		{

			//husb = OpenUsbByVidPid(0x1FC9, 0x2016);
			fs = uniOpenUsb();
			return fs;
		}
	}
	else
	{
		if (hDllInst)
		{
			char ipbuf[100] = { 0 };
			CString strIp;
			m_ipctr.GetWindowText(strIp);

			int nLength = strIp.GetLength();
			int nBytes = WideCharToMultiByte(CP_ACP, 0, strIp, nLength, NULL, 0, NULL, NULL);
			WideCharToMultiByte(CP_OEMCP, 0, strIp, nLength, ipbuf, nBytes, NULL, NULL);

			int ret = 0;
			int fs;
			BOOL bret;

			bret = uniInitNetSev();
			if (!bret)
			{
				return -1;
			}

			fs = uniConnectNetPortByIp(ipbuf, 9100, 5000);
			return fs;
		}
	}
}



void CjsprinterDLLDlg::PrintImg1d76()
{
	CString strBmpFilePath;
	// TODO: 在此添加控件通知处理程序代码
	CFileDialog BmpFileDlg(TRUE);//打开文件对话框（为了取得文件路劲）
	BmpFileDlg.m_ofn.lpstrTitle = _T("打开文件");
	BmpFileDlg.m_ofn.lpstrFilter = _T("Bmp Files(*.bmp)\0*.bmp\0All Files(*.*)\0*.*\0\0");
	if (IDOK == BmpFileDlg.DoModal())
	{
		strBmpFilePath = BmpFileDlg.GetPathName();
	}
	else
	{
		return;
	}
	int fs;
	fs = uniOpen();
	if (fs <= 0)
	{
		AfxMessageBox(_T("uniOpen false"));
		return;
	}
	char pathbuf[256] = { 0 };

	UpdateData(TRUE);

	CString strIp;
	m_ipctr.GetWindowText(strIp);

	int nLength = strBmpFilePath.GetLength();
	int nBytes = WideCharToMultiByte(CP_ACP, 0, strBmpFilePath, nLength, NULL, 0, NULL, NULL);
	WideCharToMultiByte(CP_OEMCP, 0, strBmpFilePath, nLength, pathbuf, nBytes, NULL, NULL);
	//priter width 0 is 384 ,1 is 512 ,2 is 576 ,3 is 640  { 384, 512, 576, 640 }; 
	// printer is 80 ,width is 576
	int nret = uniPrintImg1d76(fs, pathbuf, 2);
	uniClose(fs);
	if (nret>0)
	{
		AfxMessageBox(_T("print success"));
	}
	else if (nret == 0)
	{
		AfxMessageBox(_T("please open first"));
	}
	else if (nret == -1)
	{
		AfxMessageBox(_T("bmppath is empty"));
	}
	else if (nret == -2)
	{
		AfxMessageBox(_T("bmpfile is not exist"));
	}
	else if (nret == -3)
	{
		AfxMessageBox(_T("printer`s width is not avalible"));
	}
	else if (nret == -4)
	{
		AfxMessageBox(_T("bmp`s width must be greater than printer`s width"));
	}
	else if (nret == -5)
	{
		AfxMessageBox(_T("usbwrite error"));
	}
}



void CjsprinterDLLDlg::PrintImg1b2a()
{
	CString strBmpFilePath;
	// TODO: 在此添加控件通知处理程序代码
	CFileDialog BmpFileDlg2(TRUE);//打开文件对话框（为了取得文件路劲）
	BmpFileDlg2.m_ofn.lpstrTitle = _T("打开文件");
	BmpFileDlg2.m_ofn.lpstrFilter = _T("Bmp Files(*.bmp)\0*.bmp\0All Files(*.*)\0*.*\0\0");
	if (IDOK == BmpFileDlg2.DoModal())
	{
		strBmpFilePath = BmpFileDlg2.GetPathName();
		BmpFileDlg2.DestroyWindow();
	}
	else
	{
		return;
	}
	int fs;
	fs = uniOpen();
	if (fs <= 0)
	{
		AfxMessageBox(_T("uniOpen false"));
		return;
	}
	char pathbuf[256] = { 0 };

	UpdateData(TRUE);

	CString strIp;
	m_ipctr.GetWindowText(strIp);

	int nLength = strBmpFilePath.GetLength();
	int nBytes = WideCharToMultiByte(CP_ACP, 0, strBmpFilePath, nLength, NULL, 0, NULL, NULL);
	WideCharToMultiByte(CP_OEMCP, 0, strBmpFilePath, nLength, pathbuf, nBytes, NULL, NULL);
	//priter width 0 is 384 ,1 is 512 ,2 is 576 ,3 is 640  { 384, 512, 576, 640 }; 
	// printer is 80 ,width is 576
	int nret = uniPrintImg1b2a(fs, pathbuf);
	uniClose(fs);
	if (nret>0)
	{
		AfxMessageBox(_T("print success"));
	}
	else if (nret == 0)
	{
		AfxMessageBox(_T("please open first"));
	}
	else if (nret == -1)
	{
		AfxMessageBox(_T("bmppath is empty"));
	}
	else if (nret == -2)
	{
		AfxMessageBox(_T("bmpfile is not exist"));
	}
	else if (nret == -3)
	{
		AfxMessageBox(_T("printer`s width is not avalible"));
	}
	else if (nret == -4)
	{
		AfxMessageBox(_T("bmp`s width must be greater than printer`s width"));
	}
	else if (nret == -5)
	{
		AfxMessageBox(_T("usbwrite error"));
	}
}

void CjsprinterDLLDlg::DownLogo()
{
	CString strBmpFilePath;
	// TODO: 在此添加控件通知处理程序代码
	CFileDialog BmpFileDlg(TRUE);//打开文件对话框（为了取得文件路劲）
	BmpFileDlg.m_ofn.lpstrTitle = _T("打开文件");
	BmpFileDlg.m_ofn.lpstrFilter = _T("Bmp Files(*.bmp)\0*.bmp\0All Files(*.*)\0*.*\0\0");
	if (IDOK == BmpFileDlg.DoModal())
	{
		strBmpFilePath = BmpFileDlg.GetPathName();
	}
	else
	{
		return;
	}
	int fs;
	fs = uniOpen();
	if (fs <= 0)
	{
		AfxMessageBox(_T("uniOpen false"));
		return;
	}
	char pathbuf[256] = { 0 };

	UpdateData(TRUE);

	CString strIp;
	m_ipctr.GetWindowText(strIp);

	int nLength = strBmpFilePath.GetLength();
	int nBytes = WideCharToMultiByte(CP_ACP, 0, strBmpFilePath, nLength, NULL, 0, NULL, NULL);
	WideCharToMultiByte(CP_OEMCP, 0, strBmpFilePath, nLength, pathbuf, nBytes, NULL, NULL);
	//priter width 0 is 384 ,1 is 512 ,2 is 576 ,3 is 640  { 384, 512, 576, 640 }; 
	// printer is 80 ,width is 576
	int nret = uniDownLogo(fs, pathbuf);
	uniClose(fs);
	if (nret>0)
	{
		AfxMessageBox(_T("print success"));
	}
	else if (nret == 0)
	{
		AfxMessageBox(_T("please open first"));
	}
	else if (nret == -1)
	{
		AfxMessageBox(_T("bmppath is empty"));
	}
	else if (nret == -2)
	{
		AfxMessageBox(_T("bmpfile is not exist"));
	}
	else 
	{
		AfxMessageBox(_T("some errors was happend"));
	}

}



void CjsprinterDLLDlg::OnBnClickedbtnprint()
{
	
}


void CjsprinterDLLDlg::OnBnClickedBtnprinttxt()
{
	UpdateData(TRUE);
	if (SendTemp.GetLength() == 0)
	{
		AfxMessageBox(_T("The input data is empty!"), MB_ICONINFORMATION);
		return;
	}
	char *SendBuf;
	char *temp;
	//USES_CONVERSION;
	//temp= W2A(SendTemp);
	//temp = T2A(SendTemp);
	int codePage = 936;
	int ret = 0;
	int nLength = 0;
	TCHAR*str = SendTemp.GetBuffer();
	nLength = WideCharToMultiByte(codePage, 0, str, -1, NULL, 0, NULL, NULL);
	SendBuf = new char[nLength + 1];
	memset((void*)SendBuf, 0, sizeof(char) * (nLength + 1));
	WideCharToMultiByte(codePage, WC_COMPOSITECHECK, str, -1, SendBuf, nLength, NULL, NULL);
	SendBuf[nLength] = 0;
	int WriteSize = strlen(SendBuf);

	if (m_portselect == 0)
	{
		if (hDllInst)
		{
			int fs = 0;
			int ret = 0;
			char Combuff[100] = { 0 };

			int len = 0;
			UpdateData(TRUE);

			int nLength = m_LptPort.GetLength();
			int nBytes = WideCharToMultiByte(CP_ACP, 0, m_LptPort, nLength, NULL, 0, NULL, NULL);
			WideCharToMultiByte(CP_OEMCP, 0, m_LptPort, nLength, Combuff, nBytes, NULL, NULL);

			fs = uniOpenLpt(Combuff);
			if (fs <= 0)
			{
				AfxMessageBox(_T("uniOpenLpt failed"));
				return;
			}

			ret = uniWrite(fs, (char*)SendBuf, WriteSize);
			if (ret <= 0)
			{
				AfxMessageBox(_T("uniWrite failed"));
				uniClose(fs);
				return;
			}
			uniClose(fs);
		}
	}
	else if (m_portselect == 1)
	{
		if (hDllInst)
		{
			int fs = 0;
			int ret = 0;
			int nbaundrate = 0;
			char Combuff[100] = { 0 };

			int nLength = m_ComPort.GetLength();
			m_baudrate = GetDlgItemInt(IDC_COMBBaudRate);
			int nBytes = WideCharToMultiByte(CP_ACP, 0, m_ComPort, nLength, NULL, 0, NULL, NULL);
			WideCharToMultiByte(CP_OEMCP, 0, m_ComPort, nLength, Combuff, nBytes, NULL, NULL);


			fs = uniOpenCom(Combuff, m_baudrate);
			if (fs <= 0)
			{
				AfxMessageBox(_T("uniOpenCom failed"));
				return;
			}

			ret = uniWrite(fs, (char*)SendBuf, WriteSize);
			if (ret <= 0)
			{
				AfxMessageBox(_T("uniWrite failed"));
				uniClose(fs);
				return;
			}
			uniClose(fs);
		}
	}
	else if (m_portselect == 2)
	{
		if (hDllInst)
		{
			int ret = 0;
			int fs;
			int len = 0;

			//husb = OpenUsbByVidPid(0x1FC9, 0x2016);
			fs = uniOpenUsb();
			if (fs <= 0)
			{
				AfxMessageBox(_T("uniOpenUsb failed"));
				return;
			}
			//husb = OpenUsbByVidPid(0x0483, 0x5743);
			ret = uniWrite(fs, (char*)SendBuf, WriteSize);
			if (ret <= 0)
			{
				AfxMessageBox(_T("uniWrite failed"));
				uniClose(fs);
				return;
			}
			uniClose(fs);
		}
	}
	else
	{
		if (hDllInst)
		{
			char ipbuf[100] = { 0 };
			CString strIp;
			m_ipctr.GetWindowText(strIp);

			int nLength = strIp.GetLength();
			int nBytes = WideCharToMultiByte(CP_ACP, 0, strIp, nLength, NULL, 0, NULL, NULL);
			WideCharToMultiByte(CP_OEMCP, 0, strIp, nLength, ipbuf, nBytes, NULL, NULL);

			int ret = 0;
			int fs;
			BOOL bret;

			bret = uniInitNetSev();
			if (!bret)
			{
				AfxMessageBox(_T("InitNetSev failed"));
				return;
			}

			fs = uniConnectNetPortByIp(ipbuf, 9100, 5000);
			if (fs <= 0)
			{
				AfxMessageBox(_T("uniConnectNetPortByIp failed"));
				return;
			}

			ret = uniWrite(fs, (char*)SendBuf, WriteSize);
			if (ret <= 0)
			{
				AfxMessageBox(_T("uniWrite failed"));
				uniClose(fs);
				return;
			}

			uniClose(fs);
			uniCloseNetServ();
		}
	}
}


void CjsprinterDLLDlg::OnBnClickedBtnprinthex()
{
	UpdateData(TRUE);
	if (m_portselect == 0)
	{
		if (hDllInst)
		{
			int fs = 0;
			int ret = 0;
			byte sendbuff[100] = { 0 };
			char Combuff[100] = { 0 };
			sendbuff[0] = 0x31; // char 1
			sendbuff[1] = 0x32;
			sendbuff[2] = 0x33;
			sendbuff[3] = 0x34;
			sendbuff[4] = 0x35;
			sendbuff[5] = 0x36;
			sendbuff[6] = 0x37;
			sendbuff[7] = 0x38;
			sendbuff[8] = 0x39;
			sendbuff[9] = 0x0A; // 文字使用ASCII编码，以0A作为结束换行

			int len = 0;
			UpdateData(TRUE);

			int nLength = m_LptPort.GetLength();
			int nBytes = WideCharToMultiByte(CP_ACP, 0, m_LptPort, nLength, NULL, 0, NULL, NULL);
			WideCharToMultiByte(CP_OEMCP, 0, m_LptPort, nLength, Combuff, nBytes, NULL, NULL);


			//fs = OpenLptA(Combuff);
			fs = uniOpenLpt(Combuff);
			if (fs <= 0)
			{
				AfxMessageBox(_T("uniOpenLpt failed"));
				return;
			}

			ret = uniWrite(fs, (char*)sendbuff, 10);
			if (ret <= 0)
			{
				AfxMessageBox(_T("uniWrite failed"));
				uniClose(fs);
				return;
			}
			uniClose(fs);
		}
	}
	else if (m_portselect == 1)
	{
		if (hDllInst)
		{
			int fs = 0;
			int ret = 0;
			int nbaundrate = 0;
			byte sendbuff[100] = { 0 };
			char Combuff[100] = { 0 };
			sendbuff[0] = 0x31; // char 1
			sendbuff[1] = 0x32;
			sendbuff[2] = 0x33;
			sendbuff[3] = 0x34;
			sendbuff[4] = 0x35;
			sendbuff[5] = 0x36;
			sendbuff[6] = 0x37;
			sendbuff[7] = 0x38;
			sendbuff[8] = 0x39;
			sendbuff[9] = 0x0A; // 文字使用ASCII编码，以0A作为结束换行

			int len = 0;
			UpdateData(TRUE);

			int nLength = m_ComPort.GetLength();
			m_baudrate = GetDlgItemInt(IDC_COMBBaudRate);
			int nBytes = WideCharToMultiByte(CP_ACP, 0, m_ComPort, nLength, NULL, 0, NULL, NULL);
			WideCharToMultiByte(CP_OEMCP, 0, m_ComPort, nLength, Combuff, nBytes, NULL, NULL);


			fs = uniOpenCom(Combuff, m_baudrate);
			if (fs <= 0)
			{
				AfxMessageBox(_T("uniOpenCom failed"));
				return;
			}

			ret = uniWrite(fs, (char*)sendbuff, 10);
			if (ret <= 0)
			{
				AfxMessageBox(_T("uniWrite failed"));
				uniClose(fs);
				return;
			}
			uniClose(fs);
		}
	}
	else if (m_portselect == 2)
	{
		if (hDllInst)
		{
			int husb = 0;
			int ret = 0;
			byte sendbuff[100] = { 0 };
			sendbuff[0] = 0x31; // char 1
			sendbuff[1] = 0x32;
			sendbuff[2] = 0x33;
			sendbuff[3] = 0x34;
			sendbuff[4] = 0x35;
			sendbuff[5] = 0x36;
			sendbuff[6] = 0x37;
			sendbuff[7] = 0x38;
			sendbuff[8] = 0x39;
			sendbuff[9] = 0x0A; // 文字使用ASCII编码，以0A作为结束换行

			int len = 0;

			//husb = OpenUsbByVidPid(0x1FC9, 0x2016);
			husb = uniOpenUsb();
			if (husb <= 0)
			{
				AfxMessageBox(_T("uniOpenUsb failed"));
				return;
			}
			//husb = OpenUsbByVidPid(0x0483, 0x5743);
			ret = uniWrite(husb, (char*)sendbuff, 10);
			if (ret <= 0)
			{
				AfxMessageBox(_T("uniWrite failed"));
				uniClose(husb);
				return;
			}
			uniClose(husb);
		}
	}
	else
	{
		if (hDllInst)
		{
			byte sendbuff[100] = { 0 };
			sendbuff[0] = 0x31; // char 1
			sendbuff[1] = 0x32;
			sendbuff[2] = 0x33;
			sendbuff[3] = 0x34;
			sendbuff[4] = 0x35;
			sendbuff[5] = 0x36;
			sendbuff[6] = 0x37;
			sendbuff[7] = 0x38;
			sendbuff[8] = 0x39;
			sendbuff[9] = 0x0A; // 文字使用ASCII编码，以0A作为结束换行

			char ipbuf[100] = { 0 };

			UpdateData(TRUE);

			CString strIp;
			m_ipctr.GetWindowText(strIp);

			int nLength = strIp.GetLength();
			int nBytes = WideCharToMultiByte(CP_ACP, 0, strIp, nLength, NULL, 0, NULL, NULL);
			WideCharToMultiByte(CP_OEMCP, 0, strIp, nLength, ipbuf, nBytes, NULL, NULL);

			int ret = 0;
			int fs;
			BOOL bret;

			bret = uniInitNetSev();
			if (!bret)
			{
				AfxMessageBox(_T("InitNetSev failed"));
				return;
			}

			fs = uniConnectNetPortByIp(ipbuf, 9100, 5000);
			if (fs <= 0)
			{
				AfxMessageBox(_T("uniConnectNetPortByIp failed"));
				return;
			}

			ret = uniWrite(fs, (char*)sendbuff, 10);
			if (ret <= 0)
			{
				AfxMessageBox(_T("uniWrite failed"));
				uniClose(fs);
				return;
			}

			uniClose(fs);
			uniCloseNetServ();
		}
	}
}


void CjsprinterDLLDlg::OnBnClickedBtnread()
{
	UpdateData(TRUE);
	if (m_portselect == 0)
	{
	   // most lpt printer is not return data. so we don`t suppose the code 
	}
	else if (m_portselect == 1)
	{
		if (hDllInst)
		{
			int fs = 0;
			byte sendbuff[100] = { 0 };
			char Combuff[100] = { 0 };
			sendbuff[0] = 0x10; // char 1
			sendbuff[1] = 0x04;
			sendbuff[2] = 0x01;

			int len = 0;
			int ret = 0;
			UpdateData(TRUE);

			int nLength = m_ComPort.GetLength();
			int nBytes = WideCharToMultiByte(CP_ACP, 0, m_ComPort, nLength, NULL, 0, NULL, NULL);
			WideCharToMultiByte(CP_OEMCP, 0, m_ComPort, nLength, Combuff, nBytes, NULL, NULL);

			m_baudrate = GetDlgItemInt(IDC_COMBBaudRate);
			fs = uniOpenCom(Combuff, m_baudrate);
			if (fs <= 0)
			{
				AfxMessageBox(_T("uniOpenCom failed"));
				return;
			}

			ret = uniWrite(fs, (char*)sendbuff, 3);
			if (ret <= 0)
			{
				AfxMessageBox(_T("uniWrite failed"));
				uniClose(fs);
				return;
			}


			ret = uniRead(fs, (char*)sendbuff, sizeof(sendbuff));
			if (ret <= 0)
			{
				AfxMessageBox(_T("uniRead failed"));
				uniClose(fs);
				return;
			}
			else
			{
				for (int i = 0; i < ret; i++)
				{
					CString msg;
					msg.Format(_T("The %d Received hex byte is : 0x%x"), i + 1, sendbuff[i]);
					AfxMessageBox(msg);
				}

			}

			uniClose(fs);
		}
	}
	else if (m_portselect == 2)
	{
		if (hDllInst)
		{
			int fs = 0;
			byte sendbuff[100] = { 0 };
			sendbuff[0] = 0x10; // char 1
			sendbuff[1] = 0x04;
			sendbuff[2] = 0x01;

			int ret = 0;

			fs = uniOpenUsb();
			if (fs <= 0)
			{
				AfxMessageBox(_T("uniOpenUsb failed"));
				return;
			}
			//husb = OpenUsbByVidPid(0x0483, 0x5743);
			ret = uniWrite(fs, (char*)sendbuff, 3);
			if (ret <= 0)
			{
				AfxMessageBox(_T("uniWrite failed"));
				uniClose(fs);
				return;
			}

			ret = uniRead(fs, (char*)sendbuff, sizeof(sendbuff));
			if (ret <= 0)
			{
				AfxMessageBox(_T("uniRead failed"));
				uniClose(fs);
				return;
			}
			else
			{
				for (int i = 0; i < ret; i++)
				{
					CString msg;
					msg.Format(_T("The %d Received hex byte is : 0x%x"), i + 1, sendbuff[i]);
					AfxMessageBox(msg);
				}

			}
			uniClose(fs);
		}
	}
	else
	{
		if (hDllInst)
		{
			byte sendbuff[100] = { 0 };
			char ipbuf[100] = { 0 };


			UpdateData(TRUE);
			CString strIp;
			m_ipctr.GetWindowText(strIp);

			int nLength = strIp.GetLength();
			int nBytes = WideCharToMultiByte(CP_ACP, 0, strIp, nLength, NULL, 0, NULL, NULL);
			WideCharToMultiByte(CP_OEMCP, 0, strIp, nLength, ipbuf, nBytes, NULL, NULL);

			int ret = 0;
			int fs;
			BOOL bret;

			bret = uniInitNetSev();
			if (!bret)
			{
				AfxMessageBox(_T("uniInitNetSev failed"));
				return;
			}

			fs = uniConnectNetPortByIp(ipbuf, 9100, 5000);
			if (!fs)
			{
				AfxMessageBox(_T("uniConnectNetPortByIp failed"));
				return;
			}

			sendbuff[0] = 0x10; // char 1
			sendbuff[1] = 0x04;
			sendbuff[2] = 0x01;

			ret = uniWrite(fs, (char*)sendbuff, 3);
			if (ret <= 0)
			{
				AfxMessageBox(_T("uniWrite failed"));
				uniClose(fs);
				return;
			}

			ret = uniRead(fs, (char*)sendbuff, sizeof(sendbuff));
			if (ret <= 0)
			{
				AfxMessageBox(_T("uniRead failed"));
				uniClose(fs);
				return;
			}
			else
			{
				for (int i = 0; i < ret; i++)
				{
					CString msg;
					msg.Format(_T("The %d Received hex byte is : 0x%x"), i + 1, sendbuff[i]);
					AfxMessageBox(msg);
				}

			}

			uniClose(fs);
			uniCloseNetServ();
		}
	}
}


void CjsprinterDLLDlg::OnBnClickedBtncall()
{
	UpdateData(TRUE);
	if (m_strfunc == _T("uniPrintImg1d76"))
	{
		PrintImg1d76();
	}
	else if (m_strfunc == _T("uniDownLogo"))
	{
		DownLogo();
	}
	else if (m_strfunc == _T("uniprintBmpInFLASH"))
	{
		int fs = uniOpen();
		uniprintBmpInFLASH(fs,0x01,0x30);
		uniClose(fs);
	}
	else if (m_strfunc == _T("uniPrintImg1b2a"))
	{
		PrintImg1b2a();
	}
	
	
}
