Witam. Mam program, który pobiera zasób internetowy z podanej mu strony internetowej. Następnie ma go wyświetlić w konsoli, zapisać do pliku, lub też dopisać do istniejącego już pliku. Oto listing:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
namespace WWWInt
{
class Program
{
static void Main(string[] args)
{
if (args.Length > 0)
{
string url = "url";
if (args[0] == url)
{
try
{
WebRequest request = WebRequest.Create(args[1]); //Send request
request.Credentials = CredentialCache.DefaultCredentials; // If required by the server, set the credentials.
WebResponse response = request.GetResponse(); // Get the response.
Console.WriteLine(((HttpWebResponse)response).StatusDescription); // Display the status.
Stream dataStream = response.GetResponseStream(); // Get the stream containing content returned by the server.
StreamReader reader = new StreamReader(dataStream); // Open the stream using a StreamReader for easy access.
string responseFromServer = reader.ReadToEnd(); // Read the content.
if (args[2] == "konsola") // Display the content.
{
Console.WriteLine(responseFromServer);
reader.Close(); // Clean up the streams and the response.
response.Close();
}
else if (args[2] == "nadpisz")
{
if (args[3].Contains("C:/SOiAK/"))
{
File.WriteAllText(args[3], responseFromServer);
}
else
{
Console.WriteLine("bledna sciezka pliku");
}
}
else if (args[2] == "dopisz")
{
if (args[3].Contains("C:/SOiAK/"))
{
File.AppendAllText(args[3], Environment.NewLine + responseFromServer);
}
else
{
Console.WriteLine("bledna sciezka pliku");
}
}
}
catch (UriFormatException)
{
Console.WriteLine("bledny adres strony internetowej");
}
}
else
{
Console.WriteLine("bledne formatowanie: URL adres_internetowy KONSOLA_do_pliku_NADPISZ_lub_DOPISZ ewentualny_adres_plik ");
}
}
}
}
}
Mam też drugi program, który ma przekazać dane do portu szeregowego. Dane mogą być z pliku lub tekst wprowadzony do tablicy "args". Oto listing:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.Ports;
using System.ComponentModel;
namespace komszer
{
class Program
{
static SerialPort com7;
static void Main(string[] args)
{
com7 = new SerialPort("COM7", 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
if (args[0] == "port")
{
if (args[1] == "COM7")
{
if (args[2] == "tekst")
{
com7.Open();
com7.Write(args[3]);
}
else if (args[2] == "plik")
{
string plik = File.ReadAllText(args[3]);
com7.Open();
com7.Write(plik);
}
else
Console.WriteLine("nie podano zrodla");
}
else
Console.WriteLine("nie ma takiego interfejsu!");
}
else
Console.WriteLine("nie podano polecenia!");
}
}
}
Moje pytanie brzmi: Jak możnaby zmodyfikować oba te programy, aby zasób internetowy przesłać potokowo do portu szeregowego przez konsolę windows wpisując w konsoli:
{program_zasob_danych} {parametry} | {program_port_szeregowy} {parametry}
Prosiłbym o pomoc w kodzie, lub odesłanie linkiem do informacji, które mogłyby pomóc. Za wszelką pomoc serdecznie dziękuję. Pozdrawiam