Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

serial port - again

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tat - 18 Mar 2008 18:59 GMT
Hi,

I need to write an application which two computers can transmit data
via serial port (COM1). So I wrote two simple test program write to
comm and read from comm. However, when I test these two programs, I
could not read any data from COM1. Data seems to send Ok but the
receive program can not read anything from COM1. Please tell me what
wrong with my approach. The test code is below

---for write to com
using System;
using System.IO.Ports;

namespace WriteCommExample
{
 public class WriteCommData
 {
   private SerialPort port = new SerialPort("COM1", 9600,
Parity.None, 8, StopBits.One);

   [STAThread]
   static void Main(string[] args)
   {
     new WriteCommData("hello, world");
   }

   private WriteCommData(string text)
   {
     Console.WriteLine("Outgoing data");
     port.Open();
     if (port.WriteBufferSize - port.BytesToWrite > text.Length)
    {
     port.Write(text);
    }
     else
    {
     Console.WriteLine("Not enough room in transmit buffer");
    }
   }

 }

}

-- for read from comm
using System;
using System.IO.Ports;

namespace ReadCommExample
{
 public class ReadCommData
 {
   private SerialPort port = new SerialPort("COM1", 9600,
Parity.None, 8, StopBits.One);

   [STAThread]
   static void Main(string[] args)
   {
     new ReadCommData();
   }

   private ReadCommData()
   {
     Console.WriteLine("Incoming data");
     port.Open();
     if (port.BytesToRead > 0)
    {
     byte received_data = port.ReadByte();
     Console.WriteLine((char)received_data);
    }
     else
    {
     Console.WriteLine("No data available");
    }
     port.Close();
   }

 }

}
tat - 18 Mar 2008 19:01 GMT
> Hi,
>
[quoted text clipped - 76 lines]
>
> }

By the way, the line
byte received_data = port.ReadByte();
should be
int received_data = port.ReadByte();
Thanks,
tat
Hoop - 18 Mar 2008 19:20 GMT
> Hi,
>
[quoted text clipped - 78 lines]
>
> - Show quoted text -

Hi,
I used that serial port class in a VB app about 6 months ago, its a
thread and event driven. If I remember right you have to handle the
serial ports dataRecieved event. And then something like, someData =
port.ReadExisting();
Jeff
Ben Voigt [C++ MVP] - 18 Mar 2008 19:47 GMT
> Hi,
>
[quoted text clipped - 4 lines]
> receive program can not read anything from COM1. Please tell me what
> wrong with my approach. The test code is below

Are you running both at once?  I would be surprised if more than one process
can open "COM1" at the same time.

> ---for write to com
> using System;
[quoted text clipped - 67 lines]
>
> }
tat - 18 Mar 2008 20:05 GMT
Hi Ben,

I ran the programs on two different computers. If I ran on the same
computer, you would be right about two processes can't open the same
port at the same time.

TAT

> > Hi,
>
[quoted text clipped - 79 lines]
>
> > }
pjf - 18 Mar 2008 21:19 GMT
> Hi Ben,
>
[quoted text clipped - 87 lines]
>
> > > }

Hi there,
Have you used a null modem cable between the 2 PC's ?
This will ensure that the Tx from PC1 goes to the Rx of PC2 and vice
versa (Tx = Transmit , Rx = Receive)

I speak from experience (been there done that)

Good luck

Peter
Ben Voigt [C++ MVP] - 19 Mar 2008 00:07 GMT
>>>    private ReadCommData()
>>>    {
>>>      Console.WriteLine("Incoming data");
>>>      port.Open();
>>>      if (port.BytesToRead > 0)

You've not left much time for the serial port to receive any data.  In fact,
the previous two statements will execute far faster than even a single
character could arrive, so it is no wonder you never get data.

Just call ReadByte, the program will wait for a byte to arrive.  You should
be able to set a timeout as well to prevent an infinite wait.

>>> {
>>>   byte received_data = port.ReadByte();
[quoted text clipped - 10 lines]
>>
>>> }

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.