serialPort

SerialPort (RS-232 Serial COM Port) in C# .NET

Published 23 March 5 7:28 PM | coad

This article is about communicating through the PC's Serial COM RS-232 port using Microsoft .NET 2.0 or later by using the System.IO.Ports.SerialPort class.

Way in the Past...

Back in my early (pre C# MVP) days, I had written an article on DevHood titled Serial COM Simply in C#. It became quite popular and is (currently) the top Google search for "serial c#". Now every week I get several e-mails with people asking questions and it is high time for some serious updating.

The article was (originally) written soon after .NET was introduced to the world, and other .NET serial port controls had not yet been written. So at the time, this was an easy solution. Just use the MSComm.ocx control from VS6 which most Devs had at the time. Now however, there are many easier and preferred methods than dealing with the complexities of interoping with an old legacy (non-.NET) ActiveX OCX control.

 

A Bright Future is Here!

Today, the primary solution is to use the new SerialPort control that is part of .NET 2.0 and is freely available in C# Express on MSDN. It is considerably easier to use and is a true .NET component.

Download Button - Small For loyal fans of the tutorial, I've written a sample code application, SerialPortTerminal.zip, which you can try out to see how the new SerialPort control is used. Requires Visual Studio 2005 & .NET 2.0 or later to compile & run.

 

Continued Support

Due to the high volume of e-mail I've been receiving on serial port communication in general, I've wrote this post and added an FAQ section to the bottom of the post.  Check this first if you have questions.  If you post a question as a comment that hasn't been answered in the post, I'll add it to the FAQ section.

For those of you who I've directed to this blog post, please understand that I'm just another guy working away at a full time job (helping manage the release of Team System) who has worked with serial RS-232 ports in the past (moving on to USB now). I like to help people, hence I wrote the original article, this sample code, and the FAQ, but do not have the resources to answer additional serial port communication questions. Please check out the SerialCom FAQ for other .NET COM Port solutions and technical support options.

 

Get Connected Up

You can obtain USB to Serial adapters (from NewEgg, search using Froogle) and have just about as many ports on your PC as you like. I carry around two adapters with a null modem (what is it?, search Froogle) between them so I can create a loopback to send & receive through to separate ports on most any computer. I'd recommend doing the same for when writing code for the serial port.

If you'd like to quickly and easily create your own external devices to communicate with the PC, I recommend starting with the Parallax BASIC Stamp modules. Parallax has lots of easy accessories (such as LCDs, RF, Sounds, AD & DA, etc) for their modules. After that you could migrate to an Atmel Microcontroller (recommended) or Microchip PIC.

 

Let's Have the Code

Here is an example of how easy it is to use the new SerialPort control.

Very simply, here is how you can send a bit of data out the port.

                          
                         
                         
                         
// This is a new namespace in .NET 2.0
// that contains the SerialPort class using System.IO.Ports; private static void SendSampleData() { // Instantiate the communications
// port with some basic settings SerialPort port = new SerialPort(
" COM1 " , 9600 , Parity.None, 8 , StopBits.One); // Open the port for communications port.Open(); // Write a string port.Write( " Hello World " ); // Write a set of bytes port.Write( new byte [] { 0x0A , 0xE2 , 0xFF }, 0 , 3 ); // Close the port port.Close(); }


Now let's take a look at what it takes to read data in from the communications port. This demonstrates reading text; for an example of reading binary data, see my SerialPortTerminal.zip sample app.

  1. Create a new "Console Application" and replace all the default class code with this code
  2. Add a reference to "System.Windows.Forms" to the project
  3. Run w/ F5, to exit the app, press Ctrl-Break.
  4. Get Connected Up with two USB to Serial adapters and a null modem
  5. Use another app, the code above, or the SerialPortTerminal.zip example to send data and watch it come in with this code

 

                          
                         
                         
                         
#region Namespace Inclusions using System; using System.IO.Ports; using System.Windows.Forms; #endregion namespace SerialPortExample { class SerialPortProgram { // Create the serial port with basic settings private SerialPort port = new SerialPort( " COM1 " ,
9600 , Parity.None, 8 , StopBits.One); [STAThread] static void Main( string [] args) { // Instatiate this class new SerialPortProgram(); } private SerialPortProgram() { Console.WriteLine( " Incoming Data: " ); // Attach a method to be called when there
// is data waiting in the port's buffer port.DataReceived += new
SerialDataReceivedEventHandler(port_DataReceived); // Begin communications port.Open(); // Enter an application loop to keep this thread alive Application.Run(); } private void port_DataReceived( object sender,
SerialDataReceivedEventArgs e) { // Show all the incoming data in the port's buffer Console.WriteLine(port.ReadExisting()); } } }


One of the (several) new methods that is supported, and one I'm very glad is finally here, is the ability to obtain a list of the COM ports installed on the computer (ex: COM1, COM2, COM4). This is definately helpful when you want to present the list of ports avalible for the user to select from (as in the SerialPortTerminal.zip Win App example).

                          
                         
                         
                         
foreach ( string s in SerialPort.GetPortNames()) System.Diagnostics.Trace.WriteLine(s);


Here are two helpful little methods for sending files through the serial port. Of course, these are the bare essentials and as always, you should check to make sure the port is open first (port.IsOpen) and use try/catch around trying to open a file, but you get the gist with this code. The binary sending routine is limited to about 2GB (the size of an int), but this should be okay for most uses.

                          
                         
                         
                         
using System.IO; private static void SendTextFile(
SerialPort port, string FileName) { port.Write(File.OpenText(FileName).ReadToEnd()); } private static void SendBinaryFile(
SerialPort port, string FileName) { using (FileStream fs = File.OpenRead(FileName)) port.Write(( new BinaryReader(fs)).ReadBytes(
( int )fs.Length), 0 , ( int )fs.Length); }


 

RS-232 Project Photos

Each of these involve RS-232 serial port communications.

Just what's needed to get started with microcontrollers,
a Basic Stamp, mini LCD display, power, and RS-232 port.

Two USB to Serial adapters with a null modem
to loopback and test your serial software.

The brains to a mini automated radio station that let me
control my PC & home using my HAM radio from around town.


Port Wiring Notes

DB9 Male (Pin Side)                   DB9 Female (Pin Side)
DB9 Female (Solder Side)              DB9 Male (Solder Side)
    -------------                          -------------
    / 1 2 3 4 5 /                          / 5 4 3 2 1 /
     / 6 7 8 9 /                            / 9 8 7 6 /
      ---------                              ---------

DB9 Female to DB9 Female Null-Modem Wiring
 2 |  3 |  7 |  8 | 6&1|  5 |  4
---- ---- ---- ---- ---- ---- ---- 
 3 |  2 |  8 |  7 |  4 |  5 | 6&1

9-pin   25-pin  Assignment                 From PC
------  ------  -------------------------  ------------
Sheild  1       Case Ground                Gnd
1       8       DCD (Data Carrier Detect)  Input
2       3       RX  (Receive Data)         Input
3       2       TX  (Transmit Data)        Output
4       20      DTR (Data Terminal Ready)  Output
5       7       GND (Signal Ground)        Gnd
6       6       DSR (Data Set Ready)       Input
7       4       RTS (Request To Send)      Output
8       5       CTS (Clear To Send)        Input
9       22      RI  (Ring Indicator)       Input

- RTS & DTR are binary outputs that can be manually set and held
- DCD, DSR, CTS, and RI are binary inputs that can be read
- RX & TX can not be set manually and are controlled by the UART
- maximum voltages are between -15 volts and +15 volts
- binary outputs are between +5 to +15 volts and -5 to -15 volts
- binary inputs are between +3 to +15 volts and -3 to -15 volts
- input voltages between -3 to +3 are undefined while output voltages
  between -5 and +5 are undefined
- positive voltages indicate ON or SPACE, negative voltages indicate
  OFF or MARK

 

Other Resources

Here are some additional sites, libraries, tutorials, etc. These are links that I just found around the net and am providing for convenience (they are not endorsed).

  • SerialPort on MSDN
  • Search on Google #1, #2, #3
  • CFSerialIO
  • OpenNETCF Port

Where CF = Compact Framework

 

The Final Say (aka Conclusion)

The new SerialPort class in .NET 2.0 rocks! It is much easier to use than getting the old MSComm.ocx control going in a .NET app, contains new functionality, is a 'native' .NET control, has docs built into the MSDN Library, and is easy to use.

 

Frequently Asked Questions (FAQ)

I'm adding this section (as of 8/10/06) to address the common questions I get on this post and through e-mail.  Chances are, if you ask a good question in the comments here, I'll post it here for others to see easily.

  1. Q: When updating a control (like a text box) while in the DataRecieved event, I get an error.
    A: The SerialPort class raises events on a separate thread than the main form was create on.  Windows Forms controls must be modified only on their original thread.  Thankfully there is an easy way to do this.  Each Windows control has a "Invoke" method which will run code on the control's original thread.  So to put the recently received data into a text box (txtLog), this would do it:   txtLog.Invoke(new EventHandler(delegate { txtLog.Text += comport.ReadExisting(); });   You can see this more in action in the "Log" event of "Terminal.cs" my sample code project, SerialPortTerminal.zip.
     
  2. Q: I can't find the System.IO.Ports namespace.
    A: Using Visual Studio 2003?  The new namespace, and SerialPort class, is part of .NET 2.0 and Visual Studio 2005.  It is not included in .NET 1.x (and Visual Studio 2003).  Even if you have .NET 2.0 or Visual Studio 2005 installed, you can not access the class from within Visual Studio 2003.
     
  3. Q: I only have .NET 1.1, what can I do?
    A: Upgrade to .NET 2.0.  Seriously, it's free.  In fact, you can get the great C# and VB Visual Studio Interactive Development Environment (IDE) editors for FREE now with C# Express and VB Express.  The .NET 2.0 Software Development Kit (SDK) for command-line development is also free.  If you really must stay in .NET 1.1, you can use the technique I talk about in Serial COM Simply in C# or a 3rd party library.
     
  4. Q: I'm sending data to my device, but it is not responding.
    A: First make sure the device will respond using a standard app like Hyperterminal.  Check the settings (baud rate, data bits, stop bits, etc) and make sure they match with your code.  Try sending binary data via binary arrays.  Many devices expect a carriage return at the end of a command, so be sure to send 0x0D or /n.  String data can be easily converted to a binary array using:
    byte[] data = System.Text.ASCIIEncoding.Default.GetBytes("Hello World/n");
    com.Write(data, 0, data.Length);
    Many devices require several carriage returns first to sync baud rates, so send several, like: com.Output("".PadLeft(9, '/n'));  It you're communicating with a modem, make sure Echo Back is turned on.  Send "ATE1/n".  Other than this, just keep trying and playing around with it.  It can be hard because you don't see the response from the device as easily as you would with a terminal app.
     
  5. Q: When I put received data to a text box or rich text box, I get a strange symbols.
    A: The default font of text boxes is designed only to show standard characters.  Try using "CharMap" (a free tool in WinXP, click "Start", "Run", type "CharMap", enter).  "Terminal" is a font designed to show classic ASCII characters and is what most terminal apps (like my sample code and Hyperterminal) use.  There are also many ASCII codes that won't display correctly.  This is why I choose to show the hex data instead of an ASCII string a lot of the time.  System.Convert.ToString(mybyte, 16) will convert a byte to a string hex code, for example: byte b = 13; string s = Convert.ToStrong(b, 16).PadLeft(2, '0'), then s will contain "0D".  See the "ByteArrayToHexString" and "HexStringToByteArray" methods in my sample app, SerialPortTerminal.zip.
     
  6. Q: What about USB communications?  How can I do USB?
    This post isn't about USB.  Believe me, I wish the .NET framework supported USB natively, and I'm doing what I can here at Microsoft to see USB get into the framework in the future.  For now, you can use a USB to Serial adapter.  I use a lot of these.  They plug into the USB port, then appear just as a SerialPort to the PC.  Microcontroller vendors such as Microchip, Atmel, and TI make chips that do this for projects.  There is a lot of info on USB and C# out there too (such as this great article).
     
  7. Q: Can I use the sample code here in my own projects (commercial or not)?
    Yes!  All sample code on my blog is free public domain material.  I'm not a legal guy so I don't know the exact words to use, but I'll try...  I'm not responsible for any problems!  Use at your own rick etc.  However, have at it, if it helps you out, fantastic, that's what it's here for.
     
  8. Q: When using SerialPort.Open() (or SerialPort.PortOpen = true) I get the exception "UnauthorizedAccessException" or the error "An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in axinterop.mscommlib.dll"
    It may be one of a few factors:
    • It may require Administrative privileges to use the port.
    • The port may already be open by another program, only one app can use a port at a time.
    • The port may not exist on the computer (this happens a lot).  Verify the port you're trying to open is listed in the Device Manager (FAQ #9).
    • The name being provided is not exactly correct.
      Use the full name of the port when opening, like "COM1" (not "1")
  9. Q: How do I know what COM ports are on my PC?
    Use the Device Manager ("Start", "Run", "devmgmt.msc") and look for the "Ports" node (see below).  If you don't see a Ports node, it's because there are no Serial or Parallel ports installed in the PC.  You can also use System.IO.Ports.SerialPort.GetPortNames() to return the list of available ports.  Many laptops these day's don't have a serial port.  You can get more serial ports very easily today with USB to Serial adapters.


     
  10. Q: How do I communicate with my specific device?  Modem, Mobile Phone, LED/LCD Display, Scanner, etc
    This post is specific to general, device independent, serial port communications.  You will need to find information about the protocol used for your specific device elsewhere.  Comments that ask about specific devices will be deleted (to reduce spam).  I'd recommend looking on the manufacture's website, writing/calling the manufacture, or searching online for your specific device, like "Motorola Razor Serial Protocol".
     
  11. Q: What pins can I use for powering devices, a high signal, or for boolean input & output?
    The TX & RX pins carry the standard serial signal, but the other pins can be used as high/low input/output pins.  The output pins (4 DTR or 8 CTS), supply 5 to 15 volts (15v is proper RS-232 standard) when high and 0 to -15 volts when low.  They only supply flea current so they're not meant to be used for powering any devices (like USB is designed for).  However, they can be used as a reference voltage or for switching to one of the input pins for a high or low signal.  The input pins (1 DCD, 6 DSR, 8 CTS, and 9 RI) can be used to detect a high or low signal.  Proper RS-232 signal levels are -15v for a low and +15v for a high (compared to ground, pin 5).  A MAX232 or similar chip takes a TTL 0-5v input and produces the -15v to +15v levels.  However, most PC RS-232 COM ports will accept a 0v for low and 5v for high, but it is not guaranteed and alters from PC to PC.  If you want a simple "toggle high", just hold pin 4 DTR high, and switch it to pin 1 DCD.  The .NET SerialPort class has easy to use properties for switching the output pins high or low and for detecting the current level of the input pins.  I have been able to use pin 4 DTR for a very low current (20ma max) PIC processors, but not reliably.  I prefer to always supply external power and use pin 4 as a signal to turn on or off my device.  I'll attach pin 4 to a transistor that switches my power source to my PIC to turn it on or off.
     
  12. Q: What about ‘packets’?  Does RS-232 support any commands or data segregation?  OR  Data comes in at seemingly random times?
    Serial data flow through RS-232 has nothing to do with ‘packets’.  It’s just a stream of bytes in and out.  There is no guarantee that data arrives together.

    Packet Protocols
    Any notion of data compartmentalization (packets) would have to be coded by you for your unique use.  Much of my time working with serial has been spent on defining useful packet like protocols, that usually include some type of header, command structure, and CRC check.  For example, the first two bytes are the packet length, the next two bytes is the command, next two bytes are parameters, and the last byte is a CRC.  Then my apps would buffer incoming data and look in the buffer for valid packets.  Of course it differs depending on the device you’re working with and your specific needs.  USB does have specific communications protocol defined, one of them being command based, like the little packet just mentioned.  But with USB, you’re able to get the whole command and parameter together at once, with serial you have to create the protocol yourself, buffer, and parse the data.  My previous serial post has more info under the header “Protocol Development”.

    Buffering Incoming Data
    Since bytes may come in at any time, buffering incoming data is critical.  For example, you may send a command out to your device, and the response back to the PC could trigger a single DataReceived event with all the 30 bytes of response data in the receive buffer.  Or more likely, it could be any number of separate triggers of the DataReceived (up to the number of bytes received), like 4 triggers, first with 2 bytes, then 15 bytes, then 1 byte, then 12 bytes.  Don’t look for a complete response in a single DataReceived call, instead:
    1. buffer the incoming data
    2. then scan your buffer to find complete data
    3. remove the used data from the buffer

    To buffer incoming data, use a coding pattern like this:
    (download the code SerialComBuffering.zip)

    using System;

    using System.IO.Ports;

    using System.Collections.Generic;

     

    namespace SerialComBuffering

    {

      class Program

      {

        SerialPort com = new SerialPort(SerialPort.GetPortNames()[0],

          9600, Parity.None, 8, StopBits.One);

        List<byte> bBuffer = new List<byte>();

        string sBuffer = String.Empty;

     

        static void Main(string[] args)

        { new Program(); }

     

        Program()

        {

          com.DataReceived +=

            new SerialDataReceivedEventHandler(com_DataReceived);

          com.Open();

     

          Console.WriteLine("Waiting for incoming data...");

          Console.ReadKey();

        }

     

        void com_DataReceived(object sender,

          SerialDataReceivedEventArgs e)

        {

          // Use either the binary OR the string technique (but not both)

     

          // Buffer and process binary data

          while (com.BytesToRead > 0)

            bBuffer.Add((byte)com.ReadByte());

          ProcessBuffer(bBuffer);

     

          // Buffer string data

          sBuffer += com.ReadExisting();

          ProcessBuffer(sBuffer);

        }

     

        private void ProcessBuffer(string sBuffer)

        {

          // Look in the string for useful information

          // then remove the useful data from the buffer

        }

     

        private void ProcessBuffer(List<byte> bBuffer)

        {

          // Look in the byte array for useful information

          // then remove the useful data from the buffer

        }

      }

    }

          
     
  13. Q: How do you detect when a device is connected or disconnected?
    Simply put, the device usually start or stop sending data.  There is no built in events on a connect or disconnect.  But there are a few tricks you can do, if you’re creating the serial device you have more options.  I’ve had my devices (and PC apps) send a constant “are you there” set of bytes, like 00 00 11 11 00 00 hex (I’d use a ‘are you there’ custom ‘packet’ as in Q12 above) till a device on the other end responds.  You could also use hardware, that’s what some of the other signals lines are for (CDC, DSR, RTS, CTS, RI), you can tie one of these high and then catch an event when the line goes high you know there’s a device there, when it goes low the device is gone.
     
  14. Q: How can I get more support?  What are my support options?
    • Read this FAQ section!  :) 
    • You can try leaving a comment below.  Perhaps someone knows the answer
    • Try doing a search and look for other content.  "SerialPort C#", "Serial Com C#", "serial port .net"
    • Post a question in the .NET Base Class Library in MSDN Forums
Filed under: C#

Comments

# coad said on March 27, 2005 5:28 PM:

Nice!
And for devs searching for rs232 for the Compact Framework feel free to come here:
http://www.danielmoth.com/Blog/2004/09/serial-rs232-communications-in-net.html

# coad said on April 24, 2005 10:29 PM:

Cool stuff.

# coad said on May 4, 2005 1:30 AM:

hey

i cant find the name space
using System.IO.Ports
there is nothing like Ports.
i'm using Visual Studio.net 2003
Please help me urgently
my email ID is [email protected]

# coad said on May 5, 2005 8:54 AM:

>I cant find the name space

Check this phrase near the start of this posting:

"the new SerialPort control that is part of .NET 2.0 (currently in Beta)"

# coad said on June 6, 2005 12:45 AM:

Hi,

I really thank you for being the only mangaed .Net source of seial comm.:)
I have .Net 2.0 Beta installed. And can compile your hello world! example. But when it tries to Open the port. Exception says there is no COM1 port.

Any Comments !

# coad said on June 6, 2005 12:57 AM:

You may not have a COM1 installed on your system, or it may already be in use.

# coad said on June 8, 2005 3:07 PM:

Nice Article..quite informative

# coad said on June 8, 2005 3:13 PM:

if I set the port.ReceivedBytesThreshold to 5 would the following event
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
be called after there is 5 bytes in the buffer...

Also can you show how to use port.Read(buff,0,5) where buff is defined as

byte buff = new byte[5];

I am calling the port.Read function after the event is called 5 times to make sure there are 5 bytes in the buffer but still it doesn't work.

# coad said on June 8, 2005 11:09 PM:

The method's parameters are port.Read(byte[], int, int), so the first parameter is a byte array (byte[]). Try: byte[] buf = new byte[5];

But I don't recommend reading a fixed # of bytes if possible since it is very easy to get one or two bytes off in the incoming buffer and hence not receive the data when expected or get unexpected results.

This is much preferred since it handles any amount of incoming data. Just process what ends up in the PortBuffer and remember to clean it out from time to time to prevent memory creep.

private List PortBuffer = new List();

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort port = (SerialPort)sender;

byte[] data = new byte[port.BytesToRead];

port.Read(data, 0, data.Length);

PortBuffer.AddRange(data);

ProcessIncomingData(PortBuffer);
}

private void ProcessIncomingData(List PortBuffer)
{
// Search through the bytes to find the data you want
// then remove the data from the list. It is good to
// clean up unused bytes (usually everything before
// what you're looking for)
}

# coad said on June 9, 2005 9:55 AM:

Thanks for your help man....

One question.... I have data coming through bus in sets of 5 bytes at a time at very high speeds...about 1.5ms delay. Is there anyway I can force the program to call SerialDataReceivedEventHandler after it gets 5 bytes of data.... by setting port.ReceivedBytesThreshold to 5 do it????

# coad said on June 9, 2005 1:51 PM:

I'd suppose so, have you tried it? I use the technique above so I've never tried triggering the event on x # of bytes. The purpose of changing the ReceivedBytesThreshold is not to capture little 'packets' like that, but to reduce the # of times the method is called if large amounts of data are streaming in.

# coad said on June 10, 2005 4:53 AM:

i cant find the name space
"System.IO.Ports"
i'm using Visual Studio.net 2003
Please help me, its urgent
my email ID is [email protected]

# coad said on June 10, 2005 11:51 AM:

thanks Noah... I'll try it if doesn't work i'll try the othe way ...

Kapil system.io.ports comes with visual studio 2005 beta version.

# coad said on June 11, 2005 6:24 PM:

I guess any client applications will need to upgrade to .net2.0 ? I am skeptical about this as .net is still in .net.

Can any one share any experience about this.

Seshagiri

# coad said on June 14, 2005 2:55 PM:

What I was thinking was would client applications need to install .net 2.0 if I used the new feature in my application. If it is so, in that case is it recommendable as .net 2.0 is still in beta stage and there could be compatibility issues with the final release.

Seshagiri

# coad said on June 14, 2005 5:50 PM:

Seshagiri: Yes, clients must be running .NET 2.0. The deployment version from Beta2 will be different than that for RTM. VS05 Beta2 is not for public release yet, that's for when the product is finalized and released to market the second week of November. In the mean time, it is for testing and deployment to select targets (w/ a Go Live licence, see MSDN).

# coad said on June 21, 2005 10:31 AM:

Hi Noah!

I'm trying to send a simple "ati4" command to a modem and receive its response, but all I get back is whatever ati command I send.

If I Send: ati4

I Expect To Get Back:
USRobotics Sportster 28800 V.34 Fax Settings...

B0 E1 F1 M1 Q0 V1 X4 Y0
BAUD=38400 PARITY=N WORDLEN=8
DIAL=PULSE ON HOOK, etc..

But Instead I Get Back: ati4

I've tried both my own test application as well as the one you created. Any ideas? Thanks so much!

# coad said on June 22, 2005 9:34 AM:

Update
------
Hi Noah!

After further debugging, I observe that the port_DataReceived function in your SerialPort Terminal example is never being called. I see the function being added as an EventHandler earlier:

comport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);

I can't figure for the life of me why it's never reaching the function though. All I know is this is why, for example, I only receive an echo of my "at" command and not the actual "ok" response I'm expecting. I didnt change any of the code, so why would this be happening?

Cheers!

# coad said on July 7, 2005 11:30 PM:

This is a reply to 'Nathan Ie' question about the ATI4 question.

I had the same problem and found out, that if you use WriteLine() method to write the "ATI4" string it will echo the sting back.

You have to use the Write() method and specify the file feed "/r". If you call method Write("ATI4/r") you will get the expected result. You can also change the SerialPort.NewLine Property from "/r/n" to "/r" to make it work.

Hope that helped

# coad said on July 8, 2005 5:23 PM:

when i do "port.Open()", it says

UnauthorizedAccessException was unhandled.
Access to the port "COM1" is denied.
Make sure you have sufficient privileges to access this resource

I just put your code segment into a button1_Click handler. What should I do to make it work?

# coad said on July 8, 2005 5:44 PM:

Actually nevermind my previous post, i solved the problem. But i have a new question :)

How can I manually send bit signals to RTS & DTR?

I need to send something like "0110000011010000" to RTS and at the same time (synchronized) send its logic inverses to DTR.

I am a student from university of toronto and very new to serial programming. please help me!!

# coad said on July 12, 2005 2:05 PM:

I run your program on my desktop (Windows XP Professional with SP2 and Visual Studio.NET 2005 Beta 2), the port can't be opened. The IDE said that "Access to the port 'COM1' is denied." It is UnauthorizedAccessException. Please tell me how to solve the problem? Thank you very much!

# coad said on July 12, 2005 2:49 PM:

The port may not exist on your computer or may be in use by another application (like for a modem). Change "COM1" in the code to another port, like "COM2" and try that. You can also check to see what ports are avalible on your machine through the Device Manager (press Windows-Break, "Hardware" tab, "Device Manager" button, "Ports (COM & LPT)" node).

# coad said on July 14, 2005 11:34 AM:

You are right. I install ActiveSyn 4.0 which uses COM1. After I delete it, the program works very well. I have another question. I modify you program in DataReceive event handler:



void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int intBytes = serialPort.BytesToRead;
byte[] bytes = new byte[intBytes];
serialPort.Read(bytes, 0, intBytes);

tbReceive.Text += intBytes.ToString() + ";";
}
The tbReceive is a TextBox control. When the program excutes at the last line,

tbReceive.Text += intBytes.ToString() + ";";

there is an exception, System.InvalidOperationException was unhandled. The error message is "Cross-thread operation not valid: Control 'tbReceive' accessed from a thread other than the thread it was created on." Please tell me what's reason and how to solve it. Thanks!

# coad said on July 16, 2005 9:40 AM:

Hi Charlie!

int intBytes;
void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
intBytes = serialPort.BytesToRead;
byte[] bytes = new byte[intBytes];
serialPort.Read(bytes, 0, intBytes);

this.Invoke(new EventHandler(SetText));


}
void SetText()
{
tbReceive.Text += intBytes.ToString() + ";";
}

# coad said on July 25, 2005 5:40 PM:

Hi Noah,

I read your article on System.IO.Ports.SerialPort class of new
C# Express. I tried to achieve a basic communication between my pc and the lab instrument with the following code:

After the usual port initialization,

if (com.IsOpen) com.Close();
com.Open();

// send char 34,14,192,51,0,0
com.Write(new string(new char[] { (char)34, (char)14, (char)192, (char)51, (char)0, (char)0 }, 0, 6));

// for testing purposes I connected TX and RX pins of the port...

// receive routine:
private void com_DataReceived(object sender, SerialDataReceivedEventArgs e)
{

// This method will be called when there is data waiting in the port's buffer

// Obtain the number of bytes waiting in the port's buffer
bytes = com.BytesToRead;

// Create a byte array buffer to hold the incoming data
buffer = new char[bytes];

// Read the data from the port and store it in our buffer
com.Read(buffer, 0, bytes);

}

After I activate the send routine, 6 bytes of data is received by pc but with some difference to the originally sent data.

I receive : 34,14,63,51,0,0 everytime instead of 34,14,192,51,0,0

I tried to transmit 34,14,127,51,0,0 and received the same data sequence...

As I try to tranmit anything bigger than 127 I receive 63 instead of the original data...

Do you have any idea why this is happening consistently?

Regards,
Haluk Gokmen

# coad said on July 25, 2005 10:47 PM:

Haluk: Have you tried using a byte array instead of a character array?
byte[] buffer = new byte[com.BytesToRead];

# coad said on July 25, 2005 10:53 PM:

Green,
Thanks for the update to charlie's post. The reason for this is that WinForms controls' properties must be modified on the same thread that they were created on. By using this.Invoke (where "this" is the form the control is on), it asks the form to run another method on the same thread the form is on. serialPort_DataReceived could be triggered at anytime (not just when the form is avalible) since the SerialPort control runs outside of the form's thread. This is a big advantage in that it will always be responsive to incoming serial data even if the form is busy with an intensive redraw or update.

# coad said on July 26, 2005 2:44 AM:

Dear Noah,
I tried byte array on the receiving side without a difference on the result.

I was succeding on a similar operation in VS.NET by utilizing MSComm OCX as suggested in your article at www.devhood.com/tutorials/ tutorial_details.aspx?tutorial_id=320
The following is the code:

private byte[] recdata = new byte[40]; //received data array


public void InitComPort()
{

// Set the com port to be 1
com.CommPort = 1;

// This port is already open, close it to reset it.
if (com.PortOpen) com.PortOpen = false;

// Trigger the OnComm event whenever data is received
// Loop based receieve activated...
com.RThreshold = 0;

// Set the port to 9600 baud, no parity bit, 8 data bits, 1 stop bit (all standard)
com.Settings = "9600,n,8,1";

// Force the DTR line high, used sometimes to hang up modems
com.DTREnable = true;

// No handshaking is used
com.Handshaking = MSCommLib.HandshakeConstants.comNone;

// Don't mess with byte arrays, only works with simple data (characters A-Z and numbers)
com.InputMode = MSCommLib.InputModeConstants.comInputModeBinary;

// Use this line instead for byte array input, best for most communications
//com.InputMode = MSCommLib.InputModeConstants.comInputModeText;

// Read the entire waiting data when com.Input is used
com.InputLen = 0;

// Don't discard nulls, 0x00 is a useful byte
com.NullDiscard = false;

// Open the com port
com.PortOpen = true;
}

private void cmdRead_Start_Click(object sender, System.EventArgs e)
{
long TimeStamp = DateTime.Now.Ticks; // Time Stamp for Time-out
bool ElapsedTime;

com.Output = new string(new char[]{(char) 34, (char) 14, (char) 192, (char) 51, (char) 0, (char) 0});

// collect data during the first one second period...
do
{
// Dont lock up the entire application
// release control to process other messages.
System.Windows.Forms.Application.DoEvents();

// If there is data waiting, buffer it in our own byte array...
if (com.InBufferCount >= 34) recdata = (byte[])com.Input;

// Look for the Elapsed Time
ElapsedTime = DateTime.Now.Ticks - TimeStamp > TimeSpan.TicksPerSecond * 1;

// Keep waiting for 1 second before processing incoming data stream...
}
while (!ElapsedTime);

Global.samplescounter = recdata[27] * 100 + recdata[26];
lblRead_RecordNo.Text = Global.samplescounter.ToString();
.
.
.
}

My instrumentation which is target to this communication action is responding to data sent in char format. It doesnt respond to byte arrays.
This previous code is still working but I cant get the new one working.
Regards..

# coad said on July 27, 2005 7:50 AM:

Briefly, I would like to transmit Unicode 192 (u/192) from the serial port and read it back again. How can I do this in C# 2005 Express?

# coad said on July 28, 2005 9:52 PM:

You're right.. it's much easier!

but i have a little problem

I get this from the serial port:

U001W1 N 1,852 kg
U001W1 N 1,852 kg
U001W1 N 1,852 kg
U001W1 N 1,852 kg
U001W1 N 1,852 kg

How can i fix it, that i just geht the "1,852" from it?

thanxx

# coad said on July 29, 2005 4:59 PM:

Hello Noah and every visitor. Thanks for the great introduction to the SerialPort.
I have written a simple code, and to test i have used a cross over cable from COM1 to COM2. If I run 2 instances of the code on the 2 ports it works (send and receive) perfectly. But if I use Hyper Terminal on one end (say for example Hyper terminal on COM2 and my code on COM1) my code can send and hyper terminal receives it but if hyper terminal sends my code do not see anything. The same thing happended when i used your sample code. But of course when i test two instances of hyper terminal can eailty talk to each other.
What am I missing here?

# coad said on August 1, 2005 2:59 AM:

How do you set serial port encoding?

# coad said on August 1, 2005 9:46 AM:

Haluk,
Try using the SerialPort.Encoding property. For example: port.Encoding = System.Text.Encoding.UTF8;

# coad said on August 4, 2005 6:52 AM:

Great intro maan.

I tried reading from COM1 using VB.NET (2005 beta 2). I am not able to get the DataReceived event to get triggered. The data from a connected scale transmits whenever its weight is stable.

I coded this in a form. Does it have to be a console application instead ?
I am using ReadExisting(). Do you need to use ReadByte() ? If yes, does that code need to be in a thread to avoid sync/no-timeout ?

Thanks for any advice.

# coad said on September 1, 2005 7:42 AM:

I have written a program that sends commands to and receives the responses from a serial device. This work normaly. However the device is also outputing data every .x seconds. I am having a very difficult time receiving this data and getting it onto my form. Your suggestions and advice are greatly appreciated.


Private Sub Write_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Write.Click

Dim gxTXDBuffer As String
Dim sInput As String

Dim lTimeout As Long
Dim Time As Long

'clear input buffer counter
AxMSComm1.InBufferCount = 0

'setup command to send
gxTXDBuffer = sendtext.Text & Chr(10)

'send command
AxMSComm1.Output = gxTXDBuffer

'-----------------------------------------------------------------------
'not sure how to get data when counting
'
'While sendtext.Text = "C"
' Microsoft.VisualBasic.Timer > lTimeout
'
'read in response
'sInput = AxMSComm1.Input
' receivetext.Text = sInput
' End While
'-----------------------------------------------------------

'wait for response or timeout
lTimeout = Microsoft.VisualBasic.Timer + 5

Do
Application.DoEvents()

Loop Until AxMSComm1.InBufferCount >= 8 Or Microsoft.VisualBasic.Timer > lTimeout
'
'read in response
sInput = AxMSComm1.Input
receivetext.Text = sInput

End Sub
'another attempt at reading counts when com event
'
' Private Sub AxMSComm1_OnComm()
' Dim sInput As String
' commevent 2 = Received RThreshold number of characters. This event is generated continuously
' until you use the Input property to remove the data from the receive buffer.
' If AxMSComm1.CommEvent() = 2 And AxMSComm1.InBufferCount > 0 Then
' sInput = AxMSComm1.Input
' ' receivetext.Text = sInput
' End If
'' End Sub

# coad said on September 1, 2005 10:23 PM:

How do you program the Basic Stamp to output a string that you sent from your PC?

Anyone can help?

Thanks!!!

# coad said on September 5, 2005 10:59 PM:

I am using VS.Net 2003, Actually i ve to do hardware interface in which i need to recieve data and plot it as graph and save it in database. Can anyone help me out...
Thanks

# coad said on September 8, 2005 3:55 PM:

Hi All,

I have the following problem:

After sending a Write command to a device, directly followed by a Read command it seems that the data on the device is not yet available.

It seems that the write/read part is too fast for the device to 'generate' the data.

The baudrate is 9600 and cannot be higher. Also i would not like to add a pause.

Does anyone have a solution here ?

Thanx,

Jan

# coad said on September 14, 2005 12:59 AM:

What about the Timer in windows forms, for data arriving at any time and various durations?
does firing an event during reading current event would cause problems?

thanks for your efforts

thanks,

elwolv

# TrackBack said on September 25, 2005 1:55 AM:

# coad said on September 26, 2005 6:26 AM:

Hi All,

Noha - a great article!

For some reason I'm not reaching the 'port_DataReceived' function. Any ideas why?
I'm running the 'SerialPortTerminal' application on my computer with the 'Portmon' application monitoring all data at the serial port so nothing is physically connected to the COM. May this be the reason?

Thanks for your help.


Elad

# coad said on October 12, 2005 3:27 AM:

Hi there,

Your article is awesome and the information is so great and very useful.

However, I have a problem when I tried to write a software to communicate to bluetooth ports(COM4 and COM7) in PDA. The .NET 2.0 does not provide a platform for mobile device.

Suggestions please.

Thanks a lot,
Ikkyusan ;)

# coad said on October 17, 2005 8:53 PM:

Ikkyusan:
The .NET Compact Framework does not have built in support for serial communications. There are many third-party controls available. Try a Google search like:
http://www.google.com/search?hl=en&q=serial+com+compact+framework

# coad said on October 28, 2005 3:07 PM:

This is an awesome and extremely useful article! My one suggestion is that you add some minor clarifications on the DtrEnable property. I'm connecting to an Ultra 5 Sun box and was having a lot of troubles with my commands just getting echoed back to me. Setting the DtrEnable = true solved that problem. My understanding is that it tells the serial port that you're talking from one data transmitter to another data transmitter.

# coad said on November 12, 2005 10:21 AM:

Im trying to contact my sony wavehawk scanner with.
port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
port.Encoding = System.Text.Encoding.Default;
port.NewLine = "/r";
port.ReceivedBytesThreshold = 1;

port.DtrEnable = true;


It reacts, but I dont get the "ok." that I'm expecting... Any ideas?

I use Serial monitor so that I see what goes in and out.

//J

# coad said on November 13, 2005 8:29 PM:

I am try to get exactly Method that line the Hexa Method using for sending data throught RS232, that I want is the function fo sendding binary. How can I slove this problem, thank for helping!

# TrackBack said on November 19, 2005 4:53 PM:

# coad said on November 25, 2005 2:14 PM:

Jimmy:
Don't forget to attach a delegate to the DataReceived event, open the port (.Open), and send data (.Write). Beyond that, it may be something specific to your scanner device.

# coad said on November 28, 2005 10:42 AM:

The .NET 2.0 class library does not seem to support devices powered by the serial port.

I have a couple different magstripe readers here at work which will work correctly after making a connection with HyperTerminal or opening the port in a similar .NET 1.1 library, but the device will not function after SerialPort.Open() on .NET 2.0, whether I write a driver program from scratch or try something like Noah's Serial Terminal. No exception is thrown and SerialPort.PortOpen return True so I know at least, the class library thinks it is funnctional. The light on the device, however, is off, so I know it's not getting power.

Does anyone have a serial port powered device working?

# coad said on November 28, 2005 11:19 AM:

Great tutorial. I really want to read data from the com port using managed c++ rather than C#. Can't find a sample anywhere, Heeellllp!!!

# coad said on December 8, 2005 7:15 AM:

I am trying to open a connection to COM3 on my machine. I have a USB device attached to the computer and its drivers emulate a serial port on COM3.

SerialPort.GetPortNames() returns "COM1" and "COM3" and if i connect to the port using TeraTerm its fully functional and i can see the data.

When i invoke open on the port after having set the port name to "COM3" i get an argument exception that the name doesn't start with COM or that there is no such port. The name DOES start with COM and as i said, GetPortNames() includes COM3.

Anyone experienced this? Solutions?

I am using VS 2005 Final on Win XP SP2.

/// Isak

# TJ said on December 13, 2005 5:10 PM:

Does anyone know how to send a special character over the serial connection? Specifically I'm trying to send a ctrl+c.

Thanks!

# TJ said on December 23, 2005 11:09 AM:

How would I send a ctrl+c (break) via serial?

Thanks!

# Daniel said on January 5, 2006 6:43 PM:

Ive written a Serial Port monitoring app that reads data from the COM port real time and displays it to a Rich Text Box (along with any special characters such as Carridge Returns, Line Feeds etc). This program works perfectly on Windows XP however when I transfer it to my Server running Windows Server 2003 Standard Edition, it doesnt work. I have installed the NET.Framework 2 on the Server.

Further troubleshooting I redesigned the program to write a line of text to a text file when the method is called for Data waiting at the Serial Port Buffer. That line of code is :-

port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);

So it appears to me that the method is never being called. The text file also logs when the Serial port is opened and close and when any exceptions are thrown.

I know the device attached to the serial port is working as I fire up a 3rd Party Serial Port Monitor &/or Hyperterminal and I see the data continuously coming through.

Can anyone offer any assistance with this problem?

Thanks,
Daniel.

# Kintesh Patel said on January 13, 2006 4:30 PM:

Those who are using Serial to USB convertor, Please check article "USB-to-RS232 Hurdle Race" published in elektor electronics Sept. 2005.

It describe behind the scene effect and it's reason.

# Ashley said on January 17, 2006 6:16 AM:

Hi guys,

Many terminal programs that are available (Hyperterminal being one of them) do not implement handshaking correctly.

A normal 232 connection will most likely require DTR enabled as well as RTS as well.

For you apps try setting your serialport with

port.Handshake = System.IO.Ports.Handshake.RequestToSend;

This is what most terminal programs call hardware flow control. It will enable RTS/CTS flow control which most devices rely on for transmission timing.

You may have played with the handshaking in HyperTerminal but if you use a 232 hardware analyser you will notice that it does not change any lines on your port. Changing these setting with a .net v2 app certainly will

Setting DTR to enabled and handshaking to hardware should power the pin that manufacturers use for power robbing as well.

Hope that made sense and is of some use

Ashley



# Gan esh said on January 18, 2006 3:41 AM:

Hi, I am using Visual Studio .NET 2005 which has serial port class built with it.
I am developing a Web Application in which i require to do serial communication. and i just wanted to know how exactly to include the SerialDataReceivedEventHandler Event in my web application, please help me out.

Thanks in advance
gan esh

# Mark said on February 1, 2006 2:31 AM:

Thanks for the article Noah! I've run the sample as it is and have been unable to get a response from the Wavecom GSM Modem that I am trying to talk to - the port_DataReceived() event is never fired.

This is driving me nuts! Any ideas? I'm running this on a Windows 2003 Server using VC# Express 2005.

Regards,
Mark.

# Tim Nguyen said on February 3, 2006 7:46 PM:

If you are having trouble with this, remember to send a "/r/n" command. It works perfectly if you do this.

# LarsPetter said on February 4, 2006 11:21 AM:

I have connected my PC to a BS2 sitting on a Board of Education(using an USB to Serial adapter). When i write something to the BS2, it gets there, but it also ends up in the input buffer. Why does this happen, and how can i prevent it from happening?

# Glenn said on February 13, 2006 5:18 PM:

Using your code:
-----------------------------------------------
private List PortBuffer = new List();

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort port = (SerialPort)sender;

byte[] data = new byte[port.BytesToRead];

port.Read(data, 0, data.Length);

PortBuffer.AddRange(data);

ProcessIncomingData(PortBuffer);
}

private void ProcessIncomingData(List PortBuffer)
{
// Search through the bytes to find the data you want
// then remove the data from the list. It is good to
// clean up unused bytes (usually everything before
// what you're looking for)
}
-------------------------------------------------
I need to find a two byte sequence (E0 80) in the data. I can find one byte or the other with something like this:
----------------------------------------------
int found = PortBuffer.FindIndex(delegate(byte i)
{return i==0xE0;}
);

Console.WriteLine(found);
----------------------------------------------
But how can I find the index of a exact sequence? Thanks.

# Mark Holt said on February 14, 2006 11:36 AM:

The SerialPort Terminal worked fine for me...that is if I was communicating with another SerialPort Terminal app.
When I tried to communicate with HyperTerminal I had to change the handshaking.

After:
comport.PortName = cmbPortName.Text;

Add:
comport.Handshake = Handshake.RequestToSend;

Or:
comport.Handshake = Handshake.RequestToSendXOnXOff;

I used SysInternals' Portmon to compare how HyperTerminal connects to the COM port vs. the .Net 2.0 SerialPort class and noticed quite a few differences that I cannot control via the SerialPort class such as:

IOCTL_SERIAL_SET_HANDFLOW has a different XonLimit and XoffLimit (1024 instead of 80 and 1024 instead of 200, respectively)

IOCTL_SERIAL_SET_TIMEOUTS has a different RI, RM, RC and WC (-1 instead of 10, -1 instead of 0, -2 instead of 0 and 0 instead of 5000, respectively)

IOCTL_SERIAL_SET_WAIT_MASK checks for RLSD and ERR (like HyperT) but also checks for RXCHAR, RXFLAG, CTS, DSR, BRK, and RING.

IOCTL_SERIAL_SET_QUEUE_SIZE has an InSize of 4096 (instead of 8192) and OutSize of 2048 (instead of 8192).

# Leslie Viljoen said on March 16, 2006 8:54 AM:

You probably have something else using that com port and it's locked. Make sure your terminal program is closed.

# Vincent Collin said on March 30, 2006 2:35 PM:

Thanks alot Noah,

Great tutorial, was very very useful in my project.

Framework 2.0 is pretty useful when you need to use those good old serial port.

# Carlos said on April 11, 2006 12:33 PM:

Hi, I am trying to read the data on COM7 (USB) on my machine.
When add the event handler to the progam i get an JIT debugging error: Class not registered.

Anyone expirienced this?

I'm using Visual C# Express ed. on Win XP SP2

# rachel said on May 11, 2006 1:15 AM:

port = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);
port.Open();


USB Cable Off - > CPU 100%

Help me

# EP said on May 29, 2006 10:50 AM:

what is the source codes for RS232 connection in framework 1.0 ? please help me!!!!!

# Srikanta said on June 9, 2006 6:56 AM:

Thanks for this article.i am new to .net development.i am fresher in development.
i want to learn more deep in serial programming.
i have connected to com1.
i am able to check whether bonus card is present or not,but i want to raed data from card from a particular byte to n byte.
so which method i have to use and how to use.plz reply me with syntax and code

# hassan said on June 19, 2006 10:35 AM:

thinks for these information ,i want do this ,but in Visual Studio2003 . I mean open Port COM3  and  have a communication with a modem and call from My Pocket PC

# hitesh said on June 20, 2006 1:24 AM:

hello,i want to require vb.net code for reading stream of data asynchronously  from serial port using multithreading as a background thread,so that is required,if more than one RFID tag read by serial port at a time.so how i can store all the tag read by serial port.

# Ahmet Vehbi said on June 26, 2006 3:23 AM:

Dear All ;
Would you please send me the tha samples data's to compile in my own pc.
Because i couldn't achive to make the form in the appropriate shape,size or position.
Thanks to whom it may concern..

# Punx said on June 29, 2006 1:35 PM:

Why does my timer dont start when I use timer1.Enable=true inside the DataReceived event handler?

# TheRunningBoard said on June 29, 2006 8:14 PM:

Great work!

# Zak said on July 3, 2006 6:07 AM:

hi
I'm using Visual Studio.net 2003
I have installed the .NET 2.0 (not the beta version)
and I cant find the name space
System.IO.Ports
any idea why
please help me

# coad said on July 7, 2006 7:40 PM:

Zak:

Visual Studio 2003 is only for writing code against the .NET 1.1 framework.  Even if you have .NET 2.0 installed, you won't see it in VS 2003.  You need VS 2005.  You can write the code with a plain text editor and compile it through the .NET 2.0 command line compiler.

# David Briggs said on July 9, 2006 8:41 PM:

I am doing some serial communication to a divice using .NET 2.0.  The device has a set of simple commands, each command and responce is a fix size. Each command will generate a responce and a new command can not be sent until the responce from the previous command has been recived.  I have set the DataReceived event handler to a method that will read the responce data. That part works OK.  There is a flag in the event handler that I set when I read the response. I poll for that flag to see when I can sent the next command. My question is is there any way I can know when it is OK to send the next command without polling? The DataReceived event is raised on a secondary thread. If there was some way to get the thread Id of the event handler I could do a thread Join to tell when the data reading is done.

# De IT y cosas peores » Java vs .NET: Puerto Serie (Parte II) said on July 10, 2006 2:14 PM:

PingBack from http://cesarolea.com/index.php/archives/157

# shinoy said on July 20, 2006 11:49 PM:

it is good. But if some one could explain about if someone need to get data from an epbx, wat method can be used.

# silibug said on July 23, 2006 1:43 AM:

Hi... nice article :)

btw..

How can  we  communicate with our serials port on LAN environment.?.
any idea how to convert rs232 output for tcp/ip input?

PC <-> LAN/switch <-> Microcontroller <-> Rs232 <-> devices

thanks:)

# Noah Coad [MS] said on July 24, 2006 11:33 AM:

silibug,
There are devices that let you control RS232 ports remotely over an ethernet network.  For example, the $99 LS100 by Aaxeon at http://www.aaxeon.com/products/Productdetail.aspx?cate=2&modelno=LS100    A Google search on "ethernet rs232" (without quotes) shows some more such devices.  

# SR said on July 27, 2006 12:48 AM:

Thanks for creating this, it has helped me greatly.  This is my first day using visual C# express and tutorials like this are making progress possible.  Hopefully I can contribute in the future.

One note:  In order to build, I had to comment the line //File.WriteAll(TempFile, html); out as I got an error "'System.IO.File' does not contain a definition for 'WriteAll'.  Not sure if I am an isolated case, but it doesn't seem to affect the program.

# Allan said on August 9, 2006 8:53 AM:

I'm sending commands ATs for modem port, and the modem do not receive the commands. I use (SerialPort Terminal) .... HELP me


thanks

# Noah Coad [MS] said on August 10, 2006 12:22 PM:

Allan,
There isn't enough info in your comment to figure out what's going on.  Try [ENTER] [ENTER] [ENTER] ATE1 [ENTER]      The first set of [ENTER] commands is for the modem to detect the baud rate.  ATE1 turns on echoing back commands so you can see if the modem is responding.  Good luck.

# Noah Coad [MS] said on August 10, 2006 12:22 PM:

SR,
Thanks for the pointer! :)

# Julio Silva said on August 10, 2006 3:15 PM:

Hello,

I'm a begginer, and I'm tring to do a simple terminal (receive only) for my
pocket pc, built in C#, with CF.Net 2.0.

I have 2 bottons, one for serialport.open, other serialport.close.
And works fine, since it's for a GPS connection, and I see it
connecting/disconnecting.

I have a label, that if I do a label1.text=serialport.readexisting(); it
displays all.
but if I do it in serialport.datareceived event, it get's an runtime
error...
If I do anything like label1.text=serialport.readchar(); it get's the same
error.
What I'm doing wrong?
I only have some programming experience in Delphi and C++...

Thanks for your help,

jS

# Noah Coad [MS] said on August 10, 2006 7:24 PM:

jS,
I've answered your question in the (new) FAQ section:
http://msmvps.com/blogs/coad/archive/2005/03/23/39466.aspx#FAQ

# Julio Silva said on August 11, 2006 6:41 AM:

Sorry I missed it.
The problem it's now solved, thanks.

BTW there is an link error in SerialPort Terminal, the filename has a space, SerialPort Terminal.zip, and the link doesnt, so it say's it's not found...

Thanks again for the help,

jS

# coad said on August 14, 2006 5:39 PM:

Julio,
I agree the file name is confusing with the space (and has been for a long time).  I've removed the space from all the links in the post and the filename.

# Seng said on August 16, 2006 8:46 PM:

Hi All

I try to use this example to send a ATDT6129 command to dial an extension number. I got an error message from my Modem. If I try to use Microsoft Hyper termanal it works. Any ideas?

Thanks

# Sheng Zhou said on August 16, 2006 9:33 PM:

Hi Coad

This example is very good, but atd or atdt command doesn't work. I have try other commands such as 'at', 'ati4' etc. they works. I don't know how to send a atd or atdt command to my modem. At moment, when I send atd or atdt command to my modem it got 'error' message back. Any ideas?

Thanks

# NAFF said on August 17, 2006 9:08 AM:

I am looking to setup serialport in VC++.NET V2.  Do you have an example. All I wnt is to be able to read data from a microcontroller. I an new to VC++ and cant understand the c# example.

many thanks

# Bill - WV7G said on August 18, 2006 2:06 PM:

I've been searching for the simplest way of coding the deligate to send received text to a textbox.  I should have known it would be a fellow HAM who had the answer!
Great stuff and very well done!

# Todor’s Weblog » Serial Communication said on August 22, 2006 2:53 PM:

PingBack from http://todor.be/blog/?p=45

# prem said on August 23, 2006 12:23 AM:

hi,
iam using visual studio3.0 and framework version is 1.1
Now ia want to access the serial prot using c#.

in this version SerialPort namespace can't use

how can i use API to access the serial port in C#(version1.1)
or
any method top access the SerialPort namespace in this version

plz help me
i want this very urgent
plz send
my email.id is
[email protected]

# coad said on August 29, 2006 5:44 PM:

Hey prem, I just wrote an answer to your question in the FAQ portion of this post as question #3. See: http://msmvps.com/blogs/coad/archive/2005/03/23/39466.aspx#FAQ

# Tsahi said on September 3, 2006 3:20 PM:

hi, regarding .net 2.0 being free, well, the sdk is free, but who wants to code in notepad? you will have to pay for visual studio 2005 lot's of dollars, and my understanding is that the "express" editions are only free until november 2006. but there's a free open souce alternative called SharpDevelop, at http://www.icsharpcode.net/OpenSource/SD/Default.aspx . i tried their version 1.1 a while back and it was not bad, except for a few bugs. i hope their version 2.0 (for .net 2.0) is better, but i didn't try.

# rajnish said on September 4, 2006 10:44 PM:

Dear Coad, I am an MCA student and need your help in , in Bi Directional interfacing an instrument, wherein I have to send some Delimited String to the instrument with certain commands Like ACK NAK STX etc. and Take the Data Instrument sends, So far I have been able to receive the data from the instrument, but while sending the data I don't get any reply, and nothing reaches to the instrument, where as the cable I have made is correct, and I know there is something wrong with the code it self please help me I am posting the details below please guide me how I can send the string to the Instrument i am using c# 2005 Please guide me Thanking you in anticipation Rajnish

# SilentDawn said on September 5, 2006 7:10 AM:

.net 2.0串口通信新控件--(serialPort)

# Gaurav said on September 11, 2006 6:18 AM:

Hello Sir, The code you have provided is very appreciable to me coz almost all my problems have been solved. But only one problem is there i m explaining you: I m using a RichTextBox to display all the data that is in Buffer, But while displaying that data that is coming to textbox is some symbols not the actual text data. Like as if we use HYPERTERMINAL a windows program where it will display all the calls,duration,Number etc. I want to display the same data in RichTextBox. Only the textual data. Plz Help????? Urgent??????????

# Noah Coad [MS] said on September 11, 2006 12:32 PM:

rajnish,
I just wrote an answer to your question in the FAQ portion of this post as question #4.
See: http://msmvps.com/blogs/coad/archive/2005/03/23/39466.aspx#FAQ

# Pockey said on September 13, 2006 8:56 PM:

Hi, thanks for your article!

I have a problem.  In your demo program, after I click the "Close Port" when there are still data sending from the Serial Port, the application will die. And I have to use "Ctrl + Shift + Delete" to end it. Any method to solve this? With "comport.close()" and data still in process, where all the data will be sent to?

# coad said on September 22, 2006 12:23 AM:

Pockey,

In a case like this, you can do something like when the user clicks "close", in the form's closing event handler, make a loop that check's the comport's sending buffer, when it is 0, then allow the close to contine.  I'd also set the cursor to a wait hourglass and add a timeout (in case it takes too long).

# Huy Tran said on October 4, 2006 12:03 AM:

hi Coad,

I am using serialport in framework 2.0 to make a connection to another port.I have some problem, some attribute in serialport are not has like AxInterop.MSCommLib.dll's attribute, such as : in AxInterop.MSCommLib.dll, there are have EOF enable, Sthreshold, Inputlen, InputMode. But in serialport, there is no these attributes.

One more question : Is serialport just suppose for Smart device (POCKET PC, WINDOWN CE)?

I am writing a app to connect another machine to send/receive data.

Please give me some advice!!

Thanx for reading.

# Jon said on October 16, 2006 9:08 AM:

Coad, thanks for the excellet article.

I have tried to implement it in C++.NET 2 and below is some of the code in question which I am having problems with, when I run this it gives me exception which says BUFFER CAN NOT BE NULL.

Any help would be greatly appreciated. I am working on a PIC MCU, I will make my work public on your blog once it works hopefully.

void SendRs232Data()

{

 // Instantiate the communications port - this is done in //the form properties of serialPort control from the toolbox

 // Open the port for communications

 serialPort->Open();

 // Write a string

 serialPort->Write("Hello World");

 int offset = 1;//The offset in the buffer array to begin //writing.

 int count = 1; //The number of bytes to read.

 array^ buffer ;//The buffer array to write //the input to.

serialPort->Read(buffer, offset, count);

 serialPort->Close();

}

# Mach said on October 17, 2006 2:23 AM:

hi,

I want to have  local resources (COM1, COM2) available to a terminal server.

I made my local serial port available in a session in Remote Desktop Connection window.

On terminal server I can see my serial port mapped to some TS033 and TS034 ports.

How can I open/use/connect these ports?

private SerialPort port = new SerialPort("TS033", 9600, Parity.None, 8, StopBits.One);  deasn't work.

On WinServer2003 it says:  The given port name does not start with COM/com or does not resolve to a valid serial port.

Thanks for any help.

# coad said on October 17, 2006 11:14 AM:

Jon,

You've defined the variable for the buffer, but you need to create an array as well.  In C#, it would look like this:

byte[] buffer = new byte[10];

# coad said on October 17, 2006 11:15 AM:

Mach,

Usually ports are mapped to COMx, sometimes as high as COM32.  Please see if you can map them to a COM number like this.

# coad said on October 17, 2006 11:17 AM:

Huy Tran,

The .NET 2.0 SerialPort class is not for devices, only full PCs.  There are 3rd party serial classes for mobile devices.  The .NET classes are wrappers around the Win32 APIs, so some of the members are renamed and they may not all be avalible.

# jon said on October 18, 2006 8:46 AM:

coed,

thanks for pointing me in the right direction. as you said buffer was not defined as an array correctly.

the program sort of works but does no print hello world, it prints the numbber 5, which is the byte size i am looking to print.  i expected hello to print. any ideas as to where i am going wrong?  many thanks.

void SendRs232Data()

{

 serialPort->Open();

  serialPort->Write("Hello World");

 int offset = 0;//The offset in the buffer array to begin writing.

 int count = 5; //The number of bytes to read.

  array^ buffer = gcnew array (10);//The buffer array to write the input to.

 serialPort->Read(buffer, offset, count);

lblSerial->Text= (serialPort->Read(buffer, offset, count)).ToString();

  serialPort->Close();

}

# Dejan said on October 20, 2006 6:44 AM:

My project requires external GPRS modem attached to serial port to send and receive SMS messages.

In addition to original Noah's code at the start of the blog, I've laso added and changed some things according to hints in the blog (thanx, guys); following i s a short summary of my add-ons to make the modem work:

1. Set modem handshake, like:

serialPort1.Handshake = Handshake.RequestToSend;

2. Prior to send first command, try to synchronize modem by sending "/r/n" after port.Open(), like:

serialPort1.Write("/r/n"); // sync purpose only

3. Always use "/r/n" at the end of the command, like:

serialPort1.Write("AT+CMGF=1/r/n");

(just in the case of sending SMS message itself, I use something like:

serialPort1.Write(textBox1.Text + "/u000D/u000A/u001A/n");

)

4. As described in FAQ (Q1), use Invoke and event delegation, like:

private void MyLog(string msg)

       {

           listBox1.Invoke(new EventHandler(delegate

           {

               listBox1.Items.Add(msg);

           }));

       }

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)

       {

           // Read data

           string data = serialPort1.ReadExisting();

           // Display the text to the user in the terminal

           MyLog(data);

       }

And this is it. After a couple of hours of being frustrated because of simplicity of using SerialPort class in .NET 2.0, but no result, it finally works.  

# Yonas said on October 23, 2006 9:08 AM:

RE: Test,

you dont say what programe you are using, but if you are sending and receiving data from the same port for a test, i suggest you wire up a NULL MODEM cable with one end connected to your COM2 port and at the other end connect PIN2 and PIN 3 using a wire. This will send what ever is in the Tx line to Rx line and you should be able to send TEST and read TEST on you monitor. A quick test to verify your cable is wired right would be to use hyperterminal. I hope I understood what you are trying to do correctly.  

# Matthias said on October 31, 2006 3:59 AM:

I need to send/recieve data to/from a uC using rs485 as protocol. I use a transducer to get the PC's rs232 and the uC's rs485 connected and therefore I need to have the RTS signal set at the start of writing data and unset it after all bytes have been written.

Unforutnately the given Handshakes set the RTS line but don't unset them after write.

Manually setting and unsetting RTS causes the RTS to be unset before all data have been written (although while loop until writebuffer is empty)

P.S.:

I can get it working if I run the obsolete c++ program, that was used for the same purpose, and send a command to the uC once. After that the RTS handling with the c# prog works fine. But there has to be a way to get it working directly from c#.

Regards

Matthias

# pegah said on November 8, 2006 1:12 AM:

how can i access to pin ??? i want set CTS or ...???

# Vinay said on November 8, 2006 6:32 AM:

Hi...

Your demo project is very good.

In my application i want to read text recieived on PC comport upto a string and after that i've to transmit some data on my port. So can you tell me how i can do this.

(e.g. I want to set the date & for that i have to wait until i receive text "Set Date :". )

Thanks,

Vinay

# Jack said on November 21, 2006 2:07 PM:

ClosePort() is also sending a byte '00' to receiver, how avoid it? I can't set DiscardNull=true because I am using rs232 for binary data communication.

Thanks!

# bidi said on November 27, 2006 11:04 AM:

i have a problem when i use GetPortName , i take port name for Com like this Com4, and i use the following code

--------------

foreach (string s in SerialPort.GetPortNames())

           {

               cmbCom.Items.Add(s);

           }

------------------------

the com4 is bluetooth . is  anyonethat have any ideea about this?

thks bidi

# zanis said on November 28, 2006 10:54 AM:

Hello,

I have a zebra printer connected to the com1 port, i use

the serial port class to send command to the printer but an exception is thrown on the Open method

"The given port name does not start with COM/com or does not resolve to a valid serial port"

Can someone help me please!!!

# Onur Gorgulu said on November 30, 2006 12:12 PM:

Hi friends;

In my program I am calculating a number in Int16 type between 0 and 255, and then I want to send this number to my microcontroller via serial port in 1 byte. for example 139 = 10001011. But the command comport.write(string  s) only accepts string format to send. if I convert byte = 139 to string type and then send, the ascii codes of 1 3 and 9 are going separetly. I want to send the byte as a whole- the Hex equivalent of 139 in one byte. How can I do that? please explain me with useful commands. I am very newbie in this subject but I have a little time to do this project. Please response ASAP. Thanks alot for your help.

Best Regards

Onur

# seval said on December 12, 2006 12:40 PM:

if DataReceived event dos not trigger

try

port.Handshake = System.IO.Ports.Handshake.RequestToSend;

it solved my problem

good luck

# trichloramine said on December 13, 2006 1:25 AM:

My bad. It works.

# zxf92183 said on December 13, 2006 1:41 AM:

hello,

I just use the  sample program from SerialPortTerminal to get data from a machine, it can received data at first time, but if the machine make a new test, and the program can't receive the data any more, after i restart the PC, the program can receive the data again. can someone help me.

thank you.

# Reddie said on December 13, 2006 5:12 PM:

I have the same problem as Matthias, some body already got a solution?

# Reddie said on December 15, 2006 4:19 AM:

@satya_chaitanya pleaze RTFM.

@Matthias on http://www.gotdotnet.com/ there is a serial port project which contains a sendI() function who direct sends a byte to the device. I've try'ed it and it works fine for our purpose.

# Sergey said on December 15, 2006 10:02 AM:

Need send to com1 command "{$04A$03$46}". If send this commant on Terminal - all work. If send command in c# - dont work. Please Help me !

# Reddie said on December 15, 2006 12:01 PM:

@Sergey: something like

byte[] b = {0x4a, 0x03, 0x46};

Serialport.Write(b,0,b.Length);

Is there a possibility to reach the Serialport handle???

# zxf92183 said on December 18, 2006 11:28 PM:

hello,

I just use the  sample program from SerialPortTerminal to get data from a machine, it can received data at first time, but if the machine make a new test, and the program can't receive the data any more, after i restart the PC, the program can receive the data again. can someone help me.

thank you.

# Sergey said on December 19, 2006 1:37 AM:

May be motheboard or servise havs the "Time out" for you port or connect.

# Mark said on December 23, 2006 3:03 AM:

Thank you!! After trying to make sense of MSDN tutorials, I landed here, and let me tell you this is much better. But the SerialCom FAQ.zip can't be accesses... Can you help? Or for the others that were able to access it before, please send it to [email protected]. Thanks much!

# tesha said on January 5, 2007 12:24 AM:

when i do "port.Open()", it says

UnauthorizedAccessException was unhandled.

Access to the port "COM1" is denied.

Make sure you have sufficient privileges to access this resource

# Muhammad Sajid said on January 10, 2007 10:55 AM:

Hi

Your Project is Excellent in every aspect

I want to use some part of ur code in my industrial project i request for a permission please !!!!!!!!!!!!!

# Ravichandran said on January 11, 2007 9:37 AM:

hi everyone,

i don't  know how to connect usb port in c# 2003,

i can't able to use 2005 because my projects are already developed in 1.1 ,any can help me

thanks

# helpstudent said on January 17, 2007 8:29 AM:

does anyone know how to set StopBits to None ????

I always get a runtime error!

thanks in advance

# coad said on January 19, 2007 11:32 AM:

Ravichandran, This isn't about USB, it's about SerialPort.  You can get a USB to Serial adapter:

http://www.google.com/search?hl=en&q=USB+Serial+Adapter

If you have to use .NET 1.1, you can use info from my old post:

http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=320

# coad said on January 19, 2007 11:34 AM:

Muhammad Sajid,

Sure!  Feel free to use any code you find on my blog for any project.  This goes for any and everyone here.

# coad said on January 19, 2007 12:31 PM:

Muhammad Sajid, Ravichandran, tesha, Mark, and others,

I've updated the FAQ section of the post to answer many of your questions, as well as added more support options.  Hope it helps!

FAQ Section: http://msmvps.com/blogs/coad/archive/2005/03/23/39466.aspx#faq  

# Noah Coad said on January 20, 2007 8:50 PM:

Onur Gorgulu,

Try: com.Write(new byte[] { (byte)(new Random().Next(0, 255)) }, 0, 1);

# Traverer said on January 23, 2007 8:52 AM:

Thanks for such resourceful material on c# serial tutorial!

I am just a beginner to c# and serial port programming, after download the SerialPortTerminal.zip and try with TC35i terminal, I discover the reason that causing "my modem not responce to my at command".

It seem to me that the comport.Write method does not send the "/r/n" I type in txtSendData textbox. Thus i add the "/r/n" string to the SendData() function as shown below and it works !!

==================================

  private void SendData()

   {

     if (CurrentDataMode == DataMode.Text)

     {

       // Send the user's text straight out the port

         comport.Write(txtSendData.Text + "/r/n");   // <-- add "/r/n" here

       // Show in the terminal window the user's text

       Log(LogMsgType.Outgoing, txtSendData.Text + "/r/n");   // <-- add "/r/n" here

     }

=====================================

Hope this could help those newbie like me to have a good starting experience in dealling the C# SerialPort and GSM terminal.....

# sth_Weird said on January 25, 2007 4:15 AM:

hello,

thanx for this article, it helps a lot and is much better than anything else I found on the net!

I have a question...

I have two  classes "MyMainClass" (let's call it A to make it shorter...) and "MySerialConnectionClass" (A). A instanciates B and calls function SendToSerial. SendToSerial opens the connection and I add the event. then I send data to the device, and after some time the device is supposed to answer. the problem is that SendToSerial does not wait for this answer. How does the code have to look to make the function wait for the answer? I already tried a while-loop in the function which will only exit if a variable is true, and the variable is set to true by the data-receive event, but it didn't help, if I do that the programm stays in this loop forever. I also tried Application.DoEvents() inside the loop but it doesn't work. Any ideas?

# Noah Coad said on January 27, 2007 11:17 AM:

Traverer,

Thanks for posting this back!  This is a question a lot of people have asked and it's good to see an answer posted.

# Noah Coad said on January 27, 2007 11:20 AM:

sth_Weird,

What you need to do is attach to the SerialPort class' DataRecieved event that will call your code whenever there's incoming data.  See the 2nd block of code on this post, that what it does.  You can create the SerialPort class in your MySerialConnectionClass and put the DataRecieved event code there too.

# Phil said on February 3, 2007 1:21 PM:

Hello,

    I am new to C# and this article has been very informative and helpful in the startup of my serial port coding. I was wondering if anyone knew how to parse the data coming from the serial port. I want to enter the data into 3 textboxs. I've been looking up information on how to do this, but haven't found much. A sample code to startoff would be very helpful. Thanks for your time.

# Jim Mason said on February 9, 2007 6:14 AM:

Thanks to your article titled "Serial Com Simply in C#", I was finally able to communicate with a serial device that I struggled with for quite some time.  I was trying to use old C++ dlls that even though they worked in C++ .NET, they would not work in C#.  Your article saved me!

I learned alot about serial communications in the process too.  I will definitely be studying the info posted on this web site.

Thanks Again.

Best Regards,

Jim

# coad said on February 12, 2007 11:02 PM:

Phil,

There is a sample application that does something simular to this linked to above in the post.

# Georges said on February 13, 2007 2:58 AM:

Hi, i'm trying connecting to aCisco Catalyst 3560, when I use the port.Open(); after setting all the properties, the port.CDHolding is False, so i cannot transmit data to the Serial Port If someone have the same problem.

# balaji said on February 13, 2007 6:04 AM:

sir

ur article is awesome

im incorporating the same concept in my proj

without using interfaces i should get the data from the port connected and

convert it to the form compatible to the cimplicity

software

plz give the suggestion to do that

revert imediately

# balaji said on February 13, 2007 6:06 AM:

my id is [email protected]

# silibug said on February 13, 2007 9:29 PM:

hi...

your project seems interesting..

can u give us a link of ur schematic design..etcs..

so.. we can build/learn almost like u did...

thanks.. :)

# balaji said on February 14, 2007 7:48 AM:

hi

ill give that tommoro

# Tim said on February 14, 2007 10:29 AM:

When I try to make the example project I get an error. In the file about.cs the System.IO.File.WriteAll method is referenced, but this is not a valid method. The valid ones are listed here:

http://msdn2.microsoft.com/en-us/library/system.io.file_methods.aspx

I'm using C# Express, .Net v2. Anyone else have this problem? I fixed it by changing the call to File.WriteAllText

My previous post on this disappeared......

# altan said on February 14, 2007 4:52 PM:

hi guys

currently i m trying to program PP- 6750 Access Controller

but i only have a vb6.0 source code not the instruction set. the problem is i m writing code in c#. when i investigated the vb6 source code , it seems that it  s too complex and too long approximately 35000 lines! do you think that i can do it in a short way by using c#?

my mail: [email protected]

thanks

# balaji said on February 14, 2007 10:22 PM:

hi folks iam in halfway to finish my project. as i use 1.1 i wrote code on my own i can read the data from the port but unable to write that into a file in csv format. here i can create the file but iam unable to write the data received into the file;

# Gemballa said on February 17, 2007 5:05 AM:

Hi,

I currently programming an atmega168 which is connected to my computer. The communication between the 2 devices works but c# doesnt always take the correct things. For instance if i send with my atmel 123,456,789 (100ms between each number) then my c# application sometimes gets 123,45,78

He just looses some numbers. Is 100ms to fast for a serial port? So that the buffer is full and he cant get any data ? While if i listen to my serial port with hyperterminal i receive all my data correct.

Something weird.

Thanks for helping.

# Karabulut said on February 20, 2007 4:12 AM:

Hi Dear Noah,
Thank you very much Noah! It was a very nice and helpful tutorial for me. I have jumped to C# in Visual Studio 2005 nowadays. But I have a problem with showing the incoming data on my textBox. It works with MessageBox, but not with my textBox. It gives error with my textBox. It gives yellow line and says something like, How to make Cross-Thread Call.... Could you help me?

# Santos said on February 20, 2007 8:44 PM:

Hi I just wanna know if the DataReceived event of SerialPort in C# .Net 2.0 also works in USB to USB?if yes how could I make it work thanks

# Noah Coad's Code : Post 100 - Blog Stats and Post Highlights said on February 21, 2007 11:56 AM:

PingBack from http://blogs.msdn.com/noahc/archive/2007/02/21/post-100-blog-stats-and-post-highlights.aspx

# coad said on February 21, 2007 6:00 PM:

Santos,

See question #6 in the FAQ:

http://msmvps.com/blogs/coad/archive/2005/03/23/39466.aspx#faq

# coad said on February 21, 2007 6:09 PM:

Karabulut,

See question #1 in the FAQ:

http://msmvps.com/blogs/coad/archive/2005/03/23/39466.aspx#faq

# coad said on February 21, 2007 6:11 PM:

Tim,

Perhaps it is deprecated method?  The project still compiles so it shouldn't be too bad.

# coad said on February 21, 2007 6:13 PM:

silibug,

If you buy a Parallax BASIC Stamp starter kit, it comes with schematics and parts to get you started.  http://basicstamp.com

# coad said on February 21, 2007 6:14 PM:

Gemballa,

Keep in mind that data comes in to the serial port's buffer whenever it is avalible and not in nice easy to use packets.  It can be a challange to parse the data correctly to where it appears you have it all.

# Jan Axelson said on February 22, 2007 10:44 AM:

For anyone who would like a look at another .NET SerialPort example, my COM Port Terminal application is available here:

http://www.lvr.com/serport.htm

It includes exception handling, detecting newly attached ports, support for user-selected handshaking/flow control, and more. There are C# and Visual Basic versions.

# karabulut said on February 23, 2007 3:47 AM:

Thanks Noah!

It works now well. This was what I want.

# balaji said on February 26, 2007 12:35 AM:

hi noah

          i had cached the incoming data from the rs232 port

       into the csv file format.

      now i need to convert this into a cimplicity file format.

      i have explored cimplicity in many ways.

      but cant convert to the cimplicity compatible format.

     Is there any provision for it noah.

     i need to convert the csv file raw data(#12$0$0$0) and so on.

    plz give any suggestion so that i could finish as soon as possible.

   revert immediately anyone

# raghu said on February 28, 2007 9:25 AM:

i am doing a project on railway signalling in vb.net.

my requirement is i have to control or adjust the form designed in my computer from another computer connected through a serial port cable.

to be in detail i have designed a track circuit with some picture boxes and buttons.so i need to change the color of the picture boxes and buttons through xl sheet in another computer by changing the status from 1 to 0 and viceversa.

first i tested for the communication between two systems by writing a chatting application which is working fine but i am not getting the idea to do the main task

please help me out in this

# balaji said on February 28, 2007 10:47 PM:

hi raghu

ur project seems interesting

could u send me the project details and

codes so that i can help you

# raghu said on March 1, 2007 12:51 AM:

hi balaji thanks for offering  me help.

i dont know whether i can send u the whole details but i will try to send to send atleast the main code.

first of all i just want a confirmation whether is it possible to change the status of a programm in one computer with xl sheet or some sought of table in another computer through serial port.

as i said in my previous post, communication between the two systems is possible.in that we can only send and receive text strings.

# Chris said on March 5, 2007 7:00 AM:

Your posts were very helpful.

"Access to the port COM4"  was being denied and I have absolute and total authority over this computer! Simply switching to a different USB, which also changed the name to COM6 worked for me!

# Miguel said on March 5, 2007 12:21 PM:

Hi, i´m from Argentina. This page was very usefull for me to make my modem make a call. Now i want to know how to know when the other side pick up the phone and answer the call i make or when is busy??

this is my simplified code to make a call:

               SerialPort myport = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);

               myport.NewLine = "/n";

               myport.DiscardNull = false;

               myport.Handshake = Handshake.XOnXOff;                

               myport.Open();

               myport.Write("atdt xxxx/r"); //xxx phone number

...

 ...

Now, how can i know if is busy o someone answer my call?

Thanks for your support, all of you!!!

# Luke said on March 13, 2007 4:16 PM:

Hello,

Is it possible to develop a code that enables the COM port to send and receive data at the same time (bi-directional communication)? and if the answer is yes, how is it possible to develop such a code?

many thanks.

# coad said on March 14, 2007 12:01 PM:

Miguel,

Does the modem give a signal/command back if it detects a busy signal?  Can you look for this response from the modem?

# coad said on March 14, 2007 12:08 PM:

Luke,

You can think of the send and receive as two completely separate operations.  So you can definitely do "bi-directional" communications, just send data whenever you need to and receive data whenever it comes in.  Whether your device support bi-directional communications is another story entirely and up to the device.

# Luke said on March 14, 2007 2:28 PM:

Well i have created two infrared devices that can send and receive data (using infrared LED to transmitt and Photo detector to receive the data on each device) each device i connected to computer using RS232 in order to do a bi-directional communication between the two computers using the infrared devices that i created. I used hyperterminal to test the devices and they are working except that the letters being received have different symbols than the key pressed! what i am trying to acheive is to use c# in order to send a "hello" word for example from one computer to the other and to make the second computer send an acknowledgement back saying "the word have been received" as an example. So how can i acheive that? is it actually possible?

# ahmed gad said on March 15, 2007 9:47 AM:

hi all

i need one to help me about the interfacing the at89c52 to the pc . i finished the connection of MAX232 AND AT89C52 BUT THE PROBLEM IS HOW TO INRTERFAC PC TO IT.i think that i must have vesual basic software in my pc  i thik that,

but i dont have it.

please help me

thanks all

# zafar said on March 16, 2007 9:07 AM:

hi.

its nice to see such competent people on net.

but i am still not satisfied bcz my requirements are still not fulfilled.bcz i wants to have input from my 8051 uc  and process that data in c++ programme.for example if i press a button on keypad of uc then pc should show a message well come if i press any other button it sholud say good bye

.or ican do numeriacel calcualtion.so kindly guide me.my email is

[email protected]

# Saeed said on March 17, 2007 1:38 AM:

Hi Miguel and All:

First of all thanks for such a nice article. Secondly i want to dial my ISP to connect to internet using Modem on Serial Port...

Miguel i think u have worked on this.. can u give me some more help

Waiting ,

n thanks once more..

SAEED

# oliver456 said on March 19, 2007 6:57 AM:

hi all

 very nice article!

 I have a problem , a serialPort is in use by an another application , I want to  monitor its data ,how can I do that

thanks

# Miguel said on March 19, 2007 7:50 AM:

Saeed, i´ve been in vacations. First i have to check something that coad askme about my problem. In your question, i don´t understand very well what you want to do, but the code i write up in the post works very fine to dial any number you want. I´m using VS2005 with Serv Pack and a internal modem on my notebook. I recommend you to use the static method: string[] myPorts = SerialPort.GetPortNames(); so you can see what aviable ports your pc have. Then you can use my code post up here to make your modem dial a phone number. I think that if you want to handle the internet connection you will have to use the

void myport_DataReceived(object sender, SerialDataReceivedEventArgs e) method to handle what you want. The code up load by Coad is very help full. Just remember to make phone call via the modem you have to use AT commands and have all your connection cables set well (i mean the line cables with the modem). my mail is: [email protected] bye and thank every one for all the post.

# ghalip said on March 21, 2007 5:48 AM:

now i face to the problem that ..

when i change the baund rate from 38400 to 9600 and begin initialize the port (serial port sp1;)it dosn't work?

why?

# mark said on March 28, 2007 10:40 AM:

the device manager on my laptop doesn't show any serial ports, but getportnames() returns "com3". what does that mean?

# JohnH said on March 29, 2007 8:11 AM:

mark, com3 could just be the port used by an internal modem. You can check your modem properties to be sure.

# JohnH said on March 29, 2007 8:21 AM:

Has anyone managed to get the SerialPort class to work using redirected com ports on a Remote Desktop Connection?

I'm running the SerialPortTerminal example app on the server (using RDC to connect to the server) and trying to send/receive data through the client PC's com port.

Sending data to the client port seems to work okay but no data is received. I know MSComm (VB6) had problems with Remote Desktop but I was hoping that framework 2.0 serial driver would do the job.

# Good said on March 30, 2007 6:19 AM:

Hi Noah, first of all thanks for this tutorial, it's very useful for me, a newbie of C#.NET.

I've encountered some problems with serial port communications between PC and a reader, hopefully can get some help from you.

I just wrote a simple program to send a byte array from PC to reader and the reader will send back another byte array to the PC. here is the code:

private void button1_Click(object sender, EventArgs e)

       {

           open_serialPort1();

           byte[] data = new byte[7];

           serialPort1.Write(addCheckSum(resetReader), 0, 4);

           serialPort1.Read(data, 0, data.Length);

           close_serialPort1();

           string str = BitConverter.ToString(data);

           richTextBox1.Text = str;

       }

When I first time press the button1, the reader can receive the byte array correctly from the PC and the PC also can receive the data which is sent back by the reader, everything is correct.

But when i press again the button1, only the reader can receive the data from PC, but PC cannot receive any data from reader, the richTextBox1 showing 7 bytes of 00.

Can you pls help me to figure out the problem, is there anything wrong with my program?

# ... said on March 31, 2007 2:02 AM:

Du musst ein Fachmann sein - wirklich guter Aufstellungsort, den du hast!

# jithen said on April 13, 2007 8:54 AM:

Hi im trying to get my c#.net app to work the problem is that when using

SerialDataReceivedEventArgs it echos all bytes/strings that is sent to the

port. If I send the AT command it always echos the AT and OK I just want it

to echo the OK.

# SWAPNIL M GHOLAP said on April 15, 2007 8:35 AM:

RESPECTED SIR ,

    I AM STUDENT OF THIRD YEAR,STUDYING IN INSTRUMENTATION AND CONTROL ENG.

SIR,I AM DOING PROJECT, IN WHICH I WANT TO READ THE SIGNAL FROM COM PORT1 THROUGH RS232 PARALLEL(DB25)AND THEN WITH RESPECT TO TIME,I WANT TO DISPLAY GRAPH ON VISUAL BASIC 6.0 SOFTWARE.

    I WANT CODE IN VB,WHICH WILL DISPLAY THE GRAPH WHEN THE INPUT WILL CHANGE,GRAPH ALSO CHANGE FROM RS232(DB25). I HOPE U WILL DEFINEATELY SOLVE MY PROBLEM.I AM WAITING

FOR UR RESPONSE TILL WEDNESDAY.

# wiwi develop said on April 16, 2007 7:10 PM:

i need some help i have a bluetooth printer and send the string to the port COM9 example puerto.WriteLine("something");

but a need send to the printer code for barcodes is send it by hexadecimal code o how?

# Ngoc Lan said on April 19, 2007 1:53 AM:

Pls help me!

I am learning programing for Serial Port in C# and I have encountered  a problem. I don't understand why SerialPort.Write method don't act. I used that method , but I didn't receive data from the port.

Pls show the reason!

# David Howell said on April 29, 2007 2:41 PM:

Thanks for the useful post on serialport. I am making a program for reading numbers and a newline trigger from serial connected balances.

Just a note on your SerialPortTerminal program. There seems to be no System.IO.File.Writeall method anymore, and I think .WriteAllText is what you need there.

I get the part about setting a buffer and reading it and writing it, but how do I write it to any active field on screen? I don't want to write to file or console, but rather to an information management system that can take key entries. Somewhat like a keyboard wedge.

# kranthi said on April 30, 2007 5:42 AM:

am kranthi

am working a project in small company

am fresher so please give me code about my problem.

what am saying my sir one modem(that is one type of modem) connected to com1 port(serial port) in my pc.

am using .net 1.1 with c#.net , so i will created one form.

am used one text box and one button in that form ,

we will type some message in that textbox and click button.

how will send that messge to one any mobile number through comport settings(serial port).

please give me that code.please u can solve my problem.

# girija said on May 2, 2007 2:10 AM:

how will know the receiver is availabel or not?

we sended the data to receiver but that is availlabel or not how will know.that is not availabel how much time will wait and how will display receiver not found.

# girijapakala said on May 2, 2007 2:13 AM:

we send the data to reciver but that is not found how much time wait and how will display receiver not found message

please tell that anwser

# Carmelo said on May 3, 2007 4:09 AM:

Thank you very much for the code...if I could ever return the favour feel free.  Recieving serial was not as straightforward as sending.

But using invoke and the way you've demonstrated has really really help...cant thank you enough.

# Dean said on May 4, 2007 9:03 AM:

I used this code as a starting point for interacting with an old barcode scanner that connected via RS232.  Only real thing to add to what can be found here is that I needed to set RtsEnable to TRUE in order to receive data, otherwise the scanner just hung.

Oh, and if anyone's looking for info on a Handheld Products (formerly Welch Allyn) 3400 scanner, don't believe the defaults given in the user guide - it's 9600 baud, 8 data, 2 stop and no parity!  ;-)

# Yasser said on May 7, 2007 11:15 PM:

Hi, Folks,

Can anyone help me with next probem

I have notebook with 1 Com (rs232) port.

On this laptop also have modem and infrared device.

I d'like to create from modem or infrared another COM rs232 port, So bi visible under pure dos withouth any drivers.

Some of programs under dosr does not working when using PCMCIA - rs232 ot USB - rs232 adapters.

Best regards

Yasser

# Syntax76 said on May 8, 2007 6:30 AM:

Thanks for the great site. Intresting thing was i was looking for a tutorial for SerialPort, since my try went somehow wrong (i thought).

Im experimenting with serial communication between BASIC Stamp (BS2 for now) and PC; i already found the SerialPort class in .NET and was really glad for that. Unfortunatly the DataReceived event handler  isn't willing to work, neither at my code nor at yours (SerialPortTerminal) ; when using e.g.  PuTTY it displays my data sent.

I tried both in PBASIC: SEROUT and DEBUG

SEROUT 16, $4054, ["100 GET", 13]

as well as

DEBUG "100 GET", 13

(of course both are equal, but you never know...)

that sending both ways works is, as written, visible in PuTTY

in all 3 testing apps (my, yours and PuTTY) the serial settings were the same (9600 baud, 8 bit, 1 stopbit, no parity, no flow control)

and the .NET apps don't even jump into the event-handler (it never stops at the breakpoint inside).

Maybe you have a hint for me?

Best regards

Syntax76

# Syntax76 said on May 8, 2007 6:35 AM:

oh yes, to mention:

i use Win XP x64 may be this is the reason? But in the docs for this class is explicitly written, that it works under XP x64...

# Clement said on May 9, 2007 4:55 PM:

I get an "Access to the port 'COM1' is denied" when I run the GUI version of my C# program, but when I run the console equivalent, I don't get the error and everything works perfectly.

I did the testing with the console and put the cs file in the other project, renaming the namespace of course. Why is that? I don't have a similiar problems with the other COM ports, but I need to use COM1 since the others are taken.

# Ryan Dutton said on May 10, 2007 10:09 AM:

I had the "UnauthorizedAccessException" error.  It turned out that running the application from a mapped network drive was the problem.  If you have this issue, ensure its running from your C: drive and see what happens.

# Clement said on May 15, 2007 3:08 PM:

I do run it from the C drive. I think there is a problem with using the serial port in other forms other than the main form. If I put the code in the main form, it works fine.

Whatever works. LOL.

# Surya said on May 16, 2007 8:18 AM:

Hi,

I am sending data in bytes over serial port using .net 2.0.

problem is I am not getting the response from the device.

# behkian said on May 20, 2007 4:50 AM:

Hi I have writen apog by using MSCOMM.ocx in vc++.net(windows application form) but I can not get numbers more than 127

if u can help me about this my prog must get this valuse and put them in thier text boxex and thier showers and I 'm getting data from 3 ports at one time .

if i can't use mscomm in vc++.net please guide me to use serial port component

thanks a lot

# Michae said on May 24, 2007 7:17 AM:

Hello their developers..  can you help me with my problem... i'm using a foot pedal device and i want to read the data it sends, so i use the serial port in VS2005 but i dont know what code for it to recieve the data from the foot pedal??... can anyone help me.. pls

# Surender Rawat said on May 25, 2007 2:02 AM:

Hello all developers

i have to develop an application in dot net which communicate with a infrared sensor which count the number of people passing away from. there is need of serial communication.

Dot net know how to start of it.

How to detectet by the application whether device is connected or net, how to receive and sent data.

# Joesy said on June 1, 2007 5:36 AM:

Hey,

I found your article which is an execellent one. I implemented okie but I got problem with receiving reply message:

In order to receive reply, I changed a little bit in your send button event:

port.RtsEnable = true;

SendData();

port.RtsEnable = false;

But with the same command send, I received diffrent reply messages:

<<< Sent: 02 30 D0 31 30 30 31 30 30 30 52 03

>>> Received: 60 83 06 AF 4C 36 56 76 0C 00

<<< Sent: 02 30 D0 31 30 30 31 30 30 30 52 03

>>> Received: 60 E8 15 32 33 35 17 00 00

Could you please help me? I uploaded my code here: download.yousendit.com/BEF9777251C1D8EF

This is very urgent project of mine, Can you please spend some time to help me? Could I have your email, I will contact you?

Thank you very much,

Joesy

# Jun said on June 3, 2007 1:45 AM:

I need to to simultaneously open and use 8 Comport!

Hello there,

Im communicating with 8 serial devices (Atmel 89S52 microcontroller) using usb2RS232 adapter connected in usb hub. Enumerated port are Com1 to Com8.

In order to send and receive data, i have to open and close each comport and sequently move to next comport.

I can exchanged data to and from serial devices however most of the time it throw an exception  " access denied to Comx". And eventualy my application would hang up.

I figure its hard to close and dispose serial port resources in moving from i.e. Com3 to another Com4.

Even I give enough time to Close the comport using timer.

I would like to Open and Use 8 Comport (Com1 to Com8) simultaneously.

I would deeply appreciate any hints, clue, HELP from anyone!

Jun

# Joesy said on June 4, 2007 8:38 AM:

I got a probem when event fires: DataReceived:

Frame: The hardware detected a framing error.

Can you help me to explain about this error: When, how and where this error ocurrs?

Thanks,

Joesy

# JD said on June 5, 2007 3:35 PM:

Hi,

I'm working a project using audio data gathered with a mic, using C# and DirectSound, that I'm trying to send to a device via serial, however because I'm so new to this, I cant get the audio to stream.  I can send files already recorded, but the goal is a "live" transmission of sorts.

Can anyone possibly help me?  

I'm new to C# (like 5 or 6 days new...), so code with an explanation would be a great big help!  Thanks!

JD

# Phillipe said on June 7, 2007 3:37 AM:

Hey Everybody! i think this is a little more complicated but maybe someone could help me, am working on a PIC - 18F258, i am sending data between the PIC and the program that Noah created, but every time my PIC send data it only appear 3F for every hex that my pic send, the weird thing is when i test the pic with terminal v1.09 because it works perfect, every hex that my pic send appear on terminal but can't do this with the program in c#, could someone give me some help? plz........... my e-mai is [email protected]

# Abebe said on June 8, 2007 2:48 PM:

Currently I am doing my final year project  and I came across Serial C# interfacing . I want to generate 40khz,50% duty ctcle square wave. Can any one help me PLZ?

# kiwi said on June 10, 2007 10:20 PM:

hi guys.. i need to read tags using rfid reader. the reader is connected to the computer using serial port. can anyone advise me the codes for receving the tags data?

thanks

# kiwi said on June 10, 2007 10:25 PM:

hi guys.. i need to read tags using rfid reader. the reader is connected to the computer using serial port. can anyone advise me the codes for receving the tags data? i'm using microsoft visual studio 2005.

thanks. your help is appreciated.

# andrew said on June 13, 2007 5:53 AM:

hi just wondered if you had ever tried coding to something like this

www.mp3car.com/.../102176-need-advice-obd-c-net-app.html

it connecting to a cars computer

andrew

# dan said on June 14, 2007 9:48 AM:

upon receiving properly formatted data according to a given protocol, i need to send back an ACK message within 2ms. is there any way this is possible using the SerialPort class? or is this only possible with drivers? i would really like to keep my implementation as simple and high-level as possible. i really don't want to have to write a driver to do this.

any suggestions?

# vishnu said on June 19, 2007 12:38 AM:

Hi Noah,

Great article..... it help me lots of think........

keep it up......

Cheers........!

# Chris said on June 20, 2007 9:39 AM:

Thanks for sharing your great info - I have a VS2005 .Net2.0 app which is reading a Truck Scale.  It works great with 1 huge exception - the datareceived event is not firing soon enough.  I am running a port monitor along with my application and realize my application lags about 20 lines (roughly 12 characters each line) behind the actual data being read in the port.  Is there a way to speed the datareceived event to trigger faster?  Each line being received from the scale ends with a CRLF.  Thanks for your help!!!

# Miguel said on July 4, 2007 4:56 AM:

Thanks a lot for this article.

# kumaran said on July 5, 2007 4:35 AM:

Can anyonr guide me on writing barcode scanner(PT2000 TopGun) program. Just to download data from it

# Miha said on July 6, 2007 8:44 AM:

Hello, I am new to Serial Port Programming. Liked your article. I have bought two usb to com adapters and gender changer (is this the same stuff as null modem adapter?). I setup everything, and I now have COM3 and COM4 ports (USB2COM adapters). If i programaticaly send writeline on com3 port the light on the port blinks. My question is how to loopback these two connections, so I can test software with it.... I think the problem is, that  do not have null modem adapter. Please if anyone has any ideas and comments please, let me know. Thanks!

# hussein said on July 6, 2007 10:21 AM:

Thanks for the great sample. I have one issue, I used the code provided to lsiten to a port that has a card reader attached to it, I am receiving the data in the receive event but it gets split, so i receive part of the message and then receive the rest. Your assistance is much appreciated.

# Miha said on July 6, 2007 11:02 AM:

OK, I have resolved my issue. I've created Loopback adapter and connected it to USB2COM cable and stuff now works!!!

Here is how to connect pins to create loopback adapter.

www.passmark.com/.../loopback.htm

# logki said on July 9, 2007 1:29 PM:

Hi I am trying to write to a serial port  using a protocol which looks like this

< data >

and heres is how i am writing it

public void SendDatathruPort(int addr, int length, int comd, int checksum)

       {

           byte[] data7 = new byte[17];

           //byte[] data1 = datainuput();

int totalsum = data7[0] + data7[1] + data7[2] + data7[3] + data7[4] + data7[5] + data7 + data7[7] + data7 + data7 + data7[9] + data7[10] + data7[11] + data7[12] + data7[13] + data7[14] + data7[15];

           checksum = (totalsum) & (0xFF);

           data7[0] = (byte)addr;

           data7[1] = (byte)length;

           data7[2] = (byte)comd;

           data7[3] = Convert.ToByte("1");

           data7[4] = Convert.ToByte(txBxP1.Text);

           data7[5] = Convert.ToByte(txBxP2.Text);

           data7 = Convert.ToByte(txBxP3.Text);

           data7[7] = Convert.ToByte(txBxP4.Text);

           data7 = Convert.ToByte(txBxP5.Text);

           data7[9] = Convert.ToByte(txBxP6.Text);

           data7[10] = Convert.ToByte(txBxP7.Text);

           data7[11] = Convert.ToByte(txBxP8.Text);

           data7[12] = Convert.ToByte(txBxP9.Text);

           data7[13] = Convert.ToByte(txBxP10.Text);

           data7[14] = Convert.ToByte(txBxP11.Text);

           data7[15] = Convert.ToByte(txBxP12.Text);

           data7[16] = (byte)checksum;

           comport.Write(data7, 0, data7.Length);  

       }

and this is how i call it

private void setvalue_Click(object sender, EventArgs e)

       {

           // Send the user's text straight out the port

           //comport.Write(txtSendData.Text)

           //SendData();

           SendDatathruPort(0x7B, 14,0x0A,0);

       }

# logki said on July 9, 2007 1:30 PM:

When I set the values it doesnt seem to be seeting the values on the controller ,... any help is appreciated

# ahmad said on July 10, 2007 2:04 AM:

hi;

Is there any way for me to use my .net 2 .exe aplication in other PCs (or platforms) which has not .net frame work?

regards

# Geoff said on July 12, 2007 7:28 AM:

Hi Noah and all

This is a very useful page and has helped me greatly.

As a matter of interest has anyone had issues with

SerialErrorReceivedEventHandler on a windows XP pro

SP2 laptop using USB serial converters?.

My code displays framing and parity errors ,but will not

generate any events using USB on XP.

A PCMCIA serial card port on the same laptop works fine.

The same code also works well on Win 2k pro using either PCI or USB ports.

Best Regards

Geoff

# Rafael said on July 19, 2007 5:41 PM:

I' am trying to control a power suply using RS-232 interface

But the power suply has a frame of 26 bytes

I am newbie and I just now hot to send strings through serial port, not how to sent bytes,

Please help me

[email protected]

# Georgi said on July 20, 2007 1:51 PM:

Hi,

I need to read binary data from comp port on C#, do I can use

byte[] signal;

signal = new byte[5000];

port.Read(signal, 0, 500);

And how to be able to read binary frames?

# Georgi said on July 20, 2007 1:51 PM:

very interesting article

[email protected]

# Chris said on July 22, 2007 3:34 PM:

Hey Guys,

I can get a list of com ports in use to print out to the command window but i would like more information than just "com1, com2" i would like to see for example  "com1 Max Stream UVG, com2 4G cruiser mini" get my drift?

Any one have an idea how to do that?

Thanks!!!

# Jones said on July 26, 2007 5:20 AM:

Hey nice site!

Am currently doing a project with CS and USB to UART controller (Virtual COM port), my aim at the moment is to be able to write a string of data from one from COM1 port to COM3, then get a reply back acknowledging reciept.

what have done so far is use some "if and else if" statment in my OUTPUT  box (textbox)  so that if the input we require is inside the textbox, we get a reply in the other COM port saying recieved.

This works fine, BUT AFTER RECIEVEING THE 1ST TEXT, I CAN'T READ NEW TEXT INPUT, BECAUSE MY READ COMMAND READS ALL THE TEXT IN THE OUTPUT TEXTBOX, I HAVE TRIED READLINE, IT DOESNT WORK NEITHER.

Can someone advise on what method i can call to read new string from my output textbox rather that reading everything, and also how can i go to a new line automatically inside a text box. (this doesnt work /r/n)

if (this.txtData.Text == "Hello")

           {

               serialPort1.WriteLine("recieved/r/n");

                           }

           else if (this.txtData.Text == "Hiya")

           {                

               serialPort1.WriteLine("hey");

           }

so what am saying is that "Hello" goes into the output text box and "Recieved" is printed out, but when "Hiya" goes into the same output box, the readExisting() or readLine() method reads everything inside the text box and doesnt print/writes out "Hey"

Please help if you can!

Regards

Jones

# mina said on July 31, 2007 2:16 AM:

hi.what i can connect 2 pc whit port usb whit c#?

# mina said on July 31, 2007 3:15 AM:

hi

what i can connect 2 pc whit port usb?

i need code c#!

please help me!!

# junied said on August 1, 2007 2:18 PM:

Excellent artical

i was searching for serial communication example using .net2005 v2

but this was the best

# Adrian said on August 2, 2007 7:29 AM:

Nice article.

May you help me with a question?

Can i set DTR value? I need this signal to reset a PIC mcu.

# YZ said on August 16, 2007 5:06 PM:

I  can get SerialDataReceivedEventHandler to work if I send a command to a serial device in a timer or click botton event. However, if I use a while-loop to continue send comands with a Thread.Sleep(1000) in between, the receive event does not even fire once. Why cannot I use while-loop in this case?

I would greatly appeciate your help.

# krishna said on August 18, 2007 1:09 AM:

i want connect foot padel through comm port.

if pin change then how to call pin change event ?

# krishna said on August 20, 2007 7:34 AM:

if i change comm port pin then auto working as foot padel for use in wav file.

how to work

send me

[email protected]

# hassan said on August 24, 2007 2:53 PM:

hi i want make a dailing with modem with c# code but i dont what work i must to do please help me thanks

# vissu said on August 28, 2007 12:04 AM:

HI i am also trying the same thing with vs.net 2005.

what i am looking for?

I am having a weighting scale which will give lt/wt/depth/hgt etc.

I am a system with .net 2005

using RS 232 cable i am trying to get those data from scale to my application.

for that using serialport class i am configuring the com and then i am taking the data from scale.

Problem facing:

step1: In my .net windows applcation i click an Activate button.In this i had written a code

a.send a request to scale

b.sclae will send an acknowledgement as * to my application.

c.then user will keep an weight in scale and click print button in device.

d. In my application i kept a message button which user can peform above step.

e. then i am getting the bytes of data to my application and am storing thta information into my database.

Now problem is

In 2nd time if user has kept another weight in scale and user click a print button then its not getting that data. Even i kept a while(True) loop.

How can i achieve this in my application.

is there any way to achieve that 2nd, 3rd ... data from scale with out restarting comport repeatedly.

if u had give code it will be very helpful to me.

# sam said on September 6, 2007 10:35 AM:

Hi, I am using VS 2005 and have a Thermal Printer connected to COM1.  I am trying to sniff text being send to the printer but every time I try to open COM1 it will give me the following exception:

{"The given port name does not start with COM/com or does not resolve to a valid serial port./r/nParameter name: portName"}

My printer is plugged in and turned on, the driver is installed... I'm not sure how to get rid of the exception.  

Thanks for anyone's help in advance!

# sundar said on September 8, 2007 9:53 AM:

Hi,

How to send and receive data from com1(port)?

any dll files add   to the port  access?

can you replay clearly?

Many thanks s s s s s  s s s s sssssssssssssssss

# jenny said on September 9, 2007 9:03 PM:

Hi, I would like to ask the same question as Haluk.

I am running the serialport transmitting program very fine in computer which using english language for non-unicode, and also standard and format, but when I change them to Chinese PRC, I got problem to send unicode 192.

I am using VB .net,

Dim buff() As Byte

       Dim i As Integer

       If SerialPort1.IsOpen Then

           ReDim buff(2)

           buff(0) = 192

           buff(1) = 80

           buff(2) = 192    

           SerialPort1.Write(buff, i, 3)

       End If

when I tried to capture with hyperterminal, i will get 192 192 80. Why this happend, and how to solve?

I really hope you can help me.. Thanks

# ali said on September 11, 2007 3:03 AM:

hi

I write a Pocket PC program with c# and use it's serial port to communicate with my device. I have a problem with serial port. I defined a "static" class for serial port

communication. I have several form and used serial

class in most of them. When I run program, for firs time

I can connect via serial port. after first communication

I never can send or transmit data with serial port.

I checked serial port to be open. It open but do not response.

# rajini said on September 17, 2007 4:42 AM:

hey all,

i need to receive a file from pos terminal to system in vb.net any body can help me........

# rajesh patel said on September 19, 2007 2:09 AM:

Hi

 i need add swipecard device into my project (asp.net c#) so which step i follow for this task pls help

# Ajay Khatri said on September 19, 2007 11:59 PM:

very nice ...............!!!!!

# David Amyotte said on October 1, 2007 10:53 AM:

Hello,

I have a question about programming in C# and .net 2.0 with serial ports..

I'm writing an application to read data from a micro controller. I know the data array the controller spits back is 24 bytes.

I used the bytethreshold property and set it to 24. What the program does it sends a hex 93 command and its spits back data. Problem is when I started using the bytethreshold property and if I hit the send command for  a second time the data in the array comes back isnt in the order it should be.

The reason why i used the bytethreshold property is to prevent the datarecieved event from firing until I have all the data in the buffer then I can store the data in my byte buffer.

What is the issue? Should I be clearing my buffer after each send command?

# Darshan said on October 4, 2007 1:29 PM:

Hey I was just wondering if there was away around opening COM ports (more than one app).  Thanks

# cute-solutions said on October 9, 2007 6:58 AM:

very nice tutorial!!

# Rodrigo Silva said on October 9, 2007 11:48 AM:

Dear,    

the application usually works, when I open first in the hyperterminal and closed.    

Else makes this procedure the application doesn't work.    

Did anybody already go by this problem?    

I am using C #. Net 2.0    

Thanks

# Stefan Matokic said on October 12, 2007 4:51 AM:

hello,

i've got a problem with comports, too. i need a way to close a opened comport in c# after the device on it already were turned of. it's not enough for me just to catch the exceptions, for regulare it would be myPort.Close(); but if the device is missed at the comport you'll recive a unauthorizedaccess exception.

someone an idea?

i would be very happy about some ideas --> stefan(at)matokic.de

thanks...

# krishna said on October 12, 2007 5:07 AM:

i have connect comm port and also call pinchange event but does not able to connect DataReceiveEvent.

so pls help code how to connect .

[email protected]

thanks

# german said on October 12, 2007 1:20 PM:

hi i'm working  with serialport1 and i send to an aplication in C#  10 bytes and the aplicacion receive just 8 byte always, my buffer rx is of 20 bytes, what is wrong

this is reception

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)

       {

          int total = serialPort1.BytesToRead;// here always read 8 just bytes and bufer_rx is a20 bytes

           serialPort1.Read(bufer_rx, 0, total);

# NloGoloMRaR // Yuhn said on October 13, 2007 3:22 AM:

Hi German ... Check this out

Just put in a Thread.Sleep(250); in the start of the method, then it haves time to read all the incomming date in the buffer before empty it.

private void port_DataReceived(Object sender, System.IO.Ports.SerialDataReceivedEventArgs e)

       {

           Thread.Sleep(250);

           int total = sp.BytesToRead  

           buffer_rx = new char[total];

           sp.Read(buffer_rx, 0, total);

           for (int i = 0; i < buffer_rx.Length; i++)

           {

               sB.Append(buffer_rx.ToString());

           }

           MessageBox.Show(sB.ToString());

           sB.Remove(0, sB.Length); //sB is a StringBuilder

       }

# paper said on October 18, 2007 11:44 PM:

hi,

I need help in a serialport communication program. i will have 2 form. form1 and form2, form1 will allow user to select their comport, baudrate,parity etc in a combobox. when i click ok, a connection should be establish in order for me to run form2. i have no idea how i shd write it, therefore i need some help here.

# Randy Douglas said on October 19, 2007 12:29 AM:

Ashley nailed the problem for me here: "Setting DTR to enabled and handshaking to hardware should power the pin that manufacturers use for power robbing as well. "

I added "comport.DtrEnable = true;" and that solved my DataReceived problem where it would output the result after passing an AT command to my ISU to send SMS messages.

Thanks Ash ;-)

# german said on October 19, 2007 1:50 PM:

hi Yuhn

thank you, it's working fine

# german said on October 19, 2007 1:52 PM:

hi Yuhn, thank you it's working fine

# mohsen said on October 29, 2007 3:22 PM:

hi

i want to make mens that cheng USB to com

please send for me shematic and file hex about it.

thank

# Meme Team · S2MIDI: Open Source Serial to MIDI Converter for Windows said on November 7, 2007 2:43 PM:

Pingback from  Meme Team  ·  S2MIDI: Open Source Serial to MIDI Converter for Windows

# Kai said on November 15, 2007 10:51 PM:

Hi mister, i doing my final year project which will link up to a solar panel to accuqire the voltage reading , it will go through the analog to digital conversion , so the signal( voltage reading becomes digital) , it will then send to a wireless transmitter , and send to the receiver which will then be interface to the computer thru rs232 to usb converter to comp.

currently i using C# , do u have any sample program to plot graph of the voltage reading against time ?, and since the voltage reading from solar panel varies, do i need a timer or so ? thanks alot for your kind attention , i using visual studio 2005

# kai said on November 15, 2007 10:53 PM:

my email is [email protected] if u need to send me any sample data thanks alot...

# Sri said on November 16, 2007 5:30 AM:

Hi,

       I Have to Pass ctrl + D thru my Serial Port Connection Can any body say how is it possible, bit urgent pls

# Vibhor Goswami said on November 18, 2007 11:56 PM:

Hi!

I'm using C# to create an interface between my card reader and my PC. I usually got Access denied but when i booted the system without my device connected to it, the error didn't appear when i run the program. Is there any way to prevent windows from accessing the 'COM1'? i also require some help to further communicate with my card reader as my establishment with the reader is done using methods from its dll but the reader doesn't respod. Pls guide me in some direction on how to proceed further? Any help is appreciated.

# Thai Do said on November 19, 2007 2:47 PM:

Hello Noah,

Is it possible to access a remote com port using c#, a com port that is on another computer?

# seph said on November 22, 2007 2:49 PM:

msdn says:

PinChanged, DataReceived, and ErrorReceived events may be called out of order, and there may be a slight delay between when the underlying stream reports the error and when the event handler is executed. Only one event handler can execute at a time.

quote

Only one event handler can execute at a time...

does this mean that if a 1st serialport event is executing and have not  yet returned while a second event fires.. the second event is lost to oblivion...

I've setup a microcontroller to send 2 parity errors but only one error receive event fires..

also is the pinchange event can only fire in the breakstate i.e. when the port is not sending and receiving..

# Anil said on November 28, 2007 11:25 PM:

I want to send Ctrl+A on serial port....How can I send the Ctrl+A... Please help me... Its very urgent.

Anil

# bahar said on December 3, 2007 2:06 AM:

Hi

  I want to use Image scanner in my project with c#.net.

so how can I send and recieve data to/from image scanner with c#.net

thanks

# Jonathan Vanpeteghem said on December 4, 2007 5:19 AM:

Great tool! I used it to communicate with the pic 16f877 and it worked perfectly on the first time. I will add some exception handling, for when the pic does not reply to the pc ;-)

Thanks!

# Manoj said on December 11, 2007 10:17 AM:

Hi I am using this

string st = serialPort1.ReadExisting();

int j =recievedB.ToString().Length();

I am using .NET 2.0 serialPort() class and ReadExisting method. I am interested in getting value not length. How do I do that. Thanks in advance.

# Taiyeb ali said on December 12, 2007 2:42 AM:

Sir, i am desinging fapplication in asp.net in which i wnat to use a port to send sms by that port to attach mobile with that port

please help i am not getting port response

# prem said on December 16, 2007 11:55 PM:

Hi

iam working on one application, using my app i should read data from device using com, i know how to read data from port.

my question is how to identify/ scan the port(com1 or com2,,etc)? is any specfic way is there.

plz help me on this.

Thanks

# tester said on December 17, 2007 4:33 AM:

hi, i'll test this applikation, will send you my solutions

# vijay said on December 19, 2007 6:11 AM:

the value of ctrl+A can be sent as (char)1,ctrl+b as (char)2 ..............ctrl+z value as (char)26

# harpal said on December 21, 2007 4:44 AM:

hi coad,

            i am trying to fire an at command on a device using your serial port tools but device si not reponds to these commands...........

# shraddha said on December 24, 2007 11:48 PM:

hey i want to connect foot pedal for player with the use of Asp.Net 2005 with C#.... online...

Can you guide me How it will bw possible?

# Yos said on January 3, 2008 11:21 AM:

Hi

I have 3 serial devices. Should I use 3 hreads to handle each incoming com port? is there any way to handle this issue with one thread/function, how?

Thank You

# senthil said on January 4, 2008 7:38 AM:

Hi,

when i send AT command to my modem it is not responding. In hyperterminal AT commands work fine, i was able to send SMS using it. When i use AT command in C# 2.0  there is no reponse from the modem. Modem is in the server, i have created the application in my server and running it from my local machine. Please help

# monica said on January 8, 2008 9:34 PM:

hi, I am using visual C# 2008 express edition and the code i found in this page does not compile, and it does not seem to be able to be converted either.

# IoNiX said on January 9, 2008 5:53 PM:

hi.. i want to learn a lot of stuff as much as i can about c# serial port.. my email is

[email protected]

if sum1 know a lot of c# and serial port.. it would be nice if u add me.. to help pls.. ty

Leave a Comment

Name:
Website:
Remember Me?

Search

  • Go

This Blog

  • Home
  • Contact

Tags

  • C#
  • General
  • Personal
  • Scripting
  • Tools
  • VSTS
  • XML

Community

Archives

  • January 2006 (1)
  • November 2005 (3)
  • October 2005 (1)
  • July 2005 (2)
  • May 2005 (1)
  • April 2005 (6)
  • March 2005 (4)
  • February 2005 (7)
  • December 2004 (1)
  • November 2004 (1)
  • October 2004 (3)
  • July 2004 (1)
  • June 2004 (16)
  • May 2004 (6)
  • April 2004 (13)

News

  • My blog has moved:
    Current Blog

General

  • Coad .NET Products

Syndication

  • RSS for Posts
  • Atom
  • RSS for Comments

Email Notifications

  • Go

你可能感兴趣的:(serialPort)