USB 小票打印机 58MM热敏打印机 用c#怎么调用
USB小票打印机58MM热敏打印机用c#怎么调用啊,给打印机发送指令,有知道的朋友,能给个提示吗,谢谢!急需...
USB 小票打印机 58MM热敏打印机 用c#怎么调用啊,给打印机发送指令,有知道的朋友,能给个提示吗,谢谢!急需
展开
展开全部
直接并口发送数帆嫌据就可以了。
首先添加一个LPTControl 类,然后调世尘用即可。
using System.Runtime.InteropServices;
namespace LptPrint_test
{
/// <summary>
/// LPTControl 的摘要说明。
/// </summary>
public class LPTControl
{
private string LptStr="lpt1";
public LPTControl(string l_LPT_Str)
{
//
// TODO: 在此处添加构造函数逻辑
//
LptStr=l_LPT_Str;
}
[StructLayout(LayoutKind.Sequential)]
private struct OVERLAPPED
{
int Internal;
int InternalHigh;
int Offset;
int OffSetHigh;
int hEvent;
}
[DllImport("kernel32.dll"态返手)]
private static extern int CreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile
);
[DllImport("kernel32.dll")]
private static extern bool WriteFile(
int hFile,
byte[] lpBuffer,
int nNumberOfBytesToWrite,
ref int lpNumberOfBytesWritten,
ref OVERLAPPED lpOverlapped
);
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(
int hObject
);
private int iHandle;
public bool Open()
{
iHandle = CreateFile(LptStr, 0x40000000, 0, 0, 3, 0, 0);
if (iHandle != -1)
{
return true;
}
else
{
return false;
}
}
public bool Write(String Mystring)
{
if (iHandle != -1)
{
OVERLAPPED x = new OVERLAPPED();
int i = 0;
byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);
bool b = WriteFile(iHandle, mybyte, mybyte.Length, ref i, ref x);
return b;
}
else
{
throw new Exception("不能连接到打印机!");
}
}
public bool Write(byte[] mybyte)
{
if (iHandle != -1)
{
OVERLAPPED x = new OVERLAPPED();
int i = 0;
WriteFile(iHandle, mybyte, mybyte.Length,
ref i, ref x);
return true;
}
else
{
throw new Exception("不能连接到打印机!");
}
}
public bool Close()
{
return CloseHandle(iHandle);
}
}
}
调用方法:
private void button1_Click(object sender, System.EventArgs e)
{
LPTControl CurrLptCtrl=new LPTControl(this.comboBox1.Text.Trim().ToLower());
CurrLptCtrl.Open();
for (int i=0;i<this.textBox1.Lines.Length;i++)
{
// CurrLptCtrl.Write(textBox1.Lines[i]);
char LF = (char)(10);
CurrLptCtrl.Write(textBox1.Lines[i]+LF);
}
CurrLptCtrl.Close();
}
首先添加一个LPTControl 类,然后调世尘用即可。
using System.Runtime.InteropServices;
namespace LptPrint_test
{
/// <summary>
/// LPTControl 的摘要说明。
/// </summary>
public class LPTControl
{
private string LptStr="lpt1";
public LPTControl(string l_LPT_Str)
{
//
// TODO: 在此处添加构造函数逻辑
//
LptStr=l_LPT_Str;
}
[StructLayout(LayoutKind.Sequential)]
private struct OVERLAPPED
{
int Internal;
int InternalHigh;
int Offset;
int OffSetHigh;
int hEvent;
}
[DllImport("kernel32.dll"态返手)]
private static extern int CreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile
);
[DllImport("kernel32.dll")]
private static extern bool WriteFile(
int hFile,
byte[] lpBuffer,
int nNumberOfBytesToWrite,
ref int lpNumberOfBytesWritten,
ref OVERLAPPED lpOverlapped
);
[DllImport("kernel32.dll")]
private static extern bool CloseHandle(
int hObject
);
private int iHandle;
public bool Open()
{
iHandle = CreateFile(LptStr, 0x40000000, 0, 0, 3, 0, 0);
if (iHandle != -1)
{
return true;
}
else
{
return false;
}
}
public bool Write(String Mystring)
{
if (iHandle != -1)
{
OVERLAPPED x = new OVERLAPPED();
int i = 0;
byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);
bool b = WriteFile(iHandle, mybyte, mybyte.Length, ref i, ref x);
return b;
}
else
{
throw new Exception("不能连接到打印机!");
}
}
public bool Write(byte[] mybyte)
{
if (iHandle != -1)
{
OVERLAPPED x = new OVERLAPPED();
int i = 0;
WriteFile(iHandle, mybyte, mybyte.Length,
ref i, ref x);
return true;
}
else
{
throw new Exception("不能连接到打印机!");
}
}
public bool Close()
{
return CloseHandle(iHandle);
}
}
}
调用方法:
private void button1_Click(object sender, System.EventArgs e)
{
LPTControl CurrLptCtrl=new LPTControl(this.comboBox1.Text.Trim().ToLower());
CurrLptCtrl.Open();
for (int i=0;i<this.textBox1.Lines.Length;i++)
{
// CurrLptCtrl.Write(textBox1.Lines[i]);
char LF = (char)(10);
CurrLptCtrl.Write(textBox1.Lines[i]+LF);
}
CurrLptCtrl.Close();
}
展开全部
一般这种东西的通讯都是C++写的返毁链类库,你可以余念用C#的平台调用Pinvoke引入dll执行命令,参考一下类库的漏孙说明知道方法签名就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你找到方法了吗 USB怎发指令,不是并口
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询