reading from a serial/com port with C#

I understand what you are doing, attaching an event handler to the serial port that gets called whenever any data arrives. You handler is declaring a local variable called dataBack which lies in a different scope to that of the public variable also called dataBack so you will never get back anything. Remove the variable type declaration to make the handler use the public variable rather than declaring a new one in its own scope.

Ta, I am now getting the response back that I am expecting - but only if i do;

Code:
System.Threading.Thread.Sleep(6000);

As this is a web service, I guess as soon as this method terminates then the event handler does too.

This might need a rethink, there is no need to do be doing any of this as web services - I can just stick it all on the back of an asp.net page.. i think

hmmmmm...

would there be any controls in asp.net that would allow me to leave the event handler running, and then any data that comes into it - comes straight onto the asp page, without the need for a reload.. AJAX?
 
Last edited:
As you suspected, the reason that it only works when you put in the sleep command is because the serial device takes a little time to respond and without the sleep command the execution speeds off and ends the thread (taking the event handler with it) long before the device responds.

You want something that can run independently of the actual web requests and talk to the serial device, you need to abstract the serial communication part a bit rather than having it bundled into the page. I'm not sure if your application is intended to work with concurrent users but at the moment you would likely get exceptions as one user would tie up the serial port hardware.

I don't know much about how ASP.net works but you need some sort of persistent object that you can communicate with between requests.
 
aye.. it does need to handle multiple users - but not concurrent, i can trap errors if the port/device is tied up.. tbh, this is just a prototype and i'm probably spending far too much time trying to get all the error reporting etc to work properly, as when its implemented it'd most likely be handled by some bulk sms gateway.
 
I've written a similar system for measuring SMS times through the network (from one serial attached GSM modem to another) eons ago. It's a slow and clunky (not to mention expensive) way of sending messages.

If you are thinking about writing a spamming application, take note that quite a few operators have spam solutions in the network that look for the behaviours that a GSM modem sending out SMSs will typically look like.

If you're sending out texts it's probably easier to use an SMPP link into SMS service aggregator who should get better prices with the operators due to their total volume of SMS..

My background is SMS software within operators..
 
ah no no, it's not a spam application. it's part of a tracking system, triggered via text message. as i said earlier, this is just a prototype - if it gets taken up, then i would use a bulk sms gateway
 
Back
Top Bottom