PC SOFT

FORUMS PROFESSIONNELS
WINDEVWEBDEV et WINDEV Mobile

Accueil → WINDEV 2025 → [WD17] Conversion du Code C++ en Wlangage
[WD17] Conversion du Code C++ en Wlangage
Débuté par igc_i, 28 sep. 2012 16:44 - 9 réponses
Posté le 28 septembre 2012 - 16:44
Bonjour,
J'ai ce long code en C++, depuis les années que je suis dans Windev,, j'ai un peu perdu la main en C++. Essayons de le traduire en Wlangage. il y a un fichier .h en cas de besoin. Comme c'est long je vous le colle en dessous.
A bientôt pour ceux qui s'y mettront.

#include <conio.h> /* required only for function declarations */
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include <stdio.h>
#include <windows.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <io.h>
#include "usb_vc.h"

/**************************************************************************************
* Prototype Definitions of local routines
**************************************************************************************/
void Choose_USB();
void Init_USB(); //Initialize USB port
void Display_Buffer(unsigned int size);
void Port_Read();
void Port_Write();
void USB_Read();
void USB_Write();
void Clear_Buffer();
void fgl_test_ticket();
void hp_test_ticket();
void Write();
void Perform_Test();
void Clear_Comm();
void history(); //Check/Establish history
void establish_history();
void record_history();
void Send_Dual();
void dual_test_ticket();
void Find_USB_Printers();
void Close_USB(int i); //Close USB port based on index into printer_table
void dual_test();

/**************************************************************************************
* Global variables
**************************************************************************************/
int ascii_value; // Ascii value for printer
char Buffer[PACKET_SIZE+1]; // char string buffer
PVOID pBuffer; // pointer to buffer
FILE *fin, *fout; // Input/Output files
int port; // Choice of ports as listed below
char port_name[5]; // 4 char name + 1 for null
int rw_function; // read=1 & write=2
int w_function; // FGL Command=1 & Test Ticket=2
DWORD error_code; // Error code
int i,k;
DWORD dwSize;
unsigned int Bytes_Transferred;
int pipe;
USB_HANDLE hUSB = NULL; //Used to point to the current Boca USB printer
HANDLE hWD;
HINSTANCE hinstLib;
char ichar;
HANDLE hPort = NULL; // Serial Port file pointer
BOOL Port_Ready;
DCB dcb;
COMMTIMEOUTS CommTimeouts;
BOOL Write_Com;
BOOL Read_Com;
DWORD Bytes_Written;
DWORD Bytes_Read;
int ix,start,size;
int Last_Port_Num = 0; //Used to track last com port used.
char port_function[1];
DWORD status;

//New additions to support the use of multiple printers
USB_PRINTER Printer_Table[USB_PRINTER_MAX]; // table used to support multiple printers
USB_PRINTER *pTable; // pointer used to support multiple printers
int USB_printer_count=0; // used to support multiple printers
DWORD P1=0,P2=0;
bool found1=false,found2=false;
USB_HANDLE P1_ptr = NULL;
USB_HANDLE P2_ptr = NULL;
USB_HANDLE P3_ptr = NULL;
int Index=0,printer=0;
char Name[64];

/**************************************************************************************
* Routine Name: main
*
* Description
* This routine demonstrates a technique for reading and writing from a Boca Systems
* USB printers. The user will be prompted for several options beginning with whether to
* read from the printer buffer or write to the printer. The user can issue individual
* commands to the printer or print an FGL test ticket. The user will have a selection
* of up to eight ports to use. This of course depends on the configuration of the PC
* and the Boca Systems USB printers.
**************************************************************************************/
int main(int argc, char* argv[])
{
int temp;
char setupPacket[8];

//Initialize DLL & pick default USB printer, if at least one Boca USB printer found.
//Make sure this line of code runs before any attempt to access a Boca USB printer.
//This is a must....
Init_USB();

if (USB_printer_count > 0) //At least one Boca USB printer found
port=USB; //Port name/flag
else
port=0;

while (port != 0)
{

/* Clear Buffer & reset write function between uses */
Clear_Buffer();
w_function=1;

/* What Does the user want to do to the currently selected printer? */
printf("\n\nCurrent Boca USB Printer Serial Number: %d\n",Printer_Table[Index].Serial_Number);
printf("===============================================\n");
printf(" 1. Read from printer.\n");
printf(" 2. Write to printer.\n");
printf(" 3. Change current Boca USB Printer.\n");
printf(" 4. Change USB Read/Write Timeout periods.\n");
printf(" 5. Read USB Interface Status.\n");
printf(" 6. Read Factory Serial Number from printer.\n");
printf(" 7. Scan For Boca USB Printers Again.\n");
//printf(" 8. Test Dual Path Send Routines.\n"); //experimental test code...
printf("99. Exit\n");
printf("\nChoose function: ");
scanf("%d",&rw_function);

/* Verify within range, else exit then exit. */
if(rw_function >= 1 && rw_function <= 8)
{

/* If read/write is write then prompt for what to write. */
if (rw_function==2)
{
printf("\n\nChoose Write Function\n");
printf("==========================\n");
printf(" 1. Send Text\n");
printf(" 2. Send ASCII value\n");
printf(" 3. Print FGL Test Ticket\n");
printf(" 4. Print HP Test Ticket\n");
printf("99. Exit\n");
printf("\nChoose function: ");
scanf("%d",&w_function); //w_function variable used in perform_test()
gets(Buffer); //Terminates last scanf into dummy buffer

if (w_function==1)
{
Clear_Buffer();
printf("\n\nEnter Text: ");
gets(Buffer);
}

if (w_function==2)
{
Clear_Buffer();
printf("\n\nEnter Decimal ASCII Value: ");
scanf("%d",&ascii_value);
Buffer[0] = ascii_value;
}
if ((w_function<1) || (w_function>4)) rw_function=0; //exit
}

switch (rw_function) //Are we to perform a read, write or exit?
{
case 1: //Read from port


strcpy(port_name,USB_S);
//if Boca Printer not previously located, then try now.
if (!hUSB)
{
Choose_USB(); //Pick a Boca USB printer
}
if (hUSB)
{
status = USB_Status(hUSB);
if(status==0xffffffff)
status = USB_Status(hUSB);
switch(status)
{
case 0xffffffff: //no response
printf("\nNo Response from Printer\n");
break;

case 0x18: //ready response
Port_Read();
break;

case 0x20: //paper out
printf("\nPrinter Out of Paper\n");
break;

case 0x10: //paper jam
printf("\nPrinter Paper Jammed\n");
break;

default:
printf("\nStatus = %d\n",status);
break;
}

}
else
printf("Printer not found on port %s\n",port_name);

break;

case 2: //Write to port

strcpy(port_name,USB_S);
//if Boca Printer not previously located, then try now.
if (!hUSB)
Choose_USB(); //Pick a Boca USB printer
if (hUSB)
{
status = USB_Status(hUSB);
if(status==0xffffffff)
status = USB_Status(hUSB);
switch(status)
{
case 0xffffffff: //no response
printf("\nNo Response from Printer\n");
break;

case 0x18: //ready response
Perform_Test(); //based on value of w_function
break;

case 0x20: //paper out
printf("\nPrinter Out of Paper\n");
break;

case 0x10: //paper jam
printf("\nPrinter Paper Jammed\n");
break;

default:
printf("\nStatus = %d\n",status);
break;
}

}
else
printf("Printer not found on port %s\n",port_name);

break;
case 3:

Choose_USB(); //Pick a Boca USB printer
if (!hUSB)
printf("Unable to open printer # %s\n",Printer_Table[Index].Serial_Number);

break;

case 4: //Set RW timeout periods

if (!hUSB) //If not already chosen,
Choose_USB(); //Pick a Boca USB printer
if (hUSB)
{

// 1000 milliseconds = 1 second

printf("\n\nEnter Read Timeout in Milliseconds: ");
scanf("%d",&temp);

//For Read timeout = 1/2 second, use temp=500;
port_function[0] = SET_READ_TIMEOUT; //setup port function
Timeout_Port(&port_function,temp); //execute function

printf("\n\nEnter Write Timeout in Milliseconds: ");
scanf("%d",&temp);

//Write timeout = 3 seconds, use temp=3000;
port_function[0] = SET_WRITE_TIMEOUT; //setup port function
Timeout_Port(&port_function,temp); //execute function
}
else printf("Printer not found on port %s\n",port_name);

break;

case 5: //Read USB Interface Status

if (!hUSB) //If not already chosen,
Choose_USB(); //Pick a Boca USB printer
if (hUSB)
{
status = USB_Status(hUSB);
if(status==0xffffffff)
status = USB_Status(hUSB);
switch(status)
{
case 0xffffffff: //no response
printf("\nNo Response from Printer\n");
break;

case 0x18: //ready response
printf("\nPrinter Ready\n");
break;

case 0x20: //paper out
printf("\nPrinter Out of Paper\n");
break;

case 0x10: //paper jam
printf("\nPrinter Paper Jammed\n");
break;

default:
printf("\nStatus = %d\n",status);
break;
}

}
else
printf("Printer not found on port %s\n",port_name);

break;

case 6: //Read USB Devices Serial Number through the control pipe

pBuffer = &Buffer; //buffer for return data
dwSize = 0x16; //amount of data requested - never greater than 0xff

setupPacket[0] = (char)0x80; //bmRequestType - Interface to Host
setupPacket[1] = 0x06; //bRequest - GET_Descriptor
setupPacket[2] = 0x03; //wValueL - descritor type - STRING = 3
setupPacket[3] = 0x03; //wValueH - which string? index = 3
setupPacket[4] = 0x00; //wIndexL
setupPacket[5] = 0x00; //wIndexH
setupPacket[6] = 0x16; //wLengthL - # of data bytes to transfer, plus control chars
setupPacket[7] = 0x00; //wLengthH

//USB_Control will return 0xffffffff for an error, if one exists or the data if OK
//On a USB control pipe transfer returning a string, the data comes
//back in a specific format
// buffer[0] contains length of the rest of the field
// buffer[1] contains the string type in this case a 3 for serial number
// buffer[2]...buffer[n] actual string data
error_code = USB_Control(hUSB, pBuffer, dwSize, setupPacket);

if (error_code == 0xffffffff)
printf ("\nNothing was read from port %s\n",port_name);
else //no error
{
printf ("\nSerial Number = ");
Bytes_Transferred = Buffer[0]; //# of bytes read
for (i = 2; i < (int)Bytes_Transferred; i++)
{
// Display only printable characters between 0 & 9.
if ((Buffer[i] >= 0x30) && (Buffer[i] <= 0x39))
printf ("%c", Buffer[i]);
}
printf ("\n");
}


break;

case 7: //Scan for printers again. Perhaps something new was plugged in.

//populate Printer_Table array
//Close any potentially open ports
for (i = 0; i < USB_printer_count; i++) //For all ports
Close_USB(i); //close it.

Find_USB_Printers(); //Fill Printer table for all Boca USB printers
hUSB = NULL; //still haven't choosen one yet, just found them
if (USB_printer_count>0) //Was at least one printer found?
{
printf ("\nDevice # \tSerial # \tProduct ID \tProduct Name\n");
printf ("======== \t======== \t========== \t============\n");

for (i = 0; i < USB_printer_count; i++)
{
if (Printer_Table[i].Product_ID==0x0001)
sprintf(Name,"Model 42 - FGL/PCL4");
else
{
if (Printer_Table[i].Product_ID==0x0101)
sprintf(Name,"Model 44 - FGL");
else
{
if (Printer_Table[i].Product_ID==0x0102)
sprintf(Name,"Model 44 - PCL4");
else
sprintf(Name," ");
}
}
printf (" %d \t\t%d \t\t0x%04x \t\t%s\n",i,Printer_Table[i].Serial_Number,Printer_Table[i].Product_ID,Name);
}

Index=0;
hUSB = Locate_Device(TRUE,Index); //choose the first one as default
if (hUSB) //if successful
{
Printer_Table[Index].hUSB = hUSB; //store pointer to structure in table
//printf ("\nOpened default Boca USB printer Serial Number: %d.\n",Printer_Table[Index].Serial_Number);
}
}
if(!hUSB) //if unable to find any Boca USB printers
{
printf ("\nUnable to locate a Boca USB printer....\n");
}

break;

case 8:

// This option is not standard communication and is not needed.
// Should you want to experiment with simulating a dual path printer
// with two USB printers uncomment this and rebuild.
/*
dual_test();

printf("\n\nDual Path Test over, so chose a default printer again.\n");

Choose_USB();
*/

break;


default:
// Used to exit cleanly
port = 0;
break;
}
}
else port=0;
};

//Close any potentially open ports
for (i = 0; i < USB_printer_count; i++) //For all potentially open ports
Close_USB(i); //close it.

//Unload library
if(hinstLib) FreeLibrary(hinstLib); //If USB Library is loaded, unload it.

hUSB=NULL; //reset pointer to NULL

//Pause placed here to allow user to see any last messages on exit, when running the program
//outside the debugger.
//When using the debugger comment out these 2 lines of code. They are redundant
printf("\nPress Enter Key to continue...\n");
gets(Buffer);

return 0; //We are done.
}

/**************************************************************************************
* Routine Name: Choose_USB
*
* Description
* This routine sets the pointer hUSB. This pointer is set to point to the active
* printer or NULL.
**************************************************************************************/
void Choose_USB()
{
int i;

//Close any potentially open ports
for (i = 0; i < USB_printer_count; i++) //For all potentially open ports
Close_USB(i); //close it.

hUSB = NULL; //reset to NULL
Index=0; //Default to first Boca USB printer found

// Calls to "hUSB = Locate_Device(FALSE,Index)" //Pick current active Boca USB printer
//If there is more than one Boca USB printer then the choice is a follows:
//If the boolean is TRUE then one will be automatically selected.
//If the boolean is FALSE then the one choosen is based on the Index.

//USB_printer_count established when Find_USB_Printers() is called.
if(USB_printer_count==0) //if no Boca USB printers found
{
//If unable to locate Boca USB printer
printf ("Unable to locate Boca USB printer....\n");
}
else
{

if(USB_printer_count==1) //if there is only one printer, pick it.
{
if (Printer_Table[Index].hUSB) //if port already open
Close_Port(Printer_Table[Index].hUSB); //close it.
hUSB = Locate_Device(TRUE,Index); //Attempt to reopen port
}
else //multiple choices found, control by Index
{
printf ("\nDevice # \tSerial # \tProduct ID \tProduct Name\n");
printf ("\n======== \t======== \t========== \t============\n");

for (i = 0; i < USB_printer_count; i++)
{
if (Printer_Table[i].Product_ID==0x0001)
sprintf(Name,"Model 42 - FGL/PCL4");
else
{
if (Printer_Table[i].Product_ID==0x0101)
sprintf(Name,"Model 44 - FGL");
else
{
if (Printer_Table[i].Product_ID==0x0102)
sprintf(Name,"Model 44 - PCL4");
else
sprintf(Name," ");
}
}
printf (" %d \t\t%d \t\t0x%04x \t\t%s\n",i,Printer_Table[i].Serial_Number,Printer_Table[i].Product_ID,Name);
}

printf("\nEnter Device # to choose a Printer: ");
scanf("%d",&printer);
gets(Buffer); //Terminates last scanf into dummy buffer

//if an invalid number entered here Index = 0 will be used.
if((printer>=0) && (printer<USB_printer_count))
{
Index = printer;
}

//check to see if port already open.
if (Printer_Table[Index].hUSB) //if port already open
Close_Port(Printer_Table[Index].hUSB); //close it.
hUSB = Locate_Device(FALSE,Index); //Pick current active Boca USB printer
}

//Verify the open was successful. At this point, hUSB points to the current
//device and it's information is updated and stored in printer table.
if (hUSB) //if successful hUSB != NULL
Printer_Table[Index].hUSB = hUSB; //store pointer to structure in table
else //else, unsuccesful
Printer_Table[Index].hUSB = NULL; //set pointer in table to NULL
}
return;
}
/**************************************************************************************
* Routine Name: Init_USB
*
* Description
* This routine loads the dll, performs verification and searches for Boca USB printers
**************************************************************************************/
void Init_USB()
{
/* Load Dynamic Link Library for BOCA Printers */
hinstLib=LoadLibrary("boca.dll");
if(hinstLib)
{
// Perform initialization and registration
if (proc_address(hinstLib) == 0) //All is good
{
if (Get_Handle(&hWD))
{
Find_USB_Printers(); //Fill Printer table for all Boca USB printers
hUSB = NULL; //still haven't choosen one yet, just found them
if (USB_printer_count>0) //Was at least one printer found?
{
printf ("\nDevice # \tSerial # \tProduct ID \tProduct Name\n");
printf ("======== \t======== \t========== \t============\n");

for (i = 0; i < USB_printer_count; i++)
{
if (Printer_Table[i].Product_ID==0x0001)
sprintf(Name,"Model 42 - FGL/PCL4");
else
{
if (Printer_Table[i].Product_ID==0x0101)
sprintf(Name,"Model 44 - FGL");
else
{
if (Printer_Table[i].Product_ID==0x0102)
sprintf(Name,"Model 44 - PCL4");
else
sprintf(Name," ");
}
}

printf (" %d \t\t%d \t\t0x%04x \t\t%s\n",i,Printer_Table[i].Serial_Number,Printer_Table[i].Product_ID,Name);
}
Index=0;
hUSB = Locate_Device(TRUE,Index); //choose the first one as default
if (hUSB) //if successful
{
Printer_Table[Index].hUSB = hUSB; //store pointer to structure in table
//printf ("\nOpened default Boca USB printer Serial Number: %d.\n",Printer_Table[Index].Serial_Number);
}
}
if(!hUSB) //if unable to find any Boca USB printers
{
printf ("\nUnable to locate a Boca USB printer....\n");
}

}
else
{
error_code = GetLastError();
printf ("Unable to Get Handle.... Error = %d\n",error_code);
}
}
else printf ("Unable to Initialize/Register DLL routines....\n");
}
else
{
error_code = GetLastError();
printf ("Unable to Open Library.... Error = %d\n",error_code);
}
return;
}

/**************************************************************************************
* Routine Name: Display_Buffer
*
* Description
* Display whatever is in the buffer two ways Hexadecimal and characters. The character
* response prints only printable text. The Hexadecimal dump will display everything read.
*
* Once it is assumed the incoming data is status data it will represent:
*
* 0x02 = "Start of Text"
* 0x03 = "End of Text"
* 0x04 = "End of Transmission"
* 0x05 = "Test Button Ticket ACK"
* 0x06 = "Ticket ACK"
* 0x10 = "Out of Tickets"
* 0x11 = "X-On"
* 0x12 = "Power On"
* 0x13 = "X-Off"
* 0x15 = "Ticket NAK"
* 0x16 = "Ribbon Low"
* 0x17 = "Ribbon Out"
* 0x18 = "Ticket Jam"
* 0x19 = "Illegal Data"
* 0x1A = "Power Up Problem"
* 0x1C = "Incomplete Logo Stored in FLASH"
**************************************************************************************/
void Display_Buffer(unsigned int size)
{
unsigned int i;

printf ("\n\nThe buffer contains %d Byte(s) of Data.",strlen(Buffer)-1);

// Display as Ascii characters
printf ("\n\nCharacter Response = ");
for (i = 1; i < size; i++)
{
// Display only printable characters. Display a space for the rest.
if ((Buffer[i] >= SPACE) && (Buffer[i] <= TILDE))
printf ("%c", Buffer[i]);
else
printf("%c",SPACE);
}

// Display as Ascii Hexadecimal
printf ("\n\nHexadecimal Response = ");
for (i = 1; i < size; i++)
printf ("%x ", Buffer[i]);
printf (" \n");


return;
}


/**************************************************************************************
* Routine Name: Port_Read
*
* Description
* Read from currently selected USB, parallel or serial port and display Buffer.
**************************************************************************************/
void Port_Read()
{

Bytes_Transferred = 0;

// USB Port
USB_Read();

if (Bytes_Transferred == 0)
printf ("\nNothing was read from port %s\n",port_name);
else
Display_Buffer(Bytes_Transferred);

return;
}


/**************************************************************************************
* Routine Name: Port_Write
*
* Description
* Port_Write was originally written to output data to the currently selected port
* be that USB, parallel or serial port. This version has been simplified for just USB.
* This routine could be eliminated and calls directly made to USB_Write. This is maintained
* to allow for backwards compatability to anyone already making calls to Port_Write().
**************************************************************************************/
void Port_Write()
{

// USB Port
USB_Write();


return;
}

/**************************************************************************************
* Routine Name: USB_Read
*
* Description
* Read data from currently selected USB port into Buffer.
**************************************************************************************/
void USB_Read()
{
pBuffer = &Buffer;
dwSize = USB_PACKET_SIZE;

error_code = Read_Data(hUSB, pBuffer, dwSize,READ_PIPE); //DLL call

if (error_code == 0xffffffff)
printf ("Read Error from USB port...... \n");
else
Bytes_Transferred = error_code; //# of bytes read
return;
}


/**************************************************************************************
* Routine Name: USB_Write
*
* Description
* Write Buffer data out to the currently selected USB port
**************************************************************************************/
void USB_Write()
{
pBuffer = &Buffer;
dwSize = strlen(Buffer);

error_code = Write_Data(hUSB, pBuffer, dwSize, WRITE_PIPE); //DLL call

if (error_code == 0xffffffff)
printf ("Error on write to USB port......\n");

return;
}

/**************************************************************************************
* Routine Name: Clear_Buffer()
*
* Description
* Clear Buffer between uses by padding string with nulls.
**************************************************************************************/
void Clear_Buffer()
{
pBuffer = &Buffer;
memset (pBuffer,0,PACKET_SIZE+1);
return;
}

/**************************************************************************************
* Routine Name: fgl_test_ticket()
*
* This routine will generate an FGL test ticket and send it to the currently
* selected port.
**************************************************************************************/
void fgl_test_ticket()
{

strcpy(Buffer,"<RL><RC300,20><F12>BOCA");
Port_Write();
strcpy(Buffer,"<RC300,110><RL><F6><HW1,1><VA7>"); // codename/version
Port_Write();
strcpy(Buffer,"<RC300,160><RL><F6><HW1,1><VA9>"); // common code.
Port_Write();
strcpy(Buffer,"<RC370,740><RU>GHOSTWRITER");
Port_Write();
strcpy(Buffer,"<RC48,880><RR><F2>FRIENDLY GHOST LANGUAGE");
Port_Write();
strcpy(Buffer,"<RC52,862>PLACE LETTERS ANYWHERE");
Port_Write();
strcpy(Buffer,"<RC80,1170><F6><RR>VOID");
Port_Write();
strcpy(Buffer,"<RC15,1100><F3>TEST TICKET ONLY");
Port_Write();
strcpy(Buffer,"<RC8,1010><X2><NXL10>*GHOST 123*");
Port_Write();
strcpy(Buffer,"<RR><RC78,915> CODE 39 ");
Port_Write();
strcpy(Buffer,"<RU><RC320,740><BS17,30>TICKET & LABEL PRINTER");
Port_Write();
strcpy(Buffer,"<NR><F3><RC0,300>300 DPI PRINT QUALITY");
Port_Write();
strcpy(Buffer,"<NR><RC30,300> Print any bar code");
Port_Write();
strcpy(Buffer,"<RC70,300><X2><OL6>^CODE 128^");
Port_Write();
strcpy(Buffer,"<RC90,250><RR>CODE 128");
Port_Write();
strcpy(Buffer,"<RC96,340><X3><NXP10>*MONKEY*");
Port_Write();
strcpy(Buffer,"<RC206,340><X2><NP6>*MONKEY*<F2><HW1,1>");
Port_Write();
strcpy(Buffer,"<NR><F1><SP180,640><LO1>");
Port_Write();
strcpy(Buffer,"<RC371,385><F2><NR>SW1= SW2= SW3=");
Port_Write();
strcpy(Buffer,"<RC371,824>LEFT = 1 RIGHT = 0");
Port_Write();
strcpy(Buffer,"<RC385,150><F11>Script printing is now standard.");
Port_Write();
strcpy(Buffer,"<RC430,150><F9>High density printing is clear and readable");
Port_Write();
strcpy(Buffer,"<RC450,150><F2>High density printing is clear and readable");
Port_Write();
strcpy(Buffer,"<RC470,150><F1>Legal size printing");
Port_Write();
strcpy(Buffer,"<p>"); // Form Feed.
Port_Write();
}


/**************************************************************************************
* Routine Name: Perform_Test
*
* This routine performs the write tests.
**************************************************************************************/
void Perform_Test()
{
switch (w_function)
{
case 1:
Port_Write(); //write an individual command to printer.
break;

case 2:
Port_Write(); //write an ASCII value/command to printer.
break;

case 3:
fgl_test_ticket(); //send an entire FGL test ticket to printer.
break;

case 4:
hp_test_ticket(); //send an entire HP test ticket to printer.
break;

default:
break;
}
return;
}

/**************************************************************************************
* Routine Name: hp_test_ticket()
*
* This routine will generate an HP test ticket by reading the file hp_id.prn.
**************************************************************************************/
void hp_test_ticket()
{
FILE *fp;
long filesize;


Clear_Buffer();

fp = fopen("hp_id.prn","rb");
if(fp == NULL)
printf("\nUnable to find hp_id.prn");
else
{
filesize = filelength(fileno(fp));
while(filesize > 0)
{
if(filesize > SER_PACKET_SIZE)
i = SER_PACKET_SIZE;
else
i = filesize;
fread(pBuffer, sizeof( char ), i, fp);
if (i > 0)
error_code = Write_Data(hUSB, pBuffer, i, WRITE_PIPE);
filesize = filesize - i;
}

fclose(fp);
}

}


//Initialization code, that searches for Boca USB printers
void Find_USB_Printers()
{
int i;
USB_printer_count=0;
for (i = 0; i < USB_PRINTER_MAX; i++) //clear table
{
Printer_Table[i].Serial_Number=0; //zero
Printer_Table[i].Product_ID=0; //zero
Printer_Table[i].hUSB=NULL; //null pointer
}
found1 = false; //initialize logical for printer A
found2 = false; //initialize logical for printer B
pTable = &Printer_Table[0]; //establish pointer to start of table
USB_printer_count = USB_Find(pTable); //Populate table, Count & identify USB printers
return;
}

void Close_USB(int i)
{
if(Printer_Table[i].hUSB) Close_Port(Printer_Table[i].hUSB); //If a USB port is open, close it.
Printer_Table[i].hUSB=NULL; //null pointer

return;
}



/**************************************************************************************
* Below is a collection of routines that allow a user to use 2 Boca USB FGL printers as
* if they were one Boca Dual FGL printer. A Boca dual printer can be commanded to
* change between paths A & B with the use of the FGL commands <P1> and <P2>.
* To exercise these routines from the code, look at the main menu code options above
* and uncomment the line of code to print menu option 7, rebuild and run again. Also
* look at the Case 7 in the switch statement in the main routine. It drives this.
* These routines (and case 7) need not be a part of the standard USB communication code
* and can be excluded if you choose If you are using just one printer or simply don't
* need to simulate a dual path printer with 2 Boca USB printers, then eliminate these routines
* to simplify it.
* At the time Dual path printer were/are not made with one of the paths being a
* magnetic printer and this is a solution to simulate that.
* Once these routines had been written to prove the functionality, it seemed a waste not
* to share it with anyone who might want to use two Boca USB printers as one Dual Path
* printer. The same FGL command procedures already written for an existing dual path system
* would work with the 2 USB printers.
**************************************************************************************/

/**************************************************************************************
* Routine Name: history()
*
* Description
* This routine uses a file named usb_history.dat to maintain the current selections
* of Printer A & B when printing with 2 printer simulating one dual printer. This
* data is maintained so that even after power outages, the printers assignment will
* be maintained for the next use.
**************************************************************************************/
void history()
{
FILE *history_fp; //File pointer for USB history data file
int i;


history_fp = fopen("usb_history.dat","r");
if(history_fp == NULL) //No history recorded, so lets establish one
{
if(USB_printer_count>0)
{
establish_history();
record_history();
}
}
else //read history file, & verify printer currently plugged in
{
fscanf(history_fp,"%d",&P1);
fscanf(history_fp,"%d",&P2);
fclose(history_fp);

//verify P1 & P2 in table of found printers
found1=false;
i=0;
do
{
if (P1==Printer_Table[i].Serial_Number) found1 = true;
i++;
}
while ((i < USB_printer_count) && (!found1));

found2=false;
i=0;
do
{
if (P2==Printer_Table[i].Serial_Number) found2 = true;
i++;
}
while ((i < USB_printer_count) && (!found2));

}
}


/**************************************************************************************
* Routine Name: establish_history()
*
* Description
* This rouinte will collect the serial numbers needed for printers P1 & P2 and then
* call record_history().
**************************************************************************************/
void establish_history()
{
int printer,i;

printf("\nNo multiple printer history exists or history out of date. ");
printf("\nWe need to setup A & B. Enter the device number for each selection.");
printf ("\nDevice # \tSerial # \tProduct ID \tProduct Name\n");
printf ("\n======== \t======== \t========== \t============\n");

for (i = 0; i < USB_printer_count; i++)
{
if (Printer_Table[i].Product_ID==0x0001)
sprintf(Name,"Model 42 - FGL/PCL4");
else
{
if (Printer_Table[i].Product_ID==0x0101)
sprintf(Name,"Model 44 - FGL");
else
{
if (Printer_Table[i].Product_ID==0x0102)
sprintf(Name,"Model 44 - PCL4");
else
sprintf(Name," ");
}
}
printf (" %d \t\t%d \t\t0x%04x \t\t%s\n",i,Printer_Table[i].Serial_Number,Printer_Table[i].Product_ID,Name);
}

printf("\nChoose Primary Printer A: ");
scanf("%d",&printer);
gets(Buffer); //Terminates last scanf into dummy buffer
if((printer>=0) && (printer<USB_printer_count))
{
P1 = Printer_Table[printer].Serial_Number;
found1 = true;
}


printf("\nChoose Secondary Printer B: ");
scanf("%d",&printer);
gets(Buffer); //Terminates last scanf into dummy buffer
if((printer>=0) && (printer<USB_printer_count))
{
P2 = Printer_Table[printer].Serial_Number;
found2 = true;
}
}

/**************************************************************************************
* Routine Name: record_history()
*
* Description
* This rouinte will write the serial numbers selected for printers P1 & P2
* to the history file usb_history.dat
**************************************************************************************/
void record_history()
{
FILE *history_fp; //File pointer for USB history data file

//record new history, destroy any old data
history_fp = fopen("usb_history.dat","w");
fprintf(history_fp, "%d\n", P1 );
fprintf(history_fp, "%d\n", P2 );
fclose(history_fp);
}
void dual_test()
{
// For this test make sure any open port is closed from some previous
// efforts above. Close any port open in the table
for (Index = 0; Index < USB_printer_count; Index++)
{
if (Printer_Table[Index].hUSB)
{
Close_Port(Printer_Table[Index].hUSB);
Printer_Table[Index].hUSB = NULL;
}
}

//Place this call anywhere needed for potential hot swapping of USB printers
Find_USB_Printers();

//Check Global veriable to determine number of printer found.
if (USB_printer_count == 0) //No printers found, error condition
{
printf("\nNo Boca Systems USB printers found.");
}
else if (USB_printer_count == 1) //Only one printer found, so set all pointers to it
{
P1=Printer_Table[0].Serial_Number; //Make sure they point to the same place.
P2=Printer_Table[0].Serial_Number; //This one print may be a dual path printer
found1 = true; //that needs to act normal.
found2 = true;
}
else if (USB_printer_count > 1) //More than one printer found
{
//Associate:
//
// Printer A (also known as P1) with FGL command <P1>
// Printer B (also known as P2) with FGL command <P2>
//
//Setting Printer A as P1 and Printer B as P2 will help simulate
//a dual feed printer for two Boca Systems USB printers.
//We will check on prior printer history, or establish a new one.
//Next, be aware of special communication with Printers P1 & P2
//will happen through calls to Send_Dual() instead of calling
//the usual routine Send() directly.
//Send_Dual() will scan the ASCII strings headed to the USB port
//for the FGL commands <P1> and <P2>. A printer pointer will be establish
//and then a call to Send() will be performed from within Send_Dual()

//Lets check on and/or establish printer history
history();

//To have arrived here, multiple printers must have been found on your system .
//The possible options at this point are:
// 1. None of these printer match up to your old setup, or you have
// no history (first time). usb_history.dat does not exist, yet.
// 2. Printer A <P1> was found but not printer B <P2>
// 3. Printer B <P2> was found but not printer A <P1>
// 4. Both printers A & B found. All is good.
//
//In the cases (2 & 3) where only one printer is found then a substitue
//be used in its place, if one is found.
//
//The variables found1 & found2 are logical flags used to indicate
//whether P1 and/or P2 have been found

//Neither A nor B were found (or yet exist), so let's establish
//the choices for printer A (P1) & B (P2)
if ((!found1) && (!found2))
{
//re-establish history...
printf("\nFound Neither A %d nor B %d",P1,P2);
establish_history();
record_history();
}
else
{
if (!found1) //A was not found but B was
{
//reassign P1...
printf("\nFound B %d but not A %d",P2,P1);
i=0;

//Look for a substitute printer on the system
while ((i < USB_printer_count) && (!found1))
{ //don't let P1 & P2 be the same printer just yet, since there
//is at least one other.
if (Printer_Table[i].Serial_Number!=P2)
{
P1=Printer_Table[i].Serial_Number;
found1 = true;
}
else i++;
}
if(found1) //if a substitue printer was found and assigned
{
printf("\nreassigned A as %d",P1);
record_history(); //update the history file usb_history.dat
}
else //if not then this is a error condition. You should never arrive here
{
printf("\nunable to reassign A");
//Error condition needs to be dealt with so Printer A (P1)
//and Printer B (P2) will become the same. No history update will
//be performed at this point. We just need a graceful exit
P1=P2;
found1 = true;
}
}
else
{
if (!found2) //B was not found but A was
{
//reassign P2...
printf("\nFound A %d but not B %d",P1,P2);
i=0;
while ((i < USB_printer_count) && (!found2))
{ //don't let P1 & P2 be the same printer just yet, since there
//is at least one other.
if (Printer_Table[i].Serial_Number!=P1)
{
P2=Printer_Table[i].Serial_Number;
found2 = true;
}
else i++;
}
if(found2) //if a substitue printer was found and assigned
{
printf("\nreassigned B as %d",P2);
record_history(); //update the history file usb_history.dat
}
else //if not then this is a error condition. You should never arrive here
{
printf("\nunable to reassign B");
//Error condition needs to be dealt with so Printer B (P2)
//and Printer A (P1) will become the same. No history update will
//be performed at this point. We just need a graceful exit
P2=P1;
found2 = true;
}
}
else
{
printf("\nFound Both A %d and B %d",P1,P2); //All is good

}
}
}
//if both paths established then, print test tickets
if ((found1) && (found2))
{
//Test Code
//set up dual pointers for Global hUSB
for (Index = 0; Index < USB_printer_count; Index++) //clear table
{

//check to see if port has already been opened.
if (Printer_Table[Index].hUSB) //if port already open
Close_USB(Index); //close it.
//Close_Port(Printer_Table[Index].hUSB); //If Printer port is open, close it.
hUSB = Locate_Device(FALSE,Index); //Pick current active Boca USB printer
if (hUSB)
{
if (Printer_Table[Index].Serial_Number==P1) //if this happens to be P1 save a copy of the pointer
{
P1_ptr = hUSB;
Printer_Table[Index].hUSB = hUSB;
}
if (Printer_Table[Index].Serial_Number==P2) //if this happens to be P2 save a copy of the pointer
{
P2_ptr = hUSB;
Printer_Table[Index].hUSB = hUSB;
}
}
hUSB = NULL;
}

//call test print routines to exercise system
hUSB = P1_ptr; //Establish Printer "A" as the default
dual_test_ticket();

for (i = 0; i < USB_printer_count; i++) //For all ports
Close_USB(i); //close it.
hUSB = NULL;

}
}
return;
}

/**************************************************************************************
* Routine Name: dual_test_ticket()
*
* Description
* This rouinte will exercise both selected FGL printers P1 & P2, by printing a text
* file containing a series of FGL commands
**************************************************************************************/
void dual_test_ticket()
{
FILE *fp;
long filesize;
int i;

//open the file, read it and send it to the open USB port

Clear_Buffer();

fp = fopen("fgl_config.txt","rb");
if(fp == NULL)
printf("\nUnable to find fgl_config.txt");
else
{
filesize = filelength(fileno(fp));
while(filesize > 0) //Read the whole file
{
if(filesize > USB_PACKET_SIZE) //if the file is larger than the packet size
i = USB_PACKET_SIZE; //send it one packet at a time
else //if the file is smaller than one packet
i = filesize; //send just whats needed

fread(pBuffer, sizeof( char ), i, fp); //read file data into BUFFER[]
if (i > 0)
{
dwSize = i;
Send_Dual(); //if read successful then call special Send
}
filesize = filesize - i; //decrement amount left to read and send
}

fclose(fp);
}

return;
}

/**************************************************************************************
* Routine Name: Send_Dual()
*
* Description
* This routine monitors all outgoing traffic for the FGL commands <P1> and <P2>. When
* one of these commands is found the device pointer is changed back and forth.
**************************************************************************************/
void Send_Dual()
{
int i,size1,size2;
bool split=false;
USB_HANDLE hUSB_Change = NULL;
PVOID pBuffer2; // pointer to buffer

//Buffer contains outgoing data
for (i = 0; i < (int)dwSize; i++) //search string for FGL <P1> <P2>
{
if(Buffer[i] == OPEN_DELIMETER) //"<" Found an FGL command
{
if(Buffer[i+1] == 80) //"P" Found an FGL P command
{
if(Buffer[i+2] == 49) //"1" Found an FGL P1 command
{
hUSB_Change = P1_ptr;
if(i>=0)
size1=i; //size from index 0 to i-1
else
size1=0;
size2=dwSize-i;
split = true;
}
else
{
if(Buffer[i+2] == 50) //"2" Found an FGL P2 command
{
hUSB_Change = P2_ptr;
if(i>=0)
size1=i; //size from index 0 to i-1
else
size1=0;
size2=dwSize-i;
split = true;
}
}
}
}
}


//When you find the <P1>'s and <P2>'s, they are typically embedded
//inside a long string of data. The data then needs to be split so that the first
//part goes to the current printer and then the second part of the string can be
//redirected to the other printer after the USB_Change takes place.
if (split)
{
if (size1>0) error_code = Write_Data(hUSB, pBuffer, size1, WRITE_PIPE); //send it the the USB port
hUSB = hUSB_Change;
pBuffer2=&Buffer[size1];
if (size2>0) error_code = Write_Data(hUSB, pBuffer2, size2, WRITE_PIPE); //send it the the USB port
}
else
error_code = Write_Data(hUSB, pBuffer, dwSize, WRITE_PIPE); //send it the the USB port

return;
}
Posté le 28 septembre 2012 - 17:46
Veuillez trouver ci-dessous le fichier entête cpp. WDAPI ne sert à rien dans ce cas.
// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the BOCA1_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// BOCA1_API functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
#ifdef BOCA_DLL_EXPORTS
#define BOCA1_API __declspec(dllexport)
#else
#define BOCA1_API __declspec(dllimport)
#endif

/* End of global definitions */
#pragma comment(lib, "boca.lib")

#define WD_USB_MAX_PIPE_NUMBER 32
#define WD_USB_MAX_ENDPOINTS WD_USB_MAX_PIPE_NUMBER
#define WD_USB_MAX_INTERFACES 30
#define WD_USB_MAX_DEVICE_NUMBER 127
#define LPT1_S "LPT1:"
#define LPT2_S "LPT2:"
#define LPT3_S "LPT3:"
#define LPT4_S "LPT4:"
#define COM1_S "COM1:"
#define COM2_S "COM2:"
#define COM3_S "COM3:"
#define USB_S "USB"

enum { PACKET_SIZE = 256 };
enum { USB_PRINTER_MAX = 127 };
enum { USB_PACKET_SIZE = 256 };
enum { SER_PACKET_SIZE = 128 };
enum { PAR_PACKET_SIZE = 128 };
enum { USB_DEFAULT_VENDOR_ID = 0x0A43 };
enum { USB_DEFAULT_PRODUCT_ID = 0x0001 };
enum { USB_DEFAULT_CONFIG = 0 };
enum { USB_DEFAULT_INTERFACE = 0 };
enum { USB_DEFAULT_ALTERNATE = 0 };
enum { READ_PIPE = 130};
enum { WRITE_PIPE = 1};
enum { USB = 1};
enum { BOCA_ID = 0x0A43 };
enum { SPACE = 32 };
enum { TILDE = 126 };
enum { OPEN_DELIMETER = 60 }; //Open Bracket -> "<"
enum { SET_READ_TIMEOUT = 114 }; //Read timeout function number
enum { SET_WRITE_TIMEOUT = 119 }; //Write timeout function number

// Types to be used later.
typedef unsigned long DWORD;
typedef int BOOL;
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef float FLOAT;
typedef void *PVOID;
typedef PVOID HANDLE;

typedef struct
{
DWORD dwVendorId;
DWORD dwProductId;
} WD_USB_ID;

typedef struct
{
DWORD dwNumber; // Pipe 0 is the default pipe
DWORD dwMaximumPacketSize;
DWORD type; // USB_PIPE_TYPE
DWORD direction; // USB_DIR
// Isochronous, Bulk, Interrupt are either USB_DIR_IN or USB_DIR_OUT
// Control are USB_DIR_IN_OUT
DWORD dwInterval; // interval in ms relevant to Interrupt pipes
} WD_USB_PIPE_INFO;

typedef struct
{
DWORD dwPipes;
WD_USB_PIPE_INFO Pipe[WD_USB_MAX_PIPE_NUMBER];
} WD_USB_DEVICE_INFO;


typedef struct USB_STRUCT
{
HANDLE hWD;
WD_USB_DEVICE_INFO deviceInfo;
DWORD hDevice;
} USB_STRUCT;

typedef struct USB_STRUCT *USB_HANDLE;

//DO NOT CHANGE THIS STRUCTURE
//This structure is used by the DLL.
typedef struct
{
USB_HANDLE hUSB;
DWORD Serial_Number;
DWORD Product_ID;
} USB_PRINTER;

typedef struct
{
char Name[64];
} PRINTER_NAME;


/**************************************************************************************
* Prototype Definitions of Dynamic Link Library routines. Exporting all of these
* functions from boca.dll.
**************************************************************************************/
extern "C" BOCA1_API DWORD Write_USB(USB_HANDLE hUSB, PVOID pBuffer, DWORD dwSize,int pipe);
extern "C" BOCA1_API DWORD Read_USB(USB_HANDLE hUSB, PVOID pBuffer, DWORD dwSize,int pipe);
extern "C" BOCA1_API void RegisterWinDriver();
extern "C" BOCA1_API BOOL USB_Get_WD_handle(HANDLE *phWD);
extern "C" BOCA1_API void USB_Printer_choice(DWORD dwVendorId, DWORD dwProductId);
extern "C" BOCA1_API USB_HANDLE USB_LocateAndOpenDevice(BOOL fUseDefault,int Index);
extern "C" BOCA1_API int proc_address(HINSTANCE hinstLib);
extern "C" BOCA1_API void USB_Timeout(PVOID pBuffer, int timeout);
extern "C" BOCA1_API int USB_Find(USB_PRINTER *Printer_Table);
//extern "C" BOCA1_API int USB_Find(int *Printer_Table);
extern "C" BOCA1_API void USB_Close(USB_HANDLE hUSB);
extern "C" BOCA1_API BOOL USB_Get_WD_handle(HANDLE *phWD);
extern "C" BOCA1_API DWORD Status_USB(USB_HANDLE hUSB);
extern "C" BOCA1_API DWORD Control_USB(USB_HANDLE hUSB, PVOID pBuffer, DWORD dwSize, CHAR setupPacket[8]);

/* Type definitions as pointers to functions with parameters */
typedef DWORD (*Read_USB_type)(USB_HANDLE hUSB, PVOID pBuffer, DWORD dwSize,int pipe);
typedef DWORD (*Write_USB_type)(USB_HANDLE hUSB, PVOID pBuffer, DWORD dwSize,int pipe);
typedef void (*RegisterWinDriver_type)();
typedef void (*USB_Printer_choice_type)(DWORD dwVendorId, DWORD dwProductId);
typedef USB_HANDLE (*USB_LocateAndOpenDevice_type)(BOOL fUseDefault,int Index);
typedef int (*proc_address_type)(HINSTANCE hinstLib);
typedef void (*USB_Timeout_type)(PVOID pBuffer, int timeout);
typedef int (*USB_Find_type)(USB_PRINTER *Printer_Table);
//typedef int (*USB_Find_type)(int *Printer_Table);
typedef void (*USB_Close_type)(USB_HANDLE hUSB);
typedef BOOL (*USB_Get_WD_handle_type)(HANDLE *phWD);
typedef DWORD (*Status_USB_type)(USB_HANDLE hUSB);
typedef DWORD (*Control_USB_type)(USB_HANDLE hUSB, PVOID pBuffer, DWORD dwSize, CHAR setupPacket[8]);


/* Global Definitions for all DLL routines */

extern "C" BOCA1_API Read_USB_type Read_Data;
extern "C" BOCA1_API Write_USB_type Write_Data;
extern "C" BOCA1_API RegisterWinDriver_type Register_Driver;
extern "C" BOCA1_API USB_Printer_choice_type Choose_Printer;
extern "C" BOCA1_API USB_LocateAndOpenDevice_type Locate_Device;
extern "C" BOCA1_API USB_Timeout_type Timeout_Port;
extern "C" BOCA1_API USB_Find_type Find_Printers;
extern "C" BOCA1_API USB_Close_type Close_Port;
extern "C" BOCA1_API USB_Get_WD_handle_type Get_Handle;
extern "C" BOCA1_API Status_USB_type USB_Status;
extern "C" BOCA1_API Control_USB_type USB_Control;
Membre enregistré
512 messages
Posté le 28 septembre 2012 - 18:06
Bonjour Ralph,

Avant de commencer la traduction en WLangage, voici la traduction en euros:

300 euros


PS. désolé, la tentation était trop forte de faire cette mauvaise blague. ;)


--
Pour me contacter par courrier électronique, cliquez sur le lien ci-dessous (protection antispam): http://cerbermail.com/…
Membre enregistré
58 messages
Posté le 28 septembre 2012 - 19:05
il est malin, moi je vais déposer des CDC sous forme de jeux gratuits ;-)


--
Mac Toutim
Posté le 01 octobre 2012 - 11:56
C compliqué tout ça.
Membre enregistré
163 messages
Posté le 01 octobre 2012 - 13:55
Ralph a écrit dans le message de news <4cf085189df3948ded02ef981764fbfb@news.pcsoft> :
> C compliqué tout ça.

Je dirais même plus : C++ compliqué tout ça. lol
Plus sérieusement, il me semble que WD sait interpréter d'autres langages (dont le C/C++), par contre je n'en ai encore jamais eu besoin, donc je ne sais pas comment.
Mais par curiosité je cherche ça et je vous tiens au courant. Ça éviterai de traduire pour rien...

@+

--
FredB
--
Le développement c'est la vie (des processus) et la vie c'est le développement (de soi)
--
Posté le 01 octobre 2012 - 18:14
Bonjour,
Le 01/10/2012 13:06, FredB a écrit :
Ralph a écrit dans le message de news <4cf085189df3948ded02ef981764fbfb@news.pcsoft> :
C compliqué tout ça.

Je dirais même plus : C++ compliqué tout ça. lol
Plus sérieusement, il me semble que WD sait interpréter d'autres langages (dont le C/C++), par contre je n'en ai encore jamais eu besoin, donc je ne sais pas comment.
Mais par curiosité je cherche ça et je vous tiens au courant. Ça éviterai de traduire pour rien...

Windev ne sait pas interpréter du C ou C++ mais est capable d'interagir
avec des dev fait en C ou C++, ou d'autres langages.


--
suivre ce lien pour répondre:
http://cerbermail.com/…
Daniel
;-)
Posté le 02 octobre 2012 - 00:44
Je retourne aux bord voir
Membre enregistré
163 messages
Posté le 02 octobre 2012 - 10:51
Daniel a écrit dans le message de news <5069a2d3$1@news.pcsoft.fr> :
Windev ne sait pas interpréter du C ou C++ mais est capable d'interagir
avec des dev fait en C ou C++, ou d'autres langages.



Effectivement après quelques recherches je confirme WD ne sait pas interpréter du C ou C++ mais il existe des ordres (et des bibliothèques) pour interfacer un programme écrit avec un autre langage (comme C ou C++) avec un programme en WD.

Bon courage.
@+

--
FredB
--
Le développement c'est la vie (des processus) et la vie c'est le développement (de soi)
--
Posté le 02 octobre 2012 - 12:04
C Tout cela que je cherche les gars.
Je vais maintenant molo molo. C complexe