Add serial port to web page - c#

Is it possible to add a serial port control to a web application. I've tried creating one programmatically, but i have issues with the port staying open. I'm not sure how to fix that except by adding a serial port control somehow to the web page. Any ideas on how I can accomplish this task? Thanks in advance.
the following is the code I currently have:
public partial class LoadCellTest : System.Web.UI.Page
{
SerialPort serialPort1 = new SerialPort("COM3",9600,Parity.None,8,StopBits.One);
protected void Page_Load(object sender, EventArgs e)
{
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
serialPort1.ErrorReceived += new SerialErrorReceivedEventHandler(serialPort1_ErrorReceived);
}
delegate void SerialDataReceivedDelegate(object sender, SerialDataReceivedEventArgs e);
delegate void SerialErrorReceivedDelegate(object sender, SerialErrorReceivedEventArgs e);
protected void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
TextBox1.Text = (serialPort1.ReadExisting());
if (serialPort1.ReadExisting().Length == 0)
{
ListBox1.Items.Insert(0, TextBox1.Text);
TextBox1.Text = "";
}
}
protected void button1_Click(object sender, EventArgs e)
{
try
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
button1.Text = "Start";
}
else
{
serialPort1.Open();
button1.Text = "Stop";
}
}
catch (IOException ex)
{
MessageBox.Show(ex.Message);
}
}

Turning my comments into an answer...
The serial port code is server-side code. You can't do it on the client with ASP.NET.
Creating an ActiveX or other fat client control is a load of work, and just not a good idea.
My recommendation would be to continue on with your WinForms app for the code where you need the scanner, and add a menu to it to enable you to launch a separate ASP.NET web app for the reports/data access.
If you want to make it more "seamless" for the users, you can add a form with a WebBrowser control that loads your report/data access site. To them it will just be a "part of the application".
THAT SAID, depending on the device you have connected to the port, there may be an even simpler option.
One of our barcode scanners comes with software that just takes barcode data as it's scanned and pastes it into whatever open document has focus. If you're working in Notepad, the scanned data is pasted into Notepad. If you have a web app open, and the cursor in a text box, the data simply pastes in there.
It's a simpler option to implement, BUT it's harder on the users, because if they're not technical, they're going to call you wondering why the barcode beeps but doesn't populate the text box. (The answer will be "Because your cursor isn't in the text box or the form doesn't have focus")
So I go back to recommendation #1.

Your code just accesses the serial port of the server since it is running on the server...
IF you really need to access the serial port on the client from a web app then you will need to use some technology that runs directly on the client... this could be an ActiveX control embedded into your web page...
I am really not sure perhaps a Silverlight application embedded into your web page could achieve this too...
Beware that doing so in a web page is a possible security problem !

Related

Sendkeys.SendWait C#

I am trying to click a button in a Windows Form Application and use the SendKeys function in C# to write button1.Text to a separate window. Kind of like a "paste" function. However, when I use SendKeys.SendWait, it writes the text incorrectly. For instance, the button1.Text = "Hello World!"
It will paste "HHHelloo Worlddd!!"
Is there a way I can get the exact string to send over?
Here's a sample of what's going on:
private void button1_Click(object sender, EventArgs e)
{
this.Visible = false;
SendKeys.SendWait(button1.Text);
this.Visible = true;
}
I have found the solution to this problem. Instead of sending the text one key at a time and using the extra xml in the config file, you can simply call SendKeys.SendWait("^(v)"); to simulate Paste.

Alarm without message dialog

I would like to make and App that when users set an alarm (like wake up alarm) and alarms sounds, doesnt show a message dialog to confirm and stop the alarm.
On web I have discovered some methods called Obscured and UnObscured that fired when the message's alarm shows, but the message still showed!
In Code:
public OverAlarmPage()
{
InitializeComponent();
PhoneApplicationFrame phoneAppRootFrame = (Application.Current as App).RootFrame;
phoneAppRootFrame.Obscured += OnObscured;
phoneAppRootFrame.Unobscured += Unobscured;
}
void OnObscured(Object sender, ObscuredEventArgs e)
{
txtStatus.Text = "Obscured event occurred";
}
void Unobscured(Object sender, EventArgs e)
{
txtStatus.Text = "Unobscured event occurred";
}
I just want to make stop the alarm sound with the vibrating of mobile (with gyroscope) but without the hell message!
There are any solution or is this a O.S feature that i cant touch?
The built in notification classes (Alarm and Reminder) don't support any customization of the UI that is displayed when they are triggered.
The only alternative I can think of would require the application to remain running (possibly under the lock screen) and then you just play the sound from the app at the appropriate time.
Obviously this requires that the app remain running though.
Other than that you will be prevented from trying to create the behaviour you are after.

Reading from a serialport and displaying the result

here's the code I've come-up with so far:
protected void Page_Load(object sender, EventArgs e)
{
try {
serialPort1.PortName = "COM4";
serialPort1.BaudRate = 9600;
serialPort1.Open();
this.serialPort1.DataReceived += new
System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived);
Label1.Text = "Connected";
UpdatePanel1.Update();
}
catch (Exception ex) {
}
}
string x = "";
private void serialPort1_DataReceived(object sender,
System.IO.Ports.SerialDataReceivedEventArgs e){
x = serialPort1.ReadExisting();
TextBox1.Text = x;
UpdatePanel1.Update();
}
the problem is after the code runs the text box remains empty... (Im using AJAX update panel to refresh the textbox text) the thing is when I set breakpoints during debug, the data received from the serial port is in the variable and is set as the new textbox text but when the code finishes nothing is displayed.... I'm pretty sure the updatepanel works because I've tested it.
PS The serialport is connected to an rfid reader and im trying to read tags. I have successfully coded a windows form app to do what I want but I need to migrate it to ASP.NET
Reading data from serial port in ASP.NET application will only work if the reader is connected to the server (not the machine that is running the browser). It seems to be working only because you are testing it locally.
The serialPort1 object will be destroyed after the initial page render is complete. The ASP.NET page only lives until the page is rendered. Then it is destroyed. The next request again recreates all objects. So the serial port would be opened again and again every time the browser goes to the server.
UpdatePanel only changes the classic browser requests into AJAX - it does not change how ASP.NET server processing works. It is still request-response actions triggered by the client browser. In your case the UpdatePanel would have to query the server on a timer.
Your best bet is to create a ActiveX control based on your Windows Forms code and embed that in your website.

Change value control in asp.net basend on custom event triger

Hi i want to update value in textbox when event trigger..i know how to do it in winform..but i cant do it in webform..
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
return;
else
myClass.MyEvent += new MyClass.MyEventDelegate(Default_MyEvent);
}
private void Default_MyEvent(string value)
{
txtBox.Text = value;
}
above code doesn't change text value of my textbox in asp.net but it works in winform. How to do it in webform?
*Edit
I try to create file transfer application via socket on web. I already done this in desktop application.
Basically when client open web page then client will connected to server on spesific port. and every data transmitted to client from server, then in then web form there will be an changing in text of the textbox.

Open another page (not web) on C#

I'm creating a native C# application and I need to do a simple thing:
Once the user clicks some certain button, another .cs file is opened (with its own design, code and stuff). If it is possible, I would like to know how to close the current form at the same time.
EDIT: what I exactly need:
namespace Mokesciai
{
public partial class Mokesciai : Form
{
public Mokesciai()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//write code here to open another page called "NewPage.cs" with its own subfiles "NewPage.Designer.cs" and
//"NewPage.resx", as shown in the solution explorer
}
}
}
The application is C# Windows application
EDIT2: what I want in the graphical way: http://sdrv.ms/JXKVEL
By clicking "Click me" I want to open the new form
private void button1_Click(object sender, EventArgs e)
{
YourSecondForm objForm=new YourSecondForm();
objForm.Show();
this.Close();
}
Assuming YourSecondForm is the name of your another form which you want to display on the button click event.
That is a form.
You can create a new instance of the form class, then call Show().
As far as i understand from your question, may be you want to do this. You can use Process.Start() to start any other application from your native app.
using System.Diagnostics;
string path=#"path to the app"
Process.Start(path);
OR
Create a new form place a multi line text box, then read the file using StreamReader & fill its result on the text box. For more information on how to use Stream Reader Check out this or this

Categories

Resources