0

I have a MT2070 scanner that needs to communicate to a PC app (send and receive). I have got the sending from the scanner to the PC going using ScannerServicesClient.SendLabel in the Symbol.MT2000.ScannerServices assembly.

I have not had any success however in receiving data sent from the PC intended for the scanner. I haven't managed to find anything in the Symbol.MT2000 assemblies that look like handling this or have I found any examples for receiving data.

If anyone can point me to an example or knows how I can achieve this it would be much appreciated.

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 never seems to be raised. Using SerialPort.GetPortNames() gives me COM1,COM2,COM4,COM5,COM9,$device\COM21,$device\COM23. I have tried setting the port to all of those except the last two (not sure if I'm supposed to add some device name for those)

UPDATE

Working solution

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

}

JKF
  • 216
  • 5
  • 13
  • Are you sending your data over your wireless network, via Bluetooth, or Infra Red? Is there some sample code that you could post? –  Feb 27 '12 at 22:36
  • @jp2code Bluetooth. No sample code for receiving as haven't even found anything that looks like it might do the job. – JKF Feb 28 '12 at 00:08
  • @JKF, may I get the sample code of sending from the scanner to the PC? Thank you. – soclose Sep 12 '12 at 08:07
  • @soclose added in update above. – JKF Sep 13 '12 at 06:51
  • Thank you, @JKF. I've deployed MC 3000 / 55 / 65. But in MT2070, it could not run CAB file. Please share me some information. – soclose Sep 14 '12 at 09:08

2 Answers2

0

There is a good generic example at inthehand.com #2522.

Also, there is also a good example here in Stack Overflow Question 1528379. He says he gave up on the built in COM Port and used a Serial Port connection to establish his connection. That seems to be the route I've done in the past as well, and it also does not tie you to Symbol's proprietary DLL.

EDIT:

I don't know if this can ever help, but here is an old piece of code that I wrote a few years back.

You'll have to clean it up so that it works for your application. My app had a TextBox control that it read from and logged errors to a Global class. Change that to work with what you have, and it should basically be good.

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

}
Community
  • 1
  • 1
  • I have updated question with sample code which I am still unable to get working – JKF Feb 28 '12 at 21:54
  • I added a big piece of code. Piece? Not really. This was my entire Serial Reading App. –  Feb 28 '12 at 22:59
  • 1
    DataReceived event is not raised for me using your code either. I have marked as answer as you did answer my original question. I appreciate the help. – JKF Feb 29 '12 at 00:25
0

Not sure if you still need an answer for your MT2070 problem -- there's a method called ReadRawData to read from the computer.

Also, it needs to be in Raw mode, using something like:

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

Also also, if you're using Bluetooth SPP this won't work. Needs to be through the cradle (if you have one) or direct to the USB cable.

lowpass
  • 16
  • 2