void Dbhelp::OnOpen() 
{	
	//UpdateData(TRUE);
	LPCTSTR strCom;
	DCB dcb;
	COMMTIMEOUTS comtimeout;
	m_bSerial=TRUE;
	if (m_hComm!=NULL)
	{
		CloseHandle(m_hComm);  //close communication port
		m_hComm=NULL;
	}
	//
	m_hComm=CreateFile(TEXT("COM2:"),GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	if (m_hComm==INVALID_HANDLE_VALUE)
	{		
		m_hComm=NULL;
		return ;
	}
	comtimeout.ReadIntervalTimeout =MAXDWORD;
	comtimeout.ReadTotalTimeoutConstant =1000;
	comtimeout.ReadTotalTimeoutMultiplier =0;
	comtimeout.WriteTotalTimeoutConstant =-1;
	comtimeout.WriteTotalTimeoutMultiplier =20000;
	SetCommTimeouts(m_hComm,&comtimeout);   //set over time time 
	if (m_bSerial)
	{
		dcb.DCBlength =sizeof(DCB);
		GetCommState(m_hComm,&dcb);  //take communication port original parameters		dcb.BaudRate =9600;    //baud rate
		dcb.ByteSize =8;         //data bits
		dcb.Parity =NOPARITY;    //parity check bit 
		dcb.StopBits =ONESTOPBIT; //stop bit
		dcb.fBinary =TRUE;
		dcb.fParity =TRUE;
		dcb.fOutxCtsFlow =0;
		dcb.fOutxDsrFlow =0;
		dcb.fRtsControl =1;
		dcb.fDtrControl =1;定通讯口参数
		SetCommState(m_hComm,&dcb);  //set communication port parameters 
		EscapeCommFunction(m_hComm,SETDTR);
	}
}

DWORD Dbhelp::WriteData(void* wbuf, DWORD dwLen)
{
	DWORD dwWrited;
	if (!WriteFile(m_hComm,wbuf,dwLen,&dwWrited,NULL)) 
	{
		return 0;
	}
	FlushFileBuffers(m_hComm);
	return dwWrited;
}

The following code snippet is sending process 

	cmd[0]=27;
	cmd[1]=64;
	help.WriteData(cmd,2);	
	cmd[0]=27;
	cmd[1]=33;
	cmd[2]=16;

	char ticket_head[128]={0};
	char patientName[10]={0};
	char newNum[10]={0};
	char branchName[10]={0};
	char typeName[10]={0};
	char docName[10]={};

	int res= lb_pjsua_getTriageTicketPrefix(ticket_head);
	help.WriteData(cmd,3);
	sprintf(wdata,"        ");
	strcat(wdata,ticket_head);
	strcat(wdata,"\n\n");
	help.WriteData(wdata,strlen(wdata));
	cmd[2]=0;
	help.WriteData(cmd,3);

	sprintf(wdata,"………………………………………………\n");
	strcat(wdata,"\n");
	help.WriteData(wdata,strlen(wdata));

	cmd[2]=0;
	help.WriteData(cmd,3);

	sprintf(wdata,"your name：   ");

	wch2ch(patientName,name.GetBuffer(),sizeof(patientName));
	strcat(wdata,patientName);

	strcat(wdata,"\n");
	help.WriteData(wdata,strlen(wdata));

	sprintf(wdata,"your number：   ");
	wch2ch(newNum,number.GetBuffer(),sizeof(newNum));
	strcat(wdata,newNum);

	strcat(wdata,"\n");
	help.WriteData(wdata,strlen(wdata));

	sprintf(wdata,"administrative office：   ");

	wch2ch(branchName,departmentname.GetBuffer(),sizeof(branchName));
	strcat(wdata,branchName);

	strcat(wdata,"\n");
	help.WriteData(wdata,strlen(wdata));

	sprintf(wdata,"outpatient service type：   ");
	wch2ch(typeName,typaname.GetBuffer(),sizeof(typeName));
	strcat(wdata,typeName);

	strcat(wdata,"\n");
	help.WriteData(wdata,strlen(wdata));

	cmd[2]=0;
	help.WriteData(cmd,3);
	sprintf(wdata,"doctor：   ");
	wch2ch(docName,docname.GetBuffer(),sizeof(docName));
	strcat(wdata,docName);

	strcat(wdata,"\n");	
	help.WriteData(wdata,strlen(wdata));
	cmd[2]=0;
	help.WriteData(cmd,3);
	sprintf(wdata,"the people who before you \n");
	strcat(wdata,"pls wait patiently，thanks for cooperation\n\n");	
	help.WriteData(wdata,strlen(wdata));
	cmd[2]=0;
	help.WriteData(cmd,3);
	sprintf(wdata,"………………………………………………\n\n");
	help.WriteData(wdata,strlen(wdata));	
	cmd[2]=0;
	help.WriteData(cmd,3);

	sprintf(wdata,"registration time： ");
	strcat(wdata,cregtime);
	strcat(wdata,"\n\n");
	help.WriteData(wdata,strlen(wdata));

	//LF feed paper
	cmd[0]=10;
	cmd[1]=10;
	help.WriteData(cmd,2);
	//feed paper dot
	cmd[0]=27;
	cmd[1]=74;
	cmd[2]=150;
	help.WriteData(cmd,3);
	//cut paper
	cmd[0]=29;
	cmd[1]=86;
	cmd[2]=1;
	cmd[3]=49;
	help.WriteData(cmd,3);
	lb_triage_updateAllDeviceDispMsg_toNewPatient();

	OnClose();