Symbol MT2070扫描仪接收来自PC的数据

本文关键字:PC 数据 MT2070 扫描仪 Symbol | 更新日期: 2023-09-27 18:25:54

我有一台MT2070扫描仪,它需要与PC应用程序通信(发送和接收)。我已经使用Symbol.MT2000.ScannerServices程序集中的ScannerServicesClient.SendLabel从扫描仪发送到PC。

然而,我没有成功地接收到从电脑发送的用于扫描仪的数据。我在Symbol.MT2000程序集中没有找到任何类似于处理此问题的内容,也没有找到任何接收数据的示例。

如果有人能给我举个例子,或者知道我如何做到这一点,我将不胜感激。

private readonly SerialPort _port = new SerialPort()
private void SetupPort()
{
  _port.PortName = "COM1";
  _port.BaudRate = 9600;
  _port.DataBits = 8;
  _port.Parity = Parity.None;
  _port.StopBits = StopBits.One;
  try
  {
    _port.Open();
    _port.DataReceived += PortDataReceived;
    _port.ErrorReceived += PortErrorReceived;
  }
  catch (Exception Ex)
  {
    OnCommsMessage("Exception opening port: " + Ex.Message);
  }
}
private void PortDataReceived(object sender, SerialDataReceivedEventArgs e)
{
  OnCommsMessage("PortDataReceived");
}
private void PortErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
  OnCommsMessage("PortErrorReceived");
}

DataReceived似乎从未被引发。使用SerialPort.GetPortNames()可获得COM1、COM2、COM4、COM5、COM9、$device''COM21和$device''COM 23。我已经尝试将端口设置为除最后两个之外的所有端口(不确定我是否应该为这些端口添加一些设备名称)

更新

工作溶液

internal class MT2000CradleCommunicator : BaseCradleCommunicator
{
private readonly ScannerServicesClient _scannerServicesClient = new ScannerServicesClient();

public override void Start()
{
  if (_scannerServicesClient.Connect(true))
  {
    OnCommsMessage("ScannerServicesClient Connected");
    SetRawMode();
  }
  else OnCommsMessage("ScannerServicesClient Failed to connect");
}
public override void Send(string message)
{
  RESULTCODE result = _scannerServicesClient.SendRawData(MessageToRawData(message), 1000);
  if (result == RESULTCODE.E_OK)
  {
    Receive();
  }
  else OnCommsMessage("Error sending data: " + result);
}
private void SetRawMode()
{
  const int ATTRIBUTE_NUM_WIRED_HOST_NUM = 383;
  const byte API_HOST_RAW = 18;
  ScannerHostParameters hostParameters = new ScannerHostParameters(_scannerServicesClient);
  RawParameters rawHostParameters;
  RESULTCODE result = hostParameters.GetRawParameters(out rawHostParameters);
  if (result == RESULTCODE.E_OK)
  {
    rawHostParameters.Type = RawParameters.RawHostType.RS232;
    rawHostParameters.BaudRate = RawParameters.RawBaudRates.RAWSERIAL_9600;
    result = hostParameters.StoreRawParameters(rawHostParameters);
    if (result != RESULTCODE.E_OK)
      OnCommsMessage("Set Parameters failed: " + result);
  }
  else OnCommsMessage("GetParams failed: " + result);
  byte wHostNum;
  result = _scannerServicesClient.GetAttributeByte(ATTRIBUTE_NUM_WIRED_HOST_NUM, out wHostNum);
  if (result == RESULTCODE.E_OK)
  {
    OnCommsMessage("Get host: " + wHostNum);
    result = _scannerServicesClient.SetAttributeByte(ATTRIBUTE_NUM_WIRED_HOST_NUM, API_HOST_RAW);
    if (result != RESULTCODE.E_OK)
      OnCommsMessage("Set host failed: " + result);
  }
  else OnCommsMessage("Get host failed: " + result);
}
private static RawData MessageToRawData(string message)
{
  byte[] bytes = Encoding.ASCII.GetBytes(message);
  return new RawData(bytes, bytes.Length, 1);
}
private void Receive()
{
  RawData rawData;
  RESULTCODE result = _scannerServicesClient.ReadRawData(out rawData, 5000);
  if (result == RESULTCODE.E_OK)
  {
    OnCradleMessageReceived(BytesToAsciiString(rawData.Data));
  }
  else
  {
    OnCommsMessage("Comms timeout: Failed to receive data");
  }
}
private static string BytesToAsciiString(byte[] data)
{
  return Encoding.ASCII.GetString(data, 0, data.Length);
}

}

Symbol MT2070扫描仪接收来自PC的数据

不确定您是否还需要MT2070问题的答案——有一种叫做ReadRawData的方法可以从计算机中读取。

此外,它需要处于原始模式,使用类似于的东西

myScannerSvcClient.SetAttributeByte(
    (ushort)ATTRIBUTE_NUMBER.ATT_MIA_HOSTNUM,
    (byte)ENUM_HOSTS.HOST_RAW
    );

此外,如果你使用蓝牙SPP,这将不起作用。需要通过支架(如果您有)或直接连接到USB电缆。

在inthehand.com#2252中有一个很好的通用示例。

此外,在堆栈溢出问题1528379中也有一个很好的例子。他说,他放弃了内置的COM端口,并使用串行端口连接来建立连接。这似乎也是我过去做过的路线,而且它也没有将您与Symbol的专有DLL联系起来。

编辑:

我不知道这是否有帮助,但这是我几年前写的一段旧代码。

你必须清理它,这样它才能为你的应用程序工作。我的应用程序有一个TextBox控件,它从中读取并将错误记录到全局类中。改变这一点,用你现有的东西工作,基本上应该是好的。

static class Scanner {
  const string _CODEFILE = "Scanner.cs - Scanner::";
  static int _baud = 9600;
  static int _bits = 8;
  static string _dataIn = null;
  static string _port = "COM1";
  static Parity _parity = Parity.None;
  static StopBits _stop = StopBits.One;
  static SerialPort _com1 = null;
  static TextBox _textbox = null;
  public enum ControlType { None, BadgeID, PartNumber, SerialNumber, WorkOrder };
  static ControlType _control;
  public static bool Available { get { return ((_com1 != null) && (_com1.IsOpen)); } }
  public static bool Close {
    get {
      if (_com1 == null) return true;
      try {
        if (_com1.IsOpen) {
          _com1.Close();
        }
        return (!_com1.IsOpen);
      } catch { }
      return false;
    }
  }
  public static string Open() {
    const string na = "Not Available";
    if (_com1 == null) {
      string reset = Reset();
      if (!String.IsNullOrEmpty(reset)) return reset;
    }
    try {
      _com1.Open();
      return (_com1.IsOpen) ? null : na;
    } catch (Exception err) {
      return err.Message;
    }
  }
  static void ProcessData(string incoming) {
    _dataIn += incoming;
    if ((_control != ControlType.None) && (_textbox != null)) {
      bool ok = false;
      string testData = _dataIn.Trim();
      switch (_control) {
        case ControlType.BadgeID:
          if (testData.Length == 6) {
            if (testData != BarCode.LOGOFF) {
              Regex pattern = new Regex(@"[0-9]{6}");
              ok = (pattern.Matches(testData).Count == 1);
            } else {
              ok = true;
            }
          }
          break;
        case ControlType.PartNumber:
          if (testData.Length == 7) {
            Regex pattern = new Regex(@"[BCX][P057][0-9]{5}");
            ok = (pattern.Matches(testData).Count == 1);
          }
          break;
        case ControlType.SerialNumber:
          if (testData.Length == 15) {
            Regex pattern = new Regex(@"[BCX][P057][0-9]{5} [0-9]{4} [0-9]{2}");
            ok = (pattern.Matches(testData).Count == 1);
          }
          break;
        case ControlType.WorkOrder:
          if (testData.Length == 6) {
            Regex pattern = new Regex(@"[0-9]{6}");
            ok = (pattern.Matches(testData).Count == 1);
          }
          break;
      }
      if (ok) {
        _textbox.Text = testData;
        _textbox.ScrollToCaret();
        _dataIn = null;
      }
    }
  }
  static string Reset() {
    if (_com1 != null) {
      try {
        if (_com1.IsOpen) {
          _com1.DiscardInBuffer();
          _com1.Close();
        }
      } catch (Exception err) {
        return err.Message;
      }
      Global.Dispose(_com1);
      _com1 = null;
    }
    try {
      _com1 = new SerialPort(_port, _baud, _parity, _bits, _stop);
      _com1.DataReceived += new SerialDataReceivedEventHandler(Serial_DataReceived);
      _com1.Open();
    } catch (Exception err) {
      return err.Message;
    }
    return null;
  }
  public static void ScanSource(ref TextBox objTextBox, ControlType objType) {
    _textbox = objTextBox;
    _control = objType;
    _dataIn = null;
  }
  static void Serial_DataReceived(object sender, SerialDataReceivedEventArgs e) {
    ProcessData(_com1.ReadExisting());
  }
  public static void Settings(string ComPort, int BaudRate, Parity ParityValue, int Bits, StopBits StopBit) {
    _port = ComPort;
    _baud = BaudRate;
    _parity = ParityValue;
    _bits = Bits;
    _stop = StopBit;
  }
  /// <summary>
  /// Closes the COM Port
  /// COM Port routines are ready to add as soon as I am
  /// </summary>
  static bool ComPortClose {
    get {
      if (_com1 == null) ComPortReset();
      return ((_com1 == null) ? true : _com1.IsOpen ? false : true);
    }
    set {
      if (_com1 == null) ComPortReset();
      else if (_com1.IsOpen) {
        _com1.DiscardInBuffer();
        _com1.Close();
      }
    }
  }
  /// <summary>
  /// Opens the COM Port
  /// </summary>
  static bool ComPortOpen {
    get {
      if (_com1 == null) ComPortReset();
      return (_com1 == null) ? false : _com1.IsOpen;
    }
    set {
      if (_com1 == null) ComPortReset();
      if ((_com1 != null) && (!_com1.IsOpen)) _com1.Open();
    }
  }
  /// <summary>
  /// Initialized the Serial Port on COM1
  /// </summary>
  static void ComPortReset() {
    if ((_com1 != null) && (_com1.IsOpen)) {
      _com1.Close();
      _com1 = null;
    }
    try {
      _com1 = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
    } catch (IOException err) {
      Global.LogError(_CODEFILE + "ComPortReset", err);
    }
  }
}