Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,

I am trying to interface my microcontroller with ASP.net in Web Form but have met with some difficulties.
My coding is working correctly in VB Windows Form but once ported to ASP.net Web form, it encounters error.

Basically, im taking in 2 inputs from microcontroller and update the Web Form through use of serial port.

Below is my coding for VB
<br />
Imports System<br />
Imports System.IO.Ports<br />
Imports System.Threading.Thread<br />
Imports System.ComponentModel<br />
<br />
Public Class frmMain<br />
<br />
    Dim WithEvents Serialport7 As New IO.Ports.SerialPort<br />
<br />
    Public Delegate Sub MyDelegate(ByVal ReceivedData As String)<br />
<br />
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<br />
<br />
    With Serialport7<br />
        .PortName = "Com7"<br />
        .BaudRate = 9600<br />
        .Parity = Parity.None<br />
        .DataBits = 8<br />
        .StopBits = StopBits.One<br />
    End With<br />
<br />
    Try<br />
        Serialport7.Open()<br />
    Catch ex As Exception<br />
        MsgBox(ex.ToString)<br />
    End Try<br />
<br />
End Sub<br />
<br />
Private  Sub Serialport7_DataReceived(ByVal sender As Object, ByVal e As  System.IO.Ports.SerialDataReceivedEventArgs) Handles  Serialport7.DataReceived<br />
<br />
    Me.BeginInvoke(New MyDelegate(AddressOf DisplayReading), Serialport7.ReadLine)<br />
<br />
End Sub<br />
<br />
Private Sub DisplayReading(ByVal ReceivedData As String)<br />
<br />
    If ReceivedData.Substring(0, 1) = "T" Then<br />
<br />
        RichTextBox1.Text = ReceivedData.Remove(0, 1)<br />
<br />
    ElseIf ReceivedData.Substring(0, 1) = "H" Then<br />
<br />
        RichTextBox2.Text = ReceivedData.Remove(0, 1)<br />
<br />
    End If<br />
<br />
    RichTextBox3.Text = System.DateTime.Now<br />
<br />
End Sub<br />
<br />
Private Sub MyFormClosing(ByVal sender As Object, ByVal e As ComponentModel.CancelEventArgs) Handles MyBase.Closing<br />
<br />
    If Serialport7.IsOpen Then Serialport7.Close()<br />
<br />
End Sub<br />
<br />
End Class<br />


The error being

<br />
'BeginInvoke' is not a member of '_Default'<br />


How can i resolve this ?
Can anyone tell me how do i port the codes correctly from Window form to a Web form ?

I seek anyone's help earnestly.
Thanks.
Posted

1 solution

You need to know that BeginInvoke is the member of System.Windows.Forms.Control or System.Windows.Threading.Dispatcher:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.aspx[^] (WPF).

On its use, please see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

To port the code to ASP.NET you would need to write it again. The programming models are too different to discuss it seriously. First of all, you need to learn how Web and HTTP work, as well as .NET and programming in general. So far, you have problem understanding classes and their members, so I would advice not to rush into ASP.NET and other advanced topics. Further detail would depend on your goal you did not share with us. At least, do you understand that you can only work with your serial port on server side? I don't know what is connected to it and why.

Your code has other problems, I don't want to discuss them now, especially if you want to migrate to ASP.NET; so I just answered your questions.

—SA
 
Share this answer
 
Comments
CPallini 28-Mar-13 4:06am    
My 5.
Sergey Alexandrovich Kryukov 28-Mar-13 10:11am    
Thank you, Carlo.
—SA
Sergey Alexandrovich Kryukov 28-Mar-13 10:12am    
[OP commented:]

Hi Sergey,

Thanks for reply.

I understand im still a far way off from understanding about Web and its uses.
Im an Electronics student and this is my final year project involving Microcontroller and ASP.net Web Form.

Basically, im taking in 2 inputs from microcontroller and update the Web Form through use of serial port.

Just to give you a clearer idea of what my project involves, the whole ASP.net Web Form will only be as a local host on my PC alone with no LAN or shared access. So it will be just a single page Web Form that will display 2 readings continuously coming from a Microcontroller through the Serial Port.

I have spent much effort learning about C++ programming for the Microcontroller side and the VB coding in WinForm. Hence, i am left with limited time to learn about Web Form and its style.

Are you able to help me re-code my VB coding into a ASP.net coding ?
If not, are you able to provide some guidance to the coding ?
Sergey Alexandrovich Kryukov 28-Mar-13 10:16am    
First of all, please don't post questions as "Solution", to avoid abuse reports, etc. Also, I would not see your post, as it wasn't a reply to mine, so I did not get a notification.

To start with, please tell me what the serial port is connected to, what microcontroller, what this microcontroller is going to do and why. Do you understand that only the server side code can work with the port?

—SA
Member 9948376 29-Mar-13 9:01am    
Hi Sergey,

Im sorry about the correct posting column as this is my first post on this forum.

To give you a clearer idea of what my project involves,
- A microcontroller (Microchip 18f4520) takes in 2 reading from 2 sensors namely, a temperature and a humidity sensor.
- The microcontroller is seated on a target board of which includes a serial port.
- The readings are taken in by microcontroller and output to the serial port.
- The serial port is connected to my PC via a serial port to USB converted cable.
- Im supposed to display the readings onto the Web Page by writing coding to retrieve the readings from the serial port.
- But for demo purpose, the Web Page will be accessed only on the PC that is writing the coding, ie. my PC alone and not necessary from other PCs or LAN connection. It will be a local host (if i use the term correct) to my PC only.
- Initially i have written the coding on VB thinking it is just a simple porting over to the Web Site mode in Visual Studio as told by my lecturer but i realized it is not.

If i have not explained clear enough to your understanding, please let me know.

I really hope to have your help now as most of my time were spent on the coding for Microcontroller interfacing and Visual Basic and i have limited time left for writing report and to learn a whole new concept in Web Paging.
If it is been tedious for you, maybe you can show me some samples to follow.

Thank you for your attention.

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