Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hellow
in the following 2 programs which i have got the main part of it from msdn.i tried to have it two path (read and write ) i mean 1st program in computer A (sending)and the next program (receiving ) in computer B. after many tries it was working fine then i begin to improve it (i mean to change some variable from global to local) but i got errors in the receiving program , i guess my error is how to declare the variables
name,message,_continue,and stringComparer. should they be global and static ....
something like this
programming serial port is difficult to imagine and hard to follow its flowchart
i have given it all i can
needs your fine help
many thanks

What I have tried:

C#
        static SerialPort _serialPort = new SerialPort();
        static bool _continue = true;
        //static StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
        public static void Main(string[] args)
        {
            string name;
            string message;
            StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
            Thread readThread = new Thread(Read);
//serial port setting =com1,9600,Parity.None,8,StopBits.One,Handshake.Nonereadtimeout=500,wrtetimeout=500
            _serialPort.Open();
            _continue = true;
            readThread.Start();
            Console.Write("Name: ");
            name = Console.ReadLine();
            Console.WriteLine("Type QUIT to exit");
            while (_continue)
            {
                message = Console.ReadLine();
                if (stringComparer.Equals("quit", message))
                {
                    _serialPort.WriteLine(String.Format("<{0}>: {1}", name, message));
                    _continue = false;
                }
                else
            _serialPort.WriteLine( String.Format("<{0}>: {1}", name, message) );
                  }//while
            
   readThread.Join();   _serialPort.Close();
        }//Main
        //----------------------------------------------------------
        public static void Read()
        {
            string name="MyName";
            string message;
            StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
            while (_continue)
            {
                try
                {
                    //string message = _serialPort.ReadLine();
                    message = _serialPort.ReadLine();
                    Console.WriteLine(String.Format("<{0}>: {1}", name, message));
                    if (stringComparer.Equals("quit", message))
                    {
                        _continue = false;
                    }

                }//try

                catch (TimeoutException) { }
            }//while
        }//Read

    }
}
//=============================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO.Ports;

//https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.readline(v=vs.110).aspx

namespace SerialConsol
{
    class Program
    {
        static SerialPort _serialPort = new SerialPort();
        //static StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;

        static bool _continue = true;
        //static string name;
        //static string message;

        public static void Main(string[] args)
        {
            string name;
            string message;
            //bool _continue = true;
            StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
            Thread readThread = new Thread(Read);

            // Create a new SerialPort object with default settings.
            //SerialPort _serialPort = new SerialPort();

            // Allow the user to set the appropriate properties.
            _serialPort.PortName  = "com1" ;//SetPortName(_serialPort.PortName);
            _serialPort.BaudRate  = 9600;//SetPortBaudRate(_serialPort.BaudRate);
            _serialPort.Parity    = Parity.None;//SetPortParity(_serialPort.Parity);
            _serialPort.DataBits  = 8;//SetPortDataBits(_serialPort.DataBits);
            _serialPort.StopBits  = StopBits.One;//SetPortStopBits(_serialPort.StopBits);
            _serialPort.Handshake = Handshake.None;//SetPortHandshake(_serialPort.Handshake);

            // Set the read/write timeouts
            _serialPort.ReadTimeout = 500;
            _serialPort.WriteTimeout = 500;

            _serialPort.Open();
            //_continue = true;
            readThread.Start();

            Console.Write("Name: ");
            name = Console.ReadLine();

            //Console.WriteLine("Type QUIT to exit");
            /*while (_continue)
            {
                message = Console.ReadLine();

                if (stringComparer.Equals("quit", message))
                {
                    _continue = false;
                }
                else
                {
                    _serialPort.WriteLine(
                        String.Format("<{0}>: {1}", name, message));
                }
            }//while
            */
            Read();
            readThread.Join();
            _serialPort.Close();

        }//Main
        //----------------------------------------------------------
        public static void Read()
        {
            //Console.WriteLine("hellow read");
            string name = "MyName";
            string message;
           //bool _continue = true;
            StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
            //Console.Write("Name: ");
            //name = Console.ReadLine();

            while (_continue)
            {
                try
                {
                    //string message = _serialPort.ReadLine();
                    message = _serialPort.ReadLine();
                    Console.WriteLine(String.Format("<{0}>: {1}", name, message));

                    if (stringComparer.Equals("quit", message))
                    {
                        _continue = false;
                    }
                    //else
                    //{
                    //    _continue = true;
                    //}
                }//try
                catch (TimeoutException) { }
            }//while
        }//Read

    }
}
Posted
Updated 15-Mar-16 3:58am
Comments
Engineer khalid 14-Mar-16 20:29pm    
//program B
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO.Ports;

//https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.readline(v=vs.110).aspx

namespace SerialConsol
{
class Program
{
static SerialPort _serialPort = new SerialPort();
//static StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;

static bool _continue = true;
//static string name;
//static string message;

public static void Main(string[] args)
{
string name;
string message;
//bool _continue = true;
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
Thread readThread = new Thread(Read);

// Create a new SerialPort object with default settings.
//SerialPort _serialPort = new SerialPort();

// Allow the user to set the appropriate properties.
_serialPort.PortName = "com1" ;//SetPortName(_serialPort.PortName);
_serialPort.BaudRate = 9600;//SetPortBaudRate(_serialPort.BaudRate);
_serialPort.Parity = Parity.None;//SetPortParity(_serialPort.Parity);
_serialPort.DataBits = 8;//SetPortDataBits(_serialPort.DataBits);
_serialPort.StopBits = StopBits.One;//SetPortStopBits(_serialPort.StopBits);
_serialPort.Handshake = Handshake.None;//SetPortHandshake(_serialPort.Handshake);

// Set the read/write timeouts
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;

_serialPort.Open();
//_continue = true;
readThread.Start();

Console.Write("Name: ");
name = Console.ReadLine();

//Console.WriteLine("Type QUIT to exit");
/*while (_continue)
{
message = Console.ReadLine();

if (stringComparer.Equals("quit", message))
{
_continue = false;
}
else
{
_serialPort.WriteLine(
String.Format("<{0}>: {1}", name, message));
}
}//while
*/
Read();
readThread.Join();
_serialPort.Close();

}//Main
//----------------------------------------------------------
public static void Read()
{
//Console.WriteLine("hellow read");
string name = "MyName";
string message;
//bool _continue = true;
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
//Console.Write("Name: ");
//name = Console.ReadLine();

while (_continue)
{
try
{
//string message = _serialPort.ReadLine();
message = _serialPort.ReadLine();
Console.WriteLine(String.Format("<{0}>: {1}", name, message));

if (stringComparer.Equals("quit", message))
{
_continue = false;
}
//else
//{
// _continue = true;
//}
}//try
catch (TimeoutException) { }
}//while
}//Read

}
}
Patrice T 15-Mar-16 2:33am    
Use Improve question to update your question instead of this comment.
Patrice T 15-Mar-16 2:32am    
Which error messages, where ?
Ralf Meier 15-Mar-16 4:49am    
I think, you should first check, what is inside your Serialport-Receiving-Buffer and then read the Content.
The Serialport gives you the Information about the Count of Charcters in it.
Please note : you may send different strings but what you receive is (normally) a count of chars.

1 solution

// i guess the cable was not good ( some wires were seems it is connected but actually not )which force me to blame and modify the code. here the code which finally run without any error ,sorry for the mistake
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO.Ports;

//https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.readline(v=vs.110).aspx

namespace SerialConsol
{
    class Program
    {
        static SerialPort _serialPort;
        static bool _continue;

        public static void Main(string[] args)
        {
            string name;
            string message;
            StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
            Thread readThread = new Thread(Read);

            // Create a new SerialPort object with default settings.
            _serialPort = new SerialPort();

            // Allow the user to set the appropriate properties.
            _serialPort.PortName  = "com1" ;//SetPortName(_serialPort.PortName);
            _serialPort.BaudRate  = 9600;//SetPortBaudRate(_serialPort.BaudRate);
            _serialPort.Parity    = Parity.None;//SetPortParity(_serialPort.Parity);
            _serialPort.DataBits  = 8;//SetPortDataBits(_serialPort.DataBits);
            _serialPort.StopBits  = StopBits.One;//SetPortStopBits(_serialPort.StopBits);
            _serialPort.Handshake = Handshake.None;//SetPortHandshake(_serialPort.Handshake);

            // Set the read/write timeouts
            _serialPort.ReadTimeout = 500;
            _serialPort.WriteTimeout = 500;
          
            _serialPort.Open();
            _continue = true;
            readThread.Start();

            Console.Write("Name: ");
            name = Console.ReadLine();

            Console.WriteLine("Type QUIT to exit");

            while (_continue)
            {
                message = Console.ReadLine();

                if (stringComparer.Equals("quit", message))
                {
                    _continue = false;
                }
                else
                {
                    _serialPort.WriteLine( String.Format("<{0}>: {1}", name, message) );
                }
            }//while
            
            readThread.Join();
            _serialPort.Close();

        }//Main
        //----------------------------------------------------------
        public static void Read()
        {
            while (_continue)
            {
                try
                {
                    string message = _serialPort.ReadLine();
                    Console.WriteLine(message);//String.Format("<{0}>: {1}", name, message));
                }//try
                catch (TimeoutException) { }
            }//while
        }//Read

    }
}
 
Share this answer
 
Comments
Patrice T 15-Mar-16 12:32pm    
Accept your solution to close the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900