C# + WVD2008 + Serial Port

0

Witam,

Mam pytanie czy za pomocą Visual Web Developer możiwe jest sterowanie portem szeregowym RS232? tzn. chce zrobic buttona w ASP.Net WEB Aplication za pomocą którego bedzie wysyłny syg do sterownika.
Sam wyglad programu wysylajacego syg do sterownika napisany jest w C# i dziala poprawnie. Nie wiem tylko jak teraz ten kod podczepić do buttona.

Poniżej przedstawiam kod wyslajacy syg do sterownika i widok kodu z przyciskiem:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
System.IO.Ports.SerialPort serialPort = new System.IO.Ports.SerialPort();

        // Close the serial port if it is already open
        if (serialPort.IsOpen)
        {
            serialPort.Close();
        }
        try
        {
            // Configure our serial port *** You'll likely need to change these for your config! ***
            serialPort.PortName = "COM1";
            serialPort.BaudRate = 115200;
            serialPort.Parity = System.IO.Ports.Parity.None;
            serialPort.DataBits = 8;
            serialPort.StopBits = System.IO.Ports.StopBits.One;

            //Now open the serial port
            serialPort.Open();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Couldn't open the Serial Port!");
            Console.WriteLine(ex.ToString());//Report the actual error
        }
        try
        {
            //Now we'll send some data. Specifically we want to send "#Channel Number P1500<cr>" to center the servo
            serialPort.Write("#1 P1500\r");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Couldn't send the command for some reason... I give up. Sorry!");
            Console.WriteLine(ex.ToString()); //Report the actual error
        }

        //Report our apparent success
        Console.WriteLine("Center little servo, Center!" + Environment.NewLine);
        Console.WriteLine("Sent: #1 P1500<cr>" + Environment.NewLine);

        //Now let's close our serial port.
        try
        {
            serialPort.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Couldn't close the darned serial port. Here's what it said: " + Environment.NewLine);
            Console.WriteLine(ex.ToString());
        }
        //Wait for [Enter] before we close
        Console.ReadLine();
    }
}

}

Kod z przyciskiem:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;

namespace WebApplication4
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

    }

    protected void Button1_Click(object sender, EventArgs e)
    {


    }
    protected void Button2_Click(object sender, EventArgs e)
    {

    }
}

}

0
jrkf napisał(a)

Mam pytanie czy za pomocą Visual Web Developer możiwe jest sterowanie portem szeregowym RS232?

Nie, nie jest.
Visual Web Developer to IDE służące to tworzenia aplikacji webowych w ASP.NET. Nie jest to program do sterowania portami.

Natomiast jeśli chodzi o użycie portu szeregowego w ASP.NET, to chyba jest podobne, jak użycie go wszędzie indziej w .NET, trzeba wykorzystać klasę System.IO.Ports.SerialPort.

0

Jednak sie da:)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
    System.IO.Ports.SerialPort serialPort = new System.IO.Ports.SerialPort();
    if (serialPort.IsOpen)
    {
        serialPort.Close();
    }
    try
    {
        // Configure our serial port *** You'll likely need to change these for your config! ***
        serialPort.PortName = "COM1";
        serialPort.BaudRate = 115200;
        serialPort.Parity = System.IO.Ports.Parity.None;
        serialPort.DataBits = 8;
        serialPort.StopBits = System.IO.Ports.StopBits.One;

        //Now open the serial port
        serialPort.Open();
    }
    catch (Exception ex)
    {
        Console.WriteLine("Couldn't open the Serial Port!");
        Console.WriteLine(ex.ToString());//Report the actual error
    }
    try
    {
        //Now we'll send some data. Specifically we want to send "#Channel Number P1500<cr>" to center the servo
        serialPort.Write("#2 P1500\r");
    }
    catch (Exception ex)
    {
        Console.WriteLine("Couldn't send the command for some reason... I give up. Sorry!");
        Console.WriteLine(ex.ToString()); //Report the actual error
    }
    //Report our apparent success
    Console.WriteLine("Center little servo, Center!" + Environment.NewLine);
    Console.WriteLine("Sent: #1 P1500<cr>" + Environment.NewLine);

    //Now let's close our serial port.
    try
    {
        serialPort.Close();
    }
    catch (Exception ex)
    {
        Console.WriteLine("Couldn't close the darned serial port. Here's what it said: " + Environment.NewLine);
        Console.WriteLine(ex.ToString());
    }
}

}

0

Jak to "jednak", ktoś mówił, że się nie da?

1 użytkowników online, w tym zalogowanych: 0, gości: 1