Menu

open & close com port several times cause receive issue

Anonymous
2024-01-03
2024-01-04
  • Anonymous

    Anonymous - 2024-01-03

    Dear all,
    I'm using a worker script to communicate using a serial port.
    I've 2 functions to open and close the serial port: this is mandatory for my application because of I've to switch from 2 different baudrates and protocols.
    This is part of my script:

    // Start the serial communication
    function comInit(baudrate = 115200,  rxCallback = comRxCallback)
    {
        // Disconnect the serial communication port
        comStop();
    
        // Setup the rx callback
        scriptInf.dataReceivedSignal.connect(rxCallback);
    
        // Get the current serial communication setup
        var comSettings = scriptInf.getMainInterfaceSerialPortSettings ();
    
        // Setup the baudrate
        comSettings.baudRate = baudrate;
    
        // Connect the serial communication port
        scriptInf.connectSerialPort (comSettings.name, comSettings.baudRate);
    }
    
    // Stop the serial communication
    function comStop()
    {
        // Disconnect the serial communication port
        scriptInf.disconnect ();
    }
    
    // Is called if a byte is receveid on the serial communication port
    function comRxCallback(data)
    {
        for (var i = 0;i < data.length;i++)
        {
            comRxData.push(data[i]);
        }
    }
    
    // My script
    comInit(4800, comRxCallback); // Receive after this call is ok
    comStop();
    comInit(4800, comRxCallback); // Receive after this call is not working fine
    

    If I call "comInit()" just 1 time, all comes fine: I receive the frames correctly.
    After calling "comStop()" and "comInit()" again, the receive operation doesn't work properly: it seems the receiveid characters are duplicated.
    Fill free to ask me any kind of clarification.
    KR.

    GDM

     
  • Stefan Zieker

    Stefan Zieker - 2024-01-03

    Hi,

    you have to call scriptInf.dataReceivedSignal.disconnect(rxCallback) or call connect only once. If you don't do this the signal is connected several times to the slot function and therefore the slot function will be called several times.

     
  • Anonymous

    Anonymous - 2024-01-04

    Hi Stefan,
    thank you for your kind reply.
    I confirm your solution solves the "issue".
    I just inform you the "disconnect()" method seems not documented.
    KR

     
  • Stefan Zieker

    Stefan Zieker - 2024-01-04

    I will add this to the manual for the next release. Thx for reporting this.

     

Anonymous
Anonymous

Add attachments
Cancel