符号MT2070扫描仪从PC接收数据

时间:2012-02-27 20:02:19

标签: c# compact-framework

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

然而,在接收从用于扫描仪的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 \ COM23。我已经尝试将端口设置为除最后两个之外的所有端口(不确定我是否应该为那些添加一些设备名称)

更新

工作解决方案

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);
}

}

2 个答案:

答案 0 :(得分:0)

inthehand.com #2522有一个很好的通用示例。

此外,Stack Overflow Question 1528379还有一个很好的例子。他说他放弃了内置的COM端口并使用串行端口连接来建立他的连接。这似乎也是我过去所做的路线,也不会影响到Symbol的专有DLL。

修改

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

您必须对其进行清理,以使其适用于您的应用程序。我的应用程序有一个TextBox控件,它从Global类读取并记录错误。改变它以适应你拥有的东西,它应该基本上是好的。

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);
    }
  }

}

答案 1 :(得分:0)

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

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

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

另外,如果您使用的是蓝牙SPP,则无法使用。需要通过支架(如果有的话)或直接连接USB电缆。