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

kubeek

Joined Sep 20, 2005
5,795
Rich (BB code):
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()
I think this should be
Rich (BB code):
data(0) = 92 //command ID
data(1) = 8 //set relay 4
....
SerialPort1.Write(data, 0, 2)
Why do you insist on sending three bytes when the board specifically wants just two? Also note all other commands are single-byte.
 

Thread Starter

saint1000

Joined Jan 23, 2013
20
I think this should be
Rich (BB code):
data(0) = 92 //command ID
data(1) = 8 //set relay 4
....
SerialPort1.Write(data, 0, 2)
Why do you insist on sending three bytes when the board specifically wants just two? Also note all other commands are single-byte.
thanks i missed that. i appreciate all the help.
 

Thread Starter

saint1000

Joined Jan 23, 2013
20
trying to learn the math here. ok so i get this

binary 0b 0 0 0 0 0 0 0 0
POSITION 7 6 5 4 3 2 1 0
2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
128 64 32 16 8 4 2 1

so for relays 1 and 2 would be
2^1+2^0=3=0x12 right?
 

kubeek

Joined Sep 20, 2005
5,795
is there an equation for 0x03 or just use the hex chart?
Lots of different equatios. From binary it is easiest: the upper four bits make the left number, the lower four make the right number. So 0b11010101 is 0xD5, because 1101 is 13 which is D in hex and 0101 is 5 which is 5 in hex.
 
Last edited:

Thread Starter

saint1000

Joined Jan 23, 2013
20
got it thanks. i have one more question maybee you can help me with. when i debug my program in visual studio, it will not controll any of the relays. if i minimize visual studio and run the test software that came with the board and turn on 1 relay that close out of the test software and go back to visual studio, everything works fine. it will continue to work untill i unplug the usb from the computer and plug it back in. then it wont work again untill i open the test program and close it again. what could i be missing?
 

tshuck

Joined Oct 18, 2012
3,534
got it thanks. i have one more question maybee you can help me with. when i debug my program in visual studio, it will not controll any of the relays. if i minimize visual studio and run the test software that came with the board and turn on 1 relay that close out of the test software and go back to visual studio, everything works fine. it will continue to work untill i unplug the usb from the computer and plug it back in. then it wont work again untill i open the test program and close it again. what could i be missing?
....sounds like you have too many things trying to use the COM port, some programs are bound to be locked out from using it.


Does your program work if not in debug mode?
 

Thread Starter

saint1000

Joined Jan 23, 2013
20
NO it doesnt work when i publish it unless i open some other software to access the usb board.if i have my program open then open another program to access the usb board it cant because it says the com port is unavailable. so i have to close my program, open another then close that and re open mine and it works. weird, having a hard time figuring it out,
 

panic mode

Joined Oct 10, 2011
2,753
why don't you just replace

Rich (BB code):
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
with something like

Rich (BB code):
data(2)=2^val(Form2.Label2.Text)
or

Rich (BB code):
data(2)=1 << val(Form2.Label2.Text)
 
Top