Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. vb.net 3.5 + serialport cotnrol + Threads = help

vb.net 3.5 + serialport cotnrol + Threads = help

Scheduled Pinned Locked Moved Visual Basic
helpquestioncsharpcom
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    ktheos
    wrote on last edited by
    #1

    hello to everyone, im new in .NET programming -and in multithreading- so if the answer of my question is to read more of multithreading just say it :) i try to make an application to communicate with a device through serial port, after a lot of reading i made it to communicate in a way i want, but..... now the problem, when i use a second form, and at this form i create a thread and through that thread i try to send a data through com i get an error that com port is closed! i try the .isAlive and in that thread the serial port looked closed. To be more specific IN form1 i use

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        SerialPort1.Open()
    
    End Sub
    

    IN form2 i use

    Imports System.Threading

    Public Class Form2

    Dim thr As Thread
    
    Private Sub Form2\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    End Sub
    
    Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Debug.Print(Form1.SerialPort1.IsOpen)
        thr = New Thread(AddressOf thrsub)
        thr.IsBackground = True
        thr.Start()
    
    End Sub
    
    
    
    Public Sub thrsub()
        Debug.Print(Form1.SerialPort1.IsOpen)
    End Sub
    

    End Class

    at the first Debug.Print(Form1.SerialPort1.IsOpen) if the Button1_click it gives TRUE but in the second Debug.Print(Form1.SerialPort1.IsOpen) in the thrsub it gives FALSE.... :sigh: any suggestions?

    B D 2 Replies Last reply
    0
    • K ktheos

      hello to everyone, im new in .NET programming -and in multithreading- so if the answer of my question is to read more of multithreading just say it :) i try to make an application to communicate with a device through serial port, after a lot of reading i made it to communicate in a way i want, but..... now the problem, when i use a second form, and at this form i create a thread and through that thread i try to send a data through com i get an error that com port is closed! i try the .isAlive and in that thread the serial port looked closed. To be more specific IN form1 i use

      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

          SerialPort1.Open()
      
      End Sub
      

      IN form2 i use

      Imports System.Threading

      Public Class Form2

      Dim thr As Thread
      
      Private Sub Form2\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      
      End Sub
      
      Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          Debug.Print(Form1.SerialPort1.IsOpen)
          thr = New Thread(AddressOf thrsub)
          thr.IsBackground = True
          thr.Start()
      
      End Sub
      
      
      
      Public Sub thrsub()
          Debug.Print(Form1.SerialPort1.IsOpen)
      End Sub
      

      End Class

      at the first Debug.Print(Form1.SerialPort1.IsOpen) if the Button1_click it gives TRUE but in the second Debug.Print(Form1.SerialPort1.IsOpen) in the thrsub it gives FALSE.... :sigh: any suggestions?

      B Offline
      B Offline
      Bharat Jain
      wrote on last edited by
      #2

      I think the problem is not related to threads , it is something to do with creating proper instance of a class. When from Form2 you call Form.SerialPort1.IsOpen you are not creating an instance of Form1 which means Form1_Load never gets called , and hence the code inside the Form1_Load event is not called ,keeping the the port closed , in Form2 try this , just before Debug.Print(Form1.SerialPort1.IsOpen) statements put the following statements

      Form1.Show()
      Form1.Hide()

      We are forcing the Form1 to load. Let me know if this answers you question Feel free to ask if something is not clear

      -Regards Bharat Jain bharat.jain.nagpur@gmail.com

      1 Reply Last reply
      0
      • K ktheos

        hello to everyone, im new in .NET programming -and in multithreading- so if the answer of my question is to read more of multithreading just say it :) i try to make an application to communicate with a device through serial port, after a lot of reading i made it to communicate in a way i want, but..... now the problem, when i use a second form, and at this form i create a thread and through that thread i try to send a data through com i get an error that com port is closed! i try the .isAlive and in that thread the serial port looked closed. To be more specific IN form1 i use

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            SerialPort1.Open()
        
        End Sub
        

        IN form2 i use

        Imports System.Threading

        Public Class Form2

        Dim thr As Thread
        
        Private Sub Form2\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
        End Sub
        
        Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Debug.Print(Form1.SerialPort1.IsOpen)
            thr = New Thread(AddressOf thrsub)
            thr.IsBackground = True
            thr.Start()
        
        End Sub
        
        
        
        Public Sub thrsub()
            Debug.Print(Form1.SerialPort1.IsOpen)
        End Sub
        

        End Class

        at the first Debug.Print(Form1.SerialPort1.IsOpen) if the Button1_click it gives TRUE but in the second Debug.Print(Form1.SerialPort1.IsOpen) in the thrsub it gives FALSE.... :sigh: any suggestions?

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        Using the serial port instance on your Form1 instance from Form2 is bad practice. You also can't do it with the code you're written since Form2 has no idea that Form1 even exists. If the bulk of your serial port communication is going to happen from Form2, create the port and connection on Form2, not Form1. Also, if you dragged the SerialPort from the ToolBox onto your form, you can't use it from a second thread. The event's won't fire correctly because they depend on the main UI thread to work.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups