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 8961901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:59:57+00:00 2026-06-15T15:59:57+00:00

I’m making a Gmail notifier with C# and Arduino. The code checks whether it

  • 0

I’m making a Gmail notifier with C# and Arduino.
The code checks whether it received mail and sends ‘m’ to serial if so.
It seems to work pretty well, but the problem is after I get this message once,

Unread mails: 0

this message keeps to print on the screen:

‘COM5’ port access denied.
‘COM5’ port access denied.
…

which obviously means that access to COM5 is denied after the code’s first trial.
I tried to add port.Close(); to my code, but then the code doesn’t loop after the second trial.
Kinda stuck here.
What seems to be the problem with my code?
Similar question seems to be on the web( Access to the port 'COM5' is denied ) but I don’t really understand how I could “wrap the use of SerialPort”.
Thank you!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Threading;
using System.Net;
using System.Xml;

namespace GMailNotifier
{
    class GMail
    {
        public static void Main(string[] args)
        {
            while (true)
            {
                try
                {
                    SerialPort port = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);
                    port.Open();

                    string Unreadz = "0";
                    Unreadz = CheckMail();
                    Console.WriteLine("Unread Mails: " + Unreadz);

                    if (!Unreadz.Equals("0")) port.Write("m");
                    else port.Write("n");
                }catch (Exception ee){ Console.WriteLine(ee.Message);
                }finally { System.Threading.Thread.Sleep(1000); }
            }
        }
    private static string CheckMail() {
        string result = "0";

        try {
        var url = @"https://gmail.google.com/gmail/feed/atom";
        var USER = "USER";
        var PASS = "PASS";

        var encoded = TextToBase64( USER + ":" + PASS );

        var myWebRequest = HttpWebRequest.Create( url ); 
        myWebRequest.Method = "POST";
        myWebRequest.ContentLength = 0;
        myWebRequest.Headers.Add( "Authorization", "Basic " + encoded );

        var response = myWebRequest.GetResponse();
        var stream = response.GetResponseStream();

        XmlReader reader = XmlReader.Create( stream );
        while ( reader.Read() )
            if ( reader.NodeType == XmlNodeType.Element )
            if ( reader.Name == "fullcount" ) {
                result = reader.ReadElementContentAsString();
                return result;
            }
        } catch ( Exception ee ) { Console.WriteLine( ee.Message ); }
        return result;
    }
    public static string TextToBase64(string sAscii){
        System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
        byte[] bytes = encoding.GetBytes(sAscii);
        return System.Convert.ToBase64String(bytes, 0, bytes.Length);
    }
  }
}
  • 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-15T15:59:58+00:00Added an answer on June 15, 2026 at 3:59 pm

    I don’t really understand how I could “wrap the use of SerialPort”.

    It means to do something like this:

    using (SerialPort port = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One))
    {
        port.Open();
    
        string Unreadz = "0";
        Unreadz = CheckMail();
        Console.WriteLine("Unread Mails: " + Unreadz);
    
        if (!Unreadz.Equals("0")) port.Write("m");
        else port.Write("n");
    }
    

    The using statement will ensure that the SerialPort.Dispose method is called, to release the port after you have finished using it. If you don’t release it after the first time, then the hardware is still busy and you won’t be able to open it again (until it’s garbage-collected later).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm making a simple page using Google Maps API 3. My first. One marker

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.