java控制zebra打印机_从Zebra打印机读取状态

如果你有机会使用 kernel32.dll 并省略usb-driver-bound winspool.srv ,你可以使用这种香草方法:

using System;

using System.Runtime.InteropServices;

using System.Text;

using System.Threading;

using Microsoft.Win32.SafeHandles;

{

public class USB

{

[DllImport("kernel32.dll", SetLastError = true)]

internal static extern Int32 CancelIo(SafeFileHandle hFile);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]

internal static extern IntPtr CreateEvent(IntPtr SecurityAttributes,

Boolean bManualReset,

Boolean bInitialState,

String lpName);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]

internal static extern Boolean GetOverlappedResult(SafeFileHandle hFile,

IntPtr lpOverlapped,

ref Int32 lpNumberOfBytesTransferred,

Boolean bWait);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]

internal static extern Boolean ReadFile(SafeFileHandle hFile,

IntPtr lpBuffer,

Int32 nNumberOfBytesToRead,

ref Int32 lpNumberOfBytesRead,

IntPtr lpOverlapped);

[DllImport("kernel32.dll", SetLastError = true)]

internal static extern Int32 WaitForSingleObject(IntPtr hHandle,

Int32 dwMilliseconds);

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]

internal static extern SafeFileHandle CreateFile(String lpFileName,

UInt32 dwDesiredAccess,

Int32 dwShareMode,

IntPtr lpSecurityAttributes,

Int32 dwCreationDisposition,

Int32 dwFlagsAndAttributes,

Int32 hTemplateFile);

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]

internal static extern Boolean WriteFile(SafeFileHandle hFile,

ref byte lpBuffer,

Int32 nNumberOfBytesToWrite,

ref Int32 lpNumberOfBytesWritten,

IntPtr lpOverlapped);

[DllImport("kernel32.dll", SetLastError = true)]

internal static extern int GetLastError();

private const Int32 FILE_FLAG_OVERLAPPED = 0X40000000;

private const Int32 FILE_SHARE_READ = 1;

private const Int32 FILE_SHARE_WRITE = 2;

private const UInt32 GENERIC_READ = 0X80000000;

private const UInt32 GENERIC_WRITE = 0X40000000;

private const Int32 OPEN_EXISTING = 3;

private const Int32 WAIT_OBJECT_0 = 0;

private const Int32 WAIT_TIMEOUT = 0x102;

private const Int32 ReadBufferSize = 200;

private readonly string _devicePathName;

public USB(string devicePathName)

{

this._devicePathName = devicePathName;

}

public void Send(string data)

{

var bData = this.Encoding.GetBytes(data);

this.Send(bData);

}

public void Send(byte[] data)

{

try

{

var eventObject = CreateEvent(IntPtr.Zero,

false,

false,

String.Empty);

var hidOverlapped = GetHidOverlapped(eventObject);

var unManagedBuffer = Marshal.AllocHGlobal(data.Length);

var unManagedOverlapped = Marshal.AllocHGlobal(Marshal.SizeOf(hidOverlapped));

Marshal.StructureToPtr(hidOverlapped,

unManagedOverlapped,

false);

using (var writeHandle = this.GetWriteFileHandle())

{

var numberOfBytesWritten = 0;

var success = WriteFile(writeHandle,

ref data[0],

data.Length,

ref numberOfBytesWritten,

unManagedOverlapped);

if (!success)

{

var result = WaitForSingleObject(eventObject,

100);

switch (result)

{

case WAIT_OBJECT_0:

success = true;

break;

case WAIT_TIMEOUT:

CancelIo(writeHandle);

break;

}

}

}

Marshal.FreeHGlobal(unManagedOverlapped);

Marshal.FreeHGlobal(unManagedBuffer);

}

catch (Exception ex)

{

// TODO add logging and enhance the try/catch-closure to a smaller one

}

}

private Encoding Encoding

{

get

{

return Encoding.ASCII;

}

}

public string Read()

{

var receivedBytes = 0;

var receiveBuffer = new byte[ReadBufferSize];

string data;

try

{

var eventObject = CreateEvent(IntPtr.Zero,

false,

false,

String.Empty);

var hidOverlapped = GetHidOverlapped(eventObject);

var unManagedBuffer = Marshal.AllocHGlobal(ReadBufferSize);

var unManagedOverlapped = Marshal.AllocHGlobal(Marshal.SizeOf(hidOverlapped));

Marshal.StructureToPtr(hidOverlapped,

unManagedOverlapped,

false);

using (var readHandle = CreateFile(this._devicePathName,

GENERIC_READ,

FILE_SHARE_READ /* | FILE_SHARE_WRITE*/,

IntPtr.Zero,

OPEN_EXISTING,

FILE_FLAG_OVERLAPPED,

0))

{

var success = ReadFile(readHandle,

unManagedBuffer,

receiveBuffer.Length,

ref receivedBytes,

unManagedOverlapped);

if (!success)

{

var result1 = WaitForSingleObject(eventObject,

300);

switch (result1)

{

case WAIT_OBJECT_0:

GetOverlappedResult(readHandle,

unManagedOverlapped,

ref receivedBytes,

false);

break;

case WAIT_TIMEOUT:

default:

//CancelIo(_readHandle);

break;

}

}

}

if (receivedBytes > 0)

{

Array.Resize(ref receiveBuffer,

receivedBytes);

Marshal.Copy(unManagedBuffer,

receiveBuffer,

0,

receivedBytes);

data = this.Encoding.GetString(receiveBuffer);

}

else

{

data = null;

}

Marshal.FreeHGlobal(unManagedOverlapped);

Marshal.FreeHGlobal(unManagedBuffer);

}

catch (Exception ex)

{

// TODO add logging and enhance the try/catch-closure to a smaller one

data = null;

}

return data;

}

private SafeFileHandle GetWriteFileHandle()

{

var writeHandle = CreateFile(this._devicePathName,

GENERIC_WRITE | GENERIC_READ,

FILE_SHARE_READ | FILE_SHARE_WRITE,

IntPtr.Zero,

OPEN_EXISTING,

0,

0);

return writeHandle;

}

private static NativeOverlapped GetHidOverlapped(IntPtr eventObject)

{

return new NativeOverlapped

{

OffsetLow = 0,

OffsetHigh = 0,

EventHandle = eventObject

};

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值