14 Replies - 3552 Views - Last Post: 16 March 2013 - 03:03 PM Rate Topic: -----

#1 saint1000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 22-January 13

usb relay board will not respond in vb.net unless test software is use

Posted 01 February 2013 - 04:24 AM

i am trying to write a program to controll a usb relay board. i have got everything working they way i would like except for one issue. when i debug my program, or publsih it and run it, i can not controll the usb board unless i open up the test software that came with the relay board and then close the test software. then my program will controll the usb relay board fine and with no issues untill i unplug the usb cable. when i plug the usb back in i have to open and close the test software again for my program to controll the board.

  Private Sub three()

        Form4.Label1.Text = AutoDetect()
        SerialPort1.BaudRate = Form4.Label2.Text
        SerialPort1.DataBits = 8



    End Sub

    Sub RELAYON()
        Dim data(3) As Byte

        data(0) = 0

        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" + Form4.Label1.Text
        SerialPort1.Open()
        SerialPort1.Write(data, 0, 3)
        SerialPort1.Close()
    End Sub

    Sub RELAYOFF()
        Dim data(2) As Byte

        data(0) = 0

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


Is This A Good Question/Topic? 0
  • +

Replies To: usb relay board will not respond in vb.net unless test software is use

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,371
  • Joined: 02-June 10

Re: usb relay board will not respond in vb.net unless test software is use

Posted 01 February 2013 - 06:25 AM

The boards works - That's good news.

You need to go through the documentation again because you simply missed something regarding initialization of the board or the serial port. Maybe at startup you need to tell it to start listening to you for example.

Personally, I would suggest a LOT of cleaning up for your code.
One should NEVER do what you're doing with directly accessing GUI controls on other forms like this: Form4.Label1.Text and so on. One shouldn't even keep those terrible default names of Label1 and TextBox2: Its a maintenance nightmare.

From my FAQ list:
Q: ...get Form/class 'A' to make a change or talk to Form/class 'B'

NOTE: Don't try to access GUI controls across forms. Its wrong. Nobody will hire you if you do this sort of crap. It violates every guideline for 'black box' programming, Separation of Responsibility, loose binding of components, and event driven programming. Read the tutorials and learn to do it right the first time so you don't develop bad habits that you just have to un-learn later.
A:


Some of my common tips (some may apply more than others to your specific style):
  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Don't use your GUI objects as your variable. In other words don't keep referencing TextBox4.Text everyplace. TextBox4.Text is not a variable or property. The GUI is on its own thread so as soon as you start doing multi-threading you're screwed because your worker thread can't access the GUI elements. Use properties.

  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readable. This is also how you can send parameters rather than use excessive global variables. Get in this habit even if you are using WinForms because WPF works a lot under the idea of "commands" and this will get you working towards that. Think of each gester, control click, menu option etc. as a command to do something such as a command to SAVE. It doesn't matter where the command comes from, all sources should point at the same target to do the actual saving.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attempts below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.


  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • I strongly suggest installing VMware or some other virtualization technology on your development PC so you can create a couple virtual computers for testing. This would allow you to debug and test inside: WinXP32, XP64, Vista, Win7x32, Win7x64... etc. without having to actually have 5 physical PC's. Visual Studio will let you send the debug directly into one of these virtual machines so you can watch it operate, check its variables, see the crashes and so on just as if it were debugging on your real machine.

  • This can't be stressed enough in today's world of cell phone messaging:
    Don't use txt/sms/leet/T9 speak like: dnt no wut i m do-n, coz, al gud, b4, ny1, sum1, u r, and so on like this guy. Its completely disrespectful to the senior coding professionals that volunteer here to mentor you.

    Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 saint1000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 22-January 13

Re: usb relay board will not respond in vb.net unless test software is use

Posted 01 February 2013 - 06:37 AM

thanks for the help. as you can tell im learning so you help is greatly appreicated. i will do some more reading and see if i can clean this up. i have re read the manual and dont see anything about initialization of the board. mabee the assume the user knows to do that. is there a generic way to do this
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,371
  • Joined: 02-June 10

Re: usb relay board will not respond in vb.net unless test software is use

Posted 01 February 2013 - 06:41 AM

No. I'm sure there are a dozen different boards out there by different makers and they all do things in their own way. For one the commands might be strings where another maker uses specific bytes for their commands and another maker uses single letter codes.

Write the maker and ask for help.
Was This Post Helpful? 0
  • +
  • -

#5 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: usb relay board will not respond in vb.net unless test software is use

Posted 15 March 2013 - 09:30 AM

View Postsaint1000, on 01 February 2013 - 07:37 AM, said:

i have re read the manual and dont see anything about initialization of the board. mabee the assume the user knows to do that. is there a generic way to do this

Can you tell us the brand/model of the relay board?
Was This Post Helpful? 0
  • +
  • -

#6 dbasnett  Icon User is offline

  • D.I.C Addict

Reputation: 102
  • View blog
  • Posts: 584
  • Joined: 01-October 08

Re: usb relay board will not respond in vb.net unless test software is use

Posted 15 March 2013 - 10:22 AM

View Postsaint1000, on 01 February 2013 - 07:37 AM, said:

thanks for the help. as you can tell im learning so you help is greatly appreicated. i will do some more reading and see if i can clean this up. i have re read the manual and dont see anything about initialization of the board. mabee the assume the user knows to do that. is there a generic way to do this


If you have a link to the documentation of the board please post it.
Was This Post Helpful? 0
  • +
  • -

#7 saint1000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 22-January 13

Re: usb relay board will not respond in vb.net unless test software is use

Posted 15 March 2013 - 02:38 PM

http://denkovi.com/D...T_000071%29.pdf
Was This Post Helpful? 0
  • +
  • -

#8 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 155
  • View blog
  • Posts: 571
  • Joined: 01-February 13

Re: usb relay board will not respond in vb.net unless test software is use

Posted 15 March 2013 - 02:47 PM

Hey,
If the c# sample code you showed in the other closed thread works you may want to re-post that here too. Some of the guys here that know both languages may be able to look at that and your vb.net code and figure it out. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#9 saint1000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 22-January 13

Re: usb relay board will not respond in vb.net unless test software is use

Posted 15 March 2013 - 03:38 PM

heres the c# code. this works. if i open this then close it then my code works.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FTD2XX_NET;


namespace WindowsFormsApplication1
{
    
    public partial class Form1 : Form
    {
        public FTDI myFtdiDevice = new FTDI();
        FTDI.FT_STATUS ftStatus;
        byte[] sentBytes = new byte[2];
        uint receivedBytes;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            
            ftStatus = myFtdiDevice.OpenByIndex(0);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                // Wait for a key press
                Console.WriteLine("Failed to open device (error " + ftStatus.ToString() + ")");
                Console.ReadKey();
                return;
            }

            ftStatus = myFtdiDevice.SetBaudRate(921600);
             if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                // Wait for a key press
                Console.WriteLine("Failed to set baudrate (error " + ftStatus.ToString() + ")");
                Console.ReadKey();
                return;
            }

             ftStatus = myFtdiDevice.SetBitMode(255,4);
             if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                // Wait for a key press
                Console.WriteLine("Failed to set bit mode (error " + ftStatus.ToString() + ")");
                Console.ReadKey();
                return;
            }

             sentBytes[0] = 0;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
               sentBytes[0]=(byte)(sentBytes[0] | 1);           
            else            
                sentBytes[0]=(byte)(sentBytes[0] & 254);             
            myFtdiDevice.Write(sentBytes, 1, ref receivedBytes);
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
                sentBytes[0] = (byte)(sentBytes[0] | 2);
            else
                sentBytes[0] = (byte)(sentBytes[0] & 253);
            myFtdiDevice.Write(sentBytes, 1, ref receivedBytes);
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
                sentBytes[0] = (byte)(sentBytes[0] | 4);
            else
                sentBytes[0] = (byte)(sentBytes[0] & 251);
            myFtdiDevice.Write(sentBytes, 1, ref receivedBytes);
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox4.Checked)
                sentBytes[0] = (byte)(sentBytes[0] | 8);
            else
                sentBytes[0] = (byte)(sentBytes[0] & 247);
            myFtdiDevice.Write(sentBytes, 1, ref receivedBytes);
        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox5.Checked)
                sentBytes[0] = (byte)(sentBytes[0] | 16);
            else
                sentBytes[0] = (byte)(sentBytes[0] & 239);
            myFtdiDevice.Write(sentBytes, 1, ref receivedBytes);
        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox6.Checked)
                sentBytes[0] = (byte)(sentBytes[0] | 32);
            else
                sentBytes[0] = (byte)(sentBytes[0] & 223);
            myFtdiDevice.Write(sentBytes, 1, ref receivedBytes);
        }

        private void checkBox7_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox7.Checked)
                sentBytes[0] = (byte)(sentBytes[0] | 64);
            else
                sentBytes[0] = (byte)(sentBytes[0] & 191);
            myFtdiDevice.Write(sentBytes, 1, ref receivedBytes);
        }

        private void checkBox8_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox8.Checked)
                sentBytes[0] = (byte)(sentBytes[0] | 128);
            else
                sentBytes[0] = (byte)(sentBytes[0] & 127);
            myFtdiDevice.Write(sentBytes, 1, ref receivedBytes);
        }
    }
}



Was This Post Helpful? 0
  • +
  • -

#10 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 155
  • View blog
  • Posts: 571
  • Joined: 01-February 13

Re: usb relay board will not respond in vb.net unless test software is use

Posted 15 March 2013 - 04:18 PM

Hey Saint100,
I don`t know if you tried one of the online C# to VB.NET converters but, I just converted it to VB.NET and posting it here. Maybe try this in a new project and if it works then you can figure out what it is you need from it. If you just start that c# code and then close it without pressing the Button1 to make it work then i would say it has something to do with the InitializeComponent() in the following lines
    Public myFtdiDevice As New FTDI()
    Private ftStatus As FTDI.FT_STATUS

    Public Sub New()
        InitializeComponent()
    End Sub



If you have to press Button1 to make it work then i would make sure that the following is included in your program.
    Public myFtdiDevice As New FTDI()
    Private ftStatus As FTDI.FT_STATUS
    Private sentBytes As Byte() = New Byte(1) {}
    Private receivedBytes As UInteger

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub button1_Click_1(ByVal sender As Object, ByVal e As EventArgs)

        ftStatus = myFtdiDevice.OpenByIndex(0)
        If ftStatus <> FTDI.FT_STATUS.FT_OK Then
            ' Wait for a key press
            Console.WriteLine("Failed to open device (error " + ftStatus.ToString() + ")")
            Console.ReadKey()
            Return
        End If

        ftStatus = myFtdiDevice.SetBaudRate(921600)
        If ftStatus <> FTDI.FT_STATUS.FT_OK Then
            ' Wait for a key press
            Console.WriteLine("Failed to set baudrate (error " + ftStatus.ToString() + ")")
            Console.ReadKey()
            Return
        End If

        ftStatus = myFtdiDevice.SetBitMode(255, 4)
        If ftStatus <> FTDI.FT_STATUS.FT_OK Then
            ' Wait for a key press
            Console.WriteLine("Failed to set bit mode (error " + ftStatus.ToString() + ")")
            Console.ReadKey()
            Return
        End If

        sentBytes(0) = 0
    End Sub



Here is the full converted code in VB.NET
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports FTD2XX_NET

Partial Public Class Form1
    Inherits Form
    Public myFtdiDevice As New FTDI()
    Private ftStatus As FTDI.FT_STATUS
    Private sentBytes As Byte() = New Byte(1) {}
    Private receivedBytes As UInteger

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub button1_Click_1(ByVal sender As Object, ByVal e As EventArgs)

        ftStatus = myFtdiDevice.OpenByIndex(0)
        If ftStatus <> FTDI.FT_STATUS.FT_OK Then
            ' Wait for a key press
            Console.WriteLine("Failed to open device (error " + ftStatus.ToString() + ")")
            Console.ReadKey()
            Return
        End If

        ftStatus = myFtdiDevice.SetBaudRate(921600)
        If ftStatus <> FTDI.FT_STATUS.FT_OK Then
            ' Wait for a key press
            Console.WriteLine("Failed to set baudrate (error " + ftStatus.ToString() + ")")
            Console.ReadKey()
            Return
        End If

        ftStatus = myFtdiDevice.SetBitMode(255, 4)
        If ftStatus <> FTDI.FT_STATUS.FT_OK Then
            ' Wait for a key press
            Console.WriteLine("Failed to set bit mode (error " + ftStatus.ToString() + ")")
            Console.ReadKey()
            Return
        End If

        sentBytes(0) = 0
    End Sub

    Private Sub checkBox1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        If checkBox1.Checked Then
            sentBytes(0) = CByte(sentBytes(0) Or 1)
        Else
            sentBytes(0) = CByte(sentBytes(0) And 254)
        End If
        myFtdiDevice.Write(sentBytes, 1, receivedBytes)
    End Sub

    Private Sub checkBox2_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        If checkBox2.Checked Then
            sentBytes(0) = CByte(sentBytes(0) Or 2)
        Else
            sentBytes(0) = CByte(sentBytes(0) And 253)
        End If
        myFtdiDevice.Write(sentBytes, 1, receivedBytes)
    End Sub

    Private Sub checkBox3_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        If checkBox3.Checked Then
            sentBytes(0) = CByte(sentBytes(0) Or 4)
        Else
            sentBytes(0) = CByte(sentBytes(0) And 251)
        End If
        myFtdiDevice.Write(sentBytes, 1, receivedBytes)
    End Sub

    Private Sub checkBox4_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        If checkBox4.Checked Then
            sentBytes(0) = CByte(sentBytes(0) Or 8)
        Else
            sentBytes(0) = CByte(sentBytes(0) And 247)
        End If
        myFtdiDevice.Write(sentBytes, 1, receivedBytes)
    End Sub

    Private Sub checkBox5_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        If checkBox5.Checked Then
            sentBytes(0) = CByte(sentBytes(0) Or 16)
        Else
            sentBytes(0) = CByte(sentBytes(0) And 239)
        End If
        myFtdiDevice.Write(sentBytes, 1, receivedBytes)
    End Sub

    Private Sub checkBox6_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        If checkBox6.Checked Then
            sentBytes(0) = CByte(sentBytes(0) Or 32)
        Else
            sentBytes(0) = CByte(sentBytes(0) And 223)
        End If
        myFtdiDevice.Write(sentBytes, 1, receivedBytes)
    End Sub

    Private Sub checkBox7_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        If checkBox7.Checked Then
            sentBytes(0) = CByte(sentBytes(0) Or 64)
        Else
            sentBytes(0) = CByte(sentBytes(0) And 191)
        End If
        myFtdiDevice.Write(sentBytes, 1, receivedBytes)
    End Sub

    Private Sub checkBox8_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        If checkBox8.Checked Then
            sentBytes(0) = CByte(sentBytes(0) Or 128)
        Else
            sentBytes(0) = CByte(sentBytes(0) And 127)
        End If
        myFtdiDevice.Write(sentBytes, 1, receivedBytes)
    End Sub
End Class

I just figured i would see if that helps you or not. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#11 saint1000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 22-January 13

Re: usb relay board will not respond in vb.net unless test software is use

Posted 15 March 2013 - 04:44 PM

thanks i tried copying the button1 code in to my project and it still doesnt work. i figured i was missing somthing in the conversion
Was This Post Helpful? 0
  • +
  • -

#12 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: usb relay board will not respond in vb.net unless test software is use

Posted 15 March 2013 - 04:54 PM

View Postsaint1000, on 15 March 2013 - 03:38 PM, said:


That's the programming guide for the interface, which is a USB adapter. It will adapt USB to D2XX (a proprietary FTDI interface spec), or to a serial port. What we need to see is the documentation for the relay board itself.

What is the brand name and model of the relay board?
Was This Post Helpful? 0
  • +
  • -

#13 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: usb relay board will not respond in vb.net unless test software is use

Posted 15 March 2013 - 05:48 PM

If that's a Denkovi relay board, there is good news. I was looking at the 8-relay unit, and found a VB6 example project at This Page. It also has a C# .Net project. Either of those should be relatively easy to convert.

Oh! On their 4-relay page, they even have a VB.Net example projectthat shows how to set the relays.

Turns out that it uses the Serial port, but in "bit-bang" mode, which means that the byte is sent serially, thenloaded into a register in the interface, and sent out to the board. You basically send it a byte, and each bit controls one relay.
Was This Post Helpful? 0
  • +
  • -

#14 saint1000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 22-January 13

Re: usb relay board will not respond in vb.net unless test software is use

Posted 16 March 2013 - 06:32 AM

it is a denkovi 12v 8 relay board. the c# example on their web page is the one i posted above. i thought this code was what put the board in bit bang mode


040
        ftStatus = myFtdiDevice.SetBitMode(255, 4)



Was This Post Helpful? 0
  • +
  • -

#15 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: usb relay board will not respond in vb.net unless test software is use

Posted 16 March 2013 - 03:03 PM

View Postsaint1000, on 16 March 2013 - 07:32 AM, said:

it is a denkovi 12v 8 relay board. the c# example on their web page is the one i posted above. i thought this code was what put the board in bit bang mode
        ftStatus = myFtdiDevice.SetBitMode(255, 4)


Yes, tat's exactly what sets the BitBang mode. However, the VB.Net sample project at the bottom of This Page does the same thing, and the beauty of it is that it does it in VB.Net.

The 4-relay sample will work for you, though as-is, it will only control 4 of your relays. To control them all, the only thing you have to do is to add the missing bits. The 4 relays on that board are set with the bit values 2, 8, 32, and 128. The extra bits for the other 4 relays will be bit values 1, 4, 16, and 64.

Please feel free to hit us with any more questions. I have always been interested in external hardware control, and they look like nice boards, so I'd appreciate hearing how it all works out for you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1