Commit 31f50a5d 刘韬

添加TSC打印驱动

1 个父辈 1a83758c
此文件类型无法预览
此文件类型无法预览
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
......@@ -465,6 +466,8 @@ namespace BLL
if (labelIdx == -1) return;
using (PrintDialog dlg = new PrintDialog())
{
PrinterSettings printerSettings = new PrinterSettings();
dlg.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize("C", 236, 157);
if (dlg.ShowDialog() != DialogResult.OK) return;
System.Drawing.Printing.PrintDocument print = new System.Drawing.Printing.PrintDocument {
PrinterSettings = dlg.PrinterSettings
......@@ -547,12 +550,19 @@ namespace BLL
print.PrinterSettings.PrinterName = PrinterName;
print.DefaultPageSettings.Landscape = PrintLandscape;
print.PrintPage += PrintLast_PrintPage;
print.EndPrint += Print_EndPrint;
print.Print();
//print.Dispose();
return bmp;
}
private void Print_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
//e.Cancel = true;
//throw new NotImplementedException();
}
private string StrRemoveKey(string s)
{
string rtn = "";
......
......@@ -3,7 +3,14 @@ using System;
using System.Collections.Generic;
using System.Drawing;
using System.Management;
using System.Net.NetworkInformation;
using System.Printing;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace Asa
{
......@@ -14,11 +21,12 @@ namespace Asa
private System.Threading.Thread tRun;
string dir;
public PrintLabel(string dir, int dpi = 100)
{
Common.MULTIPLE = dpi;
Common.LabelPrint = new BLL.LabelPrint();
this.dir = dir;
Common.LabelPrint.Load(dir);
}
......@@ -68,6 +76,8 @@ namespace Asa
public void SetResolution(int printerDPI)
{
Common.MULTIPLE = printerDPI;
Common.LabelPrint = new BLL.LabelPrint();
Common.LabelPrint.Load(dir);
}
public bool Rotate180 { set => Common.LabelPrint.Rotate180 = value; get => Common.LabelPrint.Rotate180; }
......@@ -234,8 +244,176 @@ namespace Asa
}
}
/// <summary>
/// 打印
/// </summary>
/// <param name="key"></param>
/// <returns>是否打印成功</returns>
public bool PrintToTsc(Dictionary<string, string> key,out TscStauts tscStauts)
{
tscStauts = CheckTscStatus();
if (tscStauts != 0) {
var err = (TscStauts)tscStauts;
Console.WriteLine($"Error: {err}");
return false;
}
var t = Task.Run(() =>
{
Bitmap bmp = null;
try
{
bmp = PrintPreview(key);
var mm = Common.PxToMM(bmp.Width);
var nn = Common.PxToMM(bmp.Height);
TSCLIB_DLL.openport("USB");
SendCommand("GETSENSOR(\"PEEL\")");
SendCommand($"SIZE {mm:0.00} mm, {nn:0.00} mm");
SendCommand("GAP 3 mm,0.3 mm");
SendCommand("SET CUTTER OFF");
SendCommand("SET RIBBON OFF"); //使用碳带
SendCommand("SET ENCODER OFF"); //碳带编码器检测
SendCommand("OFFSET -10 mm"); //出纸偏移
SendCommand("SPEED 9"); //速度最大15
SendCommand("DENSITY 12"); //浓度
SendCommand("DIRECTION 1");
SendCommand("SET PEEL OFF");
TSCLIB_DLL.clearbuffer();
TSCLIB_DLL.sendpicture(0, 0, bmp);
TSCLIB_DLL.printlabel("1", "1");
TSCLIB_DLL.closeport();
Task.Delay(350).Wait();
}
catch (Exception e) { }
finally {
bmp?.Dispose();
}
});
t.Wait(1000);
tscStauts = CheckTscStatus();
Console.WriteLine("After print:" + tscStauts);
if (tscStauts == TscStauts.打印中 || tscStauts == TscStauts.准备就绪 || tscStauts == TscStauts.None)
return true;
else {
return false;
}
//Console.WriteLine("usbportqueryprinter:" + TSCLIB_DLL.usbportqueryprinter());
}
void SendCommand(string command)
{
Console.WriteLine(TSCLIB_DLL.sendcommand(command));
}
/// <summary>
/// 打印
/// </summary>
/// <param name="key"></param>
/// <returns>是否打印成功</returns>
public bool PrintToTsc1(Dictionary<string, string> key, out TscStauts tscStauts)
{
tscStauts = CheckTscStatus();
if (tscStauts != 0)
{
var err = (TscStauts)tscStauts;
Console.WriteLine($"Error: {err}");
return false;
}
var t = Task.Run(() =>
{
Bitmap bmp = null;
try
{
bmp = PrintPreview(key);
var mm = Common.PxToMM(bmp.Width);
var nn = Common.PxToMM(bmp.Height);
TSCSDK.driver TSCLIB_DLL = new TSCSDK.driver();
TSCLIB_DLL.openport("TSC TTP-342 Pro");
//TSCLIB_DLL.sendcommand("GETSENSOR(\"PEEL\")");
TSCLIB_DLL.sendcommand("\0x27!?");
Task.Delay(1000).Wait();
///IntPtr aaa = Marshal.AllocHGlobal(256);
byte[] b = new byte[256];
IntPtr aaa = System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(b, 0);
TSCSDK.driver.ReadPrinter(TSCLIB_DLL.hPrinter, aaa, 255, out int reby);
//Marshal.Copy(aaa, b, 0, 256);
//Marshal.FreeHGlobal(aaa);
var bbbb = Encoding.Unicode.GetString(b);
TSCLIB_DLL.sendcommand($"SIZE {mm:0.00} mm, {nn:0.00} mm");
TSCLIB_DLL.sendcommand("GAP 3 mm,0.3 mm");
TSCLIB_DLL.sendcommand("SET CUTTER OFF");
TSCLIB_DLL.sendcommand("SET RIBBON OFF"); //使用碳带
TSCLIB_DLL.sendcommand("SET ENCODER OFF"); //碳带编码器检测
TSCLIB_DLL.sendcommand("OFFSET -10 mm"); //出纸偏移
TSCLIB_DLL.sendcommand("SPEED 9"); //速度最大15
TSCLIB_DLL.sendcommand("DENSITY 12"); //浓度
TSCLIB_DLL.sendcommand("DIRECTION 1");
TSCLIB_DLL.sendcommand("SET PEEL OFF");
TSCLIB_DLL.clearbuffer();
TSCLIB_DLL.sendpicture(0, 0, bmp);
TSCLIB_DLL.printlabel("1", "1");
TSCLIB_DLL.closeport();
Task.Delay(350).Wait();
}
catch (Exception e) { }
finally
{
bmp?.Dispose();
}
});
t.Wait(1000);
tscStauts = CheckTscStatus();
Console.WriteLine("After print:" + tscStauts);
if (tscStauts == TscStauts.打印中 || tscStauts == TscStauts.准备就绪 || tscStauts == TscStauts.None)
return true;
else
{
return false;
}
//Console.WriteLine("usbportqueryprinter:" + TSCLIB_DLL.usbportqueryprinter());
}
TscStauts CheckTscStatus() {
TscStauts s = TscStauts.None;
var t = Task.Run(() => {
Console.WriteLine("TID:"+ GetCurrentThreadId().ToString("X"));
s = (TscStauts)TSCLIB_DLL.usbportqueryprinter();
});
if (!t.Wait(1000))
Console.WriteLine("usbportqueryprinter time out");
return s;
}
[DllImport("kernel32.dll")]
static extern int GetCurrentThreadId();
}
public enum TscStauts
{
准备就绪 = 0x0,
打印头开启 = 0x1,
纸张卡纸 = 0x02,
打印头开启并且纸张卡纸 = 0x03,
纸张缺纸 = 0x04,
打印头开启并且纸张缺纸 = 0x05,
无碳带 = 0x08,
打印头开启并且无碳带 = 0x09,
纸张卡纸并且无碳带 = 0x0A,
打印头开启_纸张卡纸并且无碳带 = 0x0B,
纸张缺纸并且无碳带 = 0x0C,
打印头开启_纸张缺纸并且无碳带 = 0x0D,
暂停 = 0x10,
Error = 0x14,
打印中 = 0x20,
其它错误 = 0x80,
None = 0xFD,
Offline = 0xFF,
}
enum TscStautsaa {
a = 1,
b = 1<<1,
c = 1<<2,
}
}
......@@ -22,6 +22,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
......@@ -30,6 +31,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualBasic" />
......@@ -71,6 +73,8 @@
</Compile>
<Compile Include="PrintLabel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TSCLIB_DLL.cs" />
<Compile Include="TSCSDK.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="FormTextBox.resx">
......
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Asa
{
public class TSCLIB_DLL
{
[DllImport("TSCLIB.dll", EntryPoint = "about")]
public static extern int about();
/// <summary>
/// <list>說明: 指定電腦端的輸出埠</list>
/// 參數:
/// (1) 單機列印時,請指定印表機驅動程式名稱
/// 例如: TSC CLEVER TTP-243
/// (2) 若連接印表機伺服器,請指定伺服器路徑及共用印表機名稱
/// 例如: \\SERVER\TTP243
/// (3) 直接指定平行傳輸介面,請指定輸出埠名稱為 LPT1 到 LPT4
/// (4) 直接指定 USB 傳輸介面,請指定輸出埠名稱為 USB
/// </summary>
/// <param name="printername"></param>
/// <returns></returns>
[DllImport("TSCLIB.dll", EntryPoint = "openport")]
public static extern int openport(string printername);
[DllImport("TSCLIB.dll", EntryPoint = "barcode")]
public static extern int barcode(string x, string y, string type,
string height, string readable, string rotation,
string narrow, string wide, string code);
[DllImport("TSCLIB.dll", EntryPoint = "clearbuffer")]
public static extern int clearbuffer();
[DllImport("TSCLIB.dll", EntryPoint = "closeport")]
public static extern int closeport();
[DllImport("TSCLIB.dll", EntryPoint = "downloadpcx")]
public static extern int downloadpcx(string filename, string image_name);
[DllImport("TSCLIB.dll", EntryPoint = "formfeed")]
public static extern int formfeed();
[DllImport("TSCLIB.dll", EntryPoint = "nobackfeed")]
public static extern int nobackfeed();
[DllImport("TSCLIB.dll", EntryPoint = "printerfont")]
public static extern int printerfont(string x, string y, string fonttype,
string rotation, string xmul, string ymul,
string text);
[DllImport("TSCLIB.dll", EntryPoint = "printlabel")]
public static extern int printlabel(string set, string copy);
[DllImport("TSCLIB.dll", EntryPoint = "sendcommand")]
public static extern int sendcommand(string printercommand);
[DllImport("TSCLIB.dll", EntryPoint = "setup")]
public static extern int setup(string width, string height,
string speed, string density,
string sensor, string vertical,
string offset);
[DllImport("TSCLIB.dll", EntryPoint = "windowsfont")]
public static extern int windowsfont(int x, int y, int fontheight,
int rotation, int fontstyle, int fontunderline,
string szFaceName, string content);
[DllImport("TSCLIB.dll", EntryPoint = "windowsfontUnicode")]
public static extern int windowsfontUnicode(int x, int y, int fontheight,
int rotation, int fontstyle, int fontunderline,
string szFaceName, byte[] content);
[DllImport("TSCLIB.dll", EntryPoint = "sendBinaryData")]
public static extern int sendBinaryData(byte[] content, int length);
[DllImport("TSCLIB.dll", EntryPoint = "usbportqueryprinter")]
public static extern byte usbportqueryprinter();
[DllImport("TSCLIB.dll", EntryPoint = "usbprinterstatus")]
public static extern byte usbprinterstatus();
[DllImport("TSCLIB.dll", EntryPoint = "usbprinterpeel")]
public static extern int usbprinterpeel(string printercommand);
public static void sendpicture(int xpoint, int ypoint, Bitmap original_picture)
{
//Bitmap bitmap = new Bitmap(original_picture.Width, original_picture.Height);
//for (int i = 0; i < original_picture.Width; i++)
//{
// for (int j = 0; j < original_picture.Height; j++)
// {
// Color pixel = original_picture.GetPixel(i, j);
// int num = (int)((double)(int)pixel.R * 0.3 + (double)(int)pixel.G * 0.59 + (double)(int)pixel.B * 0.11);
// bitmap.SetPixel(i, j, Color.FromArgb(pixel.A, num, num, num));
// }
//}
Bitmap bitmap2 = original_picture;
new Bitmap(bitmap2.Width, bitmap2.Height);
byte[] result_utf8 = System.Text.Encoding.UTF8.GetBytes("BITMAP " + xpoint + "," + ypoint + "," + (bitmap2.Width + 7) / 8 + "," + bitmap2.Height + ", 0,");
sendBinaryData(result_utf8, result_utf8.Length);
byte[] array = new byte[(bitmap2.Width + 7) / 8 * bitmap2.Height];
for (int k = 0; k < array.Length; k++)
{
array[k] = byte.MaxValue;
}
for (int l = 0; l < bitmap2.Height; l++)
{
for (int m = 0; m < bitmap2.Width; m++)
{
Color pixel2 = bitmap2.GetPixel(m, l);
byte r = pixel2.R;
byte g = pixel2.G;
byte b = pixel2.B;
if ((r + g + b) / 3 < 128)
{
array[l * ((bitmap2.Width + 7) / 8) + m / 8] ^= (byte)(128 >> m % 8);
}
}
}
sendBinaryData(array, array.Length);
sendBinaryData(CRLF_byte, CRLF_byte.Length);
}
private static byte[] CRLF_byte = new byte[2] { 13, 10 };
}
}
此文件的差异被折叠, 点击展开。
......@@ -36,6 +36,7 @@
this.button4 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button5 = new System.Windows.Forms.Button();
this.btn_tsc = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
......@@ -113,11 +114,22 @@
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// btn_tsc
//
this.btn_tsc.Location = new System.Drawing.Point(154, 322);
this.btn_tsc.Name = "btn_tsc";
this.btn_tsc.Size = new System.Drawing.Size(123, 42);
this.btn_tsc.TabIndex = 8;
this.btn_tsc.Text = "TSC Print";
this.btn_tsc.UseVisualStyleBackColor = true;
this.btn_tsc.Click += new System.EventHandler(this.btn_tsc_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.btn_tsc);
this.Controls.Add(this.button5);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button4);
......@@ -146,6 +158,7 @@
private System.Windows.Forms.Button button4;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button btn_tsc;
}
}
using System;
using Asa;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
......@@ -63,5 +65,19 @@ namespace PrintLabel_Test
text.Add("pn", "yumtr3123123j");
pictureBox1.Image = print.PrintPreview(text);
}
private void btn_tsc_Click(object sender, EventArgs e)
{
Dictionary<string, string> text = new Dictionary<string, string>();
text.Add("ri", "123123123123123");
text.Add("pn", "yumtr3123123j");
print.SetResolution(300);
print.GetLabelName();
print.LoadLabel(textBox1.Text);
print.PrintToTsc1(text,out TscStauts tscStauts);
return;
}
}
}
......@@ -23,6 +23,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!