Menu

Establish autoconnection with the scanner

Sync tool
PEER23PEER
2011-05-22
2012-07-13
  • PEER23PEER

    PEER23PEER - 2011-05-22

    The code below should establish an auto connection.
    It cycles through all available serial comms and sees if it gets an connection and sends a handshake to see if it is the david laserscanner

    the code for the PC
    the constructor for SYNC.dll

            public SYNC()
            {
                foreach (string PN in System.IO.Ports.SerialPort.GetPortNames())
                {
                    try
                    {
                        _sp = new SerialPort(PN, 115200, Parity.None, 8, StopBits.One);
                        _sp.Open();
                        if (_sp.IsOpen == true)
                        {
                            _sp.WriteLine("ENTERSYNCMODE");
                            System.Threading.Thread.Sleep(100);
                            string davidHandshake;
                            if (_sp.BytesToRead > 0)
                            {
                                davidHandshake = _sp.ReadTo("!");
                                if (davidHandshake == "13\r\nENTERSYNCMODE")
                                {
                                    break;
                                }
                            }
                            else
                            {
                                _sp.Close();
                            }
                        }
                    }
                    catch
                    {
                        _sp.Close();
                    }
    
                }
                if (_sp.IsOpen)
                {
                    _sp.DiscardNull = false;
                    _sp.DataReceived += new SerialDataReceivedEventHandler(_sp_DataReceived);
                }
                else
                {
                    this.noConnectionEstablished(this, new EventArgs()); 
                }
            }
    

    the code for the arduino in the main loop

        if (Serial.available() > 12)
        {
            Serial.println(Serial.available());
            Serial.println("ENTERSYNCMODE!");
            Serial.flush();
        }
    
     
  • PEER23PEER

    PEER23PEER - 2011-06-02

    Rewrote the code for the constructor and the handshake routine

    Constructor

            public Sync()
            {
                foreach (string portName in SerialPort.GetPortNames())
                {
                    try
                    {
                        _sp = new SerialPort(portName, 115200, Parity.None, 8, StopBits.One);
                        _sp.Open();
                    }
                    catch
                    {
                        _sp.Close();
                        _sp.Dispose();
                    }
    
                    try
                    {
                        if (_sp.IsOpen && _handShake()) { break; } 
                        else 
                        { 
                            _sp.Close();
                            _sp.Dispose();
                        }
                    }
                }
                if (!_sp.IsOpen) { throw new ArgumentException("No Davidscanner connected"); }
                if (_sp.IsOpen && !_inSync) { throw new ArgumentException("Could not enter SYNCMODE"); }
            }
    

    Private _handShake method

            private bool _handShake()
            {
                bool handshakeEstablished = false;
                _sp.ReceivedBytesThreshold = 14;
                try
                {
                    _sp.WriteLine("ENTERSYNCMODE");
                    System.Threading.Thread.Sleep(100);
                    string davidHandshake;
                    if (_sp.BytesToRead > 0)
                    {
                        davidHandshake = _sp.ReadTo("!");
                        if (davidHandshake == "ENTERSYNCMODE") { handshakeEstablished = true; }
                    }
                }
                catch (ArgumentException)
                {
                    throw new ArgumentException("Could not enter SYNCMODE");
                }
                _inSync = handshakeEstablished;
                _sp.ReceivedBytesThreshold = 8;
                return handshakeEstablished;
            }
    

    Arduino Code Main loop

        if (Serial.available() > 12)
        {
            Serial.println("ENTERSYNCMODE!");
            Serial.flush();
            delay(10);
            control.blink(syncBlink);
            SyncMode();
        }
    

    Arduino Code SYNC.cpp

    byte SYNC::Recieve()
    {
        control.blink();
        delay(10);
    
        if (Serial.available() > 7)
        {
            byte counter = 8;
            byte incommingBlock[8];
    
            for (int i = 0; i < counter; i++)
            {
                incommingBlock[i] = Serial.read();
            }
    
            Serial.flush();
    
            if (incommingBlock[0] == 2 && incommingBlock[7] == 3)
            {
                switch (incommingBlock[1])
                {
                case 0:     //Command
                    break;
                case 1:     // Char
    
                    break;
                case 2:     // Byte
    
                    break;
                case 3:     // Int
    
                    break;
                case 4:     // Long
                    break;
                case 5:     // GetSettings
                    Send('C', incommingBlock[2]);
                    break;
                case 6:
                        isFinished = true;
                    break;
                }
    
            }
        }
    
        return 0; 
    }
    
     

Anonymous
Anonymous

Add attachments
Cancel