0

I have done some changes to my coding and tested it. It is still not working perfectly hence i hope to have some guidance here.

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.IO.Ports

Partial Class Main
    Inherits System.Web.UI.Page

Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load

    Dim Serialport7 As New IO.Ports.SerialPort

    With Serialport7
        .PortName = "COM7"
        .BaudRate = 9600
        .Parity = Parity.None
        .DataBits = 8
        .StopBits = StopBits.One
    End With

    Try
        Serialport7.Open()
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try


    Dim ReceivedData As String

    ReceivedData = Serialport7.ReadLine()

    If ReceivedData.Substring(0, 1) = "T" Then

        TextBox1.Text = ReceivedData.Remove(0, 1)

    ElseIf ReceivedData.Substring(0, 1) = "H" Then

        TextBox2.Text = ReceivedData.Remove(0, 1)

    End If

    TextBox3.Text = System.DateTime.Now

    Serialport7.Close()

End Sub

End Class

The errors i have encountered:

  • TextBox2 is not displaying the reading, only TextBox1 is showing the reading.

  • The Web form is not refreshing hence my readings are not updated. Is there any way i can auto update the Web form ?

Appreciates all guidance. Thanks.

  • do you need your webform to refresh automatically (I mean when new data comes from your serial port? Also why not doing this in a winform or wpf application, seems more suitable for that kind of things (I'm just curious)? – ppetrov Mar 30 '13 at 15:07
  • Hi ppetrov I have done it in a Win Form actually but the objective of my project is to ultimately display in a Web Form albeit just a server side for demo purpose, no client side necessary. Yes, i need to update my WebForm automatically but i am not able to see my second reading on TextBox2, are you able to help ? Thanks. – user2212394 Mar 30 '13 at 15:31
  • About TextBox2 I suspect the `ElseIf` condition is never met, have you tried to put a breakpoint on `TextBox2.Text = ...`? About updating your page everytime new data is available, it's kind of tricky. You can do it with some javascript code, I'll give you more details in my answer – ppetrov Mar 30 '13 at 16:01

1 Answers1

0

You have a simple solution to refresh your page, it won't be refreshed anytime new data is available since your server can't control the client web browser (well shouldn't, it's not impossible but it's complicated and not very safe) .

So what you can do is add a piece of javascript code to refresh your page periodically.

The best way to go would be to add a javascript function that makes an ajax call to a webservice which would read the serial port data and then send the data back to the client page. There you can update your textbox values.

To update your page you can use this javascript code: window.location.reload() If you need to update it periodically you can take a look at this SO question

To add a webservice in webforms and call it periodically, take a look at these links: http://www.asp.net/web-forms/tutorials/aspnet-ajax/understanding-asp-net-ajax-web-services

http://api.jquery.com/

If you don't want to refresh your page periodically, and just want to refresh your values when new data is available, take a look at websockets (forgot about them when I started to answer).

EDIT about websockets [Also, you should consider that your methods can be called by many different users, also in different threads. So you need to make your serial port reading thread safe, and store the results somewhere to avoid sending different data to different users.] --

Image you have a user A who updates your page, then the user B updates it. You have already read some data from the serial port when user A called the page, so user A and user B will have different data displayed.

if data is 1, 2, 3, 4

in your current implementation user A will see 1 and user B will see 2

Community
  • 1
  • 1
ppetrov
  • 3,077
  • 2
  • 15
  • 27
  • Hi, Thanks for your advices. Currently this project is a demo purpose to show that it is able to be implemented on a Web Form and thus it wont be as complicated to involve client side of coding. Hence, i would make it just a server side request,ie. on my PC alone, i will display the readings on Web Form. I will look up the tutorials to see how can i refresh my page. Regarding the error of not executing the ElseIf condition, what do you think could be the cause of it ? How can i change the coding to execute it ? Thanks. – user2212394 Mar 30 '13 at 17:22
  • If you want to show that it can be implemented in webforms, you **need** to handle multiple clients, cuz it will be the main difference with the winforms version, and that's what will cause you the most trouble in a real version (not demo). About the `ElseIf` condition I'd say that you just never meet the condition, can't tell you why since I don't know wich is the data you receive through your serial port – ppetrov Mar 30 '13 at 17:34
  • Hi, I understand what you meant by comparing between winform and webform. But the objective set by my lecturer is a simple server side coded webform will suffice for this. For the data through serial port, using Hyper Terminal, i can see T28 and H70 one after another, its just that i could not grab it properly when i start the Web form. – user2212394 Mar 30 '13 at 18:12
  • Well, I don't get why your lecturer asks you to make a web application that behaves like a winform one..... Anyway, take also a look at websockets, I think that would be the best option here, I'll add the link in my answer – ppetrov Mar 30 '13 at 18:27
  • Hm..Probably because considering I have to come up with the target board consisting of the Microcontroller and other peripherals plus the coding in it, there is not much time left for me to include other features that are time consuming and thus a simple web app will suffice. – user2212394 Mar 30 '13 at 18:52
  • Perhaps I misunderstood you, but didn't you say you already had a winforms app that was doing the same thing? For testing purpose this would have been enough – ppetrov Apr 01 '13 at 00:57