Click here to Skip to main content
15,887,945 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My project is based on a web based tutorial for abacus students. In my web project i'm using a embedded kit for abacus calculation. We provide the kit for the students who register in our website. Using this kit they will have to perform their abacus calculation in our website.The kit is connected using usb cable via serial port.In our tutorial page there are 10 sums or questions(eg: 1+1=_ ,1+2=_ ....). We provide textbox for each sum answer.

The kit will display value in its digital display and this displayed value must come to our page (to the focused textbox) whenever the student enter the "ENTER button" in the kit. Whenever the valued is entered in textboxes it will move focus to next textbox according to the tab index we provided. If the displayed value in the kit is 45,then the value it pass to the system will be "45*1".the kit will continuously pass the value to the system ,but we want the value in text box only when the student enter "Enter button". So for that when a student enter the button the value will pass as "45*2". so that we can check for "*2" from the value and when it comes we can break the value and can give the value to textbox.

I achieved my goal in windows application using "SerialDataReceivedEvent"(SerialDataReceivedEventArgs),but not in web project. I also want to know whether it is possible to right a windows service for this purpose. If so how should i do that and should i use web service in windows service..

What i tried in windows application:-

C#
SerialPort _serialPort;
     private void MainForm_Load(object sender, EventArgs e)
       {
           _serialPort = new SerialPort("COM5", 19200, Parity.None, 8, StopBits.One);
           _serialPort.Handshake = Handshake.None;
           _serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
           //_serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);
           _serialPort.ReadTimeout = 500;
           _serialPort.WriteTimeout = 500;
           _serialPort.Open();
       }
       private delegate void SetTextDeleg(string text);
       void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
       {
           Thread.Sleep(5);
           string data = _serialPort.ReadLine();
           this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data });
       }
       private void si_DataReceived(string data)
       {
           string[] SplitString = data.Split('*');
           if (SplitString.Length > 1)
           {
               string _value = SplitString[0].Trim();
               if (SplitString.Length > 1)
               {
                   if (SplitString[1].Trim() == "2")
                   {
                      tbData.Text = _value;
                   }
               }
           }
       }

but in web application this code wont work.


What i tried in web project:-

(1) I tried to communicate the kit using serial port data recieved event

in Pageload:-
C#
foreach (Control cntrl in pnlControl.Controls)
        {
            if (cntrl is TextBox)
            {
                TextBox txt = (TextBox)cntrl;
                string GetInputValue = txt.Text;
                if (GetInputValue == string.Empty)
                {
                    txt.Focus();
                    _serialPort = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
                    _serialPort.Handshake = Handshake.None;
                    _serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
                    //_serialPort.ReadTimeout = 500;
                    //_serialPort.WriteTimeout = 500;
                    _serialPort.Open();
                    break;
                }
            }
        }

          public void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        if (_serialPort.IsOpen)
        {
            string data = string.Empty;
            try
            {
                Thread.Sleep(50);
                data = _serialPort.ReadLine();
                si_DataReceived(data)
            }
            catch
            {

            }
        }
    }
      private void si_DataReceived(string data)
        {
            string[] SplitString = data.Split('*');
            if (SplitString.Length > 1)
            {
                string _value = SplitString[0].Trim();                
                if (SplitString.Length > 1)
                {
                    if (SplitString[1].Trim() == "2")
                    {
                       tbData.Text = _value;
                    }
                }
            }
        }
Posted
Updated 9-Oct-13 18:49pm
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900