Solved Error in simplest C# console application using serial port

August 12, 2012 at 08:54:41
Specs: Windows XP
Hello,

I am quite new to C# and after reading different books, documentation, experimenting with a dozen of sample programs and investing weeks of time I still cannot put my simplest program to work. I get CS0120 error message. The MSDN site gives three examples of how to generate this error but gives no hints how to avoid it. Could anyone please show me how my code could be changed so it works. Please create simplest possible solution without inserting any not absolutely necessary code, so that I could understand what I miss here without being distracted by other possibly unknown details. Many thanks!

using System;
using System.Text;
using System.IO.Ports;

namespace CommSlave
{
class Program
{
SerialPort cPort = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);

public static void Main(string[] args)
{
SerialPort cPort = new SerialPort();
cPort.DataReceived += new SerialDataReceivedEventHandler(cPort_DataReceived);
cPort.Open();
}

private static void cPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string cMeldung = cPort.ReadExisting();
Console.WriteLine(cMeldung);
}
}
}


See More: Error in simplest C# console application using serial port

Report •


#1
August 12, 2012 at 10:15:07
Out of curiosity, which languages do you know?

I could tell you how to solve your compile problem, but you're not going to get the desired results. Namely, the application has a runtime of less than a second. I suggest you re-write it as a Windows application to simplify this.

How To Ask Questions The Smart Way


Report •

#2
August 12, 2012 at 11:26:39
Hello Razor2.3

thank you for dealing with my problem. I normally use Windev. This project needs to be done in C# by customer request. It cannot be a Windows application as it will have to run as a Service, without user interaction. I will have to replace displaying the message with sending it on the Internet using sockets. I already have workable code for that. Would you please show me how to put my above code in operation. Thank you!


Report •

#3
August 12, 2012 at 12:13:23
✔ Best Answer
Your compile error is because static functions cannot interact with non-static members. Your "cPort" is a non-static member of class "Program," but both your "Main" and "cPort_DataReceived" are static. The easiest way to fix this is to make "cPort" a static member, but you'll probably end up having to rewrite it when you convert your code into a service.

static SerialPort cPort = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);

public static void Main(string[] args)
{
  //SerialPort cPort = new SerialPort();

You'll still have problems running this program, because the program ends when Main() finishes, and that happens in less than a second.

You really should be putting your logic into a new object that can easily inherit ServiceBase.

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
August 12, 2012 at 12:50:25
Thank you so much Razor2.3!!!
In addition to the changes you suggested I also inserted
while (true)
{}
as the last block of Main(), so it runs continuously. I will try to find a less CPU consuming and less amateurisch solution for this.
But the program works perfectly! It does exactly what I wanted! Thank you so much!

Report •


Ask Question