思普瑞特POS打印机VC#2008示例
2010-05-02 00:44
639 查看
//说明:我还没完全理解,只是把其VS2005编写的转成了2008,另添加了一个打开图片位置的代码,如果需要POS_SDK.dll进行测试的,可以到其官网下载的
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace TestSDKCSharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private const Int32 POS_PT_COM = 1000;
private const Int32 POS_PT_LPT = 1001;
private const Int32 POS_PT_USB = 1002;
private const Int32 POS_PT_NET = 1003;
// printer state
private const Int32 POS_PS_NORMAL = 3001;
private const Int32 POS_PS_PAPEROUT = 3002;
private const Int32 POS_PS_HEAT = 3003;
private const Int32 POS_PS_DOOROPEN = 3004;
private const Int32 POS_PS_BUFFEROUT = 3005;
private const Int32 POS_PS_CUT = 3006;
// barcode type
private const Int32 POS_BT_UPCA = 4001;
private const Int32 POS_BT_UPCE = 4002;
private const Int32 POS_BT_JAN13 = 4003;
private const Int32 POS_BT_JAN8 = 4004;
private const Int32 POS_BT_CODE39 = 4005;
private const Int32 POS_BT_ITF = 4006;
private const Int32 POS_BT_CODABAR = 4007;
private const Int32 POS_BT_CODE93 = 4008;
private const Int32 POS_BT_CODE128 = 4009;
// HRI type
private const Int32 POS_HT_NONE = 4011;
private const Int32 POS_HT_UP = 4012;
private const Int32 POS_HT_DOWN = 4013;
private const Int32 POS_HT_BOTH = 4014;
[DllImport("Resources//POS_SDK.dll", EntryPoint = "POS_Port_OpenA")]
static extern Int32 Open(String lpName, Int32 iPort, bool bFile, String path);
[DllImport("Resources//POS_SDK.dll", EntryPoint = "POS_Output_PrintStringA")]
static extern Int32 Send(Int32 printID, String strBuff);
[DllImport("Resources//POS_SDK.dll", EntryPoint = "POS_Control_CutPaper")]
static extern long CutPaper(Int32 printID, long type, long len);
[DllImport("Resources//POS_SDK.dll", EntryPoint = "POS_Status_QueryStatus")]
static extern Int32 QueryStatus(Int32 printID);
[DllImport("Resources//POS_SDK.dll", EntryPoint = "POS_Output_PrintData")]
static extern Int32 SendData(Int32 printID, byte[] strBuff, Int32 ilen);
[DllImport("Resources//POS_SDK.dll", EntryPoint = "POS_Output_PrintBarcodeA")]
static extern Int32 PrintBarcode(Int32 printID, Int32 type, Int32 width, Int32 height, Int32 hri, String strBuff);
[DllImport("Resources//POS_SDK.dll", EntryPoint = "POS_Output_PrintBmpDirectA")]
static extern Int32 PrintBmp(Int32 printID, Int32 type, String strPath);
[DllImport("Resources//POS_SDK.dll", EntryPoint = "POS_Port_Close")]
static extern Int32 Close(Int32 printID);
private enum iPortType {COM=0, LPT, USB, NET};
private iPortType m_pt;
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
m_pt = iPortType.COM;
textBox3.Text = "COM3:9600,N,8,1";
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
m_pt = iPortType.LPT;
textBox3.Text = "LPT1:";
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
m_pt = iPortType.USB;
textBox3.Text = "SP-USB1"; // SP-USB(1~5)
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
m_pt = iPortType.NET;
textBox3.Text = "192.168.1.114";
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog()==DialogResult.OK)
{
textBox2.Text = openFileDialog1.FileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
Int32 ipt, sendNum, lp;
Int32 hPort;
string strBuffer;
switch(m_pt)
{
case iPortType.COM:
ipt = POS_PT_COM;
break;
case iPortType.LPT:
ipt = POS_PT_LPT;
break;
case iPortType.USB:
ipt = POS_PT_USB;
break;
case iPortType.NET:
ipt = POS_PT_NET;
break;
default:
ipt = POS_PT_COM;
break;
}
strBuffer = textBox3.Text;
hPort = Open(strBuffer, ipt, false, "");
if (hPort == 0)
{
MessageBox.Show("Open port failed!!");
return;
}
strBuffer = textBox1.Text;
sendNum = strBuffer.Length;
string subStr;
Int32 status;
while (sendNum > 0)
{
lp = Send(hPort, strBuffer);
sendNum -= lp;
if (sendNum > 0)
{
subStr = strBuffer.Substring(lp);
strBuffer = subStr;
status = QueryStatus(hPort);
switch (status)
{
case POS_PS_PAPEROUT:
MessageBox.Show("打印机缺纸", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
System.Threading.Thread.Sleep(30000);
break;
case POS_PS_HEAT:
MessageBox.Show("打印机机头过热", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
System.Threading.Thread.Sleep(30000);
break;
case POS_PS_DOOROPEN:
MessageBox.Show("打印机纸仓门开", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
System.Threading.Thread.Sleep(30000);
break;
case POS_PS_BUFFEROUT:
MessageBox.Show("打印机缓冲区满", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
System.Threading.Thread.Sleep(10000);
break;
case POS_PS_CUT:
MessageBox.Show("打印机切刀错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
System.Threading.Thread.Sleep(30000);
break;
default:
break;
}
}
}
// Print a barcode
PrintBarcode(hPort, POS_BT_CODE39, 30, 50, POS_HT_DOWN, "");
PrintBmp(hPort, 0, textBox2.Text);
//CutPaper(hPort, 1, 0);
Close(hPort);
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
m_pt = iPortType.COM;
radioButton1.Checked = true;
textBox1.Text = "Hi, thank you for choose SPRT, We will get your the best experience!/r/n/r/n";
textBox2.Text = @"Resources/goodwork.bmp";//打印图片路径,图片只能是黑白单色的BMP格式位图,否则将不会打印的.
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace TestSDKCSharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private const Int32 POS_PT_COM = 1000;
private const Int32 POS_PT_LPT = 1001;
private const Int32 POS_PT_USB = 1002;
private const Int32 POS_PT_NET = 1003;
// printer state
private const Int32 POS_PS_NORMAL = 3001;
private const Int32 POS_PS_PAPEROUT = 3002;
private const Int32 POS_PS_HEAT = 3003;
private const Int32 POS_PS_DOOROPEN = 3004;
private const Int32 POS_PS_BUFFEROUT = 3005;
private const Int32 POS_PS_CUT = 3006;
// barcode type
private const Int32 POS_BT_UPCA = 4001;
private const Int32 POS_BT_UPCE = 4002;
private const Int32 POS_BT_JAN13 = 4003;
private const Int32 POS_BT_JAN8 = 4004;
private const Int32 POS_BT_CODE39 = 4005;
private const Int32 POS_BT_ITF = 4006;
private const Int32 POS_BT_CODABAR = 4007;
private const Int32 POS_BT_CODE93 = 4008;
private const Int32 POS_BT_CODE128 = 4009;
// HRI type
private const Int32 POS_HT_NONE = 4011;
private const Int32 POS_HT_UP = 4012;
private const Int32 POS_HT_DOWN = 4013;
private const Int32 POS_HT_BOTH = 4014;
[DllImport("Resources//POS_SDK.dll", EntryPoint = "POS_Port_OpenA")]
static extern Int32 Open(String lpName, Int32 iPort, bool bFile, String path);
[DllImport("Resources//POS_SDK.dll", EntryPoint = "POS_Output_PrintStringA")]
static extern Int32 Send(Int32 printID, String strBuff);
[DllImport("Resources//POS_SDK.dll", EntryPoint = "POS_Control_CutPaper")]
static extern long CutPaper(Int32 printID, long type, long len);
[DllImport("Resources//POS_SDK.dll", EntryPoint = "POS_Status_QueryStatus")]
static extern Int32 QueryStatus(Int32 printID);
[DllImport("Resources//POS_SDK.dll", EntryPoint = "POS_Output_PrintData")]
static extern Int32 SendData(Int32 printID, byte[] strBuff, Int32 ilen);
[DllImport("Resources//POS_SDK.dll", EntryPoint = "POS_Output_PrintBarcodeA")]
static extern Int32 PrintBarcode(Int32 printID, Int32 type, Int32 width, Int32 height, Int32 hri, String strBuff);
[DllImport("Resources//POS_SDK.dll", EntryPoint = "POS_Output_PrintBmpDirectA")]
static extern Int32 PrintBmp(Int32 printID, Int32 type, String strPath);
[DllImport("Resources//POS_SDK.dll", EntryPoint = "POS_Port_Close")]
static extern Int32 Close(Int32 printID);
private enum iPortType {COM=0, LPT, USB, NET};
private iPortType m_pt;
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
m_pt = iPortType.COM;
textBox3.Text = "COM3:9600,N,8,1";
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
m_pt = iPortType.LPT;
textBox3.Text = "LPT1:";
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
m_pt = iPortType.USB;
textBox3.Text = "SP-USB1"; // SP-USB(1~5)
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
m_pt = iPortType.NET;
textBox3.Text = "192.168.1.114";
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog()==DialogResult.OK)
{
textBox2.Text = openFileDialog1.FileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
Int32 ipt, sendNum, lp;
Int32 hPort;
string strBuffer;
switch(m_pt)
{
case iPortType.COM:
ipt = POS_PT_COM;
break;
case iPortType.LPT:
ipt = POS_PT_LPT;
break;
case iPortType.USB:
ipt = POS_PT_USB;
break;
case iPortType.NET:
ipt = POS_PT_NET;
break;
default:
ipt = POS_PT_COM;
break;
}
strBuffer = textBox3.Text;
hPort = Open(strBuffer, ipt, false, "");
if (hPort == 0)
{
MessageBox.Show("Open port failed!!");
return;
}
strBuffer = textBox1.Text;
sendNum = strBuffer.Length;
string subStr;
Int32 status;
while (sendNum > 0)
{
lp = Send(hPort, strBuffer);
sendNum -= lp;
if (sendNum > 0)
{
subStr = strBuffer.Substring(lp);
strBuffer = subStr;
status = QueryStatus(hPort);
switch (status)
{
case POS_PS_PAPEROUT:
MessageBox.Show("打印机缺纸", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
System.Threading.Thread.Sleep(30000);
break;
case POS_PS_HEAT:
MessageBox.Show("打印机机头过热", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
System.Threading.Thread.Sleep(30000);
break;
case POS_PS_DOOROPEN:
MessageBox.Show("打印机纸仓门开", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
System.Threading.Thread.Sleep(30000);
break;
case POS_PS_BUFFEROUT:
MessageBox.Show("打印机缓冲区满", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
System.Threading.Thread.Sleep(10000);
break;
case POS_PS_CUT:
MessageBox.Show("打印机切刀错误", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
System.Threading.Thread.Sleep(30000);
break;
default:
break;
}
}
}
// Print a barcode
PrintBarcode(hPort, POS_BT_CODE39, 30, 50, POS_HT_DOWN, "");
PrintBmp(hPort, 0, textBox2.Text);
//CutPaper(hPort, 1, 0);
Close(hPort);
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
m_pt = iPortType.COM;
radioButton1.Checked = true;
textBox1.Text = "Hi, thank you for choose SPRT, We will get your the best experience!/r/n/r/n";
textBox2.Text = @"Resources/goodwork.bmp";//打印图片路径,图片只能是黑白单色的BMP格式位图,否则将不会打印的.
}
}
}
相关文章推荐
- VC2008 NewControl 示例
- 在vc2008 mfcC++中使用sqlite的示例
- 在vc2008 mfcC++中使用sqlite的示例
- VC6编译的静态库,在VC2008下使用报错
- 如何在不安装VC2008发布包的情况下,发布VC2008生成的程序
- Microsoft Expression Blend 2.5 March 2008 Preview 界面及示例概览(图集+动画视频)
- ATL编写的带窗口的控件,无论VC6(ATL3.0) VC7.1(ATL7.0) 在Windows 7 或 Server 2008 下,WinForm调用时发生的问题
- VC++ 2008 Express的wxWidgets培训班
- Windows Media Player播放器(VC++ 2008)
- 汇编语言新手入门:VC2008 环境下汇编语言开发环境的配置,支持Visual Assist
- VC2008下提示找不到MSVCP90D.dll的解决办法
- delphi控制POS打印机
- SystemParametersInfo (SPI_GETNONCLIENTMETRICS... 在VC 2008 里不能正常工作
- win7 、vistar、server2008 R2 、各种64位系统 安装局域网共享打印机,找不到驱动的问题
- VC++ 2008本科毕业学生论文指导一
- VC2008获取指定的窗口句柄
- 部分解决VC2008以后CRebar 增加两个工具条,第二个工具条会右对齐的问题
- Delphi下POS打印机,控制开钱箱,客显,顾客显示屏,小票打印机
- [windows server 2008 站点系列四]六式加速域用户查找打印机的速度
- vc2008下的对应vc6.0的Components and controls