1

I want to receive input from command line when my device scans barocode and give barcode related information to the conmmand line on telnet window which we start from login in telnet server through cmd.exe by "telnet 192.168.x.x 23" and after typing this command in cmd then login succsessfull telnet window opens and and my machin connected to device , now I have to read barcode string from this window and display output related to that string. please give me any idea how to do this?

here is my code which simply manually enter input string and after pressing enter gives output.

namespace ConsoleApplication2
{
    class Program
    {
    private static System.Timers.Timer aTimer;
    string path = @"C:\Users\Priya\Desktop\Project\barcode.txt";

    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SaiNathHospital"].ToString());

    public void getConsoleInput()
    {
        try
        {
            FileInfo fi = new FileInfo(path);

            for (int i = 0; i <= 0; i++)
            {
                Console.WriteLine("");
                using (StreamWriter sw = new StreamWriter(path))
                {
                    sw.WriteLine(Console.ReadLine());
                    sw.Close();
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

    public void getConsoleInputAtRuntime()
    {

    }

    public void ReadWriteIntoFile()
    {
        try
        {
            string filename = @"C:\Users\Priya\Desktop\Project\Data.txt";
            StringBuilder sb = new StringBuilder();

            StreamReader sr = new StreamReader(path);
            string s = sr.ReadLine();
            sr.Close();

            DataExport("Select * from PATIENT_TABLE where [BARCODE] = '" + s + "'", filename);
        }
        catch { }
    }

    public void DataExport(string SelectQuery, string filename)
    {
        try
        {
            using (var dt = new DataTable())
            {
                using (var da = new SqlDataAdapter(SelectQuery, con))
                {
                    da.Fill(dt);
                    var rows =
                        from dr in dt.Rows.Cast<DataRow>()
                        select String.Join(
                            ",",
                            from dc in dt.Columns.Cast<DataColumn>()
                            let t1 = Convert.IsDBNull(dr[dc]) ? "" : dr[dc].ToString()
                            let t2 = t1.Contains(",") ? String.Format("\"{0}\"", t1) : t1
                            select t2);

                    using (var sw = new StreamWriter(filename))
                    {
                        // sw.WriteLine(header);
                        foreach (var row in rows)
                        {
                            sw.WriteLine(row);
                        }
                        sw.Close();
                    }
                }
            }
        }
        catch (Exception e) { Console.WriteLine(e.Message); }
    }

    public void WriteFileOutput()
    {
        string path = @"C:\Users\Priya\Desktop\Project\Data.txt";
        if (File.Exists(path))
        {
            string[] lines = File.ReadAllLines(path);

            foreach (string line in lines)
            {
                Console.WriteLine(line);
            }
        }
        Console.ReadLine();
    }

    public void  timer()
        {
            aTimer = new System.Timers.Timer();

            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = 10000;
            aTimer.Enabled = true;

            Console.WriteLine("Press the Enter key to exit the program.\n");
            Console.ReadLine();
        }

    private static void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            //System.Windows.Forms.SendKeys.Send("{ENTER}");
            Console.WriteLine("5 seconds Elapsed at {0} ", e.SignalTime); // for your reference to check every five seconds
        }

    public static void Main(string[] args)
    {             
      Program p = new Program();     
      p.getConsoleInput();
      p.ReadWriteIntoFile();
      p.WriteFileOutput();
    }   
}

}

Vaibhav Mule
  • 5,016
  • 4
  • 35
  • 52
Member123
  • 137
  • 1
  • 2
  • 10
  • Can you issue commands from the telnet window i.e. after the telnet to server and successful login can you issue a command like cmd.exe in that telnet window? – Siva Senthil May 26 '15 at 06:36
  • No that is not possible because after connecting to device it listens for device. After login device scans barcode and string "XE0318992" string displays and I have to retrive data from database related to that barcode and show it on this window but this task is done in my console application separately I couldnt configure my console app with this telnet server scenario. – Member123 May 26 '15 at 06:38
  • Please break the question in sentences. It's impossible to understand a question that is essentially one long sentence. In any case, *why* do you open a telnet window instead of using a library or simply use a TCPClient to send and receive telnet commands? – Panagiotis Kanavos May 26 '15 at 06:41
  • I have to do with telnet window because it is related to device and all works regarding to telnet handled by one of my colleague who works with device in embedded programming. – Member123 May 26 '15 at 06:55

1 Answers1

0

Priyanka, welcome to SO. The approach I would take towards this would be to programatically issue the telnet and wait for the response from the telnet. Now since, you are logging in the ConsoleApplication2 is not aware of the session.

So, here is the high level approach towards the solution

  1. Launch your ConsoleApplication2 application
  2. Use a Telnet library to open the connection towards the device
  3. Read the response of scan from the Telnet using the same library
  4. Do the database thing with the response.

The problem will become simpler if you have a Telnet library. However, there is a similar question in SO and a library which was recommended here - Executing commands by using Telnet in C#

Hope this helps!

Community
  • 1
  • 1
Siva Senthil
  • 610
  • 6
  • 22
  • You mean i have to create telnet client to accept connection with device and write all the above code in this program itself – Member123 May 26 '15 at 09:06
  • Hmm not exactly, what I mean is from the code create the telnet connection something similar to the SQL server connection. If you intend to use the Library in code project, then it would be `TelnetConnection tc = new TelnetConnection("gobelijn", 23);` and further operations would follow. – Siva Senthil May 26 '15 at 09:10
  • I made tcpclient but it does not connecrs – Member123 May 26 '15 at 10:11
  • I am confused, you are creating tcpclient which creates TelnetConnection or you are creating tcpclient which is creating TCP Connections? – Siva Senthil May 26 '15 at 10:56
  • I tried with tcpclient, i read somewhere it would work for telnet also is it right or not ? Should i create code with TelnetConnection ? – Member123 May 26 '15 at 11:46
  • The reason I referenced that code library was for you to try out with that Telnet library. – Siva Senthil May 26 '15 at 11:48