vb 2008 express control serial port relay

Discussion in 'Programmer's Corner' started by bn4al, Apr 8, 2012.

  1. bn4al

    Thread Starter New Member

    Apr 7, 2012
    9
    0
    hi,

    i would like to control up to 10 relays via serial port using visual basic express 2008.

    i would like to have a circuit diagram and the vb code.

    i have already started designing the interface, i need now to have the code?

    thanks to help me out..
     
  2. bn4al

    Thread Starter New Member

    Apr 7, 2012
    9
    0
    hi, is this is impossible then please do send me the code for the following pins:
    pin 7 , TRS
    pin 4, DTR

    i have already mount the circuit and using a free software i found on the net, but i would like to have my own software thats why i need help for the code.
     
  3. panic mode

    Senior Member

    Oct 10, 2011
    1,320
    304
  4. bn4al

    Thread Starter New Member

    Apr 7, 2012
    9
    0
    hi,
    does the code in vb.net differ from vb 2008 express?
     
  5. panic mode

    Senior Member

    Oct 10, 2011
    1,320
    304
    no, both are DotNet. But the project may be older and need conversion. That is ok, just let the converter do it's job when you open code sample first time. Converter works reasonably well even for VB classic code (not DotNet, here we talk about VB6, or VB5), but on DotNet it is just a formality.
     
  6. bluebrakes

    Active Member

    Oct 17, 2009
    245
    7
    using a serial port to do the job your asking is going to be a larger task than using a parallel port, which would be best suited for a job such as this.

    Especially as you're probably going to need to program a pic to understand the serial commands you intend to send.
     
  7. bluebrakes

    Active Member

    Oct 17, 2009
    245
    7
    classic vb6 is a lot easier to program serial/parallel ports with than .net in my opinion.
     
  8. panic mode

    Senior Member

    Oct 10, 2011
    1,320
    304
    i used both VB6 or VB.NET to interface to RS232 devices. Both are very straight forward when it comes to using serial port.

    it is preferred that target circuit is intelligent (MCU) but it does not have to be.
    it could be replaced by simple shift (and latch) register like 74HC565. adding driver like ULN2803 gives you a lot of freedom in terms of type of load.

    there are many chips that combine shift/latch/driver function into single chip (L9822N for example). they can be cascaded to get 8, 16, 24 or more outputs - all individually addressable.
     
  9. bn4al

    Thread Starter New Member

    Apr 7, 2012
    9
    0
    [​IMG]

    here's what am planning to do.

    i have already mount the circuit and works well with free software i found on the net.

    but i would like to have to write the codes so that i can do some modification.
     
  10. bn4al

    Thread Starter New Member

    Apr 7, 2012
    9
    0
    here's the pic of the program i actually want as
     
    • 1.jpg
      1.jpg
      File size:
      83.7 KB
      Views:
      70
  11. panic mode

    Senior Member

    Oct 10, 2011
    1,320
    304
    you mean this is what you want finished program to look like? good luck with that...

    1. unless you have computer with built in comport, and cable permanently attached to it, you need (at very least) option to select com port. most computer these days don't have serial port and usually one is added in form of USB to RS232 converter.

    2. next thing you need to have is controls to open and close connection.

    3. interface should be both ways, you need fields (text box, label, whatever) to show you what comes in and what you are sending. you only show buttons to manually force control lines (outputs on PC port).
     
  12. Felo

    Member

    Feb 20, 2012
    91
    13
    What kind of electronics do you have?, I have made this very same thing you are trying to do.

    Good Luck
     
  13. bn4al

    Thread Starter New Member

    Apr 7, 2012
    9
    0
    this is the circuit i done and works well.

    i'll have to control these pins through some codes now.

    am actually using a USB to serial converter.

    need to assign a combo box for the com port. i.e com1, com 2.....
     
    • 1.gif
      1.gif
      File size:
      3.9 KB
      Views:
      219
    • 2.jpg
      2.jpg
      File size:
      23.3 KB
      Views:
      51
  14. panic mode

    Senior Member

    Oct 10, 2011
    1,320
    304
    that will work for 2 relays, how do you plan to control up to 10 of them?
     
  15. Felo

    Member

    Feb 20, 2012
    91
    13
    Thas is just a driver, I don't feel like you are trying hard enough, Do you have anything else? or you believe that someone is going to do all the work for you?
     
  16. bn4al

    Thread Starter New Member

    Apr 7, 2012
    9
    0
    the first pic was when i was working with paralell port, i could control 8 outputs and 5 inputs with that.

    i thought of doing the same with serial port and i was wrong unless i use a PIC.

    thats why now am doing it with 2 outputs.
     
  17. bn4al

    Thread Starter New Member

    Apr 7, 2012
    9
    0
    here's the code till far i have reached


    Imports System.IO
    Imports System.IO.Ports
    Imports System.Threading
    Public Class Form1
    Shared _continue As Boolean
    Shared _serialPort As SerialPort
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    SerialPort1.Close()
    SerialPort1.PortName = "com4"
    SerialPort1.BaudRate = 9600
    SerialPort1.DataBits = 8
    SerialPort1.Parity = Parity.None
    SerialPort1.StopBits = StopBits.One
    SerialPort1.Handshake = Handshake.None
    SerialPort1.Encoding = System.Text.Encoding.Default
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    SerialPort1.Open()
    SerialPort1.DtrEnable = True
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    SerialPort1.Open()
    SerialPort1.RtsEnable = True
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    SerialPort1.Close()
    SerialPort1.DtrEnable = False

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    SerialPort1.RtsEnable = False
    SerialPort1.Close()
    End Sub
    ........................................................................................................

    there's still some bug in it which i have to amend.

    as panic mode said, i'll have to add an option to select com port, which i really dont know how to do now. i have already add the combo box and add the com port names, i.e com1 till com9. but to make it works......
     
  18. panic mode

    Senior Member

    Oct 10, 2011
    1,320
    304
    try this:

    Code ( (Unknown Language)):
    1.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         SerialPort1.Close()
    3.       '  SerialPort1.PortName = "com4"
    4.         SerialPort1.BaudRate = 9600
    5.         SerialPort1.DataBits = 8
    6.         SerialPort1.Parity = Parity.None
    7.         SerialPort1.StopBits = StopBits.One
    8.         SerialPort1.Handshake = Handshake.None
    9.         SerialPort1.Encoding = System.Text.Encoding.Default
    10.         Dim i As Integer
    11.         For i = 1 To 15 ' create Com1..Com15
    12.             Me.ComboBox1.Items.Add("Com" & i)
    13.         Next
    14.         Me.ComboBox1.SelectedIndex = 3 ' preselect Com4
    15.     End Sub
    16.  
    17.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    18.         SerialPort1.PortName = Me.ComboBox1.SelectedItem
    19.         SerialPort1.Open()
    20.         SerialPort1.DtrEnable = True
    21.     End Sub
     
  19. bn4al

    Thread Starter New Member

    Apr 7, 2012
    9
    0
    Thanks a lot Panic Mode

    so sorry for the delay,

    well i have been able to complete it by your help.

    thanks a lot.

    there's only one thing, when i scroll down for the com port, it repeats the com port, means i can see from com port 1 to com port 15 twice.

    here's the code


    Imports System.IO
    Imports System.IO.Ports
    Imports System.Threading
    Public Class Form1
    Shared _continue As Boolean
    Shared _serialPort As SerialPort
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    SerialPort1.Close()
    ' SerialPort1.PortName = "com4"
    SerialPort1.BaudRate = 9600
    SerialPort1.DataBits = 8
    SerialPort1.Parity = Parity.None
    SerialPort1.StopBits = StopBits.One
    SerialPort1.Handshake = Handshake.None
    SerialPort1.Encoding = System.Text.Encoding.Default
    Dim i As Integer
    For i = 1 To 15 ' create Com1..Com15
    Me.ComboBox1.Items.Add("Com" & i)
    Next
    Me.ComboBox1.SelectedIndex = 3 ' preselect Com4

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    SerialPort1.DtrEnable = True
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    SerialPort1.PortName = Me.ComboBox1.SelectedItem
    SerialPort1.Open()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    SerialPort1.DtrEnable = False
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    SerialPort1.Close()
    End Sub

    Private Sub AboutToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem1.Click
    MsgBox("Welcome")
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
    Me.Close()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    SerialPort1.RtsEnable = True
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    SerialPort1.RtsEnable = False
    End Sub
    End Class
     
Loading...