CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2011
    Posts
    59

    Serial port & thread use & non thread use

    Okay, I have a serial port that I am using in a thread to send / receive / display data automatically. Now I am trying to send a different set of data on command but I am getting an exception access to port is denied.

    I am sure the thread is using the port and that is not allowing me to access it when the user needs it. Does anyone know of anyways to get around this?

  2. #2
    Join Date
    Mar 2011
    Posts
    59

    Re: Serial port & thread use & non thread use

    So I have changed a couple things. I no longer call a separate instance of serialport.

    form1 < no instance of serial port
    form3 < instance of serial port being used by thread

    form1 < needs to write to serial port < calling from from3

    I can successfully call the form3 instance of serialport to be written to from form1.

    Although I have made these changes the result is still the same. I feel I either need to be able to pause the thread from another form on command or I need put the function into the thread and call it from another form? Im really at a loss for this and I find nothing on the net that will help me with with I need to accomplish. Thanks again for any help.

  3. #3
    Join Date
    Mar 2011
    Posts
    59

    Re: Serial port & thread use & non thread use

    I was able to call the function from my thread that could handle the writing to the serial port that I need. Same result. Access denied. I only have 1 serial port to work with, so how can I use it from multiple forms / threads?


    EDIT: Now I am hoping to create a seperate thread to call my function as it is needed but I am having a problem here

    Thread SW = new Thread(new ThreadStart(dataDisplay.serialWriter(data)));
    the error i get says method name expected if I write it as such

    Thread SW = new Thread(new ThreadStart(dataDisplay.serialWriter()));
    I get an error telling me that no overload for serialWriter takes 0 args (this I understand because I do need to send some bytes after it, data is a byte array)
    Last edited by DKS1282; April 12th, 2011 at 02:24 PM.

  4. #4
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    Re: Serial port & thread use & non thread use

    It's quite a coincidence that you should raise this issue just now .... I've been working with C# and C++ since 2006 but just last week and this, I've begun working with the serial port for the very first time.

    It seems to be working very well indeed but I'm not doing anything as complex as what you're doing.

    Consequently, with so little experience to draw on, I am hardly in a position to suggest ANY course of action, but my first thought would be to have a single dedicated thread take ownership of the comm port and provide a mechanism whereby other threads wishing to use the comm port place a Channel Control Block (CCB) in the owner thread's list.

    The CCB contains the string to be transmitted, a string to receive the reply (if any), a delegate pointing to a function to be called when the reply (CCB) is returned, and whatever else you think you might need.

    In that way, only one thread, the owner, is attempting to access the comm port although all are using it.

    I dunno ... might work, maybe.
    Last edited by ThermoSight; April 12th, 2011 at 10:51 PM.

  5. #5
    Join Date
    Mar 2011
    Posts
    59

    Re: Serial port & thread use & non thread use

    Quote Originally Posted by ThermoSight View Post
    but my first thought would be to have a single dedicated thread take ownership of the comm port and provide a mechanism whereby other threads wishing to use the comm port place a Channel Control Block (CCB) in the owner thread's list.
    I am not nearly as experienced as you in C sharp or C++, what exactly is a CCB, I have failed to find any topic on the internet.


    Anyone else have any idea why im having an issue reading or how I can tell the other thread to hold on while this thread writes first?

    I basicly want my automated thread to always be sending to the serial port except when users have data they would like to input. I would then like the automated thread to wait till the data from the user was sent. Looks like im for an all nighter with a rewrite?
    Last edited by DKS1282; April 14th, 2011 at 08:01 PM.

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Serial port & thread use & non thread use

    Channel Control Block (CCB)
    He explained it pretty well, other than mentioning that the CCB would be a BUFFER. Use a Stream to read and write to the port
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    Re: Serial port & thread use & non thread use

    Certainly not to belabor the point, but to offer an explanation, the term CCB goes back at least to the IBM System 360 family of machines and RCA's 360-clone, the Spectra 70 series. And it certainly may go back to even earlier times.

    Both machine families had selectors (high speed channels) which used hardware CCBs (register collections) to keep track of everything associated with the requested data transfer, in or out.

    In my earlier years in the biz I was a hardware guy and worked for RCA in Cherry Hill, NJ and Boston, MA .... hence my fondness for, and attachment to, ancient computer hardware. Indeed, I STILL have the schematics for RCA's Spectra 70/55 machine, with all of my sweat-stained notes in the margins.

    Best wishes.

    OldFool

  8. #8
    Join Date
    Mar 2011
    Posts
    59

    Re: Serial port & thread use & non thread use

    Thanks for all the advice! Looks like im going to have to rewrite this portion of code so only 1 thread handles all of the serial port communications. Other threads will load data into a queue and the serial port thread will get the data when loaded and send it, then receive it. Thanks again!

  9. #9
    Join Date
    Mar 2011
    Posts
    59

    Re: Serial port & thread use & non thread use

    Even in my rewrite im having issues.

    I have a thread that generates the same 18 bit array to send to a controller card. When this array is completed it is to change a bool in a seprate class to true. Now I have a Thread to control the serial port. This thread checkes to see if the bool is true, if its true it gets the array thats been passed to a function where it can receive the data. The problem is for my serialport thread the bool is NEVER true. Even after the first thread sets it to true. Any Ideas as to why this is happening?

    // class dataCollector

    public bool dataToSend = false;

    // thread 1
    dataCollector dc = new dataCollector
    // After filling array
    dc.dataToSend = true;

    // thread 2
    bool loop = true;
    const int arrayIndex = 19; // 1 extra for function location
    byte[] sData = new byte[arrayIndex];
    while (loop)
    {
    if (dc.dataToSend)
    {
    dc.dataToSend = false;
    sData = dc.send();
    try
    {
    if (!serialPort1.IsOpen)
    {
    serialPort1.Write(sData, 0, 18);
    }
    else
    {
    serialPort1.Open();
    serialPort1.Write(sData, 0, 18);
    MessageBox.Show("Data Sent!");
    }
    }
    catch (Exception ex)
    {
    MessageBox.Show("Serial Controll Error " + ex.Message);
    }
    }
    }
    EDIT: After watching the bool that I am trying to change it is ALWAYS true in the watch once set to true, but when my serial thread checks it, even though the value is true its getting a false return. Why would this be?
    Last edited by DKS1282; April 15th, 2011 at 09:45 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured