Link to home
Start Free TrialLog in
Avatar of showbix
showbix

asked on

What's wrong with this code?

I have a TC++ 4.5 program and would expect the below output

Expected output
+++++++++++++++++++++++++++++++++++++++++
RECOVERY  : BERMULA            
Trkh: 02/03/2004, Masa: 13:29:21
================================
Transaksi :             00246155
================================
RECOVERY  : SELESAI            
Trkh: 02/03/2004, Masa: 13:29:24
================================
********************************


---- Laporan Sign On J/Wang ----
ID J/Wang: 4417J01   P/K: 02    
W.Pendahuluan: RM          0.00
Trkh: 02/03/2004, Masa: 13:30:20
================================


      Welcome Friend To        
      ABC Comm Limited          
--------------------------------
Nombor Transaksi :      00246155
00459089 KADFON RM 5.00        
039 90392                      
    5.00     1  RM         5.00
                ----------------
Jumlah Item     RM         5.00
00459090 KADFON RM 20.00        
039 90394                      
   20.00     2  RM        40.00
                ----------------
Jumlah Item     RM        40.00
                ----------------
Jumlah Kutipan  RM        45.00
Jumlah Cukai    RM         0.00
Jumlah (Diskaun)RM         0.00
                ----------------
Jum.Perlu Bayar RM        45.00
                ================
Bayaran:                        
TUNAI           RM        45.00
--------------------------------
       Sila Datang Lagi        
         TERIMA KASIH          
================================
141702 4417J01 02032004 13:30:55
+++++++++++++++++++++++++++++++++++++++++


But the output happen to be like this, which is not correct
+++++++++++++++++++++++++++++++++++++++++
================================
RECOVERY  : BERMULA              
Trkh: 02/03/2004, Masa: 13:29:21
================================
Transaksi :             00246155
================================
RECOVERY  : SELESAI              
Trkh: 02/03/2004, Masa: 13:29:24
================================
********************************


---- Laporan Sign On J/Wang ----I




      Welcome Friend To          



      Welcome Friend To          
++++++++++++++++++++++++++++++++++++++++

This is my code.
++++++++++++++++++++++++++++++++++++++++
#define  STRICT
#include <windows.h>
#include <windowsx.h>
#include <string.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#pragma hdrstop
#include "helpex.h"
#include "helpids.h"
#pragma argsused

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                                     LPSTR lpCmdLine, int nCmdShow)
{
      MSG msg;

      if (!hPrevInstance)
             if (!InitApplication(hInstance))
             return (FALSE);

      if (!InitInstance(hInstance, nCmdShow))
             return (FALSE);

      while (GetMessage(&msg, NULL, 0, 0)) {

            /* Only translate message if it is not an accelerator message */
            if (!TranslateAccelerator(hWnd, hAccTable, &msg)) {

                   TranslateMessage(&msg);
                   DispatchMessage(&msg);
            }
      }
      return (msg.wParam);
}

BOOL InitApplication(HINSTANCE hInstance)
{

      WNDCLASS wc;

      wc.style = 0;
      wc.lpfnWndProc = MainWndProc;
      wc.cbClsExtra = 0;
      wc.cbWndExtra = 0;
      wc.hInstance = hInstance;
      wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
      wc.hCursor = LoadCursor(NULL, IDC_ARROW);
      wc.hbrBackground = (HBRUSH)( COLOR_MENU+1 ); //GetStockObject(WHITE_BRUSH);
      wc.lpszMenuName ="MainMenu";
      wc.lpszClassName = "PRNDRV16";

      RegisterClass(&wc);

      wc.style = 0;
      wc.lpfnWndProc = DeviceComm;
      wc.cbClsExtra = 0;
      wc.cbWndExtra = 0;
      wc.hInstance = hInstance;
      wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
      wc.hCursor = LoadCursor(NULL, IDC_ARROW);
      wc.hbrBackground = (HBRUSH)( COLOR_MENU+1 ); //GetStockObject(WHITE_BRUSH);
      wc.lpszMenuName =NULL;
      wc.lpszClassName = "CommDvc";

      RegisterClass(&wc);

      return TRUE;


}


BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
      char sMsg[80];
   char crlf[3]={13, 10, 0};
      hInst = hInstance;

      hAccTable = LoadAccelerators(hInst, "HelpexAcc");

      hWnd = CreateWindow(
             "PRNDRV16",
             "TMB Print Driver, Siemens ISB 2004",
             WS_OVERLAPPEDWINDOW,
             100, //CW_USEDEFAULT,
             100, //CW_USEDEFAULT,
             350, //CW_USEDEFAULT,
             500, //CW_USEDEFAULT,
             NULL,
             NULL,
             hInstance,
             NULL
             );

      if (!hWnd)
             return (FALSE);

      ShowWindow(hWnd, nCmdShow);
      UpdateWindow(hWnd);

      hCommDvc = CreateWindow(
             "CommDvc",
             "Siemens Device Control",
             WS_OVERLAPPEDWINDOW,
             10,  //CW_USEDEFAULT,
             10,  //CW_USEDEFAULT,
             300, //CW_USEDEFAULT,
             350, //CW_USEDEFAULT,
             NULL,
             NULL,
             hInstance,
             NULL
             );

      if (!hCommDvc)
             return (FALSE);

      ShowWindow(hCommDvc, nCmdShow);
      UpdateWindow(hCommDvc);

      fnLoadSetting();

      gGlobalEnd = 0;
      gGlobalOverwrite = 0;
      gIdleState = 0;

      if ((gSelectedPort == 1) | (gSelectedPort == 2))
      {
            if(!OpenPrinter(gSelectedPort))
            {
                   ExitSystem();
            }
            else
            {
                   if(gSelectedPort==1)
                   {
                        SetMenuState(1, CM_COM1, TRUE);
                        SetMenuState(1, CM_COM2, FALSE);
                   }
                   else if(gSelectedPort==2)
                   {
                        SetMenuState(1, CM_COM1, FALSE);
                        SetMenuState(1, CM_COM2, TRUE);
                   }
            }

      }
      else
      {
             sprintf(sMsg, "COM%d is invalid. Default to COM1 port.", gSelectedPort);
             MessageBox(hWnd, sMsg, "DEFAULT", MB_OK);
             if(!OpenPrinter(gSelectedPort))
                  ExitSystem();
             else
             {
                  if(gSelectedPort==1)
                        SetMenuState(1, CM_COM1, TRUE);
                  else
                        SetMenuState(1, CM_COM2, TRUE);
             }
      }

      if(strcmp("tmh5k2", gSelectedPrinter)==0)
      {
                  load_epson_tmh5k2();
                  SetMenuState(2, CM_EPSON, TRUE);
                  SetMenuState(2, CM_ND70, FALSE);
                  OutputScreen (hwndEdit, "COMMAND-> EPSON tmh5000ii Command Set Loaded");
      }
      else if(strcmp("nd70", gSelectedPrinter)==0)
      {
                  load_sni_nd70();
                  SetMenuState(2, CM_ND70, TRUE);
                  SetMenuState(2, CM_EPSON, FALSE);
                  OutputScreen (hwndEdit, "COMMAND-> Siemens Nixdorf nd70 Command Set Loaded");
      }
      else
      {
                  load_epson_tmh5k2();
                  strcpy(gSelectedPrinter, "tmh5k2");
                  SetMenuState(2, CM_EPSON, TRUE);
                  SetMenuState(2, CM_ND70, FALSE);
                  OutputScreen (hwndEdit, "COMMAND-> EPSON tmh5000ii Command Set Loaded");
      }

      OutputScreen (hwndEdit, "COMMAND-> Init Printer");
      strcpy(gsPollFile, "C:\\tmp\\devpoll");
      strcpy(gsDataFile, "C:\\tmp\\devcon");

      return (TRUE);

}

#pragma warn -eff
LRESULT CALLBACK _export MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
      DLGPROC lpProcAbout;
      DWORD   dwHelpContextId;
      //HDC hDC;
      PAINTSTRUCT ps;
      //TIMERPROC fnTimer;

      switch (message) {
             case WM_CREATE:
                  hwndEdit = CreateWindow(
                               "EDIT",     // predefined class
                               NULL,       // no window title
                               WS_CHILD | WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_VSCROLL |
                                    ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL |
                                    ES_WANTRETURN,
                               0, 0, 0, 0, // set size in WM_SIZE message
                               hWnd,       // parent window
                               (HMENU) GetMenu(hWnd),  // edit control ID
                               (HINSTANCE) hInst, // associate it with the application instance
                               NULL);


                  hWndBtn = CreateWindow(
                               "BUTTON",     // predefined class
                               "Reset Printer",       // no window title
                               WS_CHILD | WS_VISIBLE | WS_BORDER,
                               240, 410, 100, 30, // set size in WM_SIZE message
                               hWnd,       // parent window
                               (HMENU) CMD_RESET, //GetMenu(hWnd),  // edit control ID
                               (HINSTANCE) hInst, // associate it with the application instance
                               NULL);

                  SetTimer(hWnd, TIMER_ID, 2000, NULL);
                  break;

             case WM_QUIT:
                  ExitSystem();
                  break;

             case WM_TIMER:
                  if(wParam==TIMER_ID && !gBool)
                        OutputPoll();                   
                  break;

             case WM_SETFOCUS:
                        SetFocus(hwndEdit);
                        break;

             case WM_SIZE:

                        // Make the edit control the size of the window's
                        // client area.

                        MoveWindow(hwndEdit,
                               0, 0,           // starting x- and y-coordinates
                               LOWORD(lParam), // width of client area
                               HIWORD(lParam)-50, // height of client area
                               TRUE);

                        MoveWindow(hWndBtn,
                               LOWORD(lParam) - 110,           // starting x- and y-coordinates
                               HIWORD(lParam) - 35,
                               100, // width of client area
                               30, // height of client area
                               TRUE);

                        break;

             case WM_COMMAND:
                  switch (GET_WM_COMMAND_ID(wParam, lParam))
                  {
                        case CMD_RESET:
                              MessageBox(hWnd, "Reset Printer", "OK", MB_OK);
                              gGlobalOverwrite = 100;
                              break;

                        case CM_TSTCUT:

                              printer_PrintLineFeed();
                              printer_PrintLineFeed();
                              printer_PrintLineFeed();
                              printer_PrintLineFeed();
                              printer_PrintLineFeed();
                              printer_PrintReceiptHalfCut();
                              OutputScreen (hwndEdit, "DATA-> Receipt Cut");
                              break;

                        case CM_TSTLOGO:
                              printer_PrintReceiptLogo();
                              OutputScreen (hwndEdit, "DATA-> Receipt Cut");
                              break;

                        case CM_TSTPRN:
                              OnMenuTestPrint();
                              break;

                        case CM_TSTDSP:
                              printer_ClearDisplay();
                              printer_PrintDisplay("---- Laporan Sign On J/Wang ----ID J/Wang: 4417J01  ");
                              break;

                        case CM_COM1:
                              SetMenuState(1, CM_COM1, FALSE);
                              SetMenuState(1, CM_COM2, FALSE);
                              if(OpenPrinter(1))
                              {
                                    InitPrinter();
                                    SetMenuState(1, CM_COM1, TRUE);
                                    gSelectedPort = 1;
                                    OutputScreen (hwndEdit, "COMMAND-> COM Port 1 Selected");
                              }
                              else
                              {
                                    OutputScreen (hwndEdit, "COMMAND-> COM Port 1 Open Failed");
                              }
                              break;

                        case CM_COM2:
                              SetMenuState(1, CM_COM1, FALSE);
                              SetMenuState(1, CM_COM2, FALSE);
                              if(OpenPrinter(2))
                              {
                                    InitPrinter();
                                    SetMenuState(1, CM_COM2, TRUE);
                                    gSelectedPort = 2;
                                    OutputScreen (hwndEdit, "COMMAND-> COM Port 2 Selected");
                              }
                              else
                              {
                                    OutputScreen (hwndEdit, "COMMAND-> COM Port 2 Open Failed");
                              }
                              break;

                        case CM_EPSON:
                              SetMenuState(2, CM_EPSON, TRUE);
                              SetMenuState(2, CM_ND70, FALSE);
                              load_epson_tmh5k2();
                              InitPrinter();
                              strcpy(gSelectedPrinter, "tmh5k2");
                              OutputScreen (hwndEdit, "COMMAND-> EPSON tmh5000ii Command Set Loaded");
                              break;

                        case CM_ND70:
                              SetMenuState(2, CM_EPSON, FALSE);
                              SetMenuState(2, CM_ND70, TRUE);
                              load_epson_tmh5k2();
                              InitPrinter();
                              strcpy(gSelectedPrinter, "nd70");
                              OutputScreen (hwndEdit, "COMMAND-> Siemens Nixdorf nd70 Command Set Loaded");
                              break;

                        case CM_ABOUT:
                              lpProcAbout = (DLGPROC)MakeProcInstance((FARPROC)About, hInst);
                              DialogBox(hInst, "AboutBox", hWnd, lpProcAbout);
                              FreeProcInstance((FARPROC)lpProcAbout);
                              break;

                        case CM_DVC:
                              SetMenuState(0, CM_DVC, TRUE);
                              ShowWindow(hCommDvc, SW_SHOW | SW_RESTORE);
                              UpdateWindow(hCommDvc);
                              break;

                        case CM_EXIT:
                              ExitSystem();
                              break;

                        default:
                              return (DefWindowProc(hWnd, message, wParam, lParam));
                  }

                  break;

             case WM_DESTROY:
                    KillTimer(hWnd, TIMER_ID);
                    WinHelp(hWnd,szHelpFileName,HELP_QUIT,0L);
           if(hComm >= 0)CloseComm(hComm);
                    PostQuitMessage(0);
           break;

             default:
           return (DefWindowProc(hWnd, message, wParam, lParam));
   }

      return 0L;
}
#pragma warn .eff

#pragma argsused

BOOL FAR PASCAL _export About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
   switch (message) {
             case WM_INITDIALOG:
                    return (TRUE);

       case WM_COMMAND:
                  if (GET_WM_COMMAND_ID(wParam, lParam) == IDOK) {
               EndDialog(hDlg, TRUE);
               return (TRUE);
           }
                    break;

             case WM_DESTROY:
                  DestroyWindow(hDlg);
                  break;
      }

      return (FALSE);
}

BOOL FAR PASCAL _export DeviceComm(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{

      switch (message) {
             //case WM_INITDIALOG:
             //        hCommDvc = hDlg;
             //    return (TRUE);
             case WM_CREATE:
                        hComm = -1;

                        hwndEditDvc = CreateWindow(
                               "EDIT",     // predefined class
                               NULL,       // no window title
                               WS_CHILD | WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_VSCROLL |
                                    ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL |
                                    ES_WANTRETURN,
                               0, 0, 0, 0, // set size in WM_SIZE message
                               hDlg,       // parent window
                               (HMENU) GetMenu(hWnd),  // edit control ID
                               (HINSTANCE) hInst, // associate it with the application instance
                               NULL);


                        hWndBtnDvc = CreateWindow(
                               "BUTTON",     // predefined class
                               "OpenCOMM",       // no window title
                               WS_CHILD | WS_VISIBLE | WS_BORDER,
                               180, 270, 100, 30, // set size in WM_SIZE message
                               hDlg,       // parent window
                               (HMENU) CMD_OPENPORT, //GetMenu(hWnd),  // edit control ID
                               (HINSTANCE) hInst, // associate it with the application instance
                               NULL);


                        break;

             case WM_COMMAND:
                  if (GET_WM_COMMAND_ID(wParam, lParam) == CMD_OPENPORT)
                  {
                        if(hComm >=0)
                              ClosePrinter();
                        else
                              OpenPrinter(gSelectedPort);
                  }
                  break;

             case WM_SIZE:

                        // Make the edit control the size of the window's
                        // client area.

                        MoveWindow(hwndEditDvc,
                               0, 0,           // starting x- and y-coordinates
                               LOWORD(lParam), // width of client area
                               HIWORD(lParam)-50, // height of client area
                               TRUE);

                        MoveWindow(hWndBtnDvc,
                               LOWORD(lParam) - 110,           // starting x- and y-coordinates
                               HIWORD(lParam) - 35,
                               100, // width of client area
                               30, // height of client area
                               TRUE);
                        break;

             case WM_SETFOCUS:
                        SetFocus(hWndBtnDvc);
                        break;

             case WM_DESTROY:
                  //DestroyWindow(hDlg);
                  ShowWindow(hDlg, SW_SHOW);
                  break;

             default:
                    return (DefWindowProc(hDlg, message, wParam, lParam));
      }

      return (FALSE);
}

void SetMenuState(int nPos, UINT nMenuID, BOOL fOwnerDraw)
{
      HMENU hMenu;
      //BOOL fOwnerDraw;

      hMenu = GetSubMenu(GetMenu(hWnd), nPos);
      //fOwnerDraw = GetMenuState(hMenu, nMenuID, MF_BYCOMMAND) & MF_CHECKED;
      CheckMenuItem(hMenu, nMenuID, MF_BYCOMMAND | (fOwnerDraw ? MF_CHECKED : MF_UNCHECKED));

}

void fnLoadSetting()
{
      WORD wReturn;
      int nRet;
      char sINI[25];
      char szBuf[25];
      //char sMsg[80];

      wReturn = GetWindowsDirectory((LPSTR)sINI, sizeof(sINI));
      strcat(sINI, "\\tmshell.ini");

      nRet = GetPrivateProfileString("TMB Printer", "PRT", "tmh5k2",
                                                                  szBuf, sizeof(szBuf), sINI);

      if(nRet == 0)
            MessageBox(hWnd, "Application Start Failed, Parameters:TMB Printer->PRT Referential Error!!", "ERROR", MB_ICONSTOP);
      else
            strcpy(gSelectedPrinter, szBuf);

      nRet = GetPrivateProfileString("TMB Printer", "COM", "1",
                                                                  szBuf, sizeof(szBuf), sINI);

      if(nRet == 0)
            MessageBox(hWnd, "Application Start Failed, Parameters:TMB Printer->COM Referential Error!!", "ERROR", MB_ICONSTOP);
      else
            gSelectedPort = atoi(szBuf);

}

void ExitSystem()
{
      if(MessageBox(0, "Are you sure you want to quit the system ?", "QUIT", MB_YESNO)==IDYES)
      {
            //KillTimer(hWnd, TIMER_ID);
            DestroyWindow(hWnd);
      }
}

void OutputScreen(HWND handle, char sMsg[])
{
      static int entry;
      static int entry2;
      char *szBuff=NULL;
      char crlf[3]={13, 10, 0};
      int length;
      int ret=0;

      if(handle==hwndEditDvc)
      {
            entry++;
            if(entry>10)
            {
                  entry=0;
                  //strcpy(szBuff, "");
                  SendMessage(handle,WM_SETTEXT, 0, (LPARAM)"");
            }
      }

      length = SendMessage(handle, WM_GETTEXTLENGTH, 0, 0L);
      if(length > 0)// && length < 1024)
      {
            ret = (length) + strlen(sMsg) + 10;
            szBuff = (char *)GlobalAlloc(GPTR, ret);
            if(szBuff==NULL)
            {
                  SendMessage(handle,WM_SETTEXT, 0, sMsg);
            }
            else
            {
                  ret = SendMessage(handle, WM_GETTEXT, ret, (LPARAM)szBuff);
                  strcat(szBuff, sMsg);
                  strcat(szBuff, crlf);
                  SendMessage(handle,WM_SETTEXT, 0, (LPARAM)szBuff);
            }
            GlobalFree((HGLOBAL)szBuff);
      }
      else
      {
            SendMessage(handle,WM_SETTEXT, 0, (LPARAM)sMsg);
      }

      UpdateWindow(handle);


}

void OnMenuTestPrint()
{
      FILE *pFile;
      char tmp[100]={0};

      printer_Select();
      printer_PrintReceipt();
      printer_PrintReceiptLogo();
      printer_Print("123456789x123456789x123456789x123456789x12");
      printer_PrintLineFeed();
      printer_Print("abcdefghijklmnopqrstuvwxyz");
      printer_PrintLineFeed();
      printer_Print("!@#$%^&*()_+~`-=[]{}\|;:',<.>/?");
      printer_PrintLineFeed();
      printer_Print("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
      printer_PrintLineFeed();
      printer_PrintLineFeed();
      printer_PrintLineFeed();
      printer_PrintLineFeed();
      printer_PrintLineFeed();
      printer_PrintReceiptHalfCut();
   
}

//void CALLBACK OutputPoll(HWND hWnd, UINT msg, UINT timerID, DWORD dwTime)
void OutputPoll()
{
      int iState=0;
      char sPollData[80]={0};
      char sReadData[2640]={0};
      char sCommand[12];
      char sTemp[2640]={0};
      char sTemp3[2640]={0};
      char sTemp2[2640]={0};
      char sData[2650];
      char sFunction[80]={0};
      char sCutForm[80]={0};
      char sCutFormDtl[80]={0};
      int iLength=0;
      int iDataLockRetry=0;
      int iLoopData=0;
      char sMsg[80]={0};
      int bLoop=0;
      int iNCol=0;

      iState = 100;
      iDataLockRetry = 0;
      bLoop = 1;

      //MessageBox(hWnd, "Processing","OK", MB_OK);
      gBool = TRUE;
      while(bLoop==1)
      {
            bLoop = 0;
            if(iState==100)
            {
                  gIdleState = 1;
                  iState = 600;

                  if(file_check(gsPollFile) > 0)
                  {
                        if(file_poll_lock(gsPollFile) == 0)
                        {
                              //sPollData = file_read(file_poll);
                              file_read(file_poll, sPollData);
                              if(strcmp(sPollData,"WRITE=1")==0)
                              {
                                    gIdleState = 0;
                                    iState = 200;
                              }
                        }
                  }
            }// state = 100

            if(iState==200)
            {
                  if (file_data_lock(gsDataFile) == 0)
                  {
                        iDataLockRetry = 0;
                        iState = 300;
                  }
                  else
                        iDataLockRetry++;
                  if (iDataLockRetry >= 10)
                  {
                        iDataLockRetry = 0;
                        iState = 600;
                  }

            }// state = 200

            if(iState==300)
            {
                  //sReadData = file_read(file_data);
                  file_read(file_data, sReadData);
                  if(strlen(sReadData) != 0)
                  {
                         if(strcmp(sReadData, "EOF") == 0)
                                iState = 500;
                         else
                         {
                                iState = 400;
                                bLoop = 1;
                 strcpy(sData, "");
                         }
                         //MessageBox(hWnd, sReadData, "OK", MB_OK);
                  }
                  else
                        iState = 300;
            }//state = 300

            if(iState==400)
            {
                  iState = 300;
                  if(strncmp(sReadData, "CUTFORM_DTL", 11) == 0)
                  {
                        strcpy(sCommand, "CUTFORM_DTL");
                        Mid(sReadData, 13, 0, sData);
                        Trim(sData);
                        strcpy(sCutFormDtl, sData);
                  }
                  else if(strncmp(sReadData, "LENGTH_COL", 10) == 0)
                  {
                        strcpy(sCommand, "LENGTH_COL");
                        Mid(sReadData, 12, 0, sData);
                        Trim(sData);
                        iNCol = atoi(sData);
                        //if(iNCol == 0)
                        //{
                        //      MessageBox(hWnd, "Problem with atoi(LENGTH_COL).", "ERR", MB_OK);
                        //      sprintf(sMsg, "READ=>%s,DATA=>%s,ATOI=>%d", sReadData, sData, iNCol);
                        //      MessageBox(hWnd, sMsg, "ERR", MB_OK);
                        //      iNCol = atoi(sData);
                        //}
                  }
                  else if(strncmp(sReadData, "CUTFORM", 7) == 0)
                  {
                        strcpy(sCommand, "CUTFORM");
                        Mid(sReadData, 9, 0, sData);
                        Trim(sData);
                        strcpy(sCutForm, sData);
                  }
                  else if(strncmp(sReadData, "COMMAND", 7) == 0)
                  {
                        strcpy(sCommand, "COMMAND");
                        Mid(sReadData, 9, 0, sData);
                        Trim(sData);
                        strcpy(sFunction, sData);
                        iLength = 0;
                        iNCol = 0;
                        strcpy(sCutForm, "");
                        strcpy(sCutFormDtl, "");
                  }
                  else if(strncmp(sReadData, "LENGTH", 6) == 0)
                  {
                        strcpy(sCommand, "LENGTH");
                        Mid(sReadData, 8, 0, sData);
                        Trim(sData);
                        iLength = atoi(sData);
                        //if(iLength == 0)
                        //{
                        //      MessageBox(hWnd, "Problem with atoi(LENGTH).", "ERR", MB_OK);
                        //      sprintf(sMsg, "READ=>%s,DATA=>%s,ATOI=>%d", sReadData, sData, iLength);
                        //      MessageBox(hWnd, sMsg, "ERR", MB_OK);
                        //      iLength = atoi(sData);
                        //}
                  }
                  else if(strncmp(sReadData, "DATA", 4) == 0)
                  {
                        strcpy(sCommand, "DATA");
                        Mid(sReadData, 6, 0, sData);
                        //Trim(sData);
            if(strncmp(sFunction, "print", 5) == 0)
                        {
                              iState = 1000;
                              printer_Select();
                        }
                        else if(strncmp(sFunction, "aprint", 6) == 0)
                        {
                              iState = 2000;
                              printer_Select();
                        }
                        else if(strncmp(sFunction, "ejectdoc", 8) == 0)
                        {
                              iState = 3000;
                              printer_Select();
                        }
                        else if(strncmp(sFunction, "insertdoc", 9) == 0)
                        {
                              iState = 4000;
                              printer_Select();
                        }
                        else if(strncmp(sFunction, "display", 7) == 0)
                        {
                              iState = 5000;
                              printer_Select();
                        }
                        else if(strncmp(sFunction, "loadlogo", 9) == 0)
                        {
                              iState = 6000;
                              printer_Select();
                        }
                        else if(strncmp(sFunction, "printlogo", 9) == 0)
                        {
                              iState = 7000;
                              printer_Select();
                        }
                        sprintf(sMsg, "%s=%d=%d=%d", sFunction, iLength, iNCol, iState);
                        OutputScreen(hwndEdit, sMsg);
                  }
                  else
                  {
                        strcpy(sCommand, "");
                        strcpy(sData, "");
                        strcpy(sReadData, "");
                  }
            }// state = 400

            if(iState==500)
            {
                  iState = 600;
                  if(file_truncate(&file_data, gsDataFile) != 0)
                        iState = 500;
                  if(file_truncate(&file_poll, gsPollFile) != 0)
                        iState = 500;

            } //state = 500

            if(iState==600)
            {
                  if (file_data_close(file_data) == 0)
                        iState = 700;
            }// state = 600

            if(iState==700)
            {
                  if (file_poll_close(file_poll) == 0)
                        iState = 100;
            } //state = 700


            if(iState==1000) // command print
            {
                  sprintf(sMsg, "COMMAND->%s", sFunction);
                  OutputScreen(hwndEdit, sMsg);

                  if(strncmp(sCutFormDtl, "reccut", 6) ==0)
                        printer_PrintReceipt();
                  else if(strncmp(sCutFormDtl, "rec", 3) ==0)
                        printer_PrintReceipt();
         else if(strncmp(sCutFormDtl, "joucut", 6) ==0)
                        printer_PrintJournal();
                  else if(strncmp(sCutFormDtl, "jou", 3) ==0)
                        printer_PrintJournal();
                  else if(strncmp(sCutFormDtl, "doccut", 6) ==0)
                        printer_PrintDoc();
                  else if(strncmp(sCutFormDtl, "doc", 3) ==0)
                        printer_PrintDoc();

                  iLoopData = 1;
                  //iLength = strlen(sData);
                  while((iLoopData < iLength) && (iNCol > 0))
                  {
                        strcpy(sTemp,"");
                        //sTemp = Mid(sData, iLoopData, iNCol);
                        Mid(sData, iLoopData, iNCol, sTemp);
                        strcpy(sTemp3, sTemp);
                        Trim(sTemp3);
                        Mid(sCutFormDtl, 1, 3, sTemp2);

                        if (strlen(sTemp3) > 0 || strcmp(sTemp2, "doc")==0)
                        {
                               printer_Print(sTemp);
                               printer_PrintLineFeed();
                               printer_PrintCR();
                               sprintf(sMsg, "DATA->%s", sTemp);
                               OutputScreen(hwndEdit, sMsg);
                               if(strncmp(sTemp,"AKHIR LAPORAN", 13)==0 && strncmp(sCutFormDtl,"jou", 3)==0)
                                      strcpy(sCutFormDtl,"joucut");
                        }
                        iLoopData = iLoopData + iNCol;
                  }
                  iState = 2500;

            } //state = 1000

            if(iState==2000)   // command aprint
            {
                  sprintf(sMsg, "COMMAND->%s", sFunction);
                  OutputScreen(hwndEdit, sMsg);
                  if(strncmp(sCutFormDtl,"reccut", 6)==0)
                        printer_PrintReceipt();
                  else if(strncmp(sCutFormDtl,"rec", 3)==0)
                        printer_PrintReceipt();
                  else if(strncmp(sCutFormDtl,"joucut", 6)==0)
                        printer_PrintJournal();
                  else if(strncmp(sCutFormDtl,"jou", 3)==0)
                        printer_PrintJournal();
                  else if(strncmp(sCutFormDtl,"doccut", 6)==0)
                        printer_PrintDoc();
                  else if(strncmp(sCutFormDtl,"doc", 3)==0)
                        printer_PrintDoc();

                  iLoopData = 1;
                  //iLength = strlen(sData);
                  while((iLoopData < iLength) && (iNCol > 0))
                  {
                        //sTemp=0;
                        //sTemp = Mid(sData, iLoopData, iNCol);
                        Mid(sData, iLoopData, iNCol, sTemp);
                        strcpy(sTemp3, sTemp);
                        Trim(sTemp3);
                        Mid(sCutFormDtl, 1, 3, sTemp2);

                        if (strlen(sTemp3) > 0 || strcmp(sTemp2, "doc")==0)
                        {
                               printer_Print(sTemp);
                               printer_PrintLineFeed();
                               printer_PrintCR();
                               sprintf(sMsg, "DATA->%s", sTemp);
                               OutputScreen(hwndEdit, sMsg);
                               if(strncmp(sTemp,"AKHIR LAPORAN", 13)==0 && strncmp(sCutFormDtl,"jou", 3)==0)
                                      strcpy(sCutFormDtl,"joucut");
                        }
                        iLoopData = iLoopData + iNCol;
                  }
                  iState = 2500;

            } //state = 2000

            if(iState==2500)   //cut receipt
            {
                  iState = 300;
                  if(strncmp(sCutFormDtl,"doccut", 6)==0)
                  {
                        iLoopData = 1;
                        iState = 3000;
                  }
                  if(strcmp(sCutFormDtl,"reccut")==0)
                  {
                        printer_PrintLineFeed();
                        printer_PrintLineFeed();
                        printer_PrintLineFeed();
                        printer_PrintLineFeed();
                        printer_PrintLineFeed();
                        printer_PrintReceiptHalfCut();
                        OutputScreen(hwndEdit, "DATA->Receipt Cut");
                  }
                  if(strncmp(sCutFormDtl,"joucut", 6)==0 && strcmp(gSelectedPrinter,"nd70")!=0)
                  {
                        printer_PrintLineFeed();
                        printer_PrintLineFeed();
                        printer_PrintLineFeed();
                        printer_PrintLineFeed();
                        printer_PrintLineFeed();
                        printer_PrintReceiptHalfCut();
                        OutputScreen(hwndEdit, "DATA->Journal Cut(EPSON)");
                  }

            } //state = 2500

            if(iState==3000) //command ejectdoc
            {
                  sprintf(sMsg, "DATA->%s", sFunction);
                  OutputScreen(hwndEdit, sMsg);
                  printer_EjectDoc();
                  iState = 300;
            }// state = 3000

            if(iState==4000) //command insertdoc
            {
                  sprintf(sMsg, "DATA->%s", sFunction);
                  OutputScreen(hwndEdit, sMsg);
                  if(strcmp(gSelectedPrinter,"tmh5k2")==0)
                  {
                        printer_PrintDoc();
                        printer_InsertDoc();
                  }
                  else
                  {
                        printer_InsertDoc();
                  }
                  iState = 300;
            }// state = 4000

            if(iState==5000) //command display
            {
                  sprintf(sMsg, "DATA->%s", sFunction);
                  OutputScreen(hwndEdit, sMsg);
                  printer_ClearDisplay();
                  printer_PrintDisplay(sData);

                  Mid(sData, 1, 20, sTemp2);
                  sprintf(sMsg, "DATA->%s", sTemp2);

                  OutputScreen(hwndEdit, sMsg);

                  Mid(sData, 21, 20, sTemp2);
                  sprintf(sMsg, "DATA->%s", sTemp2);

                  OutputScreen(hwndEdit, sMsg);
                  iState = 300;
            }// state = 5000

            if(iState==6000) //command load logo
            {
                  sprintf(sMsg, "DATA->%s", sFunction);
                  OutputScreen(hwndEdit, sMsg);
                  printer_Select();
                  printer_PrintReceipt();
                  //load_logo(sData);
                  //sprintf(sMsg, "DATA->%s", sData);
                  //OutputScreen(hwndEdit, sMsg);

                  iState = 300;

            } //satet = 6000

            if(iState==7000) //command print logo
            {
                  sprintf(sMsg, "DATA->%s", sFunction);
                  OutputScreen(hwndEdit, sMsg);
                  printer_PrintReceiptLogo();
                  OutputScreen(hwndEdit, "DATA->Receipt Logo");
                  iState = 300;

            }// state = 7000

            if(iState==8000) //command N/A
            {
                  sprintf(sMsg, "DATA->%s", sFunction);
                  OutputScreen(hwndEdit, sMsg);
                  iState = 300;
            } // state =8000

            if(iState==9000 || gGlobalOverwrite == 100) //command init printer
            {
                  OutputScreen(hwndEdit, "DATA->Force Init");
                  InitPrinter();
                  gGlobalOverwrite = 0;
                  iState = 500;
                  bLoop = 1;
            } // state =9000

      }// end while bLoop

      //MessageBox(hWnd, "OOOO", "OK", MB_OK);

}


int file_check(char *pfile)
{
      int handle;
      int l;

      //char sMsg[100];

      handle = open(pfile, O_RDONLY);
      l = filelength(handle);
      close(handle);

      return l;
}

int file_poll_lock(char *pfile)
{
      int tmp;
      FILE *fileNum;

      tmp = 0;
      fileNum = fopen(pfile, "r");

      if(fileNum != NULL)
      {
            tmp = 0;
            file_poll = fileNum;
      }
      else
            tmp=2;

      return tmp;
}
int file_poll_close(FILE *pfile)
{
      fclose(pfile);
      return 0;
}
int file_data_lock(char *pfile)
{
      int tmp;
      FILE *fileNum;

      tmp = 0;
      fileNum = fopen(pfile, "r");

      if(fileNum != NULL)
      {
            tmp = 0;
            file_data = fileNum;
      }
      else
            tmp=2;

      return tmp;
}
int file_data_close(FILE *pfile)
{
      fclose(pfile);
      return 0;
}
int file_truncate(FILE **pfile, char * pfilename)
{
      int tmp;
      FILE *fileNum;

      fclose(*pfile);

      fileNum = fopen(pfilename, "w");
      if(fileNum != NULL)
      {
            tmp = 0;
            *pfile = fileNum;
      }
      else
            tmp=2;

      return tmp;
}

int file_read(FILE *pfile, char buff[])
{

      if(pfile==NULL)
      {
            strcpy(buff,"EOF");
            return 0;
      }

      if(feof(pfile))
            strcpy(buff,"EOF");
      else
            fgets(buff, 2650, pfile);

      return 0;//buff;
}

int Mid(char *sSource, int nStartIdx, int nLength, char tmp[])
{
      //char tmp[1000];
      int nEnd=0;
      int i=0;
      int x=0;
      int l=0;

      strcpy(tmp, "");
      l = strlen(sSource);

      if(strlen(sSource<=0))
      {
            strcpy(tmp, "");
            return 0;
      }
      if(nStartIdx > 0) nStartIdx--;
      if(nLength==0)
            nEnd = l;
      else if(nLength > strlen(sSource))
            nEnd = l;
      else
            nEnd = nLength;

      x = nEnd - nStartIdx + 1;

      for(i=0;i<x;i++)
      {
            tmp[i] = sSource[i+nStartIdx];
      }

      tmp[i]=0;
      return 0;
}

void Trim(char pData[])
{
      char tmp[80]={0};
      int c=0;
      int i=0;

      // trim the white space
      while(pData[c]!=NULL)
      {
            if(pData[c] != ' ' && pData[c] != '\n')
            {
                  if(i<80)
                  {
                        tmp[i]=pData[c];
                        i++;
                  }
            }
            c++;
      }
      tmp[i]=NULL;
      strcpy(pData, tmp);
}

BOOL COMMAvailable(int port)
{
      return TRUE;
}

char *ReadCOMMInput()
{
      char sMsg[80]={0};
      int ret;

      if(hComm < 0)
            strcpy(sMsg, "");
      else
            ret = ReadComm(hComm, sMsg, 1024);


      return sMsg;

}

BOOL OpenPrinter(int nCOM)
{
      int err;
      char setting[30];
      char com[10];
      DCB dcb;
      BOOL bRet;

      if(hComm >= 0)return TRUE;

      bRet = TRUE;
      sprintf(com, "COM%d", nCOM);

      hComm = OpenComm(com, 1024, 512);
      if(hComm < 0)
   {
            MessageBox(hCommDvc, "Invalid COM port", "OK", MB_OK);
            bRet =FALSE;
            return bRet;
      }

      if(strcmp(gSelectedPrinter, "tmh5k2")==0)
            sprintf(setting, "COM%d:%d,%c,%d,%d",nCOM,9600,'n',8,1);
      if(strcmp(gSelectedPrinter, "nd70")==0)
            sprintf(setting, "COM%d:%d,%c,%d,%d",nCOM,9600,'o',8,1);
      else
            sprintf(setting, "COM%d:%d,%c,%d,%d",nCOM,9600,'n',8,1);

      err = BuildCommDCB(setting, &dcb);
      if(err<0)
      {
            MessageBox(hCommDvc, "Error BuildDCB", "OK", MB_OK);
            bRet =FALSE;
      return bRet;
      }

      dcb.fRtsDisable=0;
      dcb.fDtrDisable=0;
      err = SetCommState(&dcb);
      if(err<0)
      {
            MessageBox(hCommDvc, "Error SetCommState", "OK", MB_OK);
            bRet =FALSE;
      }
      else
      {
            SendMessage(hWndBtnDvc, WM_SETTEXT, 0, (LPARAM)"CloseCOMM");
      }

      return bRet;
}

void ClosePrinter()
{
      if(hComm < 0)return;

      CloseComm(hComm);
   hComm = -1;
      SendMessage(hWndBtnDvc, WM_SETTEXT, 0, (LPARAM)"OpenCOMM");
}

void cmdCOMStatus()
{
      char setting[12]={0};
      char sMsg[30]={0};

      if(strcmp(gSelectedPrinter, "nd70") == 0)
            strcpy(setting, "9600,O,8,1");
      else
            strcpy(setting, "9600,N,8,1");

      if(hComm < 0)
      {
            OutputScreen(hwndEditDvc, "Close COM Port.");
            sprintf(sMsg, "Setting -> %s", setting);
            OutputScreen(hwndEditDvc, sMsg);

      }

}

void SendMsg(char *pMsg)
{
      char *input;
      char sMsg[80]={0};
      int i=0;
      for(i=0;i<2000;i++)
      {
          ;
      }

      cmdCOMStatus();
      if(hComm <= 0)
      {
            WriteComm(hComm, pMsg, strlen(pMsg));
            input = ReadCOMMInput();
            sprintf(sMsg, "Status : [%s].", input);
            OutputScreen(hwndEditDvc, sMsg);
      }
      else
            OutputScreen(hwndEditDvc, "Status : COM Port NOT Opened.");

}

void InitPrinter()
{
      OutputScreen(hwndEditDvc, "COMMAND-> Init printer");
      SendMsg(CmdInit);
      printer_PrintDisplay ("                                        ");
}

void load_epson_tmh5k2()
{
      char sSelectPrt[10]={0};
      char sSelectPrtDoc[10]={0};

      CmdPrinter[0]=0x1B;CmdPrinter[1]=0x3D;CmdPrinter[2]=0x01;CmdPrinter[3]=0;

      sSelectPrtDoc[0]=0x1B;sSelectPrtDoc[1]=0x0C;sSelectPrtDoc[2]=0;sSelectPrtDoc[3]=0x1B;
      sSelectPrtDoc[4]=0x63;sSelectPrtDoc[5]=0x30;sSelectPrtDoc[6]=0x04;sSelectPrtDoc[7]=0x1B;
      sSelectPrtDoc[8]=0x61;sSelectPrtDoc[9]=0x02;sSelectPrtDoc[10]=0;

  Print_LineFeed[0]=0x0A;Print_LineFeed[1]=0;
  Print_CR[0]=0x0D;Print_CR[1]=0;
  Print_HomeCursor[0]=0x1B;Print_HomeCursor[1]=0x3C;Print_HomeCursor[2]=0;

  CmdInit[0]=0x10;CmdInit[1]=0x14;CmdInit[2]=0x08;CmdInit[3]=0x01;CmdInit[4]=0x03;CmdInit[5]=0x20;
  CmdInit[6]=0x01;CmdInit[7]=0x06;CmdInit[8]=0x02;CmdInit[9]=0x08;CmdInit[10]=0x10;CmdInit[11]=0x02;
  CmdInit[12]=0x1B;CmdInit[13]=0x40;CmdInit[14]=0;

  CmdDisplay[0]=0x1B;CmdDisplay[1]=0x3D;CmdDisplay[2]=0x02;CmdDisplay[3]=0;

  CmdDoc[0]=0x1B;CmdDoc[1]=0x0C;CmdDoc[2]=0;CmdDoc[3]=0x1B;CmdDoc[4]=0x61;CmdDoc[5]=0x02;
  CmdDoc[6]=0x1B;CmdDoc[7]=0x63;CmdDoc[8]=0x30;CmdDoc[9]=0x04;CmdDoc[10]=0;

  CmdDoc_Eject[0]=0x0C;CmdDoc_Eject[1]=0;

  CmdDoc_Insert[0]=0x1B;CmdDoc_Insert[1]=0x19;CmdDoc_Insert[2]=0;

  CmdDoc_Release[0]=0x1B;CmdDoc_Release[1]=0x71;CmdDoc_Release[2]=0;

  CmdDoc_Feed[0]=0x1B;CmdDoc_Feed[1]=0x58;CmdDoc_Feed[2]=0x32;CmdDoc_Feed[3]=0x00;CmdDoc_Feed[4]=0;

  strcpy(CmdReceipt, CmdPrinter);
  CmdReceipt[3]=0x1B;CmdReceipt[4]=0x61;CmdReceipt[5]=0x01;CmdReceipt[6]=0;

  CmdReceipt_HalfCut[0]=0x1B;CmdReceipt_HalfCut[1]='=';CmdReceipt_HalfCut[2]=0x01;CmdReceipt_HalfCut[3]=0x1D;
  CmdReceipt_HalfCut[4]=0x56;CmdReceipt_HalfCut[5]=0x00;CmdReceipt_HalfCut[6]=0;

  CmdReceipt_FullCut[0]=0x1B;CmdReceipt_FullCut[1]='=';CmdReceipt_FullCut[2]=0x01;CmdReceipt_FullCut[3]=0x1D;
  CmdReceipt_FullCut[4]=0x56;CmdReceipt_FullCut[5]=0x01;CmdReceipt_FullCut[6]=0;

  CmdReceipt_LogoPrint[0]=0x1D;CmdReceipt_LogoPrint[1]='/';CmdReceipt_LogoPrint[2]=0x03;CmdReceipt_LogoPrint[3]=0x0A;
  CmdReceipt_LogoPrint[4]=0;

  Receipt_LogoLoad_Start[0]=0x1B;Receipt_LogoLoad_Start[1]=0x26;Receipt_LogoLoad_Start[2]=0x03;
  Receipt_LogoLoad_Start[3]=0x00;Receipt_LogoLoad_Start[4]=0x00;Receipt_LogoLoad_Start[5]=0x01;
  Receipt_LogoLoad_Start[6]=0x01;Receipt_LogoLoad_Start[7]=0;

  Receipt_LogoLoad_Data[0]=0x1B;Receipt_LogoLoad_Data[1]=0x26;Receipt_LogoLoad_Data[2]=0x03;
  Receipt_LogoLoad_Data[3]=0x01;Receipt_LogoLoad_Data[4]=0x00;Receipt_LogoLoad_Data[5]=0x8C;
  Receipt_LogoLoad_Data[6]=0;

  Receipt_LogoLoad_End[0]=0x1B;Receipt_LogoLoad_End[1]=0x26;Receipt_LogoLoad_End[2]=0x03;
  Receipt_LogoLoad_End[3]=0xFF;Receipt_LogoLoad_End[4]=0x00;Receipt_LogoLoad_End[5]=0x00;
  Receipt_LogoLoad_End[6]=0;

  strcpy(CmdJournal, CmdReceipt);

  ClosePrinter();
  OpenPrinter(gSelectedPort);
  InitPrinter();

}

void load_sni_nd70()
{
      char sSelectPrt[10]={0};
      char sSelectPrtDoc[10]={0};

      CmdPrinter[0]=0x1B;CmdPrinter[1]=0x58;CmdPrinter[2]=0x31;CmdPrinter[3]=0x01;CmdPrinter[4]=0;

      strcpy(sSelectPrtDoc, CmdPrinter);
      sSelectPrtDoc[4]=27;sSelectPrtDoc[5]=99;sSelectPrtDoc[6]=48;sSelectPrtDoc[7]=8;
      sSelectPrtDoc[8]=0;

  Print_LineFeed[0]=0x0A;Print_LineFeed[1]=0;
  Print_CR[0]=0x0D;Print_CR[1]=0;
  Print_HomeCursor[0]=27;Print_HomeCursor[1]=60;Print_HomeCursor[2]=0;

  CmdInit[0]=0x10;CmdInit[1]=0x02;CmdInit[2]=0x1B;CmdInit[3]=0x40;CmdInit[4]=27;CmdInit[5]=33;
  CmdInit[6]=03;CmdInit[7]=0;

  CmdDisplay[0]=27;CmdDisplay[1]=8;CmdDisplay[2]=49;CmdDisplay[3]=2;CmdDisplay[4]=0;

  strcpy(CmdDoc, CmdPrinter);
  CmdDoc[4]=27;CmdDoc[5]=99;
  CmdDoc[6]=48;CmdDoc[7]=4;CmdDoc[8]=0;

  CmdDoc_Eject[0]=12;CmdDoc_Eject[1]=0;

  CmdDoc_Insert[0]=27;CmdDoc_Insert[1]=25;CmdDoc_Insert[2]=0;

  CmdDoc_Release[0]=27;CmdDoc_Release[1]=113;CmdDoc_Release[2]=0;

  CmdDoc_Feed[0]=27;CmdDoc_Feed[1]=88;CmdDoc_Feed[2]=50;CmdDoc_Feed[3]=0;

  strcpy(CmdReceipt, CmdPrinter);
  CmdReceipt[4]=27;CmdReceipt[5]=99;CmdReceipt[6]=48;CmdReceipt[7]=2;CmdReceipt[8]=0;

  CmdReceipt_HalfCut[0]=27;CmdReceipt_HalfCut[1]=109;CmdReceipt_HalfCut[2]=0;

  CmdReceipt_FullCut[0]=27;CmdReceipt_FullCut[1]=105;CmdReceipt_FullCut[2]=0;

  CmdReceipt_LogoPrint[0]=27;CmdReceipt_LogoPrint[1]=111;CmdReceipt_LogoPrint[2]=0;

  Receipt_LogoLoad_Start[0]=0x1B;Receipt_LogoLoad_Start[1]=0x26;Receipt_LogoLoad_Start[2]=0x03;
  Receipt_LogoLoad_Start[3]=0x00;Receipt_LogoLoad_Start[4]=0x00;Receipt_LogoLoad_Start[5]=0x01;
  Receipt_LogoLoad_Start[6]=0x01;Receipt_LogoLoad_Start[7]=0;

  Receipt_LogoLoad_Data[0]=0x1B;Receipt_LogoLoad_Data[1]=0x26;Receipt_LogoLoad_Data[2]=0x03;
  Receipt_LogoLoad_Data[3]=0x01;Receipt_LogoLoad_Data[4]=0x00;Receipt_LogoLoad_Data[5]=0x8C;
  Receipt_LogoLoad_Data[6]=0;

  Receipt_LogoLoad_End[0]=0x1B;Receipt_LogoLoad_End[1]=0x26;Receipt_LogoLoad_End[2]=0x03;
  Receipt_LogoLoad_End[3]=0xFF;Receipt_LogoLoad_End[4]=0x00;Receipt_LogoLoad_End[5]=0x00;
  Receipt_LogoLoad_End[6]=0;

  strcpy(CmdJournal, CmdPrinter);
  CmdJournal[4]=27;CmdJournal[5]=99;CmdJournal[6]=48;CmdJournal[7]=1;CmdJournal[8]=0;

  ClosePrinter();
  OpenPrinter(gSelectedPort);
  InitPrinter();

}

void printer_PrintDisplay(char pMsg[])
{
      char sMsg[80];

      OutputScreen(hwndEditDvc, "COMMAND-> Print Display");
      SendMsg(CmdDisplay);
      if(strcmp(gSelectedPrinter,"nd70") == 0)
      {
            //sMsg = Mid(pMsg, 1, 20);
            Mid(pMsg, 1, 20, sMsg);
            strcpy(sMsg, Print_CR);
            strcpy(sMsg, Print_LineFeed);
            SendMsg(sMsg);

            Mid(pMsg, 21, 20, sMsg);
            SendMsg(sMsg);
      }
      else
      {
            //sMsg = Mid(pMsg, 1, 40);
            Mid(pMsg, 1, 40, sMsg);
            SendMsg(sMsg);
      }

}

void printer_PrintLineFeed()
{
      OutputScreen(hwndEditDvc, "COMMAND-> LineFeed");
      SendMsg(Print_LineFeed);
}

void printer_PrintReceipt()
{
      OutputScreen(hwndEditDvc, "COMMAND-> Select Peripheral Receipt Station");
   SendMsg(CmdReceipt);
}

void printer_PrintReceiptHalfCut()
{
      char msg[80];

      OutputScreen(hwndEditDvc, "COMMAND-> Receipt 1/2 Cut");
      if(strcmp(gSelectedPrinter,"nd70") == 0)
      {
            strcpy(msg, CmdReceipt);
            strcat(msg, CmdReceipt_HalfCut);
            SendMsg(msg);
      }
      else
            SendMsg(CmdReceipt_HalfCut);

}

void printer_PrintReceiptFullCut()
{
      char msg[80];

      OutputScreen(hwndEditDvc, "COMMAND-> Receipt Full Cut");
      strcpy(msg, CmdReceipt);
      strcat(msg, CmdReceipt_FullCut);
      SendMsg(msg);
}

void printer_PrintReceiptLogo()
{
      char msg[80];
      OutputScreen(hwndEditDvc, "COMMAND-> Receipt Print Logo");
   strcpy(msg, CmdReceipt);
      strcat(msg, CmdReceipt_LogoPrint);
      SendMsg(msg);
}

void printer_ClearDisplay()
{
      char msg[80];

      OutputScreen(hwndEditDvc, "COMMAND-> Clear Display");
      SendMsg(CmdDisplay);

      if(strcmp(gSelectedPrinter,"nd70") == 0)
      {
            strcpy(msg, Print_LineFeed);
            strcat(msg, Print_LineFeed);
            strcat(msg, Print_CR);
            SendMsg(msg);
      }
      else
      {
            strcpy(msg, Print_CR);
            strcat(msg, Print_LineFeed);
            strcat(msg, Print_LineFeed);
            SendMsg(msg);
      }
}

void printer_Print(char pMsg[])
{
      OutputScreen(hwndEditDvc, "COMMAND-> Print");
      //MessageBox(hWnd, pMsg, "PRT", MB_OK);
      SendMsg(pMsg);

}

void printer_Select()
{
      OutputScreen(hwndEditDvc, "COMMAND-> Printer Peripheral Select");
      SendMsg(CmdPrinter);
}

void printer_EjectDoc()
{
      OutputScreen(hwndEditDvc, "COMMAND-> Eject Doc");
      SendMsg(CmdDoc_Eject);
}

void printer_InsertDoc()
{
      OutputScreen(hwndEditDvc, "COMMAND-> Insert Doc");
      SendMsg(CmdDoc_Insert);
}

void printer_ReleaseDoc()
{
      OutputScreen(hwndEditDvc, "COMMAND-> Release Doc");
      SendMsg(CmdDoc_Release);
}

void printer_FeedDoc()
{
      OutputScreen(hwndEditDvc, "COMMAND-> Feed Doc");
      SendMsg(CmdDoc_Feed);
}

void printer_PrintDoc()
{
      OutputScreen(hwndEditDvc, "COMMAND-> Select Peripheral Document Station");
      SendMsg(CmdDoc);
}

void printer_PrintJournal()
{
      OutputScreen(hwndEditDvc, "COMMAND-> Print Journal");
      SendMsg(CmdJournal);
}

void printer_PrintCR()
{
      OutputScreen(hwndEditDvc, "COMMAND-> Carriage Return");
      SendMsg(Print_CR);
}

void load_logo(char *pData)
{
      int iLines=0;
      int iCtr1=0;
      int iLastCtr=0;
      int iTrack=0;
      int iBoolean=0;
   char tmp[80]={0};

      iLogoData = (int)strlen(pData) / iDataPerTrack / 8;
      iLogoData--;

      if(iLogoData <=0 || iLogoData > 80)
      {
            MessageBox(hWnd, "Error in defining Logo Data.", "Logo Error", MB_ICONSTOP);
      return;
      }

      iLastCtr = 1;
      iTrack = 0;
      iLines = 0;

      for(iCtr1=1; iCtr1<=strlen(pData); iCtr1++)
      {
            Mid(pData, iCtr1, 1, tmp);
            if(strcmp(tmp, " ")==0)
                  iBoolean = 0;
            else
            {
                  iBoolean = 0;
                  switch(iTrack)
                  {
                        case 0:
                              iBoolean = 0x80;
                              break;
                        case 1:
                              iBoolean = 0x40;
                              break;
                        case 2:
                              iBoolean = 0x20;
                              break;
                        case 3:
                              iBoolean = 0x10;
                              break;
                        case 4:
                              iBoolean = 0x08;
                              break;
                        case 5:
                              iBoolean = 0x04;
                              break;
                        case 6:
                              iBoolean = 0x02;
                              break;
                        case 7:
                              iBoolean = 0x01;
                              break;
                  }
                  sLogoData[iLines][iLastCtr] = sLogoData[iLines][iLastCtr] | iBoolean;
                  iLastCtr++;
                  if((iLastCtr % (iDataPerTrack+1))==0)
                  {
                        iLastCtr = 1;
                        iTrack++;
                        if(iTrack > 7)
                        {
                              iTrack = 0;
                              iLines++;
                              if (iLines > iLogoData)
                                    goto q;

                        }
                  }
            }
      }

      q:

      for(iCtr1=0; iCtr1<=iDataPerTrack; iCtr1++)
      {
            strcpy(tmp, "");
            for(iTrack=1; iTrack<=iDataPerTrack; iTrack++)
            {
                  strcat(tmp, (char)sLogoData[iCtr1][iTrack]);

            }
      }
      if(strcmp(gSelectedPrinter, "nd70") == 0)
            decode_nd70();
      else
            decode_tmh5k2();

}

void decode_nd70()
{
      int iCtr=0;
      int iCtr2=0;
      int iTrack=0;
      int l_iLogoTrack=0;
      char l_sLogoData[80]={0};
      char tmp[80]={0};

      if(iLogoData <= 0) return;

      SendMsg(Receipt_LogoLoad_Start);

      iCtr2 = 2;
      for(iCtr=0; iCtr<=iLogoData; iCtr++)
      {
            strcpy(l_sLogoData, "");
            strcpy(tmp, "");
            for(l_iLogoTrack=1; l_iLogoTrack<=strlen(Receipt_LogoLoad_Data);l_iLogoTrack++)
            {
                  if(l_iLogoTrack==4)
                        strcat(l_sLogoData, (char)(iCtr + 1));
                  else
                  {
                        Mid(Receipt_LogoLoad_Data, l_iLogoTrack, 1, tmp);
                        strcat(l_sLogoData, tmp);
                  }
            }

            SendMsg(l_sLogoData);

            for(iTrack=1; iTrack <= iDataPerTrack; iTrack++)
            {
                  strcat(l_sLogoData, (char)sLogoData[iCtr][iTrack]);
                  SendMsg((char)sLogoData[iCtr][iTrack]);
         iCtr2++;
            }
      }

      strcpy(l_sLogoData, "");

   for(l_iLogoTrack=1; l_iLogoTrack<=strlen(Receipt_LogoLoad_Data);l_iLogoTrack++)
      {
          if(l_iLogoTrack==4)
                  strcat(l_sLogoData, (char)(6));
            else
            {
                  Mid(Receipt_LogoLoad_Data, l_iLogoTrack, 1, tmp);
                  strcat(l_sLogoData, tmp);
            }
      }

      SendMsg(l_sLogoData);

   for(iTrack=1; iTrack <= iDataPerTrack; iTrack++)
      {
             strcat(l_sLogoData, (char)(0));
       SendMsg((char)0);
      }

      SendMsg(Receipt_LogoLoad_End);
}

void decode_tmh5k2()
{
      int iCtr=0;
      int iCtr2=0;
      int iTrack=0;
      char tmp[80]={0};

      if(iLogoData <= 0) return;

      strcpy(tmp, (char)0x1D);
      strcat(tmp, "*");
      strcpy(tmp, (char)18);
      strcpy(tmp, (char)5);
      SendMsg(tmp);

      iCtr2 = 0;
      for(iCtr=1; iCtr<=iDataPerTrack; iCtr++)
      {
            for(iTrack=0; iTrack <= iLogoData; iTrack++);
            {
                  SendMsg((char)sLogoData[iTrack][iCtr]);
                  iCtr2++;
            }

      }

      iCtr = 18 * 5 * 8;
      if(iCtr2 < iCtr)
      {
            for(iTrack=iCtr2; iTrack<=iCtr; iTrack++)
            {
                  SendMsg((char)0);
            }
      }

      strcpy(tmp, (char)0x1B);
      strcat(tmp, "U");
      SendMsg(tmp);
}
++++++++++++++++++++++++++++++++++++++++


This is the input source file.
++++++++++++++++++++++++++++++++++++++++
COMMAND=print  
LENGTH=63    
LENGTH_COL=32    
CUTFORM=nd70  
CUTFORM_DTL=rec  
DATA=================================                              
COMMAND=print  
LENGTH=50    
LENGTH_COL=32    
CUTFORM=nd70  
CUTFORM_DTL=rec  
DATA=RECOVERY  : BERMULA                              
COMMAND=print  
LENGTH=63    
LENGTH_COL=32    
CUTFORM=nd70  
CUTFORM_DTL=rec  
DATA=Trkh: 02/03/2004, Masa: 13:29:21                              
COMMAND=print  
LENGTH=63    
LENGTH_COL=32    
CUTFORM=nd70  
CUTFORM_DTL=rec  
DATA=================================                              
COMMAND=print  
LENGTH=63    
LENGTH_COL=32    
CUTFORM=nd70  
CUTFORM_DTL=rec  
DATA=Transaksi :             00246155                              
COMMAND=print  
LENGTH=63    
LENGTH_COL=32    
CUTFORM=nd70  
CUTFORM_DTL=rec  
DATA=================================                              
COMMAND=print  
LENGTH=50    
LENGTH_COL=32    
CUTFORM=nd70  
CUTFORM_DTL=rec  
DATA=RECOVERY  : SELESAI                              
COMMAND=print  
LENGTH=63    
LENGTH_COL=32    
CUTFORM=nd70  
CUTFORM_DTL=rec  
DATA=Trkh: 02/03/2004, Masa: 13:29:24                              
COMMAND=print  
LENGTH=63    
LENGTH_COL=32    
CUTFORM=nd70  
CUTFORM_DTL=rec  
DATA=================================                              
COMMAND=print  
LENGTH=61    
LENGTH_COL=32    
CUTFORM=nd70  
CUTFORM_DTL=reccut  
DATA=********************************                            
COMMAND=print  
LENGTH=220  
LENGTH_COL=32    
CUTFORM=nd70  
CUTFORM_DTL=reccut  
DATA=---- Laporan Sign On J/Wang ----ID J/Wang: 4417J01   P/K: 02    W.Pendahuluan: RM          0.00 Trkh: 02/03/2004, Masa: 13:30:20================================                                                            
COMMAND=printlogo  
LENGTH=25    
LENGTH_COL=140  
CUTFORM=nd70  
CUTFORM_DTL=rec  
LOGO_PRINT                    
COMMAND=aprint  
LENGTH=1033  
LENGTH_COL=32    
CUTFORM=nd70  
CUTFORM_DTL=reccut  
DATA=      Welcome Friend To               ABC Comm Limited          --------------------------------Nombor Transaksi :      0024615500459089 KADFON RM 5.00         039 90392                           5.00     1  RM         5.00                 ----------------Jumlah Item     RM         5.00 00459090 KADFON RM 20.00        039 90394                          20.00     2  RM        40.00                 ----------------Jumlah Item     RM        40.00                 ----------------Jumlah Kutipan  RM        45.00 Jumlah Cukai    RM         0.00 Jumlah (Diskaun)RM         0.00                 ----------------Jum.Perlu Bayar RM        45.00                 ================Bayaran:                        TUNAI           RM        45.00 --------------------------------       Sila Datang Lagi                  TERIMA KASIH           ================================141702 4417J01 02032004 13:30:55                                                                                                                                        
++++++++++++++++++++++++++++++++++++++++


Hope somebody can assist me on this code debugging.
Thanks.
Avatar of Fippy_Darkpaw
Fippy_Darkpaw

lol good luck finding someone to debug that for 150 points. Make it a lot more and someone might =)
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of showbix

ASKER

Hi itsmeandnobodyelse.
Thanks for the help on code debugging. Will try to execute on my machine later.

I have another version of mine which runs OK but the problems are :-
1. It will truncate some printing if I run the program (without debugging mode) but if I run the program by putting several breakpoints in debugging mode, it will print all the output successfully.
It seems like if I put 'PAUSE' in some places, it prints good. I suspect the problem lies at WriteComm API that I used. It seems that the process of writing data to com port is to fast to handle by the printer and that makes the printer unable to catch the data and print correctly.
Well, I do need experts opinion on this matter.