none
How to USE SERIALPORT IN CHILD FORM. RRS feed

  • Question

  • Hello All,

    I'am using Visual studio 2015 community and I'am developing an application with below setup.

    FORM1 is the main form .

    FORM2 is called from FORM1 via FORM2.SHOW() command on a button click and I need to use serial port in FORM 2 only.

    My issue is that the serialport datarecieved event is not getting called in FORM2. I've added a serialport component in FORM2 and all the coding regarding serial port is done in FORM2 only. I'am able to see the correct COMPORT in combobox on FORM2.

    Do I need to declare serial port & get its Port names on FORM1 as well?

    kindly help!

    Thursday, July 13, 2017 8:24 AM

Answers

  • Hello Acamar,


    Thanks for your suggestion!!! I was able to resolve my issue.

    The data received event was not being fired because of the below line

    myPort.PortName = cobComPort.Text

    I changed the above code to my actual COM Port 15(in my case)

    myPort.PortName = "COM15" 

    and it works!!!




    • Marked as answer by sumit4271 Monday, July 17, 2017 7:53 AM
    Monday, July 17, 2017 7:53 AM

All replies

  • To get to a control or component in the parent form, here I'm using a TextBox but this will work with anything that is publically exposed in form1.

    Form1

    Dim f As New Form2 With {.Owner = Me}
    f.Show()

    Form2

    Public Class Form2
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim form1 As Form1 = CType(Me.Owner, Form1)
            form1.TextBox1.Text = "Hello"
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, July 13, 2017 8:48 AM
    Moderator
  • Hi Karen, Thanks for replying to my query.Frankly speaking I really didn't understand your suggestion.Could you please explain in some other way!
    Thursday, July 13, 2017 10:36 AM
  • There is not much to explain, if there is something you want to access on the parent form you access it via the Owner property of the form.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, July 13, 2017 11:33 AM
    Moderator
  • Hi sumit4271,

    According to your description, you said the serialport datarecieved event is not triggered, and the reason what you think is subform. I think it is no matter, you can set form2 as startup form and see the event can be triggered or not. If it is still not, you can check your code or post you code here.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 14, 2017 6:54 AM
    Moderator
  • Hello Cherry,

    I do not want to set form2 as startup form because according to UI requirements the form2 must be a subform and will depend on user action.

    Below is the screen shot of different forms.

    FORM1 is the startup form of the UI. when the user clicks on Acquisition button the acquire form is shown. On clicking the "PLAY" button the graph Is not getting plotted in the picturebox. I even tested myPortDataReceived handler  by putting a breakpoint in it , but the debugger never goes to the breakpoint.

    Option Explicit On
    Option Infer Off
    
    Imports System.IO.Ports
    Imports System.Drawing
    Imports System.Threading
    Imports System.Text
    Imports System.Delegate
    
    
    'CODE FOR ACQUIRE.VB FORM''''
    
    Public Class acquire
    
        Dim plotdata As Boolean = False
        Dim i As Integer
        Dim WithEvents myPort As New SerialPort
        'Public Shared WithEvents myPort As New SerialPort
        Dim inData1 As String
        'Dim inData2 As String
        'This is my Delegate.  It's the box that'll carry the String data from the receival thread to the main one. 
        Private Delegate Sub SetTextCallback(ByVal [text] As String)
        Dim x_start As Integer
        Dim y_start As Integer
        Dim mygraphics As Graphics
        Dim p1 As New Pen(Color.Black, 0.1)
        Dim p2 As New Pen(Color.Blue, 0.1)
        Dim Serial_data_tmp As String
        Dim Serial_array(1000000) As String
        Dim Serial_data_count As Integer
        Dim DataEnter As Boolean
        Dim k As Integer
        Dim temp_ahead1 As Integer
        'Dim temp_ahead2 As Integer
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Me.Close()
        End Sub
    
        Private Sub acquire_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim Port As String
            Dim namearray As String() = SerialPort.GetPortNames()
            Array.Sort(namearray)
    
            For Each Port In namearray
                cobComPort.Items.Add(Port)
    
            Next Port
    
            If cobComPort.Items.Count > 0 Then
                cobComPort.SelectedIndex = 0
            End If
    
            myPort.PortName = cobComPort.Text
            myPort.BaudRate = 115200
            myPort.Parity = Parity.None
            myPort.DataBits = 8
            myPort.StopBits = StopBits.One
            myPort.Encoding = Encoding.ASCII
    
    
    
    
        End Sub
    
        'Private Sub myPort_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles myPort.DataReceived
    
        '    '    'When this event happens, it's being processed by a second thread; one that's automatically created by the serialport
        '    '    'object.
    
        '    '    'inData = myPort.ReadByte()
        '    'inData1 = CType(myPort.ReadByte(), String)
        '    inData1 = myPort.ReadByte()
        '    DataEnter = True
    
        '    Serial_data_tmp = inData1
        '    Serial_array(Serial_data_count) = Chr(CInt(Serial_data_tmp))        'ASCII        
        '    Serial_data_count = Serial_data_count + 1
    
        '    PrintX(inData1)
    
        '    If Serial_data_count = 89961 Then
        '        Serial_data_count = 0
        '    End If
    
    
        'End Sub
    
        Function PrintX(ByVal Xvalue1 As Byte) As Object
            x_start = 1
            y_start = graph.Height
            mygraphics = graph.CreateGraphics
            mygraphics.PageUnit = GraphicsUnit.Pixel
            mygraphics.PageScale = 1
            mygraphics.DrawLine(p1, 0, CInt(graph.Height), CInt(graph.Width), CInt(graph.Height))
            mygraphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
            mygraphics.ScaleTransform(1, 1)
    
            If DataEnter = True Then
    
                'Xvalue1 = (Xvalue1 * 2)
    
                mygraphics.DrawLine(p2, x_start + k, y_start - temp_ahead1, x_start + (k + 1), y_start - Xvalue1)
    
                temp_ahead1 = Xvalue1
                DataEnter = False
            End If
    
            k = k + 1
    
            If k = graph.Width Then
                k = 0
                mygraphics.Clear(Color.White)
    
            End If
    
            Receivedtext(myPort.ReadByte())
    
        End Function
    
    
        Private Sub Receivedtext(ByVal [text] As String)
    
            'First, check if the control we're updating requires an invoke.
            'This is just a simple test of the thread that's calling the function vs. the thread that created the control.
            If Me.graph.InvokeRequired Then
                'This means the thread that's calling this function is NOT the one that created the TextBox.
                'So, it makes a new delegate and tells it to send the data right back to this same function.
                Dim x As New SetTextCallback(AddressOf Receivedtext)
                'This tells the main application thread that it's waiting for it to process. The data received thread is now complete.
                Me.Invoke(x, New Object() {(text)})
    
            Else
                'When the main application thread picks up the delegate, it'll run "UpdateTextbox" with the data it was handed.
                'So, it'll pass the InvokeRequired test since the main application thread IS the thread that made the Textbox as well,
                'thus, it'll just set the value as you'd expect it to.
                '
    
                'While (myPort.BytesToRead > 0)
                'temp_ahead1 = CInt([text])
    
                '    y1 = y2
                'End While
            End If
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
            myPort.Open()
    
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    
            myPort.Close()
    
        End Sub
    
        Private Sub myPort_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles myPort.DataReceived
            'When this event happens, it's being processed by a second thread; one that's automatically created by the serialport
            '    'object.
    
            '    'inData = myPort.ReadByte()
            'inData1 = CType(myPort.ReadByte(), String)
            inData1 = myPort.ReadByte()
            DataEnter = True
    
            Serial_data_tmp = inData1
            Serial_array(Serial_data_count) = Chr(CInt(Serial_data_tmp))        'ASCII        
            Serial_data_count = Serial_data_count + 1
    
            PrintX(inData1)
    
            If Serial_data_count = 89961 Then
                Serial_data_count = 0
            End If
        End Sub
    End Class

    Friday, July 14, 2017 10:22 AM
  • @sumit,

    A form is not an assembly, app, or program. It is a window on the screen. 

    Therefore for a second form which needs to accept data from the computer messages system, a modal (showdialog) screen is used. 

    But I'm almost sure that does not fit in your solution. 

    Therefore use your second form as a kind of message screen which get data from your main form. 

    Look here at the windows message system (that is not something as mail or whatsapp). The mainform gets the messages from this system.

    https://msdn.microsoft.com/en-us/library/windows/desktop/ff381405(v=vs.85).aspx

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms644927(v=vs.85).aspx


    Success
    Cor

    Friday, July 14, 2017 10:34 AM
  • I even tested myPortDataReceived handler  by putting a breakpoint in it , but the debugger never goes to the breakpoint.

    The fact that that code is in a form other than the startup form doesn't matter, assuming that the form is actually being shown.

    But if the data received event is not being fired then that indicates that the serial port is not working.  You need to confirm that the serial device is sending data, that the connection is correct, that the correct port has been selected, and that the communication protocol settings are correct. 

    Is Button2 the 'Play' button?   A first guess would be that the Open call is failing.  You should have code to set the communication parameters and to test that the Open succeeded.

    Friday, July 14, 2017 10:23 PM
  • Hello Acamar,


    Thanks for your suggestion!!! I was able to resolve my issue.

    The data received event was not being fired because of the below line

    myPort.PortName = cobComPort.Text

    I changed the above code to my actual COM Port 15(in my case)

    myPort.PortName = "COM15" 

    and it works!!!




    • Marked as answer by sumit4271 Monday, July 17, 2017 7:53 AM
    Monday, July 17, 2017 7:53 AM