C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 307,136 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,914 people online right now. Registration is fast and FREE... Join Now!




Serial Port Communication in C#

2 Pages V < 1 2  
Reply to this topicStart new topic

> Serial Port Communication in C#

sstadler
*



post 28 Mar, 2009 - 08:17 AM
Post #21
I am trying to use your program to learn how to use C# to communicate to a com port that has a USB part connected to it,

When using Hyperterminal, I can select higher baud rates when a USP part is connected, such as 921600.

I edited the collection of baud rates to include this higher rate, but I get a parameter error when I open the port using this selection for the baud rate.

What else is different when using a USB part serial comm port?

Thank You,

Steve
Go to the top of the page
+Quote Post

arie1949
*



post 22 Apr, 2009 - 01:02 AM
Post #22
QUOTE(ecsv8agb @ 28 Dec, 2007 - 11:38 PM) *

QUOTE(Louisda16th @ 20 Oct, 2007 - 10:57 PM) *

Nice tutorial! Thanx smile.gif



This code can not be use ! no react of the application


I tried to unzip the SerialPortCommunication.zip but it seems corrupted.
Can you send a correct one to arieabramovitch@hotmail.com ?

Thanks a lot
Arie
Go to the top of the page
+Quote Post

dersk
*



post 13 May, 2009 - 11:43 AM
Post #23
Great info, but I'm having some trouble understanding how the threading works here. Is the serial port code on a separate thread than the form? Do the invoke and the [STAThread] statements execute this code on the original thread? Thanks!

CODE

[STAThread]
private void DisplayData(MessageType type, string msg)
{
    _displayWindow.Invoke(new EventHandler(delegate
{
  _displayWindow.SelectedText = string.Empty;
  _displayWindow.SelectionFont = new Font(_displayWindow.SelectionFont, FontStyle.Bold);
  _displayWindow.SelectionColor = MessageColor[(int)type];
  _displayWindow.AppendText(msg);
  _displayWindow.ScrollToCaret();
}));
}
Go to the top of the page
+Quote Post

cmmapada
*



post 25 May, 2009 - 09:48 PM
Post #24
why is the zip file corrupt. cant seem to extract the file
Go to the top of the page
+Quote Post

richl
*



post 19 Jun, 2009 - 08:04 AM
Post #25
Thanks for the serial com C# lesson (I am not done reading it yet), but I have a question about com events.

I this code (below) from a C++ app that I am working on, the events created and the real time status of the port are available. I will use this to manage the states of an interface to an embedded device (a controller).

The reason I am asking this is that I would liek to know if these services and events are available in C#. I haven't found any documentation that helps in the Visual Studio documentation.

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

(o is of type OVERLAPPED)

o.hEvent = CreateEvent(
NULL, // default security attributes
FALSE, // auto reset event
FALSE, // not signaled
NULL // no name
);


// Intialize the rest of the OVERLAPPED structure to zero.
o.Internal = 0;
o.InternalHigh = 0;
o.Offset = 0;
o.OffsetHigh = 0;

assert(o.hEvent);
do
{
if(WaitCommEvent(hCom, &dwEventMask, &o))
{
for(n = 0; n < 13; n++)
{
x = dwEventMask >> n;
switch(x)
{
case EV_BREAK:
cout << "break detected" << endl;
break;
case EV_CTS:
cout << "clear to send detected" << endl;
break;
case EV_DSR:
cout << "data set ready detected" << endl;
break;
case EV_ERR:
cout << "frame, overrun or parity error detected" << endl;
break;
case EV_EVENT1:
cout << "provider-defined error type 1 detected" << endl;
break;
case EV_EVENT2:
cout << "provider-defined error type 2 detected" << endl;
break;
case EV_PERR:
cout << "printer error detected" << endl;
break;
case EV_RING:
cout << "ring indicator detected" << endl;
break;
case EV_RLSD:
cout << "receive line signal detection" << endl;
break;
case EV_RX80FULL:
cout << "receive buffer 80% full" << endl;
break;
case EV_RXCHAR:
cout << "character receive detected" << endl;
break;
case EV_RXFLAG:
cout << "event character received" << endl;
break;
case EV_TXEMPTY:
cout << "tx empty detected" << endl;
break;
default:
cout << "unknown event detected" << endl;
break;
}
}
cout << "event mask = " << dwEventMask << endl;
}
else
{
DWORD dwRet = GetLastError();
if(ERROR_IO_PENDING == dwRet)
{
cout << "I/O is pending" << endl;

// To do.
}
else
{
cout << "Wait failed with error %d" << GetLastError();
retVal = -3;
}
}
Go to the top of the page
+Quote Post

chrilsyoung
*



post 25 Jun, 2009 - 07:24 PM
Post #26
icon_up.gif
but how do l send file with serial port in c#.
if l want send a file, do l need to use some protocol such as xmodom?
Can l set transmission protocol in visual studio' serialport.
my email is jixiexi@hotmail.com.
l am waiting for your help。哈哈
Go to the top of the page
+Quote Post

atomskaze
*



post 10 Jul, 2009 - 10:16 AM
Post #27
Hello.

I just wanted to know how should be the output of the communication.

Also, I am using this with an FPGA but when I open the port, and send data. the FPGA light goes lit but doesn't turn off. Also, when I want to close the port, it looks like the program is frozen. Is it that the code needs something else, or does it not support FPGA, or there could be fault in the programming of the FPGA?

I also want to know if part of the output of the program displays a message regarding that the object received data, or displays the data that was received.

I am bad at understanding things at first time, so please don't get mad if it was explained in the first post.
Go to the top of the page
+Quote Post

teksupptom
*



post 10 Jul, 2009 - 12:23 PM
Post #28
QUOTE(atomskaze @ 10 Jul, 2009 - 10:16 AM) *

Hello.

I just wanted to know how should be the output of the communication.

Also, I am using this with an FPGA but when I open the port, and send data. the FPGA light goes lit but doesn't turn off. Also, when I want to close the port, it looks like the program is frozen. Is it that the code needs something else, or does it not support FPGA, or there could be fault in the programming of the FPGA?

I also want to know if part of the output of the program displays a message regarding that the object received data, or displays the data that was received.

I am bad at understanding things at first time, so please don't get mad if it was explained in the first post.


I just downloaded the code from the alternate link in a previous post. It seems that the click event was never created to close the port. It's an easy fix, here's what I added to make it work...

in CommunicationsManager.cs:
CODE

#region ClosePort
public bool ClosePort()
{
    try
    {
        //first check if the port is already open
        //if its open then close it
        if (comPort.IsOpen == true) comPort.Close();
        //display message
        DisplayData(MessageType.Normal, "Port closed at " + DateTime.Now + "\n");
        //return true if port is closed
        if(comPort.IsOpen == false) return true;
        DisplayData(MessageType.Normal, "Issue closing port\n");
        return false;
    }
    catch (Exception ex)
    {
        DisplayData(MessageType.Error, ex.Message);
        return false;
    }
}
#endregion


NOTE: I added some basic error checking code to make sure the port closed before returning true, but I don't do anything with the false value at this point if there was a problem...


in frmMain.cs:
CODE

private void cmdClose_Click(object sender, EventArgs e)
{
    comm.ClosePort();

    cmdOpen.Enabled = true;
    cmdClose.Enabled = false;
    cmdSend.Enabled = false;
}



Hope that helps!
Tom
Go to the top of the page
+Quote Post

atomskaze
*



post 13 Jul, 2009 - 07:48 AM
Post #29
EDIT: Ok so the FPGA's will turn on and off sometimes as data is send, sometimes will turn on and will never turn off unless I cut off the power supply, or it won't turn on at all.

I need to know how you know if there was anything to read, and how the program displays it. Also, anyone has used this to work with an FPGA?

This post has been edited by atomskaze: 13 Jul, 2009 - 07:58 AM
Go to the top of the page
+Quote Post

prasanth868
*



post 4 Aug, 2009 - 10:12 PM
Post #30
Thank you very much for your help...

I have just joined at a company based on embeded systems...

I am the only one to work in asp.net... and the admin said me to develop a project which will transfer the data to other computer and vice versa... really i got tensed and i dont know wht to do yaar..

Without anyones help.... i ws just in a state of coma....

I searched in net for the contents.... got one project which will send the data bt nt included hw to recieve.... searched for 4 dayss..

Because of GODS kindness i saw your codings....... Thank you verymuch for your help yaar..

GOD bless you.....

Go to the top of the page
+Quote Post

Blackfin
*



post 6 Aug, 2009 - 12:52 PM
Post #31
biggrin.gif Very good tutorial, compiled code code wored first time. Reason for joining this forum.
Go to the top of the page
+Quote Post

Senacharim
*



post 18 Aug, 2009 - 08:50 AM
Post #32
QUOTE(jano_rajmond @ 23 Mar, 2009 - 10:51 AM) *

Hy!

I this that the reception thread may be interrupted by something. I modified the code in order to use separate windows for Tx and Rx and also took down the RichtextBoxes and replaced the with Normal TextBoxes, but the same thing happens..

Any ideea? blink.gif


You need to add:
CODE

using System.Windows.Forms;
Go to the top of the page
+Quote Post

vinayakpict
*



post 9 Sep, 2009 - 02:04 AM
Post #33
Really good one !!!
Thanks
Go to the top of the page
+Quote Post

KrantiKishoreD
*



post 10 Sep, 2009 - 03:02 AM
Post #34
hie This is Kranti Kishore,
You Are Article Is Very Good. In That i Have Small Dought.
To WriteData We Are Displaying How To Send.
But If We Want To Receive the data How To Receive And How
To Display It Their. And if We Are Getting Continuously Then How To Get
Every Line To Line For Particular Time. please Give reply. Bcoz i need It to My
Project. And Can We Run This Exe To Multiple Times in One system for
Different Ports..........?
Go to the top of the page
+Quote Post

yilong_vz
*



post 27 Oct, 2009 - 12:04 PM
Post #35
This is very good program for me. This is my first time to use C#. I unzipped the project and compiled without any problem. When I tried to connect to COM11 (USB Serial port), it failed to do so. Through debug, I found user need to add one line of code to make it work.

private void cmdOpen_Click(object sender, EventArgs e)
{
...................
comm.PortName = cboPort.Text;
...................
}

But I still did not figure out why the receiving from Serial Communication (COM11) message did not display in display window.
If any of you know, please post it or send email to me.

Thanks,
YZ
Go to the top of the page
+Quote Post

bam_usic
*



post 29 Oct, 2009 - 10:57 AM
Post #36
Zip is corrupt
Go to the top of the page
+Quote Post

Jonadabe
*



post 5 Nov, 2009 - 04:15 AM
Post #37
Thank you very mutch, this is awsome.

I did the download at:
http://uploaded.to/?id=zr771n

and convert the VS2005 project to VS2008. It converted it very smooth, without problem.

I didn't test it yet but it seems very well done.

Thank's again!

The attached file is the original that I downloaded!

This post has been edited by Jonadabe: 5 Nov, 2009 - 04:17 AM


Attached File(s)
Attached File  SerialPortCommunication.zip ( 101.85k ) Number of downloads: 22
Go to the top of the page
+Quote Post


2 Pages V < 1 2
Fast ReplyReply to this topicStart new topic
7 User(s) are reading this topic (7 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 02:59PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month