Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Serial Port communication in C#

Serial Port communication in C#

Scheduled Pinned Locked Moved C#
helpcsharpvisual-studio
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alfie max15
    wrote on last edited by
    #1

    I am trying to start with serial port communication using C# for my project. So even though i am familiar with the C# language i am new into I/O Port communication and i seem to be having some problem with my code

    using System;
    using System.IO.Ports;

    class Program
    {
    public static void Main ()
    {
    string portName = @"\\.\COM10";
    _serialPort = new SerialPort (portName);
    _serialPort.Close ();
    }
    }

    i am getting an error :

    Error 1 The name '_serialPort' does not exist in the current context c:\users\alfie\documents\visual studio 2012\Projects\serial\serial\Serial_port.cs

    Someone pls help me get started with this. . Also i have tested with the following code and verified that the COM10 port is working and is of type "char"

    class Program
    {
    static void Main (string[] args)
    {
    string portName = @"\\.\COM10";
    IntPtr handle = CreateFile (portName, 0, 0, IntPtr.Zero, 3, 0x80, IntPtr.Zero);
    if(handle == (IntPtr) (-1))
    {
    Console.WriteLine ("Could not open " + portName + ": " + new Win32Exception ().Message);
    Console.ReadKey ();
    return;
    }

        FileType type = GetFileType (handle);
        Console.WriteLine ("File " + portName + " reports its type as: " + type);
    
        Console.ReadKey ();
    }
    
    \[DllImport ("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)\]
    public static extern IntPtr CreateFile (string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr SecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
    
    \[DllImport ("kernel32.dll")\]
    static extern FileType GetFileType (IntPtr hFile);
    
    enum FileType : uint
    {
        UNKNOWN = 0x0000,
        DISK = 0x0001,
        CHAR = 0x0002,
        PIPE = 0x0003,
        REMOTE = 0x8000,
    }
    

    }

    B OriginalGriffO D 3 Replies Last reply
    0
    • A alfie max15

      I am trying to start with serial port communication using C# for my project. So even though i am familiar with the C# language i am new into I/O Port communication and i seem to be having some problem with my code

      using System;
      using System.IO.Ports;

      class Program
      {
      public static void Main ()
      {
      string portName = @"\\.\COM10";
      _serialPort = new SerialPort (portName);
      _serialPort.Close ();
      }
      }

      i am getting an error :

      Error 1 The name '_serialPort' does not exist in the current context c:\users\alfie\documents\visual studio 2012\Projects\serial\serial\Serial_port.cs

      Someone pls help me get started with this. . Also i have tested with the following code and verified that the COM10 port is working and is of type "char"

      class Program
      {
      static void Main (string[] args)
      {
      string portName = @"\\.\COM10";
      IntPtr handle = CreateFile (portName, 0, 0, IntPtr.Zero, 3, 0x80, IntPtr.Zero);
      if(handle == (IntPtr) (-1))
      {
      Console.WriteLine ("Could not open " + portName + ": " + new Win32Exception ().Message);
      Console.ReadKey ();
      return;
      }

          FileType type = GetFileType (handle);
          Console.WriteLine ("File " + portName + " reports its type as: " + type);
      
          Console.ReadKey ();
      }
      
      \[DllImport ("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)\]
      public static extern IntPtr CreateFile (string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr SecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
      
      \[DllImport ("kernel32.dll")\]
      static extern FileType GetFileType (IntPtr hFile);
      
      enum FileType : uint
      {
          UNKNOWN = 0x0000,
          DISK = 0x0001,
          CHAR = 0x0002,
          PIPE = 0x0003,
          REMOTE = 0x8000,
      }
      

      }

      B Offline
      B Offline
      Big Daddy Farang
      wrote on last edited by
      #2

      The error message you are getting is telling us that you are using a variable called "_serialPort" but it is never defined. There needs to be a definition of that variable somewhere. Something like:

      SerialPort _serialPort;

      BDF The internet makes dumb people dumber and clever people cleverer. -- PaulowniaK

      A 1 Reply Last reply
      0
      • A alfie max15

        I am trying to start with serial port communication using C# for my project. So even though i am familiar with the C# language i am new into I/O Port communication and i seem to be having some problem with my code

        using System;
        using System.IO.Ports;

        class Program
        {
        public static void Main ()
        {
        string portName = @"\\.\COM10";
        _serialPort = new SerialPort (portName);
        _serialPort.Close ();
        }
        }

        i am getting an error :

        Error 1 The name '_serialPort' does not exist in the current context c:\users\alfie\documents\visual studio 2012\Projects\serial\serial\Serial_port.cs

        Someone pls help me get started with this. . Also i have tested with the following code and verified that the COM10 port is working and is of type "char"

        class Program
        {
        static void Main (string[] args)
        {
        string portName = @"\\.\COM10";
        IntPtr handle = CreateFile (portName, 0, 0, IntPtr.Zero, 3, 0x80, IntPtr.Zero);
        if(handle == (IntPtr) (-1))
        {
        Console.WriteLine ("Could not open " + portName + ": " + new Win32Exception ().Message);
        Console.ReadKey ();
        return;
        }

            FileType type = GetFileType (handle);
            Console.WriteLine ("File " + portName + " reports its type as: " + type);
        
            Console.ReadKey ();
        }
        
        \[DllImport ("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)\]
        public static extern IntPtr CreateFile (string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr SecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
        
        \[DllImport ("kernel32.dll")\]
        static extern FileType GetFileType (IntPtr hFile);
        
        enum FileType : uint
        {
            UNKNOWN = 0x0000,
            DISK = 0x0001,
            CHAR = 0x0002,
            PIPE = 0x0003,
            REMOTE = 0x8000,
        }
        

        }

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Well, you could start by declaring the variable...

        using System;
        using System.IO.Ports;

        class Program
        {
        private SerialPort _serialPort = null; // <<---ADD THIS LINE
        public static void Main ()
        {
        string portName = @"\\.\COM10";
        _serialPort = new SerialPort (portName);
        _serialPort.Close ();
        }
        }

        The only instant messaging I do involves my middle finger.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        A 1 Reply Last reply
        0
        • B Big Daddy Farang

          The error message you are getting is telling us that you are using a variable called "_serialPort" but it is never defined. There needs to be a definition of that variable somewhere. Something like:

          SerialPort _serialPort;

          BDF The internet makes dumb people dumber and clever people cleverer. -- PaulowniaK

          A Offline
          A Offline
          alfie max15
          wrote on last edited by
          #4

          Thanx... :) That worked like a charm... Sorry for my silly question....

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Well, you could start by declaring the variable...

            using System;
            using System.IO.Ports;

            class Program
            {
            private SerialPort _serialPort = null; // <<---ADD THIS LINE
            public static void Main ()
            {
            string portName = @"\\.\COM10";
            _serialPort = new SerialPort (portName);
            _serialPort.Close ();
            }
            }

            The only instant messaging I do involves my middle finger.

            A Offline
            A Offline
            alfie max15
            wrote on last edited by
            #5

            OriginalGriff wrote:

            using System; using System.IO.Ports;   class Program { private SerialPort _serialPort = null; // <<---ADD THIS LINE public static void Main () { string portName = @"\\.\COM10"; _serialPort = new SerialPort (portName); _serialPort.Close (); } }

            I tried this code... but i am getting this error...

            An object reference is required for the non-static field, method, or property 'Program._serialPort'

            OriginalGriffO 1 Reply Last reply
            0
            • A alfie max15

              OriginalGriff wrote:

              using System; using System.IO.Ports;   class Program { private SerialPort _serialPort = null; // <<---ADD THIS LINE public static void Main () { string portName = @"\\.\COM10"; _serialPort = new SerialPort (portName); _serialPort.Close (); } }

              I tried this code... but i am getting this error...

              An object reference is required for the non-static field, method, or property 'Program._serialPort'

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              :doh: Sorry, my bad! I forgot that Main is a static method! Add the word "static" between "private" and "SerialPort"

              The only instant messaging I do involves my middle finger.

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              A 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                :doh: Sorry, my bad! I forgot that Main is a static method! Add the word "static" between "private" and "SerialPort"

                The only instant messaging I do involves my middle finger.

                A Offline
                A Offline
                alfie max15
                wrote on last edited by
                #7

                thanx.... that did the trick...:)

                OriginalGriffO 1 Reply Last reply
                0
                • A alfie max15

                  thanx.... that did the trick...:)

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  You're welcome!

                  The only instant messaging I do involves my middle finger.

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  1 Reply Last reply
                  0
                  • A alfie max15

                    I am trying to start with serial port communication using C# for my project. So even though i am familiar with the C# language i am new into I/O Port communication and i seem to be having some problem with my code

                    using System;
                    using System.IO.Ports;

                    class Program
                    {
                    public static void Main ()
                    {
                    string portName = @"\\.\COM10";
                    _serialPort = new SerialPort (portName);
                    _serialPort.Close ();
                    }
                    }

                    i am getting an error :

                    Error 1 The name '_serialPort' does not exist in the current context c:\users\alfie\documents\visual studio 2012\Projects\serial\serial\Serial_port.cs

                    Someone pls help me get started with this. . Also i have tested with the following code and verified that the COM10 port is working and is of type "char"

                    class Program
                    {
                    static void Main (string[] args)
                    {
                    string portName = @"\\.\COM10";
                    IntPtr handle = CreateFile (portName, 0, 0, IntPtr.Zero, 3, 0x80, IntPtr.Zero);
                    if(handle == (IntPtr) (-1))
                    {
                    Console.WriteLine ("Could not open " + portName + ": " + new Win32Exception ().Message);
                    Console.ReadKey ();
                    return;
                    }

                        FileType type = GetFileType (handle);
                        Console.WriteLine ("File " + portName + " reports its type as: " + type);
                    
                        Console.ReadKey ();
                    }
                    
                    \[DllImport ("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)\]
                    public static extern IntPtr CreateFile (string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr SecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
                    
                    \[DllImport ("kernel32.dll")\]
                    static extern FileType GetFileType (IntPtr hFile);
                    
                    enum FileType : uint
                    {
                        UNKNOWN = 0x0000,
                        DISK = 0x0001,
                        CHAR = 0x0002,
                        PIPE = 0x0003,
                        REMOTE = 0x8000,
                    }
                    

                    }

                    D Offline
                    D Offline
                    David Knechtges
                    wrote on last edited by
                    #9

                    I know your question was answered by others, but I was wondering why you are using CreateFile instead of using the SerialPort class contained in the .NET framework? http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx[^]

                    OriginalGriffO 1 Reply Last reply
                    0
                    • D David Knechtges

                      I know your question was answered by others, but I was wondering why you are using CreateFile instead of using the SerialPort class contained in the .NET framework? http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx[^]

                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #10

                      If you look at the OP, there are two sections of code: the first using SerialPort is the one he has problems with, and the second is labeled as a test to prove the serial port works.

                      The only instant messaging I do involves my middle finger.

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups