LibUsbDotNet 通过usb口获取扫码盒子扫码数据,读取数据不全和不对。
以下是具体实现代码:在这里插入代码片
public void Open(int vid, int pid)
{
UsbDeviceFinder usbFinder = new UsbDeviceFinder(vid, pid);
usbDevice = UsbDevice.OpenUsbDevice(usbFinder);
ErrorCode ec = ErrorCode.None;
if (usbDevice == null)
{
// 多次尝试连接USB设备
int count = 0;
while (count < 4 && usbDevice == null)
{
Thread.Sleep(100);
usbDevice = UsbDevice.OpenUsbDevice(usbFinder);
count++;
}
}
if (usbDevice == null)
{
Console.WriteLine("打开USB设备失败");
}
else
{
IUsbDevice wholeUSBDevice = usbDevice as IUsbDevice;
if (wholeUSBDevice != null)
{
wholeUSBDevice.SetConfiguration(1);
wholeUSBDevice.ClaimInterface(0);
}
epReader = usbDevice.OpenEndpointReader(ReadEndpointID.Ep04);
epReader.DataReceivedEnabled = true;
while (ec == ErrorCode.None) //一直在读取
{
int bytesRead;
Thread.Sleep(5);
byte[] readBuffer = new byte[64];
ec = epReader.Read(readBuffer,5000, out bytesRead);
if (ec == ErrorCode.IoTimedOut)
{
continue;
}
if (bytesRead == 0) {
continue;
}
Console.WriteLine("{0} bytes read", bytesRead);
Console.Write(Encoding.ASCII.GetString(readBuffer, 0, bytesRead));
}
}
this.Close();
}
问题已经解决:
1、获取数据方式处理,以及获取数据的参数等,根据设备来变化,需要自己测试。
public string startHIDKeyBoardSM()
{
ErrorCode ec = ErrorCode.None;
string data = string.Empty;
try
{
bool isok = true;
while (isok)
{
string abctest = string.Empty;
byte[] readBuffer = new byte[512]; // 这里分配的大小对读取的数据有影响
int bytesRead;
ec = epReader.Read(readBuffer, 3000, out bytesRead); //读取超时时间也是,我另外一款设备,这个时间必须大于5000,不然取到的数据不完整。
if (ec == ErrorCode.IoTimedOut && readBuffer[2] == 0)
{
//超时,重新开启循环。
isok = true;
continue;
}
if (ec == ErrorCode.Win32Error)
{
isok = false;
Console.WriteLine(“读取异常,需要重新开始。”);
return “-2”;
}
if (readBuffer[0] == 4) //有的sm设备不支持读取中文,支持的需要单独处理。
{
isok = false;
Console.WriteLine(“二维码存在中文,请展示正常的二维码”);
return “-4”;
}
if (bytesRead > 0 || bytesRead == 0 && readBuffer[2] != 0 || bytesRead == 0 && readBuffer[0] != 0)
{
isok = false;
for (int i = 0; i < readBuffer.Length; i++)
{
//数据处理,手下数据char。
int a = readBuffer[i];
if (a == 0) continue;
if (a == 4 && readBuffer[i + 2] >= 90)
{
Console.WriteLine("二维码存在中文,请展示正常的二维码");
return "-4";
}
if (a > 39 && a < 90) //特殊符号 ,有些特殊符号处理不了,比如@,跟扫码出来的2是一个数据。
{
switch (a)
{
case 46:
abctest = abctest + "=";
break;
case 56:
abctest = abctest + "/";
break;
default:
break;
}
continue;
}
if (a.ToString().Length == 1)
{
string letter = string.Empty;
//表示是字母,需要单处理。
if (readBuffer[i] == 2 && readBuffer[i + 1] == 0)
{
#region 处理条码大写字母
switch (readBuffer[i + 2])
{
case 4:
letter = "A";
break;
case 5:
letter = "B";
break;
case 6:
letter = "C";
break;
case 7:
letter = "D";
break;
case 8:
letter = "E";
break;
case 9:
letter = "F";
break;
case 10:
letter = "G";
break;
case 11:
letter = "H";
break;
case 12:
letter = "I";
break;
case 13:
letter = "J";
break;
case 14:
letter = "K";
break;
case 15:
letter = "L";
break;
case 16:
letter = "M";
break;
case 17:
letter = "N";
break;
case 18:
letter = "O";
break;
case 19:
letter = "P";
break;
case 20:
letter = "Q";
break;
case 21:
letter = "R";
break;
case 22:
letter = "S";
break;
case 23:
letter = "T";
break;
case 24:
letter = "U";
break;
case 25:
letter = "V";
break;
case 26:
letter = "W";
break;
case 27:
letter = "X";
break;
case 28:
letter = "Y";
break;
case 29:
letter = "Z";
break;
default:
break;
}
#endregion
}
else
{
#region 处理条码小写字母
switch (readBuffer[i])
{
case 4:
letter = "a";
break;
case 5:
letter = "b";
break;
case 6:
letter = "c";
break;
case 7:
letter = "d";
break;
case 8:
letter = "e";
break;
case 9:
letter = "f";
break;
case 10:
letter = "g";
break;
case 11:
letter = "h";
break;
case 12:
letter = "i";
break;
case 13:
letter = "j";
break;
case 14:
letter = "k";
break;
case 15:
letter = "l";
break;
case 16:
letter = "m";
break;
case 17:
letter = "n";
break;
case 18:
letter = "o";
break;
case 19:
letter = "p";
break;
case 20:
letter = "q";
break;
case 21:
letter = "r";
break;
case 22:
letter = "s";
break;
case 23:
letter = "t";
break;
case 24:
letter = "u";
break;
case 25:
letter = "v";
break;
case 26:
letter = "w";
break;
case 27:
letter = "x";
break;
case 28:
letter = "y";
break;
case 29:
letter = "z";
break;
default:
break;
}
#endregion
}
//中间处理数据转为字母。
abctest = abctest + letter;
}
if (a.ToString().Length == 2)
{
if (a == 40 || a == 81)
{
//结束符号
continue;
}
string figure = string.Empty;
char[] b = a.ToString().ToCharArray();
if (int.Parse(b[0].ToString()) == 3)//表示读取到的数字,可以在第二位直接加减
{
#region 处理条码数字那快的数字。
switch (int.Parse(b[1].ToString()))
{
case 0:
figure = "1";
break;
case 1:
figure = "2";
break;
case 2:
figure = "3";
break;
case 3:
figure = "4";
break;
case 4:
figure = "5";
break;
case 5:
figure = "6";
break;
case 6:
figure = "7";
break;
case 7:
figure = "8";
break;
case 8:
figure = "9";
break;
case 9:
figure = "0";
break;
default:
break;
}
abctest = abctest + figure;
#endregion
}
}
// Console.WriteLine(readBuffer[i]);
}
data = abctest;
}
}
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
return "-3";
}
return data;
}