-2

I have been trying to stop serial communication with port.Stop and port.Dispose() but still communication doesn't stop when established once.

Here is my Code

Start Method to send data continuously till port is open

public void Start(List<byte> RGBdata)
{           
    if (!m_port.IsOpen)
    {
        m_port.Open(); -- it fails over here when reloaded
    }           
    do
    {
        Break();
        Thread.Sleep( 5 );

        m_port.Write( new byte[] { 0 }, 0, 1 );
        SendData(RGBdata);
        Thread.Sleep( 1);
    }
    while (m_port.IsOpen());
}

Break Method

    private void Break()
    {
        m_port.BreakState = true;
        Thread.Sleep( 1 );
        m_port.BreakState = false;

    }

SendData Method

    private void SendData(List<byte> data)
    {         

        m_port.Write( data.ToArray(), 0, data.Count );
    }

Stop Method

public void Stop()
{
    m_port.Close();

    if (m_port.IsOpen)
    {
        m_port.Close();
        m_port.Dispose();
    }
}
Alok Sharma
  • 85
  • 1
  • 5
  • There are some issues with your code. Can you post the code for `SendData` and `Break` as well? – Stefan Feb 19 '16 at 09:39
  • Your timing mechanism, `Thread.Sleep 1 and 5`, has really low values. Normally `Thread.Sleep` has a resolution of ~30ms. – Stefan Feb 19 '16 at 09:41
  • There is no multi-threading going on in this code. Are you sure you are using multiple threads? If not `while (m_port.IsOpen());` will always be true. As a side note its better to raise a flag to indicate a stop. – Stefan Feb 19 '16 at 09:42
  • Ya , that's what my issue is , I have two buttons Start and Stop functionality. On start Button it starts communication and loop continuous forever . So Even on Stop click it doesn't stop communication as it won't enter in while loop ,once established – Alok Sharma Feb 19 '16 at 09:47
  • One more thing Stefan, why stop command doesn't work and dispose , I mean it should stop communication whatever is status – Alok Sharma Feb 19 '16 at 09:49
  • Did you managed to fix this? – Stefan Sep 19 '18 at 07:55

1 Answers1

0

As stated in the comments, you'll need a thread and a signal flag to indicate a stop condition:

First create a thread:

List<byte> gRGBdata;
bool stopRequested = false; //signaler flag
public void Start(List<byte> RGBdata)
{     
    gRGBdata = RGBdata; //copy to "global" variable

   //start thread
   //reset flag first
   stopRequested = false;
   Thread t = new Thread (new ParameterizedThreadStart(yourThread));
   t.Start();
}

And implement it:

public void yourThread()
{      
    if (!m_port.IsOpen)
    {
         m_port.Open(); -- it fails over here when reloaded
    }           
    do
    {
        Break();
        Thread.Sleep(50);

        m_port.Write( new byte[] { 0 }, 0, 1 );
        SendData(RGBdata);
        Thread.Sleep(30);
    }
    while (!stopRequested);

    if (m_port.IsOpen)
    {
         m_port.Close();
         m_port.Dispose();
    }
}

At the stop command, just signal a flag:

public void Stop()
{
    stopRequested = true;
}

Be advised, the stop command only signals. The buffers will be flushed to the serial-port. If the flag is raised communication will not stop instantly but there will be no new Send commands until you Start again.

There are still a lot of issues in this code, but I hope it gives you a better understanding of the way multithreading and serial ports should be used. Basically: don't communicate with the serial port (or other singleton-like hardware components) from different threads.

WARNING: this code will fail if Start is called multiple times when the tread is still active.

Stefan
  • 17,448
  • 11
  • 60
  • 79
  • Sure, Thank u very much Stefam, let me try this – Alok Sharma Feb 19 '16 at 09:54
  • There are still ton's of fixes to be done, e.g.: checking a runner flag to prevent multiple thread starts. And then, preventing the fast click scenario. Maybe some `lock` statements will help you there. – Stefan Feb 19 '16 at 09:56
  • No my problem exist. I have implemented everything ,i am not able to stop communication through port . DiscardOutbuffer,DiscardInBuffer doesn't work. And whenever i try to access port again , it says Access to COM Port is denied. I need to remove USB cable again and again to send data – Alok Sharma Feb 21 '16 at 15:14
  • You must ensure that com port closes from that thread. DiscardBuffers wont work. How much bytes do you send? – Stefan Feb 22 '16 at 07:53
  • Ya Stefan, I send (512 * 3 = 1536 bytes). I tried to close with that thread but it is not accessible . For e.g I created this thread in start method. Thread t = new Thread (new ParameterizedThreadStart(yourThread)); t.Start(); But when i try to t.Abort in Stop method , the thread is not accessibe as t.Abort – Alok Sharma Feb 22 '16 at 09:13
  • Don't use thread.Abort. That requires thread-abort-exception handling in the thread. Use the boolean to signal a close. 512 * 3 isn't very big, so that won't give any problems :-) – Stefan Feb 22 '16 at 09:39
  • How do you initialize `m_port`? (and where?) – Stefan Feb 22 '16 at 09:39
  • I intitalize at my constructor as public DMX() {Serial Port m_port = new SerialPort("COM2",'250000',Parity bits,etc) m_port.Handshake = Handshake.None } . Boolean flag doesn't work for me. I see that once thread starts m_port.Write ,even signalling flag doesn't stop it. I have tried it many times – Alok Sharma Feb 22 '16 at 09:59
  • if `m_port.Write` only wrtites 3 * 512 bytes at a time, the stop flag should work. So, does the debugger hit the ` while (!stopRequested);` at all? And does it hit: `stopRequested = true;`? – Stefan Feb 22 '16 at 10:35
  • Yes it hit and stopRequested = true happens and also it gets exit from while loop but >> private void SendData(List data) { m_port.Write( data.ToArray(), 0, data.Count ); } >>This Write method doesn't stop .It writes continuously and when i try to access port COM2 it says "Access is denied ", which means port is continuously used even after port.Close() happens. Thanks for your help. See similar case over here >> http://stackoverflow.com/questions/768032/serialport-write-how-to-stop-writing – Alok Sharma Feb 22 '16 at 11:28