Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8634727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:47:37+00:00 2026-06-12T09:47:37+00:00

The following code is building without errors but when I run the program, the

  • 0

The following code is building without errors but when I run the program, the received data does not show in the output. When I place a breakpoint on the myReceivedLines = port.ReadExisting(); I get:

System.InvalidOperationException.

In the program itself, after restarting my computer it says:

Access to COM5 is denied

What can I do to fix this?

Code:

//Fields
SerialPort port;
string myReceivedLines;

   protected override void SolveInstance(IGH_DataAccess DA)
  {

    List<string> gcode = new List<string>();
    DA.GetDataList(0, gcode);
    string selectedportname = default(string);
    DA.GetData(1, ref selectedportname);
    int selectedbaudrate = default(int);
    DA.GetData(2, ref selectedbaudrate);
    bool connecttodevice=default(bool);
    DA.GetData(3, ref connecttodevice);
    bool sendtoprint= default(bool);
    DA.GetData(4, ref sendtoprint);


    if (!DA.GetDataList(0, gcode)) return;
    if (!DA.GetData(1, ref selectedportname)) return;
    if (!DA.GetData(2, ref selectedbaudrate)) return;
    if (!DA.GetData(3, ref connecttodevice)) return;
    if (!DA.GetData(4, ref sendtoprint)) return;


    port = new SerialPort(selectedportname, selectedbaudrate, Parity.None, 8, StopBits.One); //Create the serial port
    port.DtrEnable = true;   //enables the Data Terminal Ready (DTR) signal during serial communication (Handshaking)
    port.Open();             //Open the port
    port.DataReceived += this.portdatareceived;


    if (gcode == null)
    {
        AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Specify a valid GCode");
        return;
    }

    if (connecttodevice == true)
    {
        DA.SetDataList(0, myReceivedLines);
    }

        if (sendtoprint == true)
        {
            foreach (String s in gcode)
            {
                port.WriteLine(s);
            }
        }

              }


    //subscriber method for the port.DataReceived Event
    private void portdatareceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
            myReceivedLines = port.ReadExisting();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:47:38+00:00Added an answer on June 12, 2026 at 9:47 am

    The SerialPort’s DataReceived Event operates on a different thread than the UI thread. You will need to do something like the MSDN Page for The DataReceived Event suggests. The reason why you get the access denied error is because the port is still open, reboot your computer to correct restarting your program is not enough.

    i.e. from above link

    private static void DataReceivedHandler( object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code but I'm building a responive site so the height
I am building an XML document successfully with the following code: public function build($result)
I'm using the following code within XCode, building for iOS with ARC enabled. Why
I'm building a batch request for the Facebook API by looping the following code:
I’m building a blog engine in C# Webforms and I have the following code
I have following code return (EseshEntities.Current.Users.Select(u => new { Comunity = u.Apartment.Building.District.City })).ToList(); if
I'm building an API for a web app I'm developing, and the following code
More than one time I have encountered the following problem when building code with
I'm building a category manager via database. The following PHP generates the code I'll
I am getting a very confusing compiler error when building the following test code.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.