need someoard and help with usb relay board and vb.net

Discussion in 'Programmer's Corner' started by saint1000, Jan 26, 2013.

  1. saint1000

    Thread Starter New Member

    Jan 23, 2013
    20
    0
    i have a 8 relay usb board.i need to be able to send on and off commands to the board in visual studio. i am new to programming and figuring my way aroud it, so please bear with me. i do not need any user buttons to turn relays on. i have a random number generator that will generate random numbers between 1-4 and posts it to a label box. every 5 secs the rnd number generator replaces the number in the label box with another random number. i need for when a number 1 is in the label box for relay 1 to turn on. when the number is replaced i need the coresponding relay to turn on. so basiclly i need to know how to open the usb port and send on and off comands to the relay board. thanks for any help
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    No one can tell you how as no one but you knows what board you have. Open the manual and see what kind of USB interface this is.

    Oft it is made to look like a serial port to a Windows app, so the COMM control can talk to it. But you need to check that for your specific board.
     
  3. saint1000

    Thread Starter New Member

    Jan 23, 2013
    20
    0
    i have this board
    http://www.robot-electronics.co.uk/htm/usb_rly08tech.htm
    tjis is my code
    Public Class Form1

    Private Sub three()

    Form2.TextBox1.Text = AutoDetect()
    SerialPort1.BaudRate = 9600
    SerialPort1.DataBits = 8


    End Sub

    Sub RELAYON()
    Dim data(2) As Byte

    data(0) = 255

    data(1) = Form2.Label2.Text

    data(2) = 1
    SerialPort1.PortName = "COM" + Form2.TextBox1.Text
    SerialPort1.Open()
    SerialPort1.Write(data, 0, 3)
    SerialPort1.Close()
    End Sub

    Sub RELAYOFF()
    Dim data(2) As Byte

    data(0) = 255

    data(1) = Form2.Label2.Text

    data(2) = 0
    SerialPort1.PortName = "COM" + Form2.TextBox1.Text
    SerialPort1.Open()
    SerialPort1.Write(data, 0, 3)
    SerialPort1.Close()
    End Sub

    the only way i can get this to work is i have to open the trial board software on my desktop the run the debug in visual studio. but even then no matter what i do i only lights up relay 1
     
  4. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    I think you should send 0x5c first and then 0xff to turn on all relays.
    Anyway, why are you putting a string in data(1)? Are you sure it shouldn´t be an unsigned byte (I dont know visual basic...)?
    I would simply try sending data(0)=0x5c data(1)=255 SerialPort1.Write(data, 0, 2) and see what happens.
     
  5. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Your COM port settings are wrong.
    From the link you didn't bother to read:
     
  6. mcgyvr

    AAC Fanatic!

    Oct 15, 2009
    4,769
    969
    pfft.. who reads instructions anymore :p
     
  7. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    People that want their devices to work;)
     
  8. saint1000

    Thread Starter New Member

    Jan 23, 2013
    20
    0
    thanks for the help. i changed the baud rate and still have the same results. the only relay that turns on is 1. im stumped
     
  9. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    So where is it that you are sending 0x5C?

    Again, from the link you didn't read:
    So your sequence should be send 0x5C, then your desired relay states....
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    If the board does not work using the manufacturer's own supplied software then it sounds like this is a defective unit and should be returned.
     
  11. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    Did you set the com port in your device manager right?

    [​IMG]
     
  12. saint1000

    Thread Starter New Member

    Jan 23, 2013
    20
    0
    i tried switching the baud rate but makes no difference, i get the same results. when i run it like this, everything works as expected except #3. when it calls relay #3 it turns on 1+2 together.i am new to programming, so i know im going to make mistakes, there is going to be things i dont understand. so please if your not trying to help dont reply.

    Sub RELAYON()
    Dim data(2) As Byte

    data(0) = 255

    data(1) = Form2.Label2.Text

    data(2) = Form2.Label2.Text
    SerialPort1.PortName = "COM" + Form2.TextBox1.Text
    SerialPort1.Open()
    SerialPort1.Write(data, 0, 3)
    SerialPort1.Close()
     
  13. saint1000

    Thread Starter New Member

    Jan 23, 2013
    20
    0
    i forgot to mention that the form2.label2.text is the output of my random number generator
     
  14. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    I'm pretty sure everyone that has posted so far is trying to help.

    if you are entering "3", this is 0b00000011, meaning LEDs 1+2 is normal. If you want each one individually, use either 1 << some_number, or only enter 2^n, where n = 0 - 7.
     
  15. saint1000

    Thread Starter New Member

    Jan 23, 2013
    20
    0
    thanks for your reply, but i dont understand.

    if form2.label2.text=1 then relay1 turns on
    if form2.label2.text=2 then relay2 turns on
    if form2.label2.text=3 then relays 1 and 2 turn on
    if form2.label2.text=4 then relay 4 turns on

    why would 3 be any different?
     
  16. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Are you sure 4 doesn't light LED 3?

    If so, it is taking the bit position in the number to turn on a relay, at least that's what the link you provided said.
     
  17. saint1000

    Thread Starter New Member

    Jan 23, 2013
    20
    0
    tshuck you are correct, 4 turns on relay 3. so now im lost. what would form2.label2.text have to = to
    turn on relay 1
    turn on relay 2
    turn on relay 3
    turn on relay 4
    turn on all relay
    turn off all relay
     
  18. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Aside from the codes specified in the link you supplied, you would send 255 to turn on all of the relays(255 = 0xFF = 0b11111111)
    If you look at the binary representation of a byte, bit 0 is 1 << 0, bit 2 is 1 << 2, bit 7 is 1 << 7, etc. you can OR all of these together if you want, or, simply write 6 = 0x06 = 0b00000110 to turn on relays 2 and three...

    Note, when we count bits, the right most bit(the least significant bit(LSB)) is bit 0.

    So,
    turn on relay 1 = 0x01 = 1 = 1<< 0 = 0b00000001
    turn on relay 2 = 0x02 = 2 = 1<< 1 = 0b00000010
    turn on relay 3 = 0x04 = 4 = 1<< 2 = 0b00000100
    turn on relay 4 = 0x08 = 8 = 1<< 3 = 0b00001000
    turn on all relay = 0xFF = 255 = 0b11111111
    turn off all relay = 0x00 = 0 = 0b00000000
     
  19. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    So, look at this:
    bit 7 bit 6 bit 5 bit 4 bit 3 bit 2 bit 1 bit 0
    relay 8 relay 7 relay 6 relay 5 relay 4 relay 3 relay 2 relay 1
    2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0


    to turn on relays 4 and 7 = 2^(4-1) + 2^(7-1) = 72 = 0x48
     
  20. saint1000

    Thread Starter New Member

    Jan 23, 2013
    20
    0
    thank you so much! that works. i may have to study this for a while to understand, but i will get it. thank you

    Sub RELAYON()
    Dim data(2) As Byte

    data(0) = 255

    data(1) = 0


    If (Form2.Label2.Text) = 1 Then data(2) = 1
    If (Form2.Label2.Text) = 2 Then data(2) = 2
    If (Form2.Label2.Text) = 3 Then data(2) = 4
    If (Form2.Label2.Text) = 4 Then data(2) = 8


    SerialPort1.PortName = "COM" + Form2.TextBox1.Text
    SerialPort1.Open()
    SerialPort1.Write(data, 0, 3)
    SerialPort1.Close()
     
Loading...