打印机ESCPOS控制指令

合集下载

(完整版)ESC打印控制命令集

(完整版)ESC打印控制命令集

ESC打印控制命令集归纳LANPOS5896 打印机供应了ESC/POS 打印命令集。

各个命令的描述形式以下:打印命令功能格式: ASCII :以标准ASCII 字符序列表示十进制:以十进制数字序列表示十六进制:以十六进制数字序列表示说明:该命令功能和使用说明例子:为了更简单理解该命令会列出一些例子。

打印命令LF打印并换行格式: ASCII : LF十进制: 10十六进制: 0A说明:打印行缓冲器里的内容并向前走纸一行。

当行缓冲器空时只向前走纸一行。

ESC J打印并走纸N 点行格式: ASCII : ESC J十进制: 2774十六进制: 1B 4A n n n说明:打印行缓冲器里的内容,并向前走纸n 点行〔即n/203 英寸〕。

n=0~255。

该命令只本行打印有效,不改变ESC 2,ESC 3 命令设置的行间距值。

比方:行间距设置命令ESC 2设置字符行间距为1/6 英寸格式: ASCII : ESC 2十进制: 27 50十六进制: 1B 32说明:设置行间距为1/6 英寸ESC 3设置行间距为n 点行〔 n/203英寸〕格式: ASCII : ESC 3十进制: 27 51十六进制: 1B 33说明:设置行间距为n 点行。

n=0~255 。

LANPOS5896 打印机的每点距为1/203 英寸,即该命令设置行间距为n/203 英寸。

默认值为 n=30。

比方:字符打印命令ESC !设置字符打印方式格式: ASCII : ESC !N十进制: 27 33N十六进制: 1B 21N说明:ESC ! n 是综合性的字符打印方式设置命令,用于选择打印字符的大小。

打印参数n 的每位定义为:n 的默认值为0,即字符不放大。

ESC SO设置字符倍宽打印格式: ASCII : ESC SO十进制: 27 14十六进制: 1B 0E说明:在一行内该命令此后的所有字符均以正常宽度的 2 倍打印;该命令可以用回车或DC4 命令删除。

ESC POS指令手册说明书

ESC POS指令手册说明书

ESC/POS Command ManualCommand DescriptionsCommand Notation[Name]The name of the control command.[Format]The code sequence.In this description, < > H denotes hexadecimal numbers, < >denotesdecimal numbers and < > B denotes binary numbers.[ ] k indicates the contents of the [ ] should be repeated k times.[Range]The allowable range for the arguments.[Description]Description of the command function.[Details] If necessary provides important information on setting and using the printer command. [Default] The default values for the commands.[Reference]List related commands.[Example]Example of using the commands.The numbers denoted by <>H is hexadecimal.The numbers denoted by <>B is binary.Print CommandsThe WTP series supports the following commands for printing characters and advancingpaper.HT[Name] Horizontal tab[Format] ASCII HTHex 09Decimal 9[Description] Moves the print position to the next tab position.[Details] ·This command is ignored unless the next tab position has been set.·If the next horizontal tab position exceeds the printing area, the printersets the printing position to [Printing area width + 1].·Horizontal tab positions are set using “ESC D”.·If this command is received when the printing position is at [printing area width +1], the printerexecutes print buffer-full printing of the current line and horizontal tab processing from thebeginning of the next line.·The default setting of the horizontal tab position for the paper roll is font A(12 x 24) every 8th character (9th, 17th, 25th, … column).[Reference] ESC DLF[Name] Print and line feed[Format] ASCII LFHex 0ADecimal 10[Description] ·Prints the data in the print buffer and feeds one line based on the currentline spacing.[Details] ·This command sets the print position to the beginning of the line.[Reference] ESC 2, ESC 3CR[Name] Print and carriage return.[Format] ASCII CRHex 0DDecimal 13[Description] When automatic line feed is enabled, this command functions the same as LF; when automatic line feed is disabled, this command is ignored.[Details] ·Sets the print starting position to the beginning of the line.·The automatic line feed is ignored.[Reference] LFFF[Name] Print and return to standard mode in page mode.[Format] ASCII FFHex 0CDecimal 12[Description] Prints the data in the print buffer and returns to standard mode.[Details] ·The buffer data is deleted after being printed.·The printing area set by ESC W is reset to the default setting.·The printer does not execute paper cutting.·This command sets the print position to the beginning of the line.·This command is enabled only in page mode.[Reference] ESC FF, ESC L, ESC SCAN[Name] Cancel print data in page mode[Format] ASCII CANHex 18Decimal 24[Description] In page mode, delete all the print data in the current printable area. [Details] ·This command is enabled only in page mode.·If data that existed in the previously specified printable area also exists inthe currently specified printable area, it is deleted.[Reference] ESC L, ESC WDLE EOT n[Name] Real-time status transmission.[Format] ASCII DLE EOT nHex 10 04 nDecimal 16 4 n[Range] 1≤n≤4[Description] Transmits the selected printer status specified by n in real-time,according to the following parameters:n=1 : Transmit printer statusn=2 : Transmit off-line statusn=3 : Transmit error statusn=4 : Transmit paper roll sensor status[Details] ·The printer transmits the current status. Each status is represented byone-byte data.·The printer transmits the status without confirming whether the hostcomputer can receive data.·The printer executes this command upon receiving it.·This command is executed even when the printer is offline, the receivebuffer is full, or there is an error status.·When Auto Status Back (ASB) is enabled using the GS a command, thestatus transmitted by the DLE EOT command and the ASB status mustbe differentiated.·Even though the printer is not selected using ESC = (select peripheraldevice), this command is effective.[Notes] ·The status is transmitted whenever the data sequence of<10>H<04>H<n> (1≤n≤4) is received.Example :In ESC * m nL nH d1…dk d1=<10>H, d2=<04>H, d3=<01>H·This command should not be used within the data sequence of anothercommand that consists of 2 or more bytes.Example :If you attempt to transmit ESC 3 n to the printer, but DTR (DSR forthe host computer) goes to MARK before n is transmitted and thenDLE EOT 3 interrupts before n is received, the code <10> H for DLEEOT 3 is processed as the code for ESC 3 <10>H.correcting the cause of the error and executing DLE ENQ n (1 ≤ n ≤ 2). If an errordue to a circuit failure (e.g. wire break) occurs, it is impossible to recover.Bit 6: When printing is stopped due to high print head temperature until the print headtemperature drops sufficiently or when the paper roll cover is open during printing,bit 6 is On.[Reference] DLE ENQ, GS a, GS rDLE ENQ n[Name] Real-time request to printer[Format] ASCII DLE ENQ nHex 10 05 nDecimal 16 5 n[Range] 1≤n≤2[Description] Responds to a request from the host computer.[Details] ·When the printer is disabled with ESC = (Select peripheral device), thiscommand is effective.·This command is effective only when an auto-cutter error occurs.·The printer starts processing data upon receiving this command.·This command is executed even when the printer is offline, the receive buffer is full, or there isan error status with a serial interface model.·The status is also transmitted whenever the data sequence of <10>H<05>H< n> (1≤n≤2) isreceived.Example:In ESC * ** * m nL nH d k, d1 = <10>H, d2 = <05>H, d3 = <01>H·This command should not be contained within another command that consists of two or morebytes.Example:If you attempt to transmit ESC 3 n to the printer, but DTR (DSR for the host computer)goes to MARK before n is transmitted, and DLE ENQ 2 interrupts before n is received,the code <10>H for DLE ENQ 2 is processed as the code for ESC 3 <10>H. [Reference] DLE EOTDLE DC4 n m t[Name] Generate pulse at real-time[Format] ASCII DLE DC4 n m tHex 10 14 n m tDecimal 16 20 n m t[Range] n=1m=0,11≤t≤8[Description][Details] ·When the printer is in an error status when this command is processed,this command is ignored.·When the pulse is output to the connector pin specified while ESC p or DEL DC4 is executedwhile this command is processed, this command is ignored.·The printer executes this command upon receiving it.·This command is executed even when the printer is off-line, the receive buffer is full, or there is anerror status.·If print data includes the same character strings as this command, the printer performs the sameoperation specified by this command. The user must consider this.·This command should not be used within the data sequence of another command that consists of2 or more bytes.·This command is effective even when the printer is disabled with ESC = (Select peripheral device). [Reference] ESC pESC FF[Name] Print data in page mode[Format] ASCII ESC FFHex 1B 0CDecimal 27 12[Description] In page mode, prints all buffered data in the printable area collectively.[Details] ·This command is enabled only in page mode.·After printing, the printer does not clear the buffered data, setting value for ESC T and ESC W,and the position for buffering character data.[Reference] FF, ESC L, ESC SESC SP n[Name] Set right-side character spacing[Format] ASCII ESC SP nHex 1B 20 nDecimal 27 32 n[Range] 0≤n ≤255[Description] Sets the character spacing for the right side of the character to[n x horizontal or vertical motion units].[Details] ·The right-side character spacing for double-width mode is twice thenormal value. When characters are enlarged, the right-side characterspacing is n times normal value.·This command does not affect the setting of Kanji characters.·This command sets values independently in each mode (standard and page modes).·The horizontal and vertical motion units are specified by GS P.Changing the horizontal or vertical motion units does not affect the current right-side spacing.·The GS P command can change the horizontal (and vertical) motion unit. However, the valuecannot be less than the minimum horizontal movement amount, and it must be in even unitsof the minimum horizontal movement amount.·In standard mode, the horizontal motion unit is used.·In page mode, the horizontal or vertical motion unit differs in page mode, depending on startingposition of the printable area as follows:①When the starting position is set to the upper left or lower right of the printable area usingESC T, the horizontal motion unit (x) is used.②When the starting position is set to the upper right or lower left of the printable area usingESC T, the vertical motion unit (y) is used.·The maximum right-side spacing is 35.983 mm {255/180”}. Any setting exceeding the maximumis converted to the maximum automatically.[Default] n = 0[Reference] GS PESC ! n[Name] Select print mode(s)[Format] ASCII ESC ! nHex 1B 21 nDecimal 27 33 n[Range]0≤n ≤255[Description] Selects print mode(s) using n as follows:[Details] ·When both double-height and double-width modes are selected,quadruple size characters are printed.·The printer can underline all characters, but can not underline the space set by HT or 90clockwise rotated characters.·The thickness of the underline is selected by ESC-, regardless of the character size.·When some characters in a line are double or more height, all the characters on the line arealigned at the baseline.·ESC E Can also turn on or off emphasized mode. However, the setting of the last receivedcommand is effective.·ESC -Can also turn on or off underline mode. However, the setting of the last receivedcommand effective.·GS ! Can also select character size, However, the setting of the last received command iseffective.·Emphasized mode is effective for alphanumeric and Kanji. All print modes except emphasizedmode is effective only for alphanumeric.[Default] n = 0[Reference] ESC E, ESC -, GS !ESC $ n L n H[Name] Set absolute print position[Format] ASCII ESC $ n L n HHex 1B 24 n L n HDecimal 27 36 n L n H[Range] 0≤nL≤2550≤nH≤255[Description] Sets the distance from the beginning of the line to the position at which subsequent characters are to be printed.[Details] ·The distance from the beginning of the line to the print position is[(n L + n H x 256) x (vertical or horizontal motion unit)] inches.·Settings outside the specified printable area are ignored.·The horizontal and vertical motion units are specified by “GS P”.·The GS P command can change the horizontal (and vertical) motion unit.However, the value cannot be less than the minimum horizontal movement amount, and itmust be in even units of the minimum horizontal movement amount. In standard mode, thehorizontal motion unit is used.·In page mode, the horizontal or vertical motion unit differs depending on the starting position ofthe printable area as follows :1. When the starting position is set to the upper left or lower right of the printable areausing ESC T, the horizontal motion unit (x) is used.2. When the starting position is set to the upper right or lower left of the printable areausing ESC T, the vertical motion unit (y) is used.[Reference] ESC \, GS $, GS \, GS PESC % n[Name] Select/cancel user-defined character set[Format] ASCII ESC % nHex 1B 25 nDecimal 27 37 n[Range] 0≤n ≤255[Description] Selects or cancels the user-defined character set·When the LSB of n is 0, the user-defined character set is canceled.·When the LSB of n is 1, the user-defined character set is selected.[Details] ·When the user-defined character set is canceled, the internal characterset is automatically selected.·n is available only for the least significant bit.[Default] n = 0[Reference] ESC &, ESC ?ESC & y c1 c2 [x1 d1…d(y x x1)]..[ xk d1..d(y x xk)][Name] Define user-defined characters[Format] ASCII ESC & y c1 c2 [x1 d1...d(y ⨯ x1)]...[xk d1...d(y ⨯ xk)]Hex 1B 26 y c1 c2 [x1 d1...d(y ⨯ x1)]...[xk d1...d(y ⨯ xk)]Decimal 27 38 y c1 c2 [x1 d1...d(y ⨯ x1)]...[xk d1...d(y ⨯ xk)][Range] y = 332 ≤c1≤c2≤ 1260 ≤ x ≤ 12 Font A (when font A (12 x 24) is selected)0 ≤ x ≤ 9 Font B (when font B (9 x 17) is selected)0 ≤ d1 ... d(y x x k) ≤ 255[Description] Defines user-defined characters·y specifies the number of bytes in the vertical direction.·c1 specifies the beginning character code for the definition, and c2 specifies the final code.·X specifies the number of dots in the horizontal direction.[Details] ·The allowable character code range is from ASCII code <20>H to<7E>(95characters).·It is possible to define multiple characters for consecutive character codes.·If only one character is desired, use c1 = c2.·d is the dot data for the characters. The dot pattern is in the horizontal direction from the leftside. Any remaining dots on the right side are blank.·The data to define a user-defined character is (y ⨯ x) bytes.·Set a corresponding bit to 1 to print a dot or 0 to not print a dot.·This command can define different user-defined character patterns by each fonts. To select afont, use ESC !·A user-defined character and a downloaded bit image cannot be defined simultaneously. Whenthis command is executed, the downloaded bit image is cleared.·The user-defined character definition is cleared when:①ESC @ is executed.②ESC ? is executed.③FS q is executed.④GS * is executed.⑤ The printer is reset or the power is turned off.·When the user-defined characters are defined in font B (9 x 24), only the most significant bit ofthe 3rd byte of data in vertical direction is effective.ESC *m n L n H [d1...dk][Name] Select bit-image mode[Format] ASCII ESC *m n L n H d1...dkHex 1B 2A m n L n H d1...dkDecimal 27 42 m n L n H d1...dk[Range] m = 0, 1, 32, 330≤n L ≤2550≤n H ≤30≤d ≤255[Details] ·If the values of m is out of the specified range, nL and data following areprocessed as normal data.·The nL and nH indicate the number of dots of the bit image in the horizontal direction. Thenumber of dots is calculated by nL + nH x 256.·If the bit-image data input exceeds the number of dots to be printed on a line, the excess data isignored.·d indicates the bit-image data. Set a corresponding bit of 1 to print a dot or to 0 to not print a dot.·If the width of the printing area set by GS L and GS W less than the width required by the datasent with the ESC *command the following will be performed on the line in question (but theprinting cannot exceed the maximum printable area) :① The width of the printing area is extended to the right to accommodate the amount ofdata.② If step ① does not provide sufficient width for the data, the left margin is reduced toaccommodate the data.·After printing a bit image, the printer returns to normal data processing mode.·This command is not affected by print modes(emphasized, double-strike, underline, charactersize or white/black reverse printing), except upside-down printing mode.·Refer to Figure 3.12.3 for the bit image development position in page mode.·The relationship between the image data and the dots to be printed is as follows:·When 8-dot bit image is selected:ESC - n[Name] Turn underline mode on/off[Format] ASCII ESC - nHex 1B 2D nDecimal 27 45 n[Range] 0≤n ≤2, 48≤n ≤50[Details] ·The printer can underline all characters (including right-side characterspacing), but cannot underline the space set by HT.·The printer cannot underline 90˚ clockwise rotated characters and white/black invertedcharacters.·When underline mode id turned off by setting the value of n to 0 or 48, the following data is notunderlined, and the underline thickness set before the mode is turned off does not change.The default underline thickness is 1 dot.·Changing the character size does not affect the current underline thickness.·Underline mode can also be turned on or off by using ESC!. Note, however, that the lastreceived command is effective.·This command does not affect Kanji printing.[Default] n = 0[Reference] ESC !ESC 2[Name] Select default line spacing[Format] ASCII ESC 2Hex 1B 32Decimal 27 50[Description] Selects approximately 4.23 mm {1/6”} spacing.[Details] ·The line spacing can be set independently in standard mode and inpage mode.[Reference] ESC 3ESC 3 n[Name] Set line spacing[Format] ASCII ESC 3 nHex 1B 33nDecimal 27 51 n[Range] 0≤n ≤255[Description] Sets the line spacing to [n x (vertical or horizontal motion unit)] inches.[Details] ·The line spacing can be set independently in standard mode and inpage mode.·The horizontal and vertical motion unit is specified by GS P.Changing the horizontal or vertical motion unit does not affect the current line spacing.·The GS P command can change the horizontal (and vertical) motion unit.However, the value cannot be less than the minimum vertical movement amount, and it mustbe in even units of the minimum vertical movement amount.·In standard mode, the vertical motions until (y) is used.·In page mode, this command function as follows, depending on thestarting position of the printable area :①When the starting position is set to the upper left or lower right to the printable areausing ESC T, the vertical motion unit (y) is used.②When the starting position is set to the upper right or lower left ofthe printable area using ESC T, the horizontal motion unit (x) is used.·The maximum paper feed amount is 1016 mm {40”}. Even if a paper feed amount of more than1016 mm{40”}is set, the printer feeds the paper only 1016 mm{40”}[Default] Line space is equivalent to approximately 4.23 mm{1/6”}.[Reference] ESC 2, GS PESC = n[Name] Set peripheral device[Format] ASCII ESC = nHex 1B 3D nDecimal 27 61 n[Range] 1≤n ≤255[Description] Selects device to which host computer sends data, using n as follows:[Details] ·When the printer is disabled, it ignores all data except for error-recoverycommands (DLE EOT, DLE ENQ, DLE DC4) until it is enabled by this command. [Default] n=1ESC ? n[Name] Cancel user-defined characters[Format] ASCII ESC ? nHex 1B 3F nDecimal 27 63 n[Range] 32 ≤n ≤126[Description] Cancels user-defined characters.[Details] ·This command cancels the pattern defined for the character codespecified by n. After the user-defined characters is canceled, the corresponding pattern for theinternal character is printed.·This command deletes the pattern defined for the specified code in thefont selected by ESC !.·If a user-defined character has not been defined for the specifiedcharacter code, the printer ignores this command.[Reference] ESC &, ESC %ESC @[Name] Initialize printer[Format] ASCII ESC @Hex 1B 40Decimal 27 64[Description] Clears the data in the print buffer and resets the printer mode to the modethat was in effect when the power was turned on.[Details] ·The DIP switch settings are not checked again.·The data in the receive buffer is not cleared.·The macro definition is not cleared.·The NV bit image data is not cleared.·The data of the NV user memory is not cleared.ESC D [n1...nk] NUL[Name] Set horizontal tab positions[Format] ASCII ESC D n1……nk NULHex 1B 44 n1……nk 00Decimal 27 68 n1……nk 0[Range] 1≤n ≤2550≤k ≤32[Description] Set is horizontal tab positions.·n specifies the column number for setting a horizontal tab position from the beginning of the line.·k indicates the total number of horizontal tab positions to be set.[Details] ·The horizontal tab position is stored as a value of [character width x n]measured from the beginning of the line. The character width includes the right-side characterspacing, and double-width characters are set with twice the width of normal characters.·This command cancels the previous horizontal tab settings.·When setting n = 8, the print position is moved to column 9 by sending HT.·Up to 32 tab positions (k=32) can be set. Data exceeding 32-tab positions s is processed asnormal data.·Transmit [n]k in ascending order and place a NUL code 0 at the end.·When [n]k is less than or equal to the preceding value [n]k-1, tab setting is finished and thefollowing data is processed as normal data,·ESC D NUL cancels all horizontal tab positions.·The previously specified horizontal tab positions do not change, even if the character widthchanges.·The character width is memorized for each standard and page mode.[Default] The default tab positions are at intervals of 8 characters (columns 9, 17, 25, ...) for the font A (12 X 24).[Reference] HTESC E n[Name] Turn emphasized mode on/off[Format] ASCII ESC E nHex 1B 45 nDecimal 27 69 n[Range] 0≤n ≤255[Description] Turns emphasized mode on or off.·When the LSB of n is 0, emphasized mode is turned off.·When the LSB of n is 1, emphasized mode is turned on.[Details] ·Only the least significant bit of n is enabled.·This command and ESC! Turn on and off emphasized mode in the same way. Be careful whenthis command is used with ESC!.[Default] n = 0[Reference] ESC !ESC G n[Name] Turn on/off double-strike mode[Format] ASCII ESC G nHex 1B 47 nDecimal 27 71 n[Range] 0≤n ≤255[Description] Turns double-strike mode on or off.·When the LSB of n is 0, double-strike mode is turned off.·When the LSB of n is1, double-strike mode is turned on.[Details] ·Only the lowest bit of n is enabled.·Printer output is the same in double-strike mode and in emphasized mode.[Default] n = 0[Reference] ESC EESC J n[Name] Print and feed paper[Format] ASCII ESC J nHex 1B 4A nDecimal 27 74 n[Range] 0≤n ≤255[Description] Prints the data in the print buffer and feeds the paper [n x vertical or horizontal motion unit]. [Details] ·After printing is completed, this command sets the print starting positionto the beginning of the line.·The paper feed amount set by this command does not affect the values set by ESC 2 or ESC 3.·The horizontal and vertical motion unit is specified by GS P.·The GS P command can change the vertical (and horizontal) motion unit.However, the value cannot be less than the minimum vertical movement, and it must be ineven units of the minimum vertical movement amount.·In standard mode, the printer uses the vertical motion unit(y).·In page mode, this command functions as follows, depending on the starting position of theprintable area.①When the starting position is set to the upper left or lower right of the printable areausing ESC T, the vertical motion unit (y) is used.②When the starting position is set to the upper right or lower left of the printable areausing ESC T, the horizontal motion unit (x) is used.·The maximum line spacing is 1016 mm{40”}. When the setting value exceeds the maximum, it isconverted to the maximum automatically.[Reference] GS PESC L[Name] Select page mode[Format] ASCII ESC LHex 1B 4CDecimal 27 76[Description] Switches from standard mode to page mode.[Details] ·This command is enabled only when input at the beginning of a line instandard mode.·This command has no effect in page mode.·After printing by FF is completed or by using ESC S, the printer returns to standard mode.·This command sets the position where data is buffered to the position specified by ESC T withinthe printing area defined by ESC W.·This command is switches the setting for the following commands (in which the values can beset independently in standard mode and page mode) to those for page mode.① Set right-side character spacing : ESC SP, FS S②Select default line spacing : ESC 2, ESC3·Only valve settings is possible for the following commands in pagemode; these commands are not executed.① Turn 90 clockwise rotation mode on/off: ESC V② Select justification: ESC a③ Turn upside-down printing mode on/off: ESC {④ Set left margin: GS L⑤ Set printable area width: GS W·The following command is ignored in page mode:①Execute test print: GS W·The following command is not available in page mode:①Print NV bit image : FS p②Define NV bit image : FS q③Print raster bit image : GS v 0·The printer returns to standard mode when power is turned on, the printer is reset, or ESC @ isused.[Reference] FF, CAN, ESC FF, ESC S, ESC T, ESC W, GS $, GS \ESC M n[Name] Select character font[Format] ASCII ESC M nHex 1B 4D nDecimal 27 77 n[Range] n= 0, 1 , 48, 49[Description] Selects character fonts[Details] ·The ESC ! command can also select the character fonts. However, thesetting of the last received command is effective. [Reference] ESC !ESC R n[Name] Select an international character set[Format] ASCII ESC R nHex 1B 52 nDecimal 27 82 n[Range] 0≤n ≤13[Description][Default] n = 0[Reference] 3.2.12 International Character SetESC S[Name] Select standard mode[Format] ASCII ESC SHex 1B 53Decimal 27 83[Description] Switches from page mode to standard mode.[Details] ·This command is effective only in page mode.·Data buffered in page mode and the printable area developed in page mode are cleared.·This command is switches the setting for the following command (in which the values can be setindependently in standard mode and page mode) to those for standard mode:① Set right-side character spacing: ESC SP, FS S② Select default line spacing : ESC 2, ESC 3·The following commands are enabled only to set in standard mode.① Set printing area in page mode : ESC W② Set print direction in page mode : ESC T·The following commands are ignored in standard mode.①Set absolute vertical print position in page mode : GS $②Set relative vertical print position in page mode : GS \·Standard mode is selected automatically when power is turned on, the。

打印机ESCPOS 控制指令

打印机ESCPOS 控制指令

HDD-80260打印机ESC/POS控制指令ESC/POS控制指令
HT横向跳格
FF打印并走纸
CAN页模式下删除打印缓冲区内容
DLEDC4nmt实时产生钱箱开启脉冲
ESCFF页模式下打印
ESCSPn设置字符右间距
ESC!n选择打印模式
ESC$nLnH设置绝对打印位置
ESC&yc1c2[x1d1...d(y×x1)]...[xkd1...d(y×xk)]定义用户自定义字符
ESC*mnLnHd1...dk选择位图模式
ESC?n取消用户自定义字符
ESCGn选择/取消双重打印模式
ESCL选择页模式
ESCMn选择字体
ESCRn选择国际字符集
ESCS选择标准模式
ESCTn在页模式下选择打印区域方向
ESCVn选择字符旋转打印
ESCc5n允许/禁止按键
ESCpmt1t2产生钱箱控制脉冲
FSpnm打印下载到FLASH中的位图
FSqn[xLxHyLyHd1...dk]1...[xLxHyLyHd1...dk]n定义Flash位图
GS!n选择字符大小
GS#n定义当前下载图号
GS*xyd1...d(x×y×8)定义下载位图
GS/m打印下载位图
GS:开始/结束宏定义
GSHn选择HRI字符的打印位置
GSWnLnH设置打印区域宽度
GS\nLnH页模式下设置纵向相对位置
GShn选择条码高度
①GSkmd1...dkNUL②GSkmnd1...dn打印条码
FS!n设置汉字字符模式
FS&选择汉字模式
FS.取消汉字模式
FSSn1n2设置汉字字符左右间距。

ESCPOS打印控制命令

ESCPOS打印控制命令

ESC/POS打印控制命令1、打印命令一览表命令速查命令说明打印命令LF 打印并换行ESC J 打印并走纸n点行格式设置命令ESC 2 设置行间距1/6英寸ESC 3 n 设置行间距为n点行(n/137英寸) ESC a n 设置对齐方式字符设置命令ESC ! n 设置打印字符格式ESC SO 设置字符倍宽打印ESC DC4 取消字符倍宽打印ESC { n 设置/取消字符上下倒置ESC B n 设置/取消字符反白打印ESC % n 设置/取消用户自定义字符ESC & 设定用户自定义字符ESC ? 取消用户自定义字符ESC R n 选择国际字符集ESC t n 选择字符代码页图形设置命令ESC * 打印一点行图形GS * 下装点图设置GS / 打印下装点图钱箱控制命令ESC p 钱箱控制按键控制命令ESC c 5 允许/禁止按键开关初始化命令ESC @ 打印机初始化状态传输命令ESC v 向主机传送打印机状态ESC u 向主机传送周边设置状态GS a 允许/禁止状态自动上传条码设置命令GS H 选择HRI打印方式GS h 设置条码高度GS w 设置条码横向宽度GS k 打印条码位置和页模式命令ESC L 设置页模式FF 打印,并返回到标准模式ESC FF 打印页模式的数据CAN 清除页缓冲区的数据ESC W … 设置页位置和大小ESC $ nL nH 设置页模式的绝对X位置GS $ nL nH 设置页模式的绝对Y位置ESC \ nL nH 设置页模式的相对X位置GS \ nL nH 设置页模式的相对Y位置GS L nL nH 设置页左边距GS W nL nH 设置页宽切纸模式命令ESC i 切纸但不走纸2、打印命令详解概述POS-58打印机使用ESC/POS打印命令集。

打印命令按以下格式说明:命令功能格式:ASCII:以标准ASCII字符序列表示十进制:以十进制数字序列表示十六进制:以十六进制数字序列表示说明:命令功能和使用说明。

打印机ESCPOS控制指令

打印机ESCPOS控制指令

打印机ESCPOS控制指令
HDD-80260打印机ESC/POS 控制指令
ESC/POS 控制指令
HT 横向跳格
LF 打印并走纸一行
FF 打印并走纸
CR 打印并回车
CAN 页模式下删除打印缓冲区内容
DLE DC4 n m t 实时产生钱箱开启脉冲
ESC FF 页模式下打印
ESC SP n 设置字符右间距
ESC ! n 选择打印模式
ESC $ nL nH 设置绝对打印位置
ESC % n 选择/取消用户自定义字符
ESC & y c1 c2 [x1 d1...d(y ×x1)]...[xk d1...d(y ×xk)] 定义用户自定义字符
●当选择压缩ASCII 字体时(9×17)
ESC * m nL nH d1...dk 选择位图模式
选择24 点密度时:
ESC – n 选择/取消下划线模式
ESC 2 设置默认行高
ESC 3 n 设置行高
ESC = n 选择打印机
ESC ? n 取消用户自定义字符
ESC @ 初始化打印机
ESC D n1...nk NUL 设置横向跳格位置
ESC E n 选择/取消加粗模式
ESC G n 选择/取消双重打印模式ESC J n 打印并走纸
ESC L 选择页模式
ESC M n 选择字体
ESC R n选择国际字符集。

ESC打印机的控制命令

ESC打印机的控制命令

ESC打印机的控制命令ESC打印机的控制命令计算机能够完成数值计算、数据处理、图象设计、过程控制等工作都是在一种或多种计算机语言的指挥下进行的。

同样,打印机各种部件的运行和动作,各种打印动作的实现、在各种介质上打印出图形和字符,也是由一套能为打印机所识别的语言(命令)来进行控制。

通常将能完成上述功能的命令的集合成为打印机控制命令(Print Control Command)为了更好的熟知打印机控制命令,我们需先行了解打印机的数据处理流程。

二、打印机的控制命令目前主流的打印机控制命令主要有以下三种:EPSON公司的ESC PK命令集HP公司的PCL命令集Adobe公司的PostScript(简称PS)命令集ESC PK命令集是针式打印机(尤其是滚筒方式打印)领域事实上的工业标准;PCL命令集是中、低档激光打印机、喷墨机印机领域的工业标准;PS命令集则是高档排版、出版领域的应用标准。

1、名词解释1)控制命令:对打印机进行各种处理和控制动作,并为打印机所识别的命令序列。

2)仿真;控制打印机按照某种控制命令进行工作,就称为这个控制命令的仿真。

3)驱动程序;转换文件成为某种特定控制命令描述的文档,并进行输出的程序。

注:驱动程序就是打印控制命令的形体表现,其本质就是按照某一控制命令的格式,将计算机的数据进行翻译和组织,并形成相应的字符和位图的序列送往打印机。

2、命令分类打印机控制命令的种类可分为二大类:基本控制命令和扩充命令。

1)基本命令(Basic Control Code)基本命令其实就是ASCII码中规定的具有打印功能的控制命令,它的特点是均用一个ASCII码(一个字节)代表,使用比较方便。

它的数值范围如下:ASCII: 00H ~ 20H2)扩充命令(Extension Control Command)基本命令仅提供标准和基本的控制功能。

要实现字型大小、各种修饰功能、行与列间距控制、格式设定等较复杂的控制功能,就必须使用扩充命令。

打印机控制指令

打印机控制指令

HDD-80260打印机ESC/POS 控制指令ESC/POS 控制指令HT 横向跳格LF 打印并走纸一行FF 打印并走纸CR 打印并回车CAN 页模式下删除打印缓冲区内容DLE DC4 n m t 实时产生钱箱开启脉冲ESC FF 页模式下打印ESC SP n 设置字符右间距ESC ! n 选择打印模式ESC $ nL nH 设置绝对打印位置ESC % n 选择/取消用户自定义字符ESC & y c1 c2 [x1 d1...d(y ×x1)]...[xk d1...d(y ×xk)] 定义用户自定义字符ESC * m nL nH d1...dk 选择位图模式ESC – n 选择/取消下划线模式ESC 2 设置默认行高ESC 3 n 设置行高ESC = n 选择打印机ESC n 取消用户自定义字符ESC @ 初始化打印机ESC D n1...nk NUL 设置横向跳格位置ESC E n 选择/取消加粗模式ESC G n 选择/取消双重打印模式ESC J n 打印并走纸ESC L 选择页模式ESC M n 选择字体ESC R n选择国际字符集ESC S 选择标准模式ESC T n 在页模式下选择打印区域方向ESC V n 选择字符旋转打印ESC W xL xH yL yH dxL dxH dyL dyH 页模式下设置打印区域ESC \ nL nH 设置相对横向打印位置ESC a n 选择字符对齐模式ESC c 5 n 允许/禁止按键ESC d n 打印并向前走纸n 行ESC p m t1 t2 产生钱箱控制脉冲ESC t n 选择国际扩展字符码表ESC { n 选择/取消倒置打印模式FS p n m 打印下载到FLASH 中的位图FS q n [xL xH yL yH d1...dk]1...[xL xH yL yH d1...dk]n 定义Flash 位图GS ! n 选择字符大小GS # n 定义当前下载图号GS $ nL nH 页模式下设置纵向绝对位置GS * x y d1...d(x×y×8) 定义下载位图GS / m 打印下载位图GS : 开始/结束宏定义GS B n 选择/取消黑白反显打印模式GS H n 选择HRI 字符的打印位置GS L nL nH 设置左边距GS P x y 设置横向和纵向移动单位①GS V m②GS V m n 选择切纸模式并切纸GS W nL nH 设置打印区域宽度GS \ nL nH 页模式下设置纵向相对位置GS ^ r t m 执行宏命令GS f n 选择HRI 使用字体GS h n 选择条码高度①GS k m d1...dk NUL②GS k m n d1...dn 打印条码GS v 0 m xL xH yL yH d1....dk 打印光栅位图GS w n 设置条码宽度FS ! n 设置汉字字符模式FS & 选择汉字模式FS - n 选择/取消汉字下划线模式FS . 取消汉字模式FS 2 c1 c2 d1...dk 定义用户自定义汉字FS S n1 n2 设置汉字字符左右间距FS W n 选择/取消汉字倍高倍宽。

使用ESC指令控制POS机小票

使用ESC指令控制POS机小票

使用ESC指令控制POS机小票C#使用ESC指令控制POS打印机打印小票1.前言C#打印小票可以与普通打印机一样,调用PrintDocument实现。

也可以发送标注你的ESC 指令实现。

由于调用PrintDocument类时,无法操作使用串口或TCP/IP接口连接的pos 打印机,并且无法发送控制指令实现pos打印机的切纸、走纸等动作。

因此个人建议使用ESC 指令进行打印会更通用。

本类需要调用ImageProcessor.cs2.POS机打印小票ReceiptHelperusing System;usingusing System.Text;usingusing System.Threading;using System.Drawing;using System.Management;using System.IO;using LaisonTech.MediaLib;using /doc/6213217853.html,monBLL;usingnamespace LaisonTech.MediaLib{#region结构体定义[StructLayout(LayoutKind.Sequential)]public struct OVERLAPPED{int Internal;int InternalHigh;int Offset;int OffSetHigh;int hEvent;};[StructLayout(LayoutKind.Sequential)]public struct PRINTER_DEFAULTS{public int pDatatype;public int pDevMode;public int DesiredAccess;}//////对齐方式///public enum eT extAlignMode{Left = 0,Middle = 1,Right = 2}#endregion//////小票打印类/// 使用方法:/// 1 GetPrinterList获取已经安装的所有打印机列表./// Open 打开指定打印机/// 2 控制打印机动作、执行打印内容之前,必须先调用StartPrint,准备向打印机发送控制指令/// 3 调用SetLeft, SetBold, SetAlignMode, SetFontSize ... ...设置打印参数/// 4 PrintText 打印内容.注意:打印该行内容后会自动换行(本类会在该行内容末尾添加一个换行符)/// PrintImageFile 或PrintBitMap打印图片/// 5 控制指令和打印内容都发送完毕后,调用EndPrint执行真正打印动作/// 6 退出程序前调用Close///public class ReceiptHelper{#region指令定义private static Byte[] Const_Init = new byte[] { 0x1B, 0x40,0x20, 0x20, 0x20, 0x0A,0x1B, 0x64,0x10};//设置左边距private const string Const_SetLeft = "1D 4C ";//设置粗体private const string Const_SetBold = "1B 45 ";private const String Const_Bold_YES = "01";private const String Const_Bold_NO = "00";//设置对齐方式private const string Const_SetAlign = "1B 61 ";private const String Const_Align_Left = "30";private const String Const_Align_Middle = "31";private const String Const_Align_Right = "32";//设置字体大小,与SetBigFont 不能同时使用private const string Const_SetFontSize = "1D 21 ";//设置是否大字体,等同于SetFontSize = 2//private const String Const_SetBigFontBold = "1B 21 38";//private const String Const_SetBigFontNotBold = "1B 21 30";//private const String Const_SetCancelBigFont = "1B 21 00";//////打印并走纸///private static Byte[] Const_Cmd_Print = new byte[] { 0x1B, 0x4A, 0x00 };//走纸private const string Const_FeedForward = "1B 4A ";private const string Const_FeedBack = "1B 6A ";//切纸private static Byte[] Const_SetCut = new byte[] { 0x1D, 0x56, 0x30};//查询打印机状态private static Byte[] Const_QueryID = new byte[] { 0x1D, 0x67, 0x61};//回复帧以ID 开头private static String Const_ResponseQueryID = "ID";//////设置图标的指令///private static Byte[] Const_SetImageCommand = new Byte[] { 0x1B, 0x2A, 0x21 }; #endregion#region常量定义//////最大字体大小///public const Int32 Const_MaxFontSize = 8;//////最大走纸距离///public const Int32 Const_MaxFeedLength = 5000;//////最大高宽///public const Int32 Const_MaxImageLength = 480;//////每次通信最多打印的行数///public const Int32 Const_OncePrintRowCount = 24;public const Int32 Const_BrightnessGate = 100;//////无效句柄///public const Int32 Const_InvalidHandle = -1;#endregion#region私有成员//////打印机句柄///private int m_Handle = -1;//////是否已经初始化///private Boolean m_Inited = false;#endregion#region私有函数[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out Int32 hPrinter, IntPtr pd);[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool StartDocPrinter(Int32 hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool EndDocPrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool StartPagePrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool EndPagePrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool WritePrinter(Int32 hPrinter, Byte[] pBytes, Int32 dwCount, out Int32 dwWritten);[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool ClosePrinter(Int32 hPrinter);//////发送指令/////////private Boolean SendCommand(Byte[] cmd){if (m_Handle == Const_InvalidHandle || cmd == null || cmd.Length < 2){return false;}int writelen = 0;Boolean bl = WritePrinter(m_Handle, cmd, cmd.Length, out writelen);if (!bl) return false;return (writelen >= cmd.Length);}//////发送文本格式的指令/////////private Boolean SendCommand(String hexstrcmd){if (m_Handle == Const_InvalidHandle || hexstrcmd == null || hexstrcmd.Length < 4){return false;}byte[] mybyte = null;Boolean bl = DataFormatProcessor.HexStringT oBytes(hexstrcmd, out mybyte);bl = SendCommand(mybyte);return bl;}#endregion#region内部处理- 打印图片//////把图片转换为指令字节,图片最大高宽不能超过480////////////public static Boolean LoadImage(Bitmap image,ref Byte[] bitarray,ref Int32 datawidth,ref Int32 dataheight) {Int32 newwidth = 0;Int32 newheight = 0;Bitmap destimage = image;Boolean bl = false;//如果高度超过范围,或宽度超过范围,需要进行缩小if (image.Width > Const_MaxImageLength || image.Height > Const_MaxImageLength){//按照高度和宽度,较大的那一边,进行缩放if (image.Width > image.Height){newwidth = Const_MaxImageLength;newheight = (Int32)(image.Height * newwidth / (float)image.Width);}else{newheight = Const_MaxImageLength;newwidth = (Int32)(newheight * image.Width / (float)image.Height);}bl = ImageProcessor.ResizeImage(image, newwidth, newheight, ref destimage);}//把数据转换为字节数组bl = GetBitArray(image, ref bitarray, ref datawidth, ref dataheight);return bl;}//////把图片转换为指令字节,图片最大高宽不能超过480///如果图片的高度不是24的整数倍,则修改为24的整数倍////////////public static Boolean LoadImageFromFile(String imagefilename, ref Byte[] bmpbytes, ref Int32 width, ref Int32 height){Bitmap img = ImageProcessor.LoadBitImage(imagefilename);if (img == null){return false;}Boolean bl = LoadImage(img, ref bmpbytes, ref width, ref height);return bl;}//////把图片转换为位图数组,每个字节的每个比特位,对应当前像素是否需要打印////////////public static Boolean GetBitArray(Bitmap img,ref Byte[] allbitary, ref Int32 width, ref Int32 height){if (img == null){return false;}//ESC指令格式规定://1 打印图片时,每条指令最多只打印24行;不足24行的,也要用全0填充满数据字节//2 打印24行数据时,按照光栅模式纵向获取数据// 即先获取所有x=0的点(第0列)转换为3个字节;// 再获取所有x=1的点转换为3个字节;...直到获取到最右侧一列的点//3 打印完当前24行数据后,再获取后续24行的数据内容,直到所有的数据获取完毕//获取亮度数组Boolean[] briary = null;Boolean bl = ImageProcessor.ToBooleanArray(img, Const_BrightnessGate, ref briary);if (!bl){return false;}height = img.Height;//如果图像高度不是24整数倍,设置为24的整数倍if (height % Const_OncePrintRowCount != 0){height = height + Const_OncePrintRowCount - height %Const_OncePrintRowCount;}width = img.Width;//如果图像宽度不是8的整数倍,设置为8的整数倍if (width % 8 != 0){width = width + 8 - width % 8;}Int32 bytelen = height * width / 8;//每个像素对应1个比特位,因此总字节数=像素位数/8allbitary = new Byte[bytelen];Int32 byteidxInCol = 0;//当前列里首个像素,在目标字节数组里的下标Int32 byteidx = 0;//当前像素在目标数组里的字节下标Int32 bitidx = 0;//当前像素在目标数组里当前字节里的比特位下标Int32 pixidxInCol = 0;//当前像素在当前列里的第几个位置Int32 pixidx = 0;//当前像素在原始图片里的下标Int32 rowidx = 0; //当前处理的像素点所在行,不能超过图像高度Int32 curprocrows = 0;//当前需要处理的行数量while (rowidx < height){//按照纵向次序,把当前列的24个数据,转换为3个字节for (Int32 colidx = 0; colidx < img.Width; ++colidx){//如果当前还剩余超过24行没处理,处理24行if (rowidx + Const_OncePrintRowCount <= img.Height){curprocrows = Const_OncePrintRowCount;}else{//已经不足24行,只处理剩余行数curprocrows = img.Height - rowidx;}pixidxInCol = 0; //本列里从像素0开始处理for (Int32 y = rowidx; y < rowidx + curprocrows; ++y){//原始图片里像素位置pixidx = y * img.Width + colidx;//获取当前像素的亮度值.如果当前像素是黑点,需要把数组里的对应比特位设置为1if (briary[pixidx]){bitidx = 7 - pixidxInCol % 8;//最高比特位对应首个像素.最低比特位对应末个像素byteidx = byteidxInCol + pixidxInCol / 8; //由于最后一段可能不足24行,因此不能使用byteidx++DataFormatProcessor.SetBitValue(bitidx, true, refallbitary[byteidx]);}pixidxInCol++;}byteidxInCol += 3;//每列固定24个像素,3个字节}rowidx += Const_OncePrintRowCount;}return true;}#endregion#region公开函数private static ReceiptHelper m_instance = new ReceiptHelper();//////当前使用的打印机名称///public String PrinterName{get;private set;}//////单件模式//////public static ReceiptHelper GetInstance(){return m_instance;}//////获取本机安装的所有打印机//////public static List GetPrinterList(){List ret = new List();if (PrinterSettings.InstalledPrinters.Count < 1){return ret;}foreach (String printername in PrinterSettings.InstalledPrinters){ret.Add(printername);}return ret;}//////打开打印机/////////public Boolean Open(String printername){if (m_Inited){return true;}Boolean bl = OpenPrinter(printername.Normalize(), out m_Handle, IntPtr.Zero);m_Inited = (bl && m_Handle != 0);return true;}///开始打印,在打印之前必须调用此函数//////public Boolean StartPrint(){if (!m_Inited){return false;}DOCINFOA di = new DOCINFOA();di.pDocName = "My C#.NET RAW Document"; di.pDataType = "RAW";//Start a document.Boolean bl = StartDocPrinter(m_Handle, 1, di); if (!bl){return false;}// Start a page.bl = StartPagePrinter(m_Handle);return bl;}//////结束打印,在打印结束之后必须调用此函数//////public Boolean EndPrint(){if (!m_Inited)return false;}Boolean bl = EndPagePrinter(m_Handle);bl = EndDocPrinter(m_Handle);return bl;}//////销毁//////public Boolean Close(){if (!m_Inited){return true;}m_Inited = false;//关闭设备句柄ClosePrinter(m_Handle);m_Handle = -1;return true;}//////打印文本.在调用本函数之前必须先调用正确的设置字体、左边距/////////public Boolean PrintText(String content){if (!m_Inited){return false;}byte[] bytes = null;if (content.Length < 1){content = " ";}if (content[content.Length - 1] != (char)0x0D && content[content.Length - 1] != (char)0x0A){content = content + (char)0x0A;}bytes = DataFormatProcessor.StringT oBytes(content); bool bl = SendCommand(bytes);return bl;}//////设置对齐方式/////////public bool SetAlignMode(eTextAlignMode alignmode) {if (!m_Inited){return false;}String code = String.Empty;switch (alignmode){case eTextAlignMode.Left:code = Const_Align_Left; break;case eTextAlignMode.Middle: code = Const_Align_Middle; break;case eTextAlignMode.Right:code = Const_Align_Right; break;default:code = Const_Align_Left; break;}//注意:先低字节后高字节string str = Const_SetAlign + code; bool bl = SendCommand(str); return bl;}//////设置左边距/////////public bool SetLeft(int left){if (!m_Inited){return false;}//注意:先低字节后高字节String hexstr = left.ToString("X4");string str = Const_SetLeft + hexstr.Substring(2, 2) + hexstr.Substring(0, 2);bool bl = SendCommand(str);return bl;}//////设置粗体/////////public Boolean SetBold(Boolean bold){if (!m_Inited){return false;}//注意:先低字节后高字节String str = String.Empty;if (bold){str = Const_SetBold + Const_Bold_YES;}else{str = Const_SetBold + Const_Bold_NO;}bool bl = SendCommand(str);return bl;}//////切纸//////public bool Cut(){if (!m_Inited){return false;}bool bl = SendCommand(Const_SetCut);return bl;}//////打印图片/////////public bool PrintImageFile(String imgfilename){if (!m_Inited){return false;}Bitmap img = ImageProcessor.LoadBitImage(imgfilename); if (img == null){return false;}Boolean bl = PrintBitmap(img);return bl;}//////打印图片/////////public bool PrintBitmap(Bitmap bitmap){if (!m_Inited){return false;}if (bitmap == null ||bitmap.Width > Const_MaxImageLength ||bitmap.Height > Const_MaxImageLength){return false;}Byte[] bitary = null;Int32 width = 0;Int32 height = 0;Boolean bl = GetBitArray(bitmap, ref bitary, ref width, ref height);bl = PrintBitmapBytes(bitary, bitmap.Width, bitmap.Height);return bl;}//////打印图片/////////public bool PrintBitmapBytes(Byte[] imgbitarray, Int32 width, Int32 height) {if (!m_Inited){return false;}Int32 bytes = width * height / 8;//检查是否尺寸符合要求if (width > Const_MaxImageLength || height > Const_MaxFeedLength || width < 1 || height < 1 ||imgbitarray == null){return false;}//每次获取24行的数据进行发送,这24行的字节数Int32 blockbytes = width * Const_OncePrintRowCount / 8;if (blockbytes < 1){return false;}Boolean bl = false;//一共需要发送的块数量Int32 blocks = imgbitarray.Length / blockbytes;//每次发送的数据字节数= 1B 2A 21 2字节长度+数据内容Byte[] cmdbytes = new Byte[5 + blockbytes];//指令Array.Copy(Const_SetImageCommand, cmdbytes, 3);//数据长度,即每行的点数DataFormatProcessor.Int16T oBytes(width, ref cmdbytes, 3);//数据内容for (Int32 blockidx = 0; blockidx < blocks; ++blockidx){Array.Copy(imgbitarray, blockidx * blockbytes, cmdbytes, 5, blockbytes);//发送当前指令bl = SendCommand(cmdbytes);if (!bl) return false;//休眠20毫秒Thread.Sleep(20);//发送打印指令bl = SendCommand(Const_Cmd_Print);if (!bl) return false;}return bl;}//////走纸/////////public bool Feed(int length){if (!m_Inited){return false;}if (length < 1)length = 1;if (length > Const_MaxFeedLength) {length = Const_MaxFeedLength; }string len = length.ToString("X2"); len = Const_FeedForward + len; bool bl = SendCommand(len); return bl;}//////回退走纸/////////public bool FeedBack(int length) {if (!m_Inited){return false;}if (length < 1)length = 1;if (length > Const_MaxFeedLength) {length = Const_MaxFeedLength; }string len = length.ToString("X2"); len = Const_FeedBack + len;bool bl = SendCommand(len);return bl;}//////设置字体大小.本函数不可与SetBigFont同时使用//////大小倍率,取值范围1 - 8 ///public bool SetFontSize(Int32 sizerate){if (!m_Inited){return false;}if (sizerate < 1){sizerate = 1;}if (sizerate > Const_MaxFontSize){sizerate = Const_MaxFontSize;}sizerate--;String sizecodestr = Const_SetFontSize + sizerate.ToString("X1") + sizerate.ToString("X1");bool bl = SendCommand(sizecodestr);return bl;}#endregion}}3.图像处理 ImageProcessorusing System;usingusing System.Linq;using System.Text;using System.Drawing;using /doc/6213217853.html,monBLL; usingusing System.IO;usingusingusingnamespace LaisonTech.MediaLib{//////图片格式///public enum ePictureFileFormat{Bmp = 0,Gif = 1,Icon = 2,Jpeg = 3,Png = 4,}//////转为灰度图像的方式///public enum eGrayMode{///算数平均///ArithmeticAverage = 0,///加权平均///WeightedAverage = 1,}//////比较2个图片的指定区域范围,像素的相同类型///public enum eAreaDifferentType{//////所有像素都相同///AllSame = 0,//////所有像素都不同///AllDifferent = 1,//////部分相同部分不同///Partial = 2,}//////图片文件处理///public class ImageProcessor#region常量定义public const Byte Const_BrightnessWhite = 255;public const Byte Const_BrightnessBlack = 0;//////比较结果的图片里,亮度相同部分的填充颜色///public static Color Const_SameBrightnessColor = Color.Black;//////比较结果的图片里,亮度相同部分的填充颜色///public static Color Const_DifferentBrightnessColor = Color.White;public const Byte Const_BlackBrightness = 0;public const Byte Const_WhiteBrightness = 255;public const Int32 Const_MaxBrightness = 255;public const Int32 Const_MinBrightness = -255;//////亮度的中间值public const Int32 Const_MiddleBrightness = 128;#endregion#region屏幕截图,打印//////获取屏幕分辨率/////////public static void GetScreenSize(ref Int32 width, ref Int32 height) {height = Screenwidth = Screen}//////截图指定控件上显示的内容/////////public static Image CaptureControlImage(Control ctrl) {if (ctrl == null){return null;}Control parent = ctrl;if (ctrl.Parent != null){parent = ctrl.Parent;}Point screenPoint = parent.PointT oScreen(ctrl.Location); Image ret = new Bitmap(ctrl.Width, ctrl.Height); Graphics g = Graphics.FromImage(ret);g.CopyFromScreen(screenPoint.X, screenPoint.Y,0, 0, ctrl.Size);g.DrawImage(ret, 0, 0);return ret;}#endregion#region装载图片//////装载图像文件/// /// ///。

C#使用ESC指令控制POS机打印小票

C#使用ESC指令控制POS机打印小票
#region 私有函数
[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
//设置粗体 private const string Const_SetBold = "1B 45 "; private const String Const_Bold_YES = "01"; private const String Const_Bold_NO = "00";
//设置对齐方式 private const string Const_SetAlign = "1B 61 "; private const String Const_Align_Left = "30"; private const String Const_Align_Middle = "31"; private const String Const_Align_Right = "32";
public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out Int32 hPrinter, IntPtr pd);
[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]

ESCPOS命令集

ESCPOS命令集

ESC/POS命令集2007-11-19 14:56切纸指令:发送:十六进制:1B694.1 基本控制指令4.1.1 ESC @[名称] 初始化打印机[格式] ASCII码 ESC @十六进制码 1B 40十进制码 27 64[描述] 清除打印缓冲区中的数据,复位打印机打印参数到当打印机缺省参数。

[注意] * 不是完全恢复到出厂设置,用户参数设置(见三、JPM112打印机工具软件)不会被更改。

[例子] char SendStr[3];SendStr[0] = 0x1BSendStr[1] = 0x40;PrtSendData(SendStr, 2);4.1.2 FF[名称] 打印并走纸到下页首[格式] ASCII码 FF14/53----------------------- 16-----------------------用户文档 JPM112 打印机开发手册(V1.0 )十六进制码 0C十进制码 12[描述] 将打印缓冲区中的数据全部打印出来并返回标准模式。

[注意] * 打印后,删除打印缓冲区中的数据。

* 该命令设置打印位置为行的起始点。

* 如果打印纸有预印刷黑标,则打印缓冲区中的数据后,走纸到黑标处,如果打印纸无黑标,则走纸0.5m后停止,预印刷黑标的规范请见附录B.预印刷黑标说明。

[例子] char SendStr[2];SendStr[0] = 0x0C;PrtSendData( SendStr, 1);4.1.3 LF[名称] 打印并换行[格式] ASCII码 LF十六进制码 0A十进制码 10[描述] 把打印缓冲区中的数据打印出来,并换行。

[注意] * 该命令把打印位置设置为行的开始位置。

[参考] CR[例子] char SendStr[2];SendStr[0]=’\n’; //C语言中’\n’即为换行PrtSendData( SendStr, 1);4.1.4 CR[名称] 打印并回车[格式] ASCII码 CR十六进制码 0D十进制码 13[描述] 打印但不进纸。

打印机ESCPOS控制指令

打印机ESCPOS控制指令

打印机E S C P O S控制指令集团企业公司编码:(LL3698-KKI1269-TM2483-LUI12689-ITT289-H D D-80260打印机E S C/P O S控制指令ESC/POS控制指令HT横向跳格如果下一个跳格位置没有设置,该指令被忽略。

如果下一个横向跳格位置超越打印区域,将当前位置设置为[打印宽度+1]。

水平跳格位置由指令ESCD来设置。

如果接收到此命令时当前位置在[打印宽度+1],打印机执行当前行缓冲区满动作并且将打印位置移到下一行的起始位置。

默认值跳格位置是每8个标准ASCII字符(12×24)字符跳一格。

当前行缓冲区满时,打印机执行下列动作:标准模式下,打印机打印当前行内容并将打印位置置于下一行的起始位置。

页模式下,打印机进行换行并将打印位置置于下一行的起始位置。

LF打印并走纸一行该指令将当前位置置于行首。

FF打印并走纸打印后清除缓冲区的内容。

由ESCW指令设置的打印区域被重置为默认设置。

打印机不执行切纸动作。

该指令将当前位置置于行首。

该指令只在页模式下有效。

CR打印并回车设置打印位置为行首。

串行接口模式下该指令被忽略。

并口模式下该指令是否有效由打印机配置决定。

CAN页模式下删除打印缓冲区内容该指令只在页模式下有效。

如果先前设定区域与当前区域有重叠部分,重叠部分也被删除。

DLEDC4nmt实时产生钱箱开启脉冲当打印机正在执行钱箱开启命令(ESCp或DELDC4)时,该命令被忽略。

串口模式下,打印机接收该指令后立即执行。

并口模式下,打印机忙的时候该指令不执行。

如果打印数据中包含和此指令相同的数据,则这些数据将被当作该指令执行。

用户必须考虑到这种情况。

该指令尽量不要插在2个或更多字节的指令序列中。

即使打印机被ESC=(选择外设)命令设置为禁止,该指令依然有效。

ESCFF页模式下打印该指令只在页模式下有效。

打印后不清除打印缓冲区的内容、ESCT和ESCW设置和字符的位置。

C使用ESC指令控制POS机打印小票

C使用ESC指令控制POS机打印小票

C#使用ESC指令控制POS打印机打印小票1.前言C#打印小票可以与普通打印机一样,调用PrintDocument实现。

也可以发送标注你的ESC指令实现。

由于调用PrintDocument类时,无法操作使用串口或TCP/IP接口连接的pos打印机,并且无法发送控制指令实现pos 打印机的切纸、走纸等动作。

因此个人建议使用ESC指令进行打印会更通用。

本类需要调用ImageProcessor.cs2.POS机打印小票ReceiptHelperusing System;using ;using System.Text;using ;using System.Threading;using System.Drawing;using System.Management;using System.IO;using LaisonTech.MediaLib;using monBLL;using ;namespace LaisonTech.MediaLib{#region结构体定义[StructLayout(LayoutKind.Sequential)]public struct OVERLAPPED{int Internal;int InternalHigh;int Offset;int OffSetHigh;int hEvent;};[StructLayout(LayoutKind.Sequential)]public struct PRINTER_DEFAULTS{public int pDatatype;public int pDevMode;public int DesiredAccess;}///<summary>///对齐方式///</summary>public enum eTextAlignMode{Left = 0,Middle = 1,Right = 2}#endregion///<summary>///小票打印类/// 使用方法:/// 1 GetPrinterList获取已经安装的所有打印机列表./// Open 打开指定打印机/// 2 控制打印机动作、执行打印内容之前,必须先调用StartPrint,准备向打印机发送控制指令/// 3 调用SetLeft, SetBold, SetAlignMode, SetFontSize ... ...设置打印参数///4 PrintText 打印内容.注意:打印该行内容后会自动换行(本类会在该行内容末尾添加一个换行符) /// PrintImageFile 或 PrintBitMap打印图片/// 5 控制指令和打印内容都发送完毕后,调用 EndPrint执行真正打印动作/// 6 退出程序前调用Close///</summary>public class ReceiptHelper{#region指令定义private static Byte[] Const_Init = new byte[] { 0x1B, 0x40,0x20, 0x20, 0x20, 0x0A,0x1B, 0x64,0x10};//设置左边距private const string Const_SetLeft = "1D 4C ";//设置粗体private const string Const_SetBold = "1B 45 ";private const String Const_Bold_YES = "01";private const String Const_Bold_NO = "00";//设置对齐方式private const string Const_SetAlign = "1B 61 ";private const String Const_Align_Left = "30";private const String Const_Align_Middle = "31";private const String Const_Align_Right = "32";//设置字体大小,与 SetBigFont 不能同时使用private const string Const_SetFontSize = "1D 21 ";//设置是否大字体,等同于 SetFontSize = 2//private const String Const_SetBigFontBold = "1B 21 38";//private const String Const_SetBigFontNotBold = "1B 21 30";//private const String Const_SetCancelBigFont = "1B 21 00";///<summary>///打印并走纸///</summary>private static Byte[] Const_Cmd_Print = new byte[] { 0x1B, 0x4A, 0x00 };//走纸private const string Const_FeedForward = "1B 4A ";private const string Const_FeedBack = "1B 6A ";//切纸private static Byte[] Const_SetCut = new byte[] { 0x1D, 0x56, 0x30};//查询打印机状态private static Byte[] Const_QueryID = new byte[] { 0x1D, 0x67, 0x61};//回复帧以 ID 开头private static String Const_ResponseQueryID = "ID";///<summary>///设置图标的指令///</summary>private static Byte[] Const_SetImageCommand = new Byte[] { 0x1B, 0x2A, 0x21 }; #endregion#region常量定义///<summary>///最大字体大小///</summary>public const Int32 Const_MaxFontSize = 8;///<summary>///最大走纸距离///</summary>public const Int32 Const_MaxFeedLength = 5000;///<summary>///最大高宽///</summary>public const Int32 Const_MaxImageLength = 480;///<summary>///每次通信最多打印的行数///</summary>public const Int32 Const_OncePrintRowCount = 24;public const Int32 Const_BrightnessGate = 100;///<summary>///无效句柄///</summary>public const Int32 Const_InvalidHandle = -1;#endregion#region私有成员///<summary>///打印机句柄///</summary>private int m_Handle = -1;///<summary>///是否已经初始化///</summary>private Boolean m_Inited = false;#endregion#region私有函数[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out Int32 hPrinter, IntPtr pd);[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool StartDocPrinter(Int32 hPrinter, Int32 level, [In,MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool EndDocPrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool StartPagePrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool EndPagePrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool WritePrinter(Int32 hPrinter, Byte[] pBytes, Int32 dwCount, out Int32 dwWritten);[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool ClosePrinter(Int32 hPrinter);///<summary>///发送指令///</summary>///<param name="cmd"></param>///<returns></returns>private Boolean SendCommand(Byte[] cmd){if (m_Handle == Const_InvalidHandle || cmd == null || cmd.Length < 2){return false;}int writelen = 0;Boolean bl = WritePrinter(m_Handle, cmd, cmd.Length, out writelen);if (!bl) return false;return (writelen >= cmd.Length);}///<summary>///发送文本格式的指令///</summary>///<param name="cmd"></param>///<returns></returns>private Boolean SendCommand(String hexstrcmd){if (m_Handle == Const_InvalidHandle || hexstrcmd == null || hexstrcmd.Length < 4) {return false;}byte[] mybyte = null;Boolean bl = DataFormatProcessor.HexStringToBytes(hexstrcmd, out mybyte);bl = SendCommand(mybyte);return bl;}#endregion#region内部处理 - 打印图片///<summary>///把图片转换为指令字节,图片最大高宽不能超过480///</summary>///<param name="image"></param>///<param name="bmpbytes"></param>///<returns></returns>public static Boolean LoadImage(Bitmap image,ref Byte[] bitarray,ref Int32 datawidth,ref Int32 dataheight){Int32 newwidth = 0;Int32 newheight = 0;Bitmap destimage = image;Boolean bl = false;//如果高度超过范围,或宽度超过范围,需要进行缩小if (image.Width > Const_MaxImageLength || image.Height > Const_MaxImageLength) {//按照高度和宽度,较大的那一边,进行缩放if (image.Width > image.Height){newwidth = Const_MaxImageLength;newheight = (Int32)(image.Height * newwidth / (float)image.Width);}else{newheight = Const_MaxImageLength;newwidth = (Int32)(newheight * image.Width / (float)image.Height);}bl = ImageProcessor.ResizeImage(image, newwidth, newheight, ref destimage); }//把数据转换为字节数组bl = GetBitArray(image, ref bitarray, ref datawidth, ref dataheight);return bl;}///<summary>///把图片转换为指令字节,图片最大高宽不能超过480///如果图片的高度不是24的整数倍,则修改为24的整数倍///</summary>///<param name="image"></param>///<param name="bmpbytes"></param>///<returns></returns>public static Boolean LoadImageFromFile(String imagefilename, ref Byte[] bmpbytes, ref Int32 width, ref Int32 height){Bitmap img = ImageProcessor.LoadBitImage(imagefilename);if (img == null){return false;}Boolean bl = LoadImage(img, ref bmpbytes, ref width, ref height);return bl;}///<summary>///把图片转换为位图数组,每个字节的每个比特位,对应当前像素是否需要打印///</summary>///<param name="img"></param>///<param name="allbitary"></param>///<returns></returns>public static Boolean GetBitArray(Bitmap img,ref Byte[] allbitary, ref Int32 width, ref Int32 height){if (img == null){return false;}//ESC指令格式规定://1 打印图片时,每条指令最多只打印24行;不足24行的,也要用全0填充满数据字节//2 打印24行数据时,按照光栅模式纵向获取数据// 即先获取所有x=0的点(第0列)转换为3个字节;// 再获取所有x=1的点转换为3个字节;...直到获取到最右侧一列的点//3 打印完当前24行数据后,再获取后续24行的数据内容,直到所有的数据获取完毕//获取亮度数组Boolean[] briary = null;Boolean bl = ImageProcessor.ToBooleanArray(img, Const_BrightnessGate, ref briary);if (!bl){return false;}height = img.Height;//如果图像高度不是24整数倍,设置为24的整数倍if (height % Const_OncePrintRowCount != 0){height = height + Const_OncePrintRowCount - height % Const_OncePrintRowCount; }width = img.Width;//如果图像宽度不是8的整数倍,设置为8的整数倍if (width % 8 != 0){width = width + 8 - width % 8;}Int32 bytelen = height * width / 8;//每个像素对应1个比特位,因此总字节数=像素位数/8 allbitary = new Byte[bytelen];Int32 byteidxInCol = 0;//当前列里首个像素,在目标字节数组里的下标Int32 byteidx = 0;//当前像素在目标数组里的字节下标Int32 bitidx = 0;//当前像素在目标数组里当前字节里的比特位下标Int32 pixidxInCol = 0;//当前像素在当前列里的第几个位置Int32 pixidx = 0;//当前像素在原始图片里的下标Int32 rowidx = 0; //当前处理的像素点所在行,不能超过图像高度Int32 curprocrows = 0;//当前需要处理的行数量while (rowidx < height){//按照纵向次序,把当前列的24个数据,转换为3个字节for (Int32 colidx = 0; colidx < img.Width; ++colidx){//如果当前还剩余超过24行没处理,处理24行if (rowidx + Const_OncePrintRowCount <= img.Height){curprocrows = Const_OncePrintRowCount;}else{//已经不足24行,只处理剩余行数curprocrows = img.Height - rowidx;}pixidxInCol = 0; //本列里从像素0开始处理for (Int32 y = rowidx; y < rowidx + curprocrows; ++y){//原始图片里像素位置pixidx = y * img.Width + colidx;//获取当前像素的亮度值.如果当前像素是黑点,需要把数组里的对应比特位设置为1if (briary[pixidx]){bitidx = 7 - pixidxInCol % 8;//最高比特位对应首个像素.最低比特位对应末个像素byteidx = byteidxInCol + pixidxInCol / 8; //由于最后一段可能不足24行,因此不能使用byteidx++DataFormatProcessor.SetBitValue(bitidx, true, refallbitary[byteidx]);}pixidxInCol++;}byteidxInCol += 3;//每列固定24个像素,3个字节}rowidx += Const_OncePrintRowCount;}return true;}#endregion#region公开函数private static ReceiptHelper m_instance = new ReceiptHelper();///<summary>///当前使用的打印机名称///</summary>public String PrinterName{get;private set;}///<summary>///单件模式///</summary>///<returns></returns>public static ReceiptHelper GetInstance(){return m_instance;}///<summary>///获取本机安装的所有打印机///</summary>///<returns></returns>public static List<String> GetPrinterList(){List<String> ret = new List<String>();if ( < 1){return ret;}foreach (String printername in PrinterSettings.InstalledPrinters){ret.Add(printername);}return ret;}///<summary>///打开打印机///</summary>///<param name="printername"></param>///<returns></returns>public Boolean Open(String printername){if (m_Inited){return true;}Boolean bl = OpenPrinter(printername.Normalize(), out m_Handle, IntPtr.Zero);m_Inited = (bl && m_Handle != 0);return true;}///<summary>///开始打印,在打印之前必须调用此函数///</summary>///<returns></returns>public Boolean StartPrint(){if (!m_Inited){return false;}DOCINFOA di = new DOCINFOA();di.pDocName = "My C#.NET RAW Document";di.pDataType = "RAW";//Start a document.Boolean bl = StartDocPrinter(m_Handle, 1, di);if (!bl){return false;}// Start a page.bl = StartPagePrinter(m_Handle);return bl;}///<summary>///结束打印,在打印结束之后必须调用此函数///</summary>///<returns></returns>public Boolean EndPrint(){if (!m_Inited){return false;}Boolean bl = EndPagePrinter(m_Handle);bl = EndDocPrinter(m_Handle);return bl;}///<summary>///销毁///</summary>///<returns></returns>public Boolean Close(){if (!m_Inited){return true;}m_Inited = false;//关闭设备句柄ClosePrinter(m_Handle);m_Handle = -1;return true;}///<summary>///打印文本.在调用本函数之前必须先调用正确的设置字体、左边距///</summary>///<param name="content"></param>///<returns></returns>public Boolean PrintText(String content){if (!m_Inited){return false;}byte[] bytes = null;if (content.Length < 1){content = " ";}if (content[content.Length - 1] != (char)0x0D &&content[content.Length - 1] != (char)0x0A){content = content + (char)0x0A;}bytes = DataFormatProcessor.StringToBytes(content);bool bl = SendCommand(bytes);return bl;}///<summary>///设置对齐方式///</summary>///<param name="left"></param>///<returns></returns>public bool SetAlignMode(eTextAlignMode alignmode){if (!m_Inited){return false;}String code = String.Empty;switch (alignmode){case eTextAlignMode.Left:code = Const_Align_Left;break;case eTextAlignMode.Middle:code = Const_Align_Middle;break;case eTextAlignMode.Right:code = Const_Align_Right;break;default:code = Const_Align_Left;break;}//注意:先低字节后高字节string str = Const_SetAlign + code;bool bl = SendCommand(str);return bl;}///<summary>///设置左边距///</summary>///<param name="left"></param>///<returns></returns>public bool SetLeft(int left){if (!m_Inited){return false;}//注意:先低字节后高字节String hexstr = left.ToString("X4");string str = Const_SetLeft + hexstr.Substring(2, 2) + hexstr.Substring(0, 2);bool bl = SendCommand(str);return bl;}///<summary>///设置粗体///</summary>///<param name="bold"></param>///<returns></returns>public Boolean SetBold(Boolean bold){if (!m_Inited){return false;}//注意:先低字节后高字节String str = String.Empty;if (bold){str = Const_SetBold + Const_Bold_YES; }else{str = Const_SetBold + Const_Bold_NO; }bool bl = SendCommand(str);return bl;}///<summary>///切纸///</summary>///<returns></returns>public bool Cut(){if (!m_Inited){return false;}bool bl = SendCommand(Const_SetCut);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintImageFile(String imgfilename){if (!m_Inited){return false;}Bitmap img = ImageProcessor.LoadBitImage(imgfilename);if (img == null){return false;}Boolean bl = PrintBitmap(img);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintBitmap(Bitmap bitmap){if (!m_Inited){return false;}if (bitmap == null ||bitmap.Width > Const_MaxImageLength ||bitmap.Height > Const_MaxImageLength){return false;}Byte[] bitary = null;Int32 width = 0;Int32 height = 0;Boolean bl = GetBitArray(bitmap, ref bitary, ref width, ref height);bl = PrintBitmapBytes(bitary, bitmap.Width, bitmap.Height);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintBitmapBytes(Byte[] imgbitarray, Int32 width, Int32 height){if (!m_Inited){return false;}Int32 bytes = width * height / 8;//检查是否尺寸符合要求if (width > Const_MaxImageLength || height > Const_MaxFeedLength ||width < 1 || height < 1 ||imgbitarray == null){return false;}//每次获取24行的数据进行发送,这24行的字节数Int32 blockbytes = width * Const_OncePrintRowCount / 8;if (blockbytes < 1){return false;}Boolean bl = false;//一共需要发送的块数量Int32 blocks = imgbitarray.Length / blockbytes;//每次发送的数据字节数 = 1B 2A 21 2字节长度 +数据内容Byte[] cmdbytes = new Byte[5 + blockbytes];//指令Array.Copy(Const_SetImageCommand, cmdbytes, 3);//数据长度,即每行的点数DataFormatProcessor.Int16ToBytes(width, ref cmdbytes, 3);//数据内容for (Int32 blockidx = 0; blockidx < blocks; ++blockidx){Array.Copy(imgbitarray, blockidx * blockbytes, cmdbytes, 5, blockbytes);//发送当前指令bl = SendCommand(cmdbytes);if (!bl) return false;//休眠20毫秒Thread.Sleep(20);//发送打印指令bl = SendCommand(Const_Cmd_Print);if (!bl) return false;}return bl;}///<summary>///走纸///</summary>///<param name="length"></param>///<returns></returns>public bool Feed(int length){if (!m_Inited){return false;}if (length < 1)length = 1;if (length > Const_MaxFeedLength){length = Const_MaxFeedLength;}string len = length.ToString("X2");len = Const_FeedForward + len;bool bl = SendCommand(len);return bl;}///<summary>///回退走纸///</summary>///<param name="length"></param>///<returns></returns>public bool FeedBack(int length){if (!m_Inited){return false;}if (length < 1)length = 1;if (length > Const_MaxFeedLength){length = Const_MaxFeedLength;}string len = length.ToString("X2");len = Const_FeedBack + len;bool bl = SendCommand(len);return bl;}///<summary>///设置字体大小.本函数不可与SetBigFont同时使用///</summary>///<param name="sizerate">大小倍率,取值范围 1 - 8</param>///<returns></returns>public bool SetFontSize(Int32 sizerate){if (!m_Inited){return false;}if (sizerate < 1){sizerate = 1;}if (sizerate > Const_MaxFontSize){sizerate = Const_MaxFontSize;}sizerate--;String sizecodestr = Const_SetFontSize + sizerate.ToString("X1") + sizerate.ToString("X1");bool bl = SendCommand(sizecodestr);return bl;}#endregion}}3.图像处理ImageProcessorusing System;using ;using System.Linq;using System.Text;using System.Drawing;using monBLL;using ;using System.IO;using ;using ;using ;namespace LaisonTech.MediaLib{///<summary>///图片格式///</summary>public enum ePictureFileFormat{Bmp = 0,Gif = 1,Icon = 2,Jpeg = 3,Png = 4,}///<summary>///转为灰度图像的方式///</summary>public enum eGrayMode{///<summary>///算数平均///</summary>ArithmeticAverage = 0,///<summary>///加权平均///</summary>WeightedAverage = 1,}///<summary>///比较2个图片的指定区域范围,像素的相同类型///</summary>public enum eAreaDifferentType{///<summary>///所有像素都相同///</summary>AllSame = 0,///<summary>///所有像素都不同///</summary>AllDifferent = 1,///<summary>///部分相同部分不同///</summary>Partial = 2,}///<summary>///图片文件处理///</summary>public class ImageProcessor{#region常量定义public const Byte Const_BrightnessWhite = 255;public const Byte Const_BrightnessBlack = 0;///<summary>///比较结果的图片里,亮度相同部分的填充颜色///</summary>public static Color Const_SameBrightnessColor = Color.Black;///<summary>///比较结果的图片里,亮度相同部分的填充颜色///</summary>public static Color Const_DifferentBrightnessColor = Color.White;public const Byte Const_BlackBrightness = 0;public const Byte Const_WhiteBrightness = 255;public const Int32 Const_MaxBrightness = 255;public const Int32 Const_MinBrightness = -255;///<summary>///亮度的中间值///</summary>public const Int32 Const_MiddleBrightness = 128;#endregion#region屏幕截图,打印///<summary>///获取屏幕分辨率///</summary>///<param name="width"></param>///<param name="height"></param>public static void GetScreenSize(ref Int32 width, ref Int32 height) {height = ;width = ;}///<summary>///截图指定控件上显示的内容///</summary>///<param name="ctrl"></param>///<returns></returns>public static Image CaptureControlImage(Control ctrl){if (ctrl == null){return null;}Control parent = ctrl;if (ctrl.Parent != null){parent = ctrl.Parent;}Point screenPoint = parent.PointToScreen(ctrl.Location);Image ret = new Bitmap(ctrl.Width, ctrl.Height);Graphics g = Graphics.FromImage(ret);g.CopyFromScreen(screenPoint.X, screenPoint.Y,0, 0, ctrl.Size);g.DrawImage(ret, 0, 0);return ret;}#endregion#region装载图片///<summary>///装载图像文件///</summary>///<param name="filename"></param>///<returns></returns>public static Image LoadImage(String filename){//Boolean bl = FileProcessor.FileExist(filename);//if (!bl)//{// return null;//}//Bitmap image = (Bitmap)Bitmap.FromFile(filename);//return image;//以上方法会导致图片文件被锁定,无法删除移动Byte[] photodata = null;Boolean bl = FileProcessor.FileExist(filename);if (!bl){return null;}bl = FileProcessor.ReadFileBytes(filename, out photodata);if (!bl){return null;}MemoryStream ms = null;Image myImage = null;try{ms = new MemoryStream(photodata);myImage = Bitmap.FromStream(ms);ms.Close();}catch (System.Exception ex){Console.WriteLine("LoadImage error:" + ex.Message); myImage = null;}return myImage;}///<summary>///装载图像文件///</summary>///<param name="filename"></param>///<returns></returns>public static Bitmap LoadBitImage(String filename){Bitmap ret = (Bitmap)LoadImage(filename);return ret;}///<summary>///保存图片到指定路径///</summary>///<param name="img"></param>///<param name="filename"></param>///<returns></returns>public static Boolean SaveImage(Image img, String filename) {FileProcessor.DeleteFile(filename);if (img == null){return false;}//获取保存图片的路径,如果路径不存在,新建String folder = FileProcessor.GetDirectoryName(filename);if (!FileProcessor.DirectoryExist(folder)){FileProcessor.CreateDirectory(folder);}img.Save(filename);Boolean bl = FileProcessor.FileExist(filename);return bl;}#endregion#region转换图片格式///<summary>///转换图片格式///</summary>///<param name="bmpfilename"></param>///<param name="jpgfilename"></param>///<returns></returns>public static Boolean BmpToJpg(String bmpfilename, String jpgfilename){Boolean bl = ChangeFileFormat(bmpfilename, jpgfilename, ePictureFileFormat.Jpeg);return bl;}///<summary>///转换图片格式///</summary>///<param name="srcfilename"></param>///<param name="destfilename"></param>///<param name="destformat"></param>///<returns></returns>public static Boolean ChangeFileFormat(String srcfilename, String destfilename, ePictureFileFormat destformat){Boolean bl = FileProcessor.FileExist(srcfilename);if (!bl){return false;}Image image = Image.FromFile(srcfilename);ImageFormat IFMT = null;switch (destformat)case ePictureFileFormat.Bmp:IFMT = ImageFormat.Bmp;break;case ePictureFileFormat.Gif:IFMT = ImageFormat.Gif;break;case ePictureFileFormat.Icon:IFMT = ImageFormat.Icon;break;case ePictureFileFormat.Jpeg:IFMT = ImageFormat.Jpeg;break;case ePictureFileFormat.Png:IFMT = ImageFormat.Png;break;default:IFMT = ImageFormat.Jpeg;break;}image.Save(destfilename, IFMT);image.Dispose();bl = FileProcessor.FileExist(destfilename);if (!bl){return false;}Int32 filelen = FileProcessor.GetFileLength(destfilename);return (filelen > 0);}///<summary>///变成黑白图///</summary>///<param name="srcbitmap">原始图</param>///<param name="mode">模式。

ESCPOS58命令集

ESCPOS58命令集

[命令] 选择打印模式
[格式] ASCII码 ESC ! n
十六进制码 1B 21 n
十进制码 27 33 n
[范围] 0 ≤ n ≤ 255
[描述] 通过指定参数n 的值选择打印模式。参数n 的定义如下:
* 有关GBK的详细信息请参阅全国信息技术标准化技术委员会制定的 “汉字内码扩展规范”;
* 有关Unicode的详细信息可以参见Unicode的官方网站;
* 本命令不受汉字命令的影响,也不受自定义字符命令的影响;
切纸指令:
发送:十六进制:1B69
4.1 基本控制指令
4.1.1 ESC @
[名称] 初始化打印机
[格式] ASCII码 ESC @
十六进制码 1B 40
十进制码 27 64
SendStr[4]=0x55; SendStr[5]=0x00; //U+0055:’U’
SendStr[6]=0x4E; SendStr[7]=0x00; //U+004E:’N’
SendStr[8]=0x49; SendStr[9]=0x00; //U+0049:’I’
* 本命令可以嵌入其他ESC/GS/FS指令,但要采用Unicode编码。
[例子] char SendStr[64];
SendStr[0]=0x1C; SendStr[1]=’U’; SendStr[2]=11; SendStr[3]=0;
SendStr[16]=0x45; SendStr[17]=0x00; //U+0045:’E’
SendStr[18]=0x53; SendStr[19]=0x62; //U+6253:’打’

爱普生EPSONESCPOS指令集说明

爱普生EPSONESCPOS指令集说明

爱普生【EPSON】ESC/POS指令集说明英文模式下的命令代码功能CR回车ESC !设置打印方式ESC %选择或取消用户自定义字符集ESC &定义用户自定义字符集ESC *设置位映射方式ESC @初始化打印机ESC ~LED ON/OFFESC <返回行首ESC 2选择行间距为1/6英寸ESC 3设置行进为最小间距ESC BEL蜂鸣器ON/OFFESC C设置单页长度ESC c0选择打印页ESC c1选择行间距ESC c3选择纸结束信号输出ESC c4选择打印纸及检测器(终止打印)ESC c5禁止/使能面板开关ESC c6禁止/使能ON-LINE开关ESC d打印及N行进纸ESC D设置TAB位置ESC e打印病退回N行ESC f设单页等待时间ESC F选择或取消单页退纸区ESC i全切割ESC J以最小间距进行打印和进纸ESC K以最小间距进行打印和退纸ESC l选择或取消倒过来的字符ESC m局部切割ESC o印章ESC p产生指定脉冲ESC q释放纸ESC r选择打印颜色ESC R选择国际字符子集ESC SP设置右边界ESC t选择字符码表ESC U选择或取消单向打印ESC V发送打印机状态ESC z设置或取消两页并行打印FF打印送出单页HT水平TABLF换行RS流水TAB中文模式下的命令代码功能FS – n设置中文字符下划线模式开关FS ! n选择中文字体FS &选择中文字符模式FS .取消中文模式---------------------------------------------------------------------------------------- EPSON打印控制指令集打印命令功能格式:ASCII:以标准ASCII字符序列表示十进制:以十进制数字序列表示十六进制:以十六进制数字序列表示说明:该命令功能和使用说明。

HT水平制表格式:ASCII:HT十进制:09十六进制:09说明:打印位置进行到下一水平制表位置。

ESCPOS打印控制命令

ESCPOS打印控制命令

ESC/POS打印控制命令1、打印命令一览表命令速查命令说明打印命令LF 打印并换行ESC J 打印并走纸n点行格式设置命令ESC 2 设置行间距1/6英寸ESC 3 n 设置行间距为n点行(n/137英寸) ESC a n 设置对齐方式字符设置命令ESC ! n 设置打印字符格式ESC SO 设置字符倍宽打印ESC DC4 取消字符倍宽打印ESC { n 设置/取消字符上下倒置ESC B n 设置/取消字符反白打印ESC % n 设置/取消用户自定义字符ESC & 设定用户自定义字符ESC ? 取消用户自定义字符ESC R n 选择国际字符集ESC t n 选择字符代码页图形设置命令ESC * 打印一点行图形GS * 下装点图设置GS / 打印下装点图钱箱控制命令ESC p 钱箱控制按键控制命令ESC c 5 允许/禁止按键开关初始化命令ESC @ 打印机初始化状态传输命令ESC v 向主机传送打印机状态ESC u 向主机传送周边设置状态GS a 允许/禁止状态自动上传条码设置命令GS H 选择HRI打印方式GS h 设置条码高度GS w 设置条码横向宽度GS k 打印条码位置和页模式命令ESC L 设置页模式FF 打印,并返回到标准模式ESC FF 打印页模式的数据CAN 清除页缓冲区的数据ESC W … 设置页位置和大小ESC $ nL nH 设置页模式的绝对X位置GS $ nL nH 设置页模式的绝对Y位置ESC \ nL nH 设置页模式的相对X位置GS \ nL nH 设置页模式的相对Y位置GS L nL nH 设置页左边距GS W nL nH 设置页宽切纸模式命令ESC i 切纸但不走纸2、打印命令详解概述POS-58打印机使用ESC/POS打印命令集。

打印命令按以下格式说明:命令功能格式:ASCII:以标准ASCII字符序列表示十进制:以十进制数字序列表示十六进制:以十六进制数字序列表示说明:命令功能和使用说明。

打印机ESCPOS 控制指令

打印机ESCPOS 控制指令

HDD-80260打印机ESC/POS 控制指令ESC/POS 控制指令HT 横向跳格LF 打印并走纸一行FF 打印并走纸CR 打印并回车CAN 页模式下删除打印缓冲区内容DLE DC4 n m t 实时产生钱箱开启脉冲ESC FF 页模式下打印ESC SP n 设置字符右间距ESC ! n 选择打印模式ESC $ nL nH 设置绝对打印位置ESC % n 选择/取消用户自定义字符ESC & y c1 c2 [x1 d1...d(y ×x1)]...[xk d1...d(y ×xk)] 定义用户自定义字符●自定义字符的大小是(y×x)字节。

●数据的各个位为1 表示打印这个点,为0 表示不打印。

●当下列情况,用户自定义字符被清除:①ESC @被执行。

②ESC ?被执行。

③打印机复位或电源关闭。

[默认值] 内部字库设置[参考] ESC %, ESC ?[实例] ●当选择标准ASCII 字体(12×24)时●当选择压缩ASCII 字体时(9×17)ESC * m nL nH d1...dk 选择位图模式[名称] Select bit-image mode[格式] ASCII ESC * m nL nH d1...dk Hex 1B 2A m nL nH d1...dk Decimal 27 42 m nL nH d1...dk[范围] m = 0, 1, 32, 33;0 ≤ nL ≤ 255;0 ≤ nH ≤ 3;0 ≤ d ≤ 255[描述] 选择由m 指定的一种位图模式,位图点数由nL 和nH 确定:m 模式纵向横向点数分辨率分辨率数据个数(k)0 8点单密度8 67DPI 100DPI nL+nH×2561 8点双密度8 67DPI 200DPI nL+nH×25632 24点单密度24 200DPI 100DPI (nL+nH×256)×333 24点双密度24 200DPI 200DPI (nL+nH×256)×3 [注释] ●如果m 的值超出规定范围,nL 和其后的数据被作为普通数据处理。

C#使用ESC指令控制POS机打印小票

C#使用ESC指令控制POS机打印小票

C#使用ESC指令控制POS打印机打印小票1.前言C#打印小票可以与普通打印机一样,调用PrintDocument实现。

也可以发送标注你的ESC指令实现。

由于调用PrintDocument类时,无法操作使用串口或TCP/IP接口连接的pos打印机,并且无法发送控制指令实现pos 打印机的切纸、走纸等动作。

因此个人建议使用ESC指令进行打印会更通用。

本类需要调用ImageProcessor.cs25035844@qq.2.POS机打印小票ReceiptHelperusing System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;using System.Threading;using System.Drawing;using System.Management;using System.IO;using LaisonTech.MediaLib;using monBLL;using Microsoft.Win32.SafeHandles;namespace LaisonTech.MediaLib{#region结构体定义[StructLayout(LayoutKind.Sequential)]public struct OVERLAPPED{int Internal;int InternalHigh;int Offset;int OffSetHigh;int hEvent;};[StructLayout(LayoutKind.Sequential)]public struct PRINTER_DEFAULTS{public int pDatatype;public int pDevMode;public int DesiredAccess;}///<summary>///对齐式///</summary>public enum eTextAlignMode{Left = 0,Middle = 1,Right = 2}#endregion///<summary>///小票打印类/// 使用法:/// 1 GetPrinterList获取已经安装的所有打印机列表./// Open 打开指定打印机/// 2 控制打印机动作、执行打印容之前,必须先调用StartPrint,准备向打印机发送控制指令/// 3 调用SetLeft, SetBold, SetAlignMode, SetFontSize ... ...设置打印参数/// 4 PrintText 打印容.注意:打印该行容后会自动换行(本类会在该行容末尾添加一个换行符) /// PrintImageFile 或PrintBitMap打印图片/// 5 控制指令和打印容都发送完毕后,调用EndPrint执行真正打印动作/// 6 退出程序前调用Close///</summary>public class ReceiptHelper{#region指令定义private static Byte[] Const_Init = new byte[] { 0x1B, 0x40,0x20, 0x20, 0x20, 0x0A,0x1B, 0x64,0x10};//设置左边距private const string Const_SetLeft = "1D 4C ";//设置粗体private const string Const_SetBold = "1B 45 ";private const String Const_Bold_YES = "01";private const String Const_Bold_NO = "00";//设置对齐式private const string Const_SetAlign = "1B 61 ";private const String Const_Align_Left = "30";private const String Const_Align_Middle = "31";private const String Const_Align_Right = "32";//设置字体大小,与SetBigFont 不能同时使用private const string Const_SetFontSize = "1D 21 ";//设置是否大字体,等同于SetFontSize = 2//private const String Const_SetBigFontBold = "1B 21 38";//private const String Const_SetBigFontNotBold = "1B 21 30";//private const String Const_SetCancelBigFont = "1B 21 00";///<summary>///打印并走纸///</summary>private static Byte[] Const_Cmd_Print = new byte[] { 0x1B, 0x4A, 0x00 }; //走纸private const string Const_FeedForward = "1B 4A ";private const string Const_FeedBack = "1B 6A ";//切纸private static Byte[] Const_SetCut = new byte[] { 0x1D, 0x56, 0x30};//查询打印机状态private static Byte[] Const_QueryID = new byte[] { 0x1D, 0x67, 0x61};//回复帧以ID 开头private static String Const_ResponseQueryID = "ID";///<summary>///设置图标的指令///</summary>private static Byte[] Const_SetImageCommand = new Byte[] { 0x1B, 0x2A, 0x21 }; #endregion#region常量定义///<summary>///最大字体大小///</summary>public const Int32 Const_MaxFontSize = 8;///<summary>///最大走纸距离///</summary>public const Int32 Const_MaxFeedLength = 5000;///<summary>///最大高宽///</summary>public const Int32 Const_MaxImageLength = 480;///<summary>///每次通信最多打印的行数///</summary>public const Int32 Const_OncePrintRowCount = 24;public const Int32 Const_BrightnessGate = 100;///<summary>///无效句柄///</summary>public const Int32 Const_InvalidHandle = -1;#endregion#region私有成员///<summary>///打印机句柄///</summary>private int m_Handle = -1;///<summary>///是否已经初始化///</summary>private Boolean m_Inited = false;#endregion#region私有函数[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet =CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out Int32 hPrinter, IntPtr pd);[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool StartDocPrinter(Int32 hPrinter, Int32 level, [In,MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool EndDocPrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool StartPagePrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool EndPagePrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool WritePrinter(Int32 hPrinter, Byte[] pBytes, Int32 dwCount, out Int32 dwWritten);[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool ClosePrinter(Int32 hPrinter);///<summary>///发送指令///</summary>///<param name="cmd"></param>///<returns></returns>private Boolean SendCommand(Byte[] cmd){if (m_Handle == Const_InvalidHandle || cmd == null || cmd.Length < 2){return false;}int writelen = 0;Boolean bl = WritePrinter(m_Handle, cmd, cmd.Length, out writelen);if (!bl) return false;return (writelen >= cmd.Length);}///<summary>///发送文本格式的指令///</summary>///<param name="cmd"></param>///<returns></returns>private Boolean SendCommand(String hexstrcmd){if (m_Handle == Const_InvalidHandle || hexstrcmd == null || hexstrcmd.Length < 4) {return false;}byte[] mybyte = null;Boolean bl = DataFormatProcessor.HexStringToBytes(hexstrcmd, out mybyte);bl = SendCommand(mybyte);return bl;}#endregion#region部处理- 打印图片///<summary>///把图片转换为指令字节,图片最大高宽不能超过480///</summary>///<param name="image"></param>///<param name="bmpbytes"></param>///<returns></returns>public static Boolean LoadImage(Bitmap image,ref Byte[] bitarray,ref Int32 datawidth,ref Int32 dataheight){Int32 newwidth = 0;Int32 newheight = 0;Bitmap destimage = image;Boolean bl = false;//如果高度超过围,或宽度超过围,需要进行缩小if (image.Width > Const_MaxImageLength || image.Height > Const_MaxImageLength) {//按照高度和宽度,较大的那一边,进行缩放if (image.Width > image.Height){newwidth = Const_MaxImageLength;newheight = (Int32)(image.Height * newwidth / (float)image.Width);}else{newheight = Const_MaxImageLength;newwidth = (Int32)(newheight * image.Width / (float)image.Height);}bl = ImageProcessor.ResizeImage(image, newwidth, newheight, ref destimage);}//把数据转换为字节数组bl = GetBitArray(image, ref bitarray, ref datawidth, ref dataheight);return bl;}///<summary>///把图片转换为指令字节,图片最大高宽不能超过480///如果图片的高度不是24的整数倍,则修改为24的整数倍///</summary>///<param name="image"></param>///<param name="bmpbytes"></param>///<returns></returns>public static Boolean LoadImageFromFile(String imagefilename, ref Byte[] bmpbytes, ref Int32 width, ref Int32 height){Bitmap img = ImageProcessor.LoadBitImage(imagefilename);if (img == null){return false;}Boolean bl = LoadImage(img, ref bmpbytes, ref width, ref height);return bl;}///<summary>///把图片转换为位图数组,每个字节的每个比特位,对应当前像素是否需要打印///</summary>///<param name="img"></param>///<param name="allbitary"></param>///<returns></returns>public static Boolean GetBitArray(Bitmap img,ref Byte[] allbitary, ref Int32 width, ref Int32 height){if (img == null){return false;}//ESC指令格式规定://1 打印图片时,每条指令最多只打印24行;不足24行的,也要用全0填充满数据字节//2 打印24行数据时,按照光栅模式纵向获取数据// 即先获取所有x=0的点(第0列)转换为3个字节;// 再获取所有x=1的点转换为3个字节;...直到获取到最右侧一列的点//3 打印完当前24行数据后,再获取后续24行的数据容,直到所有的数据获取完毕//获取亮度数组Boolean[] briary = null;Boolean bl = ImageProcessor.ToBooleanArray(img, Const_BrightnessGate, ref briary);if (!bl){return false;}height = img.Height;//如果图像高度不是24整数倍,设置为24的整数倍if (height % Const_OncePrintRowCount != 0){height = height + Const_OncePrintRowCount - height % Const_OncePrintRowCount;}width = img.Width;//如果图像宽度不是8的整数倍,设置为8的整数倍if (width % 8 != 0){width = width + 8 - width % 8;}Int32 bytelen = height * width / 8;//每个像素对应1个比特位,因此总字节数=像素位数/8 allbitary = new Byte[bytelen];Int32 byteidxInCol = 0;//当前列里首个像素,在目标字节数组里的下标Int32 byteidx = 0;//当前像素在目标数组里的字节下标Int32 bitidx = 0;//当前像素在目标数组里当前字节里的比特位下标Int32 pixidxInCol = 0;//当前像素在当前列里的第几个位置Int32 pixidx = 0;//当前像素在原始图片里的下标Int32 rowidx = 0; //当前处理的像素点所在行,不能超过图像高度Int32 curprocrows = 0;//当前需要处理的行数量while (rowidx < height){//按照纵向次序,把当前列的24个数据,转换为3个字节for (Int32 colidx = 0; colidx < img.Width; ++colidx){//如果当前还剩余超过24行没处理,处理24行if (rowidx + Const_OncePrintRowCount <= img.Height){curprocrows = Const_OncePrintRowCount;}else{//已经不足24行,只处理剩余行数curprocrows = img.Height - rowidx;}pixidxInCol = 0; //本列里从像素0开始处理for (Int32 y = rowidx; y < rowidx + curprocrows; ++y){//原始图片里像素位置pixidx = y * img.Width + colidx;//获取当前像素的亮度值.如果当前像素是黑点,需要把数组里的对应比特位设置为1if (briary[pixidx]){bitidx = 7 - pixidxInCol % 8;//最高比特位对应首个像素.最低比特位对应末个像素byteidx = byteidxInCol + pixidxInCol / 8; //由于最后一段可能不足24行,因此不能使用byteidx++DataFormatProcessor.SetBitValue(bitidx, true, ref allbitary[byteidx]);}pixidxInCol++;}byteidxInCol += 3;//每列固定24个像素,3个字节}rowidx += Const_OncePrintRowCount;}return true;}#endregion#region公开函数private static ReceiptHelper m_instance = new ReceiptHelper();///<summary>///当前使用的打印机名称///</summary>public String PrinterName{get;private set;}///<summary>///单件模式///</summary>///<returns></returns>public static ReceiptHelper GetInstance(){return m_instance;}///<summary>///获取本机安装的所有打印机///</summary>///<returns></returns>public static List<String> GetPrinterList(){List<String> ret = new List<String>();if (PrinterSettings.InstalledPrinters.Count < 1){return ret;}foreach (String printername in PrinterSettings.InstalledPrinters){ret.Add(printername);}return ret;}///<summary>///打开打印机///</summary>///<param name="printername"></param>///<returns></returns>public Boolean Open(String printername){if (m_Inited){return true;}Boolean bl = OpenPrinter(printername.Normalize(), out m_Handle, IntPtr.Zero);m_Inited = (bl && m_Handle != 0);return true;}///<summary>///开始打印,在打印之前必须调用此函数///</summary>///<returns></returns>public Boolean StartPrint(){if (!m_Inited){return false;}DOCINFOA di = new DOCINFOA();di.pDocName = "My C#.NET RAW Document";di.pDataType = "RAW";//Start a document.Boolean bl = StartDocPrinter(m_Handle, 1, di);if (!bl){return false;}// Start a page.bl = StartPagePrinter(m_Handle);return bl;}///<summary>///结束打印,在打印结束之后必须调用此函数///</summary>///<returns></returns>public Boolean EndPrint(){if (!m_Inited){return false;}Boolean bl = EndPagePrinter(m_Handle);bl = EndDocPrinter(m_Handle);return bl;}///<summary>///销毁///</summary>///<returns></returns>public Boolean Close(){if (!m_Inited){return true;}m_Inited = false;//关闭设备句柄ClosePrinter(m_Handle);m_Handle = -1;return true;}///<summary>///打印文本.在调用本函数之前必须先调用正确的设置字体、左边距///</summary>///<param name="content"></param>///<returns></returns>public Boolean PrintText(String content){if (!m_Inited){return false;}byte[] bytes = null;if (content.Length < 1){content = " ";}if (content[content.Length - 1] != (char)0x0D &&content[content.Length - 1] != (char)0x0A){content = content + (char)0x0A;}bytes = DataFormatProcessor.StringToBytes(content);bool bl = SendCommand(bytes);return bl;}///<summary>///设置对齐式///</summary>///<param name="left"></param>///<returns></returns>public bool SetAlignMode(eTextAlignMode alignmode){if (!m_Inited){return false;}String code = String.Empty;switch (alignmode){case eTextAlignMode.Left:code = Const_Align_Left;break;case eTextAlignMode.Middle:code = Const_Align_Middle;break;case eTextAlignMode.Right:code = Const_Align_Right;break;default:code = Const_Align_Left;break;}//注意:先低字节后高字节string str = Const_SetAlign + code;bool bl = SendCommand(str);return bl;}///<summary>///设置左边距///</summary>///<param name="left"></param>///<returns></returns>public bool SetLeft(int left){if (!m_Inited){return false;}//注意:先低字节后高字节String hexstr = left.ToString("X4");string str = Const_SetLeft + hexstr.Substring(2, 2) + hexstr.Substring(0, 2);bool bl = SendCommand(str);return bl;}///<summary>///设置粗体///</summary>///<param name="bold"></param>///<returns></returns>public Boolean SetBold(Boolean bold){if (!m_Inited){return false;}//注意:先低字节后高字节String str = String.Empty;if (bold){str = Const_SetBold + Const_Bold_YES;}else{str = Const_SetBold + Const_Bold_NO;}bool bl = SendCommand(str);return bl;}///<summary>///切纸///</summary>///<returns></returns>public bool Cut(){if (!m_Inited){return false;}bool bl = SendCommand(Const_SetCut);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintImageFile(String imgfilename){if (!m_Inited){return false;}Bitmap img = ImageProcessor.LoadBitImage(imgfilename);if (img == null){return false;}Boolean bl = PrintBitmap(img);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintBitmap(Bitmap bitmap){if (!m_Inited){return false;}if (bitmap == null ||bitmap.Width > Const_MaxImageLength ||bitmap.Height > Const_MaxImageLength){return false;}Byte[] bitary = null;Int32 width = 0;Int32 height = 0;Boolean bl = GetBitArray(bitmap, ref bitary, ref width, ref height);bl = PrintBitmapBytes(bitary, bitmap.Width, bitmap.Height);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintBitmapBytes(Byte[] imgbitarray, Int32 width, Int32 height){if (!m_Inited){return false;}Int32 bytes = width * height / 8;//检查是否尺寸符合要求if (width > Const_MaxImageLength || height > Const_MaxFeedLength || width < 1 || height < 1 ||imgbitarray == null){return false;}//每次获取24行的数据进行发送,这24行的字节数Int32 blockbytes = width * Const_OncePrintRowCount / 8;if (blockbytes < 1){return false;}Boolean bl = false;//一共需要发送的块数量Int32 blocks = imgbitarray.Length / blockbytes;//每次发送的数据字节数= 1B 2A 21 2字节长度+ 数据容Byte[] cmdbytes = new Byte[5 + blockbytes];//指令Array.Copy(Const_SetImageCommand, cmdbytes, 3);//数据长度,即每行的点数DataFormatProcessor.Int16ToBytes(width, ref cmdbytes, 3);//数据容for (Int32 blockidx = 0; blockidx < blocks; ++blockidx){Array.Copy(imgbitarray, blockidx * blockbytes, cmdbytes, 5, blockbytes);//发送当前指令bl = SendCommand(cmdbytes);if (!bl) return false;//休眠20毫秒Thread.Sleep(20);//发送打印指令bl = SendCommand(Const_Cmd_Print);if (!bl) return false;}return bl;}///<summary>///走纸///</summary>///<param name="length"></param>///<returns></returns>public bool Feed(int length){if (!m_Inited){return false;}if (length < 1)length = 1;if (length > Const_MaxFeedLength){length = Const_MaxFeedLength;}string len = length.ToString("X2");len = Const_FeedForward + len;bool bl = SendCommand(len);return bl;}///<summary>///回退走纸///</summary>///<param name="length"></param>///<returns></returns>public bool FeedBack(int length){if (!m_Inited){return false;}if (length < 1)length = 1;if (length > Const_MaxFeedLength){length = Const_MaxFeedLength;}string len = length.ToString("X2");len = Const_FeedBack + len;bool bl = SendCommand(len);return bl;}///<summary>///设置字体大小.本函数不可与SetBigFont同时使用///</summary>///<param name="sizerate">大小倍率,取值围1 - 8</param>///<returns></returns>public bool SetFontSize(Int32 sizerate){if (!m_Inited){return false;}if (sizerate < 1){sizerate = 1;}if (sizerate > Const_MaxFontSize){sizerate = Const_MaxFontSize;}sizerate--;String sizecodestr = Const_SetFontSize + sizerate.ToString("X1") + sizerate.ToString("X1");bool bl = SendCommand(sizecodestr);return bl;}#endregion}}3.图像处理ImageProcessorusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Drawing;using monBLL;using System.Drawing.Imaging;using System.IO;using System.Drawing.Drawing2D;using System.Windows.Forms;using AForge.Imaging.Filters;namespace LaisonTech.MediaLib{///<summary>///图片格式///</summary>public enum ePictureFileFormat{Bmp = 0,Gif = 1,Icon = 2,Jpeg = 3,Png = 4,}///<summary>///转为灰度图像的式///</summary>public enum eGrayMode{///<summary>///算数平均///</summary>ArithmeticAverage = 0,///<summary>///加权平均///</summary>WeightedAverage = 1,}///<summary>///比较2个图片的指定区域围,像素的相同类型///</summary>public enum eAreaDifferentType{///<summary>///所有像素都相同///</summary>AllSame = 0,///<summary>///所有像素都不同///</summary>AllDifferent = 1,///<summary>///部分相同部分不同///</summary>Partial = 2,}///<summary>///图片文件处理///</summary>public class ImageProcessor{#region常量定义public const Byte Const_BrightnessWhite = 255;public const Byte Const_BrightnessBlack = 0;///<summary>///比较结果的图片里,亮度相同部分的填充颜色///</summary>public static Color Const_SameBrightnessColor = Color.Black;///<summary>///比较结果的图片里,亮度相同部分的填充颜色///</summary>public static Color Const_DifferentBrightnessColor = Color.White;public const Byte Const_BlackBrightness = 0;public const Byte Const_WhiteBrightness = 255;public const Int32 Const_MaxBrightness = 255;public const Int32 Const_MinBrightness = -255;///<summary>///亮度的中间值///</summary>public const Int32 Const_MiddleBrightness = 128;#endregion#region屏幕截图,打印///<summary>///获取屏幕分辨率///</summary>///<param name="width"></param>///<param name="height"></param>public static void GetScreenSize(ref Int32 width, ref Int32 height) {height = Screen.PrimaryScreen.Bounds.Height;width = Screen.PrimaryScreen.Bounds.Width;}///<summary>///截图指定控件上显示的容///</summary>///<param name="ctrl"></param>///<returns></returns>public static Image CaptureControlImage(Control ctrl){if (ctrl == null){return null;}Control parent = ctrl;if (ctrl.Parent != null){parent = ctrl.Parent;}Point screenPoint = parent.PointToScreen(ctrl.Location);Image ret = new Bitmap(ctrl.Width, ctrl.Height);Graphics g = Graphics.FromImage(ret);g.CopyFromScreen(screenPoint.X, screenPoint.Y,0, 0, ctrl.Size);g.DrawImage(ret, 0, 0);return ret;}#endregion#region装载图片///<summary>///装载图像文件///</summary>///<param name="filename"></param>///<returns></returns>public static Image LoadImage(String filename){//Boolean bl = FileProcessor.FileExist(filename);//if (!bl)//{// return null;//}//Bitmap image = (Bitmap)Bitmap.FromFile(filename);//return image;//以上法会导致图片文件被锁定,无法删除移动Byte[] photodata = null;Boolean bl = FileProcessor.FileExist(filename);if (!bl){return null;}bl = FileProcessor.ReadFileBytes(filename, out photodata);if (!bl){return null;}MemoryStream ms = null;Image myImage = null;try{ms = new MemoryStream(photodata);myImage = Bitmap.FromStream(ms);ms.Close();}catch (System.Exception ex){Console.WriteLine("LoadImage error:" + ex.Message);myImage = null;}return myImage;}///<summary>///装载图像文件///</summary>///<param name="filename"></param>///<returns></returns>public static Bitmap LoadBitImage(String filename){Bitmap ret = (Bitmap)LoadImage(filename);return ret;}///<summary>///保存图片到指定路径///</summary>///<param name="img"></param>///<param name="filename"></param>///<returns></returns>public static Boolean SaveImage(Image img, String filename) {FileProcessor.DeleteFile(filename);if (img == null){return false;}//获取保存图片的路径,如果路径不存在,新建String folder = FileProcessor.GetDirectoryName(filename);if (!FileProcessor.DirectoryExist(folder)){FileProcessor.CreateDirectory(folder);}img.Save(filename);Boolean bl = FileProcessor.FileExist(filename);return bl;}#endregion#region转换图片格式///<summary>///转换图片格式///</summary>///<param name="bmpfilename"></param>///<param name="jpgfilename"></param>///<returns></returns>public static Boolean BmpToJpg(String bmpfilename, String jpgfilename){Boolean bl = ChangeFileFormat(bmpfilename, jpgfilename, ePictureFileFormat.Jpeg);return bl;}///<summary>///转换图片格式///</summary>///<param name="srcfilename"></param>///<param name="destfilename"></param>///<param name="destformat"></param>///<returns></returns>public static Boolean ChangeFileFormat(String srcfilename, String destfilename, ePictureFileFormat destformat){Boolean bl = FileProcessor.FileExist(srcfilename);if (!bl){return false;}Image image = Image.FromFile(srcfilename);ImageFormat IFMT = null;switch (destformat){case ePictureFileFormat.Bmp:IFMT = ImageFormat.Bmp;break;case ePictureFileFormat.Gif:IFMT = ImageFormat.Gif;break;case ePictureFileFormat.Icon:IFMT = ImageFormat.Icon;break;case ePictureFileFormat.Jpeg:IFMT = ImageFormat.Jpeg;break;case ePictureFileFormat.Png:IFMT = ImageFormat.Png;break;default:IFMT = ImageFormat.Jpeg;break;}image.Save(destfilename, IFMT);image.Dispose();bl = FileProcessor.FileExist(destfilename);if (!bl){return false;}Int32 filelen = FileProcessor.GetFileLength(destfilename);return (filelen > 0);}///<summary>///变成黑白图///</summary>///<param name="srcbitmap">原始图</param>///<param name="mode">模式。

C#使用ESC指令控制POS机打印小票

C#使用ESC指令控制POS机打印小票

C#使用ESC指令控制POS打印机打印小票1.前言C#打印小票可以与普通打印机一样,调用PrintDocument实现。

也可以发送标注你的ESC指令实现。

由于调用PrintDocument类时,无法操作使用串口或TCP/IP 接口连接的pos打印机,并且无法发送控制指令实现pos打印机的切纸、走纸等动作。

因此个人建议使用ESC指令进行打印会更通用。

本类需要调用ImageProcessor.cs2.POS机打印小票ReceiptHelperusing System;usingusing System.Text;usingusing System.Threading;using System.Drawing;using System.Management;using System.IO;using LaisonTech.MediaLib;using monBLL;usingnamespace LaisonTech.MediaLib{#region结构体定义[StructLayout(LayoutKind.Sequential)]public struct OVERLAPPED{int Internal;int InternalHigh;int Offset;int OffSetHigh;int hEvent;};[StructLayout(LayoutKind.Sequential)]public struct PRINTER_DEFAULTS{public int pDatatype;public int pDevMode;public int DesiredAccess;}///<summary>///对齐方式///</summary>public enum eTextAlignMode{Left = 0,Middle = 1,Right = 2}#endregion///<summary>///小票打印类/// 使用方法:/// 1 GetPrinterList获取已经安装的所有打印机列表./// Open 打开指定打印机/// 2 控制打印机动作、执行打印内容之前,必须先调用StartPrint,准备向打印机发送控制指令/// 3 调用SetLeft, SetBold, SetAlignMode, SetFontSize ... ...设置打印参数/// 4 PrintText 打印内容.注意:打印该行内容后会自动换行(本类会在该行内容末尾添加一个换行符)/// PrintImageFile 或PrintBitMap打印图片/// 5 控制指令和打印内容都发送完毕后,调用EndPrint执行真正打印动作/// 6 退出程序前调用Close///</summary>public class ReceiptHelper{#region指令定义private static Byte[] Const_Init = new byte[] { 0x1B, 0x40,0x20, 0x20, 0x20, 0x0A,0x1B, 0x64,0x10};//设置左边距private const string Const_SetLeft = "1D 4C ";//设置粗体private const string Const_SetBold = "1B 45 ";private const String Const_Bold_YES = "01";private const String Const_Bold_NO = "00";//设置对齐方式private const string Const_SetAlign = "1B 61 ";private const String Const_Align_Left = "30";private const String Const_Align_Middle = "31";private const String Const_Align_Right = "32";//设置字体大小,与SetBigFont 不能同时使用private const string Const_SetFontSize = "1D 21 ";//设置是否大字体,等同于SetFontSize = 2//private const String Const_SetBigFontBold = "1B 21 38";//private const String Const_SetBigFontNotBold = "1B 21 30";//private const String Const_SetCancelBigFont = "1B 21 00";///<summary>///打印并走纸///</summary>private static Byte[] Const_Cmd_Print = new byte[] { 0x1B, 0x4A, 0x00 };//走纸private const string Const_FeedForward = "1B 4A ";private const string Const_FeedBack = "1B 6A ";//切纸private static Byte[] Const_SetCut = new byte[] { 0x1D, 0x56, 0x30};//查询打印机状态private static Byte[] Const_QueryID = new byte[] { 0x1D, 0x67, 0x61};//回复帧以ID 开头private static String Const_ResponseQueryID = "ID";///<summary>///设置图标的指令///</summary>private static Byte[] Const_SetImageCommand = new Byte[] { 0x1B, 0x2A, 0x21 };#endregion#region常量定义///<summary>///最大字体大小///</summary>public const Int32 Const_MaxFontSize = 8;///<summary>///最大走纸距离///</summary>public const Int32 Const_MaxFeedLength = 5000;///<summary>///最大高宽///</summary>public const Int32 Const_MaxImageLength = 480;///<summary>///每次通信最多打印的行数///</summary>public const Int32 Const_OncePrintRowCount = 24;public const Int32 Const_BrightnessGate = 100;///<summary>///无效句柄///</summary>public const Int32 Const_InvalidHandle = -1;#endregion#region私有成员///<summary>///打印机句柄///</summary>private int m_Handle = -1;///<summary>///是否已经初始化///</summary>private Boolean m_Inited = false;#endregion#region私有函数[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter,out Int32 hPrinter, IntPtr pd);[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool StartDocPrinter(Int32 hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool EndDocPrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool StartPagePrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool EndPagePrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool WritePrinter(Int32 hPrinter, Byte[] pBytes, Int32 dwCount, out Int32 dwWritten);[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool ClosePrinter(Int32 hPrinter);///<summary>///发送指令///</summary>///<param name="cmd"></param>///<returns></returns>private Boolean SendCommand(Byte[] cmd){if (m_Handle == Const_InvalidHandle || cmd == null || cmd.Length < 2){return false;}int writelen = 0;Boolean bl = WritePrinter(m_Handle, cmd, cmd.Length, out writelen);if (!bl) return false;return (writelen >= cmd.Length);}///<summary>///发送文本格式的指令///</summary>///<param name="cmd"></param>///<returns></returns>private Boolean SendCommand(String hexstrcmd){if (m_Handle == Const_InvalidHandle || hexstrcmd == null || hexstrcmd.Length < 4){return false;}byte[] mybyte = null;Boolean bl = DataFormatProcessor.HexStringToBytes(hexstrcmd, out mybyte);bl = SendCommand(mybyte);return bl;}#endregion#region内部处理- 打印图片///<summary>///把图片转换为指令字节,图片最大高宽不能超过480///</summary>///<param name="image"></param>///<param name="bmpbytes"></param>///<returns></returns>public static Boolean LoadImage(Bitmap image,ref Byte[] bitarray,ref Int32 datawidth,ref Int32 dataheight){Int32 newwidth = 0;Int32 newheight = 0;Bitmap destimage = image;Boolean bl = false;//如果高度超过范围,或宽度超过范围,需要进行缩小if (image.Width > Const_MaxImageLength || image.Height >Const_MaxImageLength){//按照高度和宽度,较大的那一边,进行缩放if (image.Width > image.Height){newwidth = Const_MaxImageLength;newheight = (Int32)(image.Height * newwidth /(float)image.Width);}else{newheight = Const_MaxImageLength;newwidth = (Int32)(newheight * image.Width /(float)image.Height);}bl = ImageProcessor.ResizeImage(image, newwidth, newheight, ref destimage);}//把数据转换为字节数组bl = GetBitArray(image, ref bitarray, ref datawidth, ref dataheight);return bl;}///<summary>///把图片转换为指令字节,图片最大高宽不能超过480///如果图片的高度不是24的整数倍,则修改为24的整数倍///</summary>///<param name="image"></param>///<param name="bmpbytes"></param>///<returns></returns>public static Boolean LoadImageFromFile(String imagefilename, ref Byte[] bmpbytes,ref Int32 width, ref Int32 height){Bitmap img = ImageProcessor.LoadBitImage(imagefilename);if (img == null){return false;}Boolean bl = LoadImage(img, ref bmpbytes, ref width, ref height);return bl;}///<summary>///把图片转换为位图数组,每个字节的每个比特位,对应当前像素是否需要打印///</summary>///<param name="img"></param>///<param name="allbitary"></param>///<returns></returns>public static Boolean GetBitArray(Bitmap img,ref Byte[] allbitary, ref Int32 width, ref Int32 height){if (img == null){return false;}//ESC指令格式规定://1 打印图片时,每条指令最多只打印24行;不足24行的,也要用全0填充满数据字节//2 打印24行数据时,按照光栅模式纵向获取数据// 即先获取所有x=0的点(第0列)转换为3个字节;// 再获取所有x=1的点转换为3个字节;...直到获取到最右侧一列的点//3 打印完当前24行数据后,再获取后续24行的数据内容,直到所有的数据获取完毕//获取亮度数组Boolean[] briary = null;Boolean bl = ImageProcessor.ToBooleanArray(img,Const_BrightnessGate, ref briary);if (!bl){return false;}height = img.Height;//如果图像高度不是24整数倍,设置为24的整数倍if (height % Const_OncePrintRowCount != 0){height = height + Const_OncePrintRowCount - height %Const_OncePrintRowCount;}width = img.Width;//如果图像宽度不是8的整数倍,设置为8的整数倍if (width % 8 != 0){width = width + 8 - width % 8;}Int32 bytelen = height * width / 8;//每个像素对应1个比特位,因此总字节数=像素位数/8allbitary = new Byte[bytelen];Int32 byteidxInCol = 0;//当前列里首个像素,在目标字节数组里的下标Int32 byteidx = 0;//当前像素在目标数组里的字节下标Int32 bitidx = 0;//当前像素在目标数组里当前字节里的比特位下标Int32 pixidxInCol = 0;//当前像素在当前列里的第几个位置Int32 pixidx = 0;//当前像素在原始图片里的下标Int32 rowidx = 0; //当前处理的像素点所在行,不能超过图像高度Int32 curprocrows = 0;//当前需要处理的行数量while (rowidx < height){//按照纵向次序,把当前列的24个数据,转换为3个字节for (Int32 colidx = 0; colidx < img.Width; ++colidx){//如果当前还剩余超过24行没处理,处理24行if (rowidx + Const_OncePrintRowCount <= img.Height){curprocrows = Const_OncePrintRowCount;}else{//已经不足24行,只处理剩余行数curprocrows = img.Height - rowidx;}pixidxInCol = 0; //本列里从像素0开始处理for (Int32 y = rowidx; y < rowidx + curprocrows; ++y){//原始图片里像素位置pixidx = y * img.Width + colidx;//获取当前像素的亮度值.如果当前像素是黑点,需要把数组里的对应比特位设置为1if (briary[pixidx]){bitidx = 7 - pixidxInCol % 8;//最高比特位对应首个像素.最低比特位对应末个像素byteidx = byteidxInCol + pixidxInCol / 8; //由于最后一段可能不足24行,因此不能使用byteidx++DataFormatProcessor.SetBitValue(bitidx, true, ref allbitary[byteidx]);}pixidxInCol++;}byteidxInCol += 3;//每列固定24个像素,3个字节}rowidx += Const_OncePrintRowCount;}return true;}#endregion#region公开函数private static ReceiptHelper m_instance = new ReceiptHelper();///<summary>///当前使用的打印机名称///</summary>public String PrinterName{get;private set;}///<summary>///单件模式///</summary>///<returns></returns>public static ReceiptHelper GetInstance(){return m_instance;}///<summary>///获取本机安装的所有打印机///</summary>///<returns></returns>public static List<String> GetPrinterList(){List<String> ret = new List<String>();if (PrinterSettings.InstalledPrinters.Count < 1){return ret;}foreach (String printername in PrinterSettings.InstalledPrinters){ret.Add(printername);}return ret;}///<summary>///打开打印机///</summary>///<param name="printername"></param>///<returns></returns>public Boolean Open(String printername){if (m_Inited){return true;}Boolean bl = OpenPrinter(printername.Normalize(), out m_Handle, IntPtr.Zero);m_Inited = (bl && m_Handle != 0);return true;}///<summary>///开始打印,在打印之前必须调用此函数///</summary>///<returns></returns>public Boolean StartPrint(){if (!m_Inited){return false;}DOCINFOA di = new DOCINFOA();di.pDocName = "My C#.NET RAW Document";di.pDataType = "RAW";//Start a document.Boolean bl = StartDocPrinter(m_Handle, 1, di);if (!bl){return false;}// Start a page.bl = StartPagePrinter(m_Handle);return bl;}///<summary>///结束打印,在打印结束之后必须调用此函数///</summary>///<returns></returns>public Boolean EndPrint(){if (!m_Inited){return false;}Boolean bl = EndPagePrinter(m_Handle);bl = EndDocPrinter(m_Handle);return bl;}///<summary>///销毁///</summary>///<returns></returns>public Boolean Close(){return true;}m_Inited = false;//关闭设备句柄ClosePrinter(m_Handle);m_Handle = -1;return true;}///<summary>///打印文本.在调用本函数之前必须先调用正确的设置字体、左边距///</summary>///<param name="content"></param>///<returns></returns>public Boolean PrintText(String content){if (!m_Inited){return false;}byte[] bytes = null;if (content.Length < 1){content = " ";}if (content[content.Length - 1] != (char)0x0D &&content[content.Length - 1] != (char)0x0A){content = content + (char)0x0A;}bytes = DataFormatProcessor.StringToBytes(content);bool bl = SendCommand(bytes);return bl;}///<summary>///设置对齐方式///</summary>///<param name="left"></param>///<returns></returns>public bool SetAlignMode(eTextAlignMode alignmode){return false;}String code = String.Empty;switch (alignmode){case eTextAlignMode.Left:code = Const_Align_Left;break;case eTextAlignMode.Middle:code = Const_Align_Middle;break;case eTextAlignMode.Right:code = Const_Align_Right;break;default:code = Const_Align_Left;break;}//注意:先低字节后高字节string str = Const_SetAlign + code;bool bl = SendCommand(str);return bl;}///<summary>///设置左边距///</summary>///<param name="left"></param>///<returns></returns>public bool SetLeft(int left){if (!m_Inited){return false;}//注意:先低字节后高字节String hexstr = left.ToString("X4");string str = Const_SetLeft + hexstr.Substring(2, 2) + hexstr.Substring(0, 2);bool bl = SendCommand(str);return bl;///<summary>///设置粗体///</summary>///<param name="bold"></param>///<returns></returns>public Boolean SetBold(Boolean bold){if (!m_Inited){return false;}//注意:先低字节后高字节String str = String.Empty;if (bold){str = Const_SetBold + Const_Bold_YES;}else{str = Const_SetBold + Const_Bold_NO;}bool bl = SendCommand(str);return bl;}///<summary>///切纸///</summary>///<returns></returns>public bool Cut(){if (!m_Inited){return false;}bool bl = SendCommand(Const_SetCut);return bl;}///<summary>///打印图片///</summary>public bool PrintImageFile(String imgfilename){if (!m_Inited){return false;}Bitmap img = ImageProcessor.LoadBitImage(imgfilename);if (img == null){return false;}Boolean bl = PrintBitmap(img);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintBitmap(Bitmap bitmap){if (!m_Inited){return false;}if (bitmap == null ||bitmap.Width > Const_MaxImageLength ||bitmap.Height > Const_MaxImageLength){return false;}Byte[] bitary = null;Int32 width = 0;Int32 height = 0;Boolean bl = GetBitArray(bitmap, ref bitary, ref width, ref height);bl = PrintBitmapBytes(bitary, bitmap.Width, bitmap.Height);return bl;}///<summary>///打印图片///</summary>public bool PrintBitmapBytes(Byte[] imgbitarray, Int32 width, Int32 height){if (!m_Inited){return false;}Int32 bytes = width * height / 8;//检查是否尺寸符合要求if (width > Const_MaxImageLength || height > Const_MaxFeedLength || width < 1 || height < 1 ||imgbitarray == null){return false;}//每次获取24行的数据进行发送,这24行的字节数Int32 blockbytes = width * Const_OncePrintRowCount / 8;if (blockbytes < 1){return false;}Boolean bl = false;//一共需要发送的块数量Int32 blocks = imgbitarray.Length / blockbytes;//每次发送的数据字节数= 1B 2A 21 2字节长度+数据内容Byte[] cmdbytes = new Byte[5 + blockbytes];//指令Array.Copy(Const_SetImageCommand, cmdbytes, 3);//数据长度,即每行的点数DataFormatProcessor.Int16ToBytes(width, ref cmdbytes, 3);//数据内容for (Int32 blockidx = 0; blockidx < blocks; ++blockidx){Array.Copy(imgbitarray, blockidx * blockbytes, cmdbytes, 5, blockbytes);//发送当前指令bl = SendCommand(cmdbytes);if (!bl) return false;//休眠20毫秒Thread.Sleep(20);//发送打印指令bl = SendCommand(Const_Cmd_Print);if (!bl) return false;}return bl;}///<summary>///走纸///</summary>///<param name="length"></param>///<returns></returns>public bool Feed(int length){if (!m_Inited){return false;}if (length < 1)length = 1;if (length > Const_MaxFeedLength){length = Const_MaxFeedLength;}string len = length.ToString("X2");len = Const_FeedForward + len;bool bl = SendCommand(len);return bl;}///<summary>///回退走纸///</summary>///<param name="length"></param>///<returns></returns>public bool FeedBack(int length){if (!m_Inited){return false;}if (length < 1)length = 1;if (length > Const_MaxFeedLength){length = Const_MaxFeedLength;}string len = length.ToString("X2");len = Const_FeedBack + len;bool bl = SendCommand(len);return bl;}///<summary>///设置字体大小.本函数不可与SetBigFont同时使用///</summary>///<param name="sizerate">大小倍率,取值范围1 - 8</param>///<returns></returns>public bool SetFontSize(Int32 sizerate){if (!m_Inited){return false;}if (sizerate < 1){sizerate = 1;}if (sizerate > Const_MaxFontSize){sizerate = Const_MaxFontSize;}sizerate--;String sizecodestr = Const_SetFontSize + sizerate.ToString("X1") + sizerate.ToString("X1");bool bl = SendCommand(sizecodestr);return bl;}#endregion}}3.图像处理 ImageProcessor using System;usingusing System.Linq;using System.Text;using System.Drawing;using monBLL;usingusing System.IO;usingusingusingnamespace LaisonTech.MediaLib{///<summary>///图片格式///</summary>public enum ePictureFileFormat{Bmp = 0,Gif = 1,Icon = 2,Jpeg = 3,Png = 4,}///<summary>///转为灰度图像的方式///</summary>public enum eGrayMode{///<summary>///算数平均///</summary>ArithmeticAverage = 0,///<summary>///加权平均///</summary>WeightedAverage = 1,}///<summary>///比较2个图片的指定区域范围,像素的相同类型///</summary>public enum eAreaDifferentType{///<summary>///所有像素都相同///</summary>AllSame = 0,///<summary>///所有像素都不同///</summary>AllDifferent = 1,///<summary>///部分相同部分不同///</summary>Partial = 2,}///<summary>///图片文件处理///</summary>public class ImageProcessor{#region常量定义public const Byte Const_BrightnessWhite = 255;public const Byte Const_BrightnessBlack = 0;///<summary>///比较结果的图片里,亮度相同部分的填充颜色///</summary>public static Color Const_SameBrightnessColor = Color.Black;///<summary>///比较结果的图片里,亮度相同部分的填充颜色///</summary>public static Color Const_DifferentBrightnessColor = Color.White;public const Byte Const_BlackBrightness = 0;public const Byte Const_WhiteBrightness = 255;public const Int32 Const_MaxBrightness = 255;public const Int32 Const_MinBrightness = -255;///<summary>///亮度的中间值///</summary>public const Int32 Const_MiddleBrightness = 128;#endregion#region屏幕截图,打印///<summary>///获取屏幕分辨率///</summary>///<param name="width"></param>///<param name="height"></param>public static void GetScreenSize(ref Int32 width, ref Int32 height) {height = Screenwidth = Screen}///<summary>///截图指定控件上显示的内容///</summary>///<param name="ctrl"></param>///<returns></returns>public static Image CaptureControlImage(Control ctrl){if (ctrl == null){return null;}Control parent = ctrl;if (ctrl.Parent != null){parent = ctrl.Parent;}Point screenPoint = parent.PointToScreen(ctrl.Location);Image ret = new Bitmap(ctrl.Width, ctrl.Height);Graphics g = Graphics.FromImage(ret);g.CopyFromScreen(screenPoint.X, screenPoint.Y,0, 0, ctrl.Size);g.DrawImage(ret, 0, 0);return ret;}#endregion#region装载图片///<summary>///装载图像文件///</summary>///<param name="filename"></param>///<returns></returns>public static Image LoadImage(String filename){//Boolean bl = FileProcessor.FileExist(filename);//if (!bl)//{// return null;//}//Bitmap image = (Bitmap)Bitmap.FromFile(filename);//return image;//以上方法会导致图片文件被锁定,无法删除移动Byte[] photodata = null;Boolean bl = FileProcessor.FileExist(filename);if (!bl){return null;}bl = FileProcessor.ReadFileBytes(filename, out photodata);if (!bl){return null;}MemoryStream ms = null;Image myImage = null;try{ms = new MemoryStream(photodata);myImage = Bitmap.FromStream(ms);ms.Close();}catch (System.Exception ex){Console.WriteLine("LoadImage error:" + ex.Message);myImage = null;}return myImage;}///<summary>///装载图像文件///</summary>///<param name="filename"></param>///<returns></returns>public static Bitmap LoadBitImage(String filename){Bitmap ret = (Bitmap)LoadImage(filename);return ret;}///<summary>///保存图片到指定路径///</summary>///<param name="img"></param>///<param name="filename"></param>///<returns></returns>public static Boolean SaveImage(Image img, String filename) {FileProcessor.DeleteFile(filename);if (img == null){return false;}//获取保存图片的路径,如果路径不存在,新建String folder = FileProcessor.GetDirectoryName(filename);if (!FileProcessor.DirectoryExist(folder)){FileProcessor.CreateDirectory(folder);}img.Save(filename);Boolean bl = FileProcessor.FileExist(filename);return bl;}#endregion#region转换图片格式///<summary>///转换图片格式///</summary>///<param name="bmpfilename"></param>///<param name="jpgfilename"></param>///<returns></returns>public static Boolean BmpToJpg(String bmpfilename, String jpgfilename){Boolean bl = ChangeFileFormat(bmpfilename, jpgfilename, ePictureFileFormat.Jpeg);return bl;}///<summary>///转换图片格式///</summary>///<param name="srcfilename"></param>///<param name="destfilename"></param>///<param name="destformat"></param>///<returns></returns>public static Boolean ChangeFileFormat(String srcfilename, String destfilename, ePictureFileFormat destformat){Boolean bl = FileProcessor.FileExist(srcfilename);if (!bl){return false;}Image image = Image.FromFile(srcfilename);ImageFormat IFMT = null;switch (destformat){case ePictureFileFormat.Bmp:IFMT = ImageFormat.Bmp;break;case ePictureFileFormat.Gif:IFMT = ImageFormat.Gif;break;case ePictureFileFormat.Icon:IFMT = ImageFormat.Icon;break;case ePictureFileFormat.Jpeg:IFMT = ImageFormat.Jpeg;break;case ePictureFileFormat.Png:IFMT = ImageFormat.Png;break;default:IFMT = ImageFormat.Jpeg;break;}image.Save(destfilename, IFMT);image.Dispose();bl = FileProcessor.FileExist(destfilename);if (!bl){return false;}Int32 filelen = FileProcessor.GetFileLength(destfilename);return (filelen > 0);}///<summary>///变成黑白图///</summary>///<param name="srcbitmap">原始图</param>///<param name="mode">模式。

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

打印机E S C P O S控制指令公司标准化编码 [QQX96QT-XQQB89Q8-NQQJ6Q8-MQM9N]HDD-80260打印机ESC/POS 控制指令ESC/POS 控制指令HT 横向跳格如果下一个跳格位置没有设置,该指令被忽略。

如果下一个横向跳格位置超越打印区域,将当前位置设置为[打印宽度+1]。

水平跳格位置由指令ESC D 来设置。

如果接收到此命令时当前位置在[打印宽度+1],打印机执行当前行缓冲区满动作并且将打印位置移到下一行的起始位置。

默认值跳格位置是每8 个标准ASCII 字符(12×24)字符跳一格。

当前行缓冲区满时,打印机执行下列动作:标准模式下,打印机打印当前行内容并将打印位置置于下一行的起始位置。

页模式下,打印机进行换行并将打印位置置于下一行的起始位置。

LF 打印并走纸一行该指令将当前位置置于行首。

FF 打印并走纸打印后清除缓冲区的内容。

由ESC W 指令设置的打印区域被重置为默认设置。

打印机不执行切纸动作。

该指令将当前位置置于行首。

该指令只在页模式下有效。

CR 打印并回车设置打印位置为行首。

串行接口模式下该指令被忽略。

并口模式下该指令是否有效由打印机配置决定。

CAN 页模式下删除打印缓冲区内容该指令只在页模式下有效。

如果先前设定区域与当前区域有重叠部分,重叠部分也被删除。

DLE DC4 n m t 实时产生钱箱开启脉冲当打印机正在执行钱箱开启命令(ESC p 或 DEL DC4)时,该命令被忽略。

串口模式下,打印机接收该指令后立即执行。

并口模式下,打印机忙的时候该指令不执行。

如果打印数据中包含和此指令相同的数据,则这些数据将被当作该指令执行。

用户必须考虑到这种情况。

该指令尽量不要插在2 个或更多字节的指令序列中。

即使打印机被ESC=(选择外设)命令设置为禁止,该指令依然有效。

ESC FF 页模式下打印该指令只在页模式下有效。

打印后不清除打印缓冲区的内容、ESC T和ESC W设置和字符的位置。

ESC SP n 设置字符右间距当字符放大时,间距随之放大相同的倍数。

此指令设置的值在页模式和标准模式下是相互独立的。

横向或纵向移动单位由GS P 指定。

改变横向或纵向移动单位不改变当前右间距。

标准模式下,使用横向移动单位。

在页模式下,根据区域的方向和起始位置来选择使用横向移动单位或纵向移动单位,其选择方式如下:①当打印起始位置由ESC T 设置为打印区域的左上角或右下角时,使用横向移动单位;②当打印起始位置由ESC T 设置为打印区域的左下角或右上角时,使用纵向移动单位;最大右间距是255/200 英寸。

任何超过这个值的设置都自动转换为最大右间距。

ESC ! n 选择打印模式当倍宽和倍高模式同时选择时,字符同时在横向和纵向放大两倍。

除了HT设置的空格和旋转打印的字符,其余任何字符都可以加下划线。

下划线度由ESC -确定,与字符无关。

当一行中部分字符为倍高或更高,所有字符以底端对齐。

ESC E 也能选择或取消加粗模式,最后被执行的指令有效。

ESC -也能选择或取消下划线模式,最后被执行的指令有效。

GS !也能设置字符大小,最后被执行的指令有效。

ESC $ nL nH 设置绝对打印位置如果设置位置在指定打印区域外,该命令被忽略。

横向和纵向移动单位由GS P 设置。

标准模式下使用横向移动单位。

在页模式下,根据打印区域的方向和打印起始位置来选择使用横向移动单位或纵向移动单位,其选择方式如下:①当打印起始位置由ESC T 设置为打印区域的左上角或右下角时,使用横向移动单位;②当打印起始位置由ESC T 设置为打印区域的左下角或右上角时,使用纵向移动单位。

ESC % n 选择/取消用户自定义字符选择或取消用户自定义字符。

当n 的最低位为0 时,不使用用户自定义字符。

当n 的最低位为1 时,使用用户自定义字符。

当取消使用用户自定义字符的时候,自动使用内部字库。

n 只有最低位有效。

ESC & y c1 c2 [x1 d1...d(y × x1)]...[xk d1...d(y × xk)] 定义用户自定义字符定义用户自定义字符。

y指定纵向字节数。

c1 是起始字符代码,c2 是终止字符代码。

x 指定横向点数。

允许的字符代码范围是ASCII 码的<20>H 到<7F>H(96 个字符)可以连续定义多个字符,如果只定义一个字符c1 = c2。

d 是下载字符的数据。

各点的数据从左边开始。

自定义字符的大小是(y×x)字节。

数据的各个位为1 表示打印这个点,为0 表示不打印。

当下列情况,用户自定义字符被清除:①ESC @被执行。

② ESC 被执行。

③打印机复位或电源关闭。

当选择标准ASCII 字体(12×24)时当选择压缩ASCII 字体时(9×17)ESC * m nL nH d1...dk 选择位图模式[名称]Select bit-image mode[格式]ASCII ESC*m nL nH d1...dkHex 1B2A m nL nH d1...dkDecimal2742m nL nH d1...dk[范围]m = 0, 1, 32, 33;0 ≤ nL ≤ 255;0 ≤ nH ≤ 3;0 ≤ d ≤ 255 [描述]选择由m 指定的一种位图模式,位图点数由nL 和nH 确定:m 模式纵向横向点数分辨率分辨率数据个数(k)0 8点单密度8 67DPI 100DPI nL+nH×2561 8点双密度8 67DPI 200DPI nL+nH×25632 24点单密度24 200DPI 100DPI (nL+nH×256)×333 24点双密度24 200DPI 200DPI (nL+nH×256)×3 [注释]如果m 的值超出规定范围,nL 和其后的数据被作为普通数据处理。

横向打印点数由nL 和nH 决定,总的点数为nL+nH×256。

位图超出当前区域的部分被截掉。

d 是位图的数据。

数据各个位为1 则打印这个点,为0 不打印。

位图数据发送完成后,打印机返回普通数据处理模式。

除了倒置模式,这条指令不受其它打印模式影响 (加粗、双重打印、下划线、字符放大和反显)。

数据和要打印点的关系如下:选择8 点密度时:选择24 点密度时:ESC – n 选择/取消下划线模式[名称]Select/cancel user-defined character set[格式]ASCII ESC-nHex 1B2D nDecimal2745n[范围]0≤n≤2,48≤n≤50[描述]根据n 的值选择或取消下划线模式:n 功能0,48 取消下划线模式1,49 选择下划线模式(1点宽)2,50 选择下划线模式(2点宽)[注释]下划线可加在所有字符下(包括右间距),但不包括HT 设置的空格。

下划线不能作用在字符旋转模式和反显的字符下。

当取消下划线模式时,后面的字符不加下划线,下划线的宽度不改变。

默认宽度是一点宽。

改变字符大小不影响当前下划线宽度。

下划线选择取消也可以由 ESC !来设置。

最后执行的指令有效。

n=0[默认值][参考] ESC !ESC 2 设置默认行高[名称]Select default line spacing[格式]ASCII ESC2Hex 1B32ESC 3 n 设置行高行高设置在标准模式和页模式下是相互独立的。

横向和纵向移动单位由GS P 设置,改变这个设置不影响当前行高。

标准模式下,使用纵向移动单位。

在页模式下,根据打印区域的方向和打印起始位置来选择使用横向移动单位或纵向移动单位,其选择方式如下:①当打印起始位置由ESC T 设置为打印区域的左上角或右下角时,使用纵向移动单位;②当打印起始位置由ESC T 设置为打印区域的左下角或右上角时,使用横向移动单位。

最大走纸距离是1016mm(40 英寸),如果超出这个距离,取最大距离。

ESC = n 选择打印机当打印机被禁止时,除了实时指令(DLE EOT,DLE ENQ,DLE DC4)忽略所有其他指令。

ESC n 取消用户自定义字符取消用户自定义字符中代码为n 的字符。

取消后,此字符使用内部字库。

如果自定义字符中没有该字符,该指令被忽略。

ESC @ 初始化打印机指令缓冲区内容保留。

宏定义保留。

Flash位图数据不擦除。

ESC D n1...nk NUL 设置横向跳格位置设置横向跳格位置。

由行首起第n 列设置一个跳格位置。

共有k 个跳格位置。

横向跳格位置由下式计算:字符宽度×n,字符宽度包括右间距,如果字符为倍宽,跳格距离也随之加倍。

该指令取消以前的跳格位置设置。

当n=8 时,当前位置为第九列。

最多设置32 个(k=32)跳格位置,超过32 的跳格位置数据被作为普通数据处理。

跳格位置按升序排列,结束符为NUL。

当[n]k 小于或等于前一个[n]k-1 值时,跳格设置结束,后面的数据作为普通数据处理。

ESC D NUL 取消所有的跳格位置设置。

改变字符宽度,先前指定的跳格位置并不发生变化。

字符宽度在标准模式和页模式下是独立的。

ESC E n 选择/取消加粗模式n只有最低位有效。

ESC !同样可以选择/取消加粗模式,最后接收的命令有效。

ESC G n 选择/取消双重打印模式n只有最低位有效。

该指令与加粗打印效果相同。

ESC J n 打印并走纸打印结束后,将当前打印位置置于行首。

走纸距离不受ESC 2 或ESC 3 指令设置的影响。

横向和纵向移动单位由GS P 设定。

标准模式下,使用纵向移动单位。

在页模式下,根据打印区域的方向和打印起始位置来选择使用纵向移动单位或横向移动单位,其选择方式如下:①当打印起始位置由ESC T 设置为打印区域的左上角或右下角时,使用纵向移动单位;②当打印起始位置由ESC T 设置为打印区域的左下角或右上角时,使用横向移动单位;最大走纸距离是1016mm(40 英寸).如果超出这个距离,取最大距离。

ESC L 选择页模式该指令只在标准模式的行首有效。

在页模式下,该指令无效。

当执行FF 或ESC S 后,打印机返回到标准模式。

该指令将打印位置设置到ESC T 命令和ESC W 命令确定的位置上。

该指令将下列指令的设置转换到页模式下的值:①?设置字符右间距:ESC SP,FS S②?设置行间距:ESC 2,ESC 3以下指令在页模式下只改变标志位,转换到标准模式后起作用。

①选择字符对齐模式:ESC a;②选择倒置模式:ESC {;③设置左空白:GS L;④设置打印区域宽度:GS W当电源关闭、打印机复位、或执行ESC @指令,打印机回到标准模式。

相关文档
最新文档