Link to home
Start Free TrialLog in
Avatar of jmkotman
jmkotmanFlag for United States of America

asked on

Serial Ports and Threading

I am writing a code that will output a set of bit array through the com port and too hyperterminal on another computer.  I need to have this transfer till i press stop button on my GUI interface.  I know we need to use threading to do this task.  The code below outputs the bits to hyperterminal once.  I would like to know how to make it a constant loop till i press stop.  Suggestions would be wonderful.

 public void TransmitData()
        {
            byte[] bytearray = new byte[512];
            try
            {
                SerialPort port = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
                port.Open();
                for (int i = 0; i < 512; i++)
                {
                    bytearray[i] = (byte)array[i];
                }
                port.Write(bytearray, 0, 512);
                port.Close();
            }
            catch
            {
                MessageBox.Show("Please Connect Serial Cable");
            }
        }
Avatar of so3
so3
Flag of Romania image

Put 2 buttons on the form.
  private void button1_Click(object sender, EventArgs e)
        {
            TransmitData();
        }
 
        bool working = true;
        public void TransmitData()
        {
            byte[] bytearray = new byte[512];
            try
            {
                working=true;
                SerialPort port = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
                port.Open();
 
                while (working)
                {
                    Application.DoEvents();
                    if (!working) break;
                    port.Write("write data");
                }
                port.Close();
            }
            catch
            {
                MessageBox.Show("Please Connect Serial Cable");
            }
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            working = false;
        }

Open in new window

Avatar of Dimkov
Dimkov

make while(1) loop inside the function
Created thread which points to this function.

Then you have command like thread.start, thread.stop, that tell when the thread sholud work or stop

just an idea :)
ASKER CERTIFIED SOLUTION
Avatar of gnoon
gnoon
Flag of Thailand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jmkotman

ASKER

I just implamented So3's code and it worked, but does that have anything to do with threading?  I am implamenting gnoon's code and i am receiving the compile error The best overloaded method match for 'System.threading.thread.thread(System.threading.threadstart)' has some invalid arguments.  This error is refering to: System.Threading.Thread t = new System.Threading.Thread(this.TransmitData);

Thanks for your help
No, my code is not with threading because i don't think that you need it. From what i understood you need to use only one function (with start and stop function). For what you need it will not take the processor so you can use it without threading(and also is a easy solution)
>i am receiving the compile error The best overloaded method match for 'System.threading.thread.thread(System.threading.threadstart)' has some invalid arguments
Sorry for that. I did not test it in VS before post.

Compile error because TransmitData does not matched with signature function needed by ParameterizedThreadStart class. It requires a function having a parameter of type Object.

Change the code of TransmitData to this, you will get it compiled

public void TransmitData(object param)
{
    byte[] data = (byte[]) param;
    try
    {
        ....