我们有一个与 Zebra 打印机通信的程序。该程序在 WM 设备(摩托罗拉 ES400)上运行,使用 MZ320 非常可靠,但不适用于 iMZ320。在我们使用 BluetoothClient (InTheHand) 的设备上。在 iMZ320 上,程序经常抛出异常。
在出现错误消息之前,程序会等待 80 秒。(但仅在 ES400 上 - 已建立的连接被主机中的软件中止)。打印机在此期间显示蓝牙连接打开(蓝色 LED)。如果打印机关闭,我们会在 6 秒后收到另一条消息(无法建立连接,因为目标机器主动拒绝它)。发生这种情况后,ES400 上的程序无法与 iMZ 通信,直到我们重新启动打印机。
问题:是否有人知道为什么会发生这种情况以及我们能做些什么?
示例代码:
public static Boolean BluetoothPrintES400(String label)
{
BluetoothRadio.PrimaryRadio.Mode = RadioMode.Connectable;
System.Threading.Thread.Sleep(1000);
BluetoothAddress btAddress;
btAddress = BluetoothAddress.Parse("ab465as34jd3");
BluetoothClient btClient = new BluetoothClient();
try
{
btClient.Connect(new BluetoothEndPoint(btAddress, BluetoothService.SerialPort));
}
catch (Exception ex)
{
message = ex.Message;
return false;
}
Encoding e = Encoding.GetEncoding("iso-8859-2");
label = label + "\r\n";
int x, y;
for (int i = 0; i < ((int)(label.Length / 500)) + 1; i++)
{
x = i * 500;
y = 500;
if ((x + y) > label.Length)
{
y = label.Length - x;
}
byte[] configLabel = e.GetBytes(label.ToCharArray(), x, y);
//connection.Write(configLabel);
NetworkStream ns = btClient.GetStream();
ns.Write(configLabel, 0, configLabel.Length);
}
btClient.Close();
return true;
}
约 80 秒后的第一个异常:
信息:
An established connection was aborted by the software in your host machine
堆栈跟踪:
- System.Net.Sockets.Socket.ConnectNoCheck()
- System.Net.Sockets.Socket.Connect()
- InTheHand.Net.Sockets.BluetoothClient.Connect()
- Project.Utility.Util.BluetoothPrintES400()
- Project.Forms.FrmMain.printing()
- Project.Forms.FrmMain.mniNext_Click()
- System.Windows.Forms.MenuItem.OnClick()
- System.Windows.Forms.Menu.ProcessMnuProc()
- System.Windows.Forms.Form.WnProc()
- System.Windows.Forms.Control._InternalWnProc()
- Microsoft.AGL.Forms.EVL.EnterMainLoop()
- System.Windows.Forms.Application.Run()
- Project.Program.Main()
当我再次尝试打印时,大约 6 秒后出现第二个异常:
信息:
No connection could be made because the target machine actively refused it
堆栈跟踪:
- System.Net.Sockets.Socket.ConnectNoCheck()
- System.Net.Sockets.Socket.Connect()
- InTheHand.Net.Sockets.BluetoothClient.Connect()
- Project.Utility.Util.BluetoothPrintES400()
- Project.Forms.FrmMain.printing()
- Project.Forms.FrmMain.mniNext_Click()
- System.Windows.Forms.MenuItem.OnClick()
- System.Windows.Forms.Menu.ProcessMnuProc()
- System.Windows.Forms.Form.WnProc()
- System.Windows.Forms.Control._InternalWnProc()
- Microsoft.AGL.Forms.EVL.EnterMainLoop()
- System.Windows.Forms.Application.Run()
- Project.Program.Main()