This method is suitable for most small ticket machines in the market, such as good bo, POS58, etc., not suitable for some label printers such as zebra printers, etc.
Direct Sticker Code:
PrivateFileStream fs =NULL; [DllImport ("Kernel32.dll", CharSet = CharSet.Auto)]//Call System API Print function Public Static externIntPtr CreateFile (stringFileName,//file name UINTDesiredaccess,//access Mode UINTShareMode,//Share mode UINTSecurityattributes,//Security Attributes UINTCreationdisposition,//How to Create UINTFlagsandattributes,//file Attributes stringhTemplateFile//handle to template file ); /// <summary> ///Print a small ticket/// </summary> /// <param name= "msg" ></param> /// <returns></returns> Public stringPrintmsg (stringPosport,stringMsgBOOLIsopenmoneybox) { Try{IntPtr Ihandle= CreateFile (Posport,0x40000000,0,0,3,0,NULL); if(Ihandle.toint32 () = =-1) { return "Ticket Machine Connection failed. Or the port number is wrong. Please test the printer is OK!"; } Else { Try{FS=NewFileStream (Ihandle, FileAccess.ReadWrite); StreamWriter SW=NewStreamWriter (FS, System.Text.Encoding.Default);//Write DataSW. WriteLine (msg); //Open the cash drawer if(isopenmoneybox) {sw. Write ( (Char) -). ToString () +"P"+ ((Char)0). ToString () + ((Char) -). ToString () + ((Char)255). ToString ()); } SW. Flush (); Sw. Close (); Fs. Close (); return ""; } Catch(Exception ex) {returnEx. InnerException = =NULL?Ex. Message:ex. Innerexception.message; } } } Catch(Exception exp) {return "TPL or COM port connection failed! "; } }
View Code
C # operation of the small ticket machine printing using the/C mode