【发布时间】:2015-08-07 16:18:44
【问题描述】:
我想在我的设备扫描条形码时从命令行接收输入,并将条形码相关信息提供给 telnet 窗口上的命令行,我们从“telnet 192.168.xx 23”和之后通过 cmd.exe 登录 telnet 服务器开始在 cmd 中输入此命令,然后登录 succsessfull telnet 窗口打开并且我的机器连接到设备,现在我必须从此窗口读取条形码字符串并显示与该字符串相关的输出。请告诉我如何做到这一点?
这是我的代码,只需手动输入输入字符串,然后按回车即可输出。
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();
}
}
}
【问题讨论】:
-
你能不能从 telnet 窗口发出命令,即在 telnet 到服务器并成功登录后,你能在那个 telnet 窗口发出类似 cmd.exe 的命令吗?
-
不,这是不可能的,因为在连接到设备后它会监听设备。登录设备扫描条形码和字符串“XE0318992”字符串显示后,我必须从与该条形码相关的数据库中检索数据并将其显示在此窗口上,但此任务是在我的控制台应用程序中单独完成的,我无法使用此 telnet 服务器配置我的控制台应用程序场景。
-
请用句子拆分问题。基本上是一个长句子的问题是不可能理解的。在任何情况下,为什么你会打开一个 telnet 窗口而不是使用库,或者只是使用 TCPClient 来发送和接收 telnet 命令?
-
我必须使用 telnet 窗口,因为它与设备有关,所有与 telnet 相关的工作都由我的一位在嵌入式编程中使用设备的同事处理。