Sending msg via RS232 every 5 minutes in C# - c#

I am wondering how to send msg using serialport every x minutes. In the meantime app should be fully accessible. I just want to check printer status, so i'm sending every 5 minutes "Error status request" and if there are some errors - then sound alarm or something..

See http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx

You can write to the serial port using c# fairly easily
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx
The simplest way to perform an action every x minutes is to spawn a thread off and put in a loop which performs Thread.Sleep(30000). You need to build some control code in so that you can stop your thread from the main code

Related

Is there a way to stop the execution of current request and call another function using javascript / C#

I am exporting reports which contains huge amount of data and it takes lots of time to export,
so I am calculating the time duration for export, if it taking more than 60 seconds or the timer goes above 60 seconds then want to export that report in background. by stopping current export request.
Hope you will get my query and help me. Thank you so much
If I understand correctly,
why stop current export request?
suggestion:
communicate with your server in async ajax, thus not blocking your client side.
show a blocking user interaction
(like http://malsup.com/jquery/block/#demos)
until one of the following:
your calculation say it will take > 60 sec
time passed > 60 sec
server responded
if 1 or 2 happened - you don't cancel the original processing. its just continuing in the background and your user is free.
the callback from the async ajax will tell the user when it's ready.

C# Windows Form - Serial Port problem / BackgroundWorker

I developed a Windows Form application, using C#, that receives data through the serial port sent by a microcontroller. I'm receiving the data using the serialPort_DataReceived event in which I use ReadLine() to read the serial port. Then I use the Invoke method to call a function that will print the received string into a textBox. The problem is that the app can't keep up with the rate at which it receives the strings, for example, the string that I send from the microcontroller has the time at which it was sent and even though it has already passed 2 or 3 seconds the app is still printing the strings that were sent at the 0.2 second. And when I send a string to the microcontroller to stop sending data, the app onlys stops ptinting the data after a while, this is, it keeps printing the values stored in the receiving buffer.
I believe that is happens given the large amount of data that the app receives and the rate (I'm using a baud rate of 115200), one string for every millisecond. This is, I think that the app is always being interrupted by the DataReceived event and it doesn't has time to print the data and starts falling behind.
One way I though of overcome this problem was with multi-threading but I can't figure it out how. I already tried a few examples using the BackgroundWorker but I didn't manage to make it work at all. I'm still a noob in terms of programming Windows Form apps and even C#.
Can anyone confirm the cause of the problem? And how do I make a thread to print the received data?
You can utilize SerialPort.ReceivedBytesThreshold property and SerialPort.ReadExisting() method (or a StringBuilder instance) to update UI with a batch of strings at once rather than a single string.
For more information check:
Serial port DataReceived firing too much discussions on StackOverflow
Top 5 SerialPort Tips article by Kim Hamilton
Serial Port ReadLine vs ReadExisting or how to read the data from serial port properly discussion on StackOverflow
Quickest way to Update Multiline Textbox with Large Amount of Text discussion on StackOverflow

How to check User Activity value repeatedly in website

I'm building a website using ASP.NET. It's a game which I connect two users randomly to play with each other.
What I need to do is when one of these two players logs out or has been inactive for a certain time, I finish the game and send the other player to some page to tell him or her the game is over.
My problem is I can't figure out how to track the user status every while. I know that ASP.NET has (LastActivityDate), but how can I check this value repeatedly every 30 seconds for example?
lets say you have 2 users
each user pings the server ( ajax) every 2 sec [using javascript's setInterval] and increase a counter - or set a new time in the Application or Cache ) - global accessed objects
once in a while - you scan those structures - and if the value ( lets say of DateTime) - is larger by X - so someone has stopeed sending pings - and thats how you know that someone has gone.
You should use jQuery or another Javascript library for using ajax simply. You need also to use 'setInterval' function that will verify that user still active.
Links:
http://api.jquery.com/jQuery.ajax/
http://www.w3schools.com/ajax/ajax_intro.asp

Long process with browser interface

I am using ASP.NET MVC 3 with C# and I am having a process that takes about 10 minutes to be finished. I need some help how could I show some interface (progressbar, etc).
What would happen if user turned the browser off? My process should not be stopped.
What would happen if another user tried to open the process page?
I started searching about jQuery progress bar but got those questions and looking for some help.
Thank you
If your process takes 10 minute to finish, then you must make the work on background, and keep the result somewhere to show it.
First question: What happend if the user close the browser, to solve this, you need to create a system to make the work on backgroud and leave the browser to continue. if can not make a full shedule class to make your works, a simple thead can do the same think - but is less flexible.
Second question: How to avoid the start of a new process page. You can solve this by using mutex. You set a mutex with a specidic name, and you close it when the job done, after 10minute. In the middle if some user try to re-run the same process you see that the mutex is lock and you show him a message to wait.
You need somewhere to keep the result information's, eg, Let say that you make a job of 10 minute, then store the results somewhere and the user see the results and when they are generated and if he like can rerun the procedure.
With this I describe you to not need to fully disable the page, just a message that result still running, or an automatic refresh to the page every 30 seconds to see if they are done.

How should I run a background service for creating export files for a web site in the Windows world?

I've created a shiny new ASP.Net MVC site and I have offloaded the 'save as' type of functionality to a helper program that can be run in the background on the server so that the website doesn't need to take all that load.
I created it so that it's easy to run from the command line and right now I have it running from a windows scheduler. The problem is that that has a granularity of 1 minute, which means that some unlucky users of the website will click on the link and get a 59s wait + the time to actually process the report.
I'm also slightly worried about the start up cost of my program. I'm assuming that it would be cheaper to keep the program running constantly. I am worried about the program dying and not being respawned though. With the windows scheduler option at least I don't have to worry about my program bailing so much.
How have you dealt with that?
I'd create a Windows Service and have that running instead of an exe via windows scheduler. You can then set it to auto-start. Then you can just have a timer within the service to poll for work every x seconds.
Another alternative is to use something like MSMQ. Your front end would just insert a message into the queue to represent the work to do, then have a .NET service listening to the queue (you can get it to process messages immediately they appear in the queue, or check the queue manually yourself every x seconds).
Either way, I think a Windows Service is the way to go.

Categories