Shop OBEX P1 Docs P2 Docs Learn Events
VB6 and the propeller — Parallax Forums

VB6 and the propeller

Rick_HRick_H Posts: 116
edited 2011-10-05 13:49 in Propeller 1
I have a nice tutorial for using VB.net but I am really starting to hat .net, mostly due to the fact that you can't simply create object arrays. Is their a tutorial on using MSComm for the prop about? I haven't been able to find one yet.

Comments

  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-10-03 17:04
    I have interfaced a picaxe to VB6 and it is going to be fairly similar to a propeller. http://www.picaxeforum.co.uk/archive/index.php/t-6224.html shows some vb6 code (scroll down a bit for a larger program).

    Simple code to send out some data on the serial port
    If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
     MSComm1.Settings = "1200,n,8,1"
     MSComm1.CommPort = "2" ' or whatever your com port is
     MSComm1.PortOpen = True
     MSComm1.Output = “Hello_World”
     MSComm1.PortOpen = False
    

    Then you need to collect data coming back the other way. Have you got serial working on the propeller yet?
  • Rick_HRick_H Posts: 116
    edited 2011-10-03 17:25
    Yes I actually have it working with VB.net, but its not using MSComm. I like VB.Nets interface and it works but I am so much more familiar with 6.0 that I would just rather use it instead. I have never had to use MSComm in 10 years lol, now is as good a time as any.
  • Mike GMike G Posts: 2,702
    edited 2011-10-03 18:36
    I have a nice tutorial for using VB.net but I am really starting to hat .net, mostly due to the fact that you can't simply create object arrays.
    It is very easy to create an array of objects in .NET. Take a look at Generics. Generics have been around since the 2.0 framework.

    Here's a basic C# serial port terminal.
    http://www.agaverobotics.com/serialportdemo.zip
  • Rick_HRick_H Posts: 116
    edited 2011-10-03 20:09
    Looked at the list of T thing and I am not sure how adding objects one at a time to a list is easier then just creating an array of objects like in VB6.

    If you can show me no more than 2 lines of code to add 256 text boxes to a list I will live VB.Net, I'm just not convinced.
  • Mike GMike G Posts: 2,702
    edited 2011-10-03 20:30
    Array
    TextBox[] tbArray =  new TextBox[256];
    

    List <TextBox>
    List<TextBox> tbList = new List<TextBox>(256);
    
  • Rick_HRick_H Posts: 116
    edited 2011-10-03 20:41
    ok and at what location are all them text boxes on the form?
  • Mike GMike G Posts: 2,702
    edited 2011-10-03 21:25
    This renders 256 textboxes on a parent Windows form. Rendering textboxes and instantiating an array of textboxes are two different things.
                TextBox[] tbArray = new TextBox[256];
                for (int i = 0; i < 256; i++)
                {
                    tbArray[i] = new TextBox();
                    tbArray[i].Location = new System.Drawing.Point(10, 40 + i * 25);
                    tbArray[i].Name = string.Format("textbox{0}", i);
                    tbArray[i].Size = new System.Drawing.Size(75, 23);
                    tbArray[i].TabIndex = i;
                    this.Controls.Add(tbArray[i]);
                }
    
  • localrogerlocalroger Posts: 3,452
    edited 2011-10-04 07:10
    I have found the mscomm control to be a bit flaky, plus it wasn't included in VB standard edition and it doesn't work unless it's been installed and registered in the registry. I finally got fed up with it and wrote the attached code which controls a serial port direcly via the API.
  • Rick_HRick_H Posts: 116
    edited 2011-10-04 13:51
    Rendering text boxes and instantiating an array of text boxes are two different things.
    Ya that's kind of my point, in VB 6 I just copy and past them and I am creating an array of controls. Basically I just think it would;d be easier for me too write it in VB6 as I fully understand what I can do and can't without too much extra work, Relearning .net just seams unattractive to me at this point.
    I have found the mscomm control to be a bit flaky, plus it wasn't included in VB standard edition and it doesn't work unless it's been installed and registered in the registry. I finally got fed up with it and wrote the attached code which controls a serial port direcly via the API.

    Thanks localroger, I haven't had an issue yet with MSComm yet but I am certainly gonna try your modal out. What issues have you had with MSComm?
  • Rick_HRick_H Posts: 116
    edited 2011-10-04 14:29
    Localroger, I added this function too your bass to load all available serial ports into a combo box for selection. It works, now I'm gonna try and make a routine to detect the prop on the port to identify it. I think I will just do a hand shake unless their is a quicker way.
    Function COMAvailable(COMNum As Integer) As Boolean
        Dim hCOM As Long
        Dim ret As Long
        Dim sec As SECURITY_ATTRIBUTES
    
        '// try to open the COM port
        hCOM = CreateFile("\.\COM" & COMNum & "", 0, FILE_READ_DATA + _
            FILE_WRITE_DATA, sec, OPEN_EXISTING, FILE_READ_ATTRIBUTES, 0)
        If hCOM = -1 Then
            COMAvailable = False
        Else
            COMAvailable = True
            '// close the COM port
            ret = CloseHandle(hCOM)
        End If
    End Function
    

    hear is the form code, it requires a combobox named cboComm
    Public Sub ListComPorts()
        Dim i As Integer
        
        cboComm.Clear
        For i = 1 To 16
            If COMAvailable(i) Then
                cboComm.AddItem i
            End If
        Next
        cboComm.ListIndex = 0
    End Sub
    
  • Rick_HRick_H Posts: 116
    edited 2011-10-04 16:28
    localroger, how do I pass the Settings to openport function? Do I build a string or use the DCB type like this?

    dim SettingsComm as string
    SettingsComm = "baud=1200 parity=N data=8 stop=1 to=off xon=off odsr= off octs=off dtr=off rts=off idsr=off"
    
    Command1.Caption = serapi.OpenPort("Com" & cboComm.Text, SettingsComm)
    
    when I do the above I get "Cant open port 4" so I am not sure what I am doing wrong.
    After I added the "com" & cboComm.Text to the port it just crashes VB.
    dim SettingsComm as DCB
    
    
    SettingsComm.BaudRate = 38400
    SettingsComm.ByteSize = ??
    SettingsComm.DCBlength = ??
    SettingsComm.EofChar = ??
    SettingsComm.ErrorChar = ??
    SettingsComm.EvtChar = ??
    SettingsComm.fBitFields = ??
    SettingsComm.Parity = ??
    SettingsComm.StopBits = ??
    SettingsComm.wReserved = ??
    SettingsComm.wReserved1 = ??
    SettingsComm.XoffChar = ??
    SettingsComm.XoffLim = ??
    SettingsComm.XonChar = ??
    SettingsComm.XonLim = ??
    
    Command1.Caption = serapi.OpenPort("Com" & cboComm.Text, SettingsComm)
    
  • Mike GMike G Posts: 2,702
    edited 2011-10-04 19:40
    Ya that's kind of my point, in VB 6 I just copy and past them and I am creating an array of controls. Basically I just think it would;d be easier for me too write it in VB6 as I fully understand what I can do and can't without too much extra work, Relearning .net just seams unattractive to me at this point.
    Copy and paste is a function of the editor not the language. I'm not trying to sell you on .NET. If you prefer VB6 then that's what you should use.

    Just for educational purposes, this is how to open a serial port in .NET
    System.IO.Ports.SerialPort serial = new System.IO.Ports.SerialPort('COM4', 38400);
    

    The Win API is great and I'm sure localroger has it wired up nicely. I did the same thing years ago for the same reason. I remember it took a little research to understand how the Win API worked.
  • Rick_HRick_H Posts: 116
    edited 2011-10-04 20:14
    I'm not even sure where to put this in my vb.net code
    TextBox[] tbArray = new TextBox[256];
                for (int i = 0; i < 256; i++)
                {
                    tbArray[i] = new TextBox();
                    tbArray[i].Location = new System.Drawing.Point(10, 40 + i * 25);
                    tbArray[i].Name = string.Format("textbox{0}", i);
                    tbArray[i].Size = new System.Drawing.Size(75, 23);
                    tbArray[i].TabIndex = i;
                    this.Controls.Add(tbArray[i]);
                }
    

    it tells me that a declaration is required.

    Error 1 'TextBox' is a type and cannot be used as an expression.
    Error 2 Identifier expected.
    Error 3 'For' must end with a matching 'Next'.
    Error 4 Overload resolution failed because no accessible 'Int' accepts this number of arguments.
    Error 5 ')' expected.
    Error 6 Syntax error.
    Error 7 'tbArray' is not declared. It may be inaccessible due to its protection level.
    Error 8 'i' is not declared. It may be inaccessible due to its protection level.
    Error 9 Method arguments must be enclosed in parentheses.
    Error 10 Character is not valid.
    Error 11 'tbArray' is not declared. It may be inaccessible due to its protection level.
    Error 12 'i' is not declared. It may be inaccessible due to its protection level.
    Error 13 Method arguments must be enclosed in parentheses.
    Error 14 'i' is not declared. It may be inaccessible due to its protection level.
    Error 15 Character is not valid.
    Error 16 'tbArray' is not declared. It may be inaccessible due to its protection level.
    Error 17 'i' is not declared. It may be inaccessible due to its protection level.
    Error 18 Method arguments must be enclosed in parentheses.
    Error 19 'i' is not declared. It may be inaccessible due to its protection level.
    Error 20 Character is not valid.
    Error 21 'tbArray' is not declared. It may be inaccessible due to its protection level.
    Error 22 'i' is not declared. It may be inaccessible due to its protection level.
    Error 23 Method arguments must be enclosed in parentheses.
    Error 24 Character is not valid.
    Error 25 'tbArray' is not declared. It may be inaccessible due to its protection level.
    Error 26 'i' is not declared. It may be inaccessible due to its protection level.
    Error 27 Method arguments must be enclosed in parentheses.
    Error 28 'i' is not declared. It may be inaccessible due to its protection level.
    Error 29 Character is not valid.
    Error 30 'this' is not declared. It may be inaccessible due to its protection level.
    Error 31 'tbArray' is not declared. It may be inaccessible due to its protection level.
    Error 32 Comma, ')', or a valid expression continuation expected.
    Error 33 Syntax error.
  • Mike GMike G Posts: 2,702
    edited 2011-10-04 20:43
    Rick_H, it's a C# code snippet which belongs inside of a method like any code block. This concept is no different from VB6.

    Here's the code behind of a windows form for named form1. The textbox arrays are initialized and rendered in the form constructor. I changed the arrays to 25 items.
    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 System.Collections;
    
    namespace ForumRequest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                RenderListTextBoxes();
                RenderTextBoxes();
            }
    
    
            private void RenderTextBoxes()
            {
                TextBox[] tbArray = new TextBox[25];
                for (int i = 0; i < 25; i++)
                {
                    tbArray[i] = new TextBox();
                    tbArray[i].Location = new System.Drawing.Point(150, 1 + i * 22);
                    tbArray[i].Name = string.Format("textbox{0}", i);
                    tbArray[i].Size = new System.Drawing.Size(75, 23);
                    tbArray[i].TabIndex = i;
                    this.Controls.Add(tbArray[i]);
                }
            }
    
            private void RenderListTextBoxes()
            {
                List<TextBox> tbList = new List<TextBox>(25);
                TextBox tb;
                for (int i = 0; i < 25; i++)
                {
                    tb = new TextBox();
                    tb.Location = new System.Drawing.Point(10, 1 + i * 22);
                    tb.Name = string.Format("textbox{0}", i);
                    tb.Size = new System.Drawing.Size(75, 23);
                    tb.TabIndex = i;
                    this.Controls.Add(tb);
                    tbList.Add(tb);
                }
            }
    
        }
    }
    
  • Rick_HRick_H Posts: 116
    edited 2011-10-04 20:51
    ok well C# is C# not VB, I understand it needs to be in a code block, but I don't know the proper syntax.
  • Mike GMike G Posts: 2,702
    edited 2011-10-04 21:00
    ok well C# is C# not VB, I understand it needs to be in a code block, but I don't know the proper syntax.

    VB.NET version.
    Imports System.Collections.Generic
    
    Public Class Form1
        Public Sub New()
            InitializeComponent()
            RenderTextBoxArray()
        End Sub
    
        Private Sub RenderTextBoxArray()
            Dim tbArray(25) As TextBox
            For i As Integer = 0 To 24
                tbArray(i) = New TextBox
                tbArray(i).Location = New System.Drawing.Point(150, 1 + i * 22)
                tbArray(i).Name = String.Format("textbox{0}", i)
                tbArray(i).Size = New System.Drawing.Size(75, 23)
                tbArray(i).TabIndex = i
                Me.Controls.Add(tbArray(i))
            Next
    
        End Sub
    End Class
    

    If for some reason you decided to use .NET, learn C#. It pays better.
  • Rick_HRick_H Posts: 116
    edited 2011-10-04 21:09
    I don't understand how having run time controls is useful when I have to code each location and such into it. In VB6 I actually have Visual controls that I can play with the design of my form. I need to organize them in tabed pages and into groups with labels and such, run time would take me forever to figure out how to program all their locations and that's a heck of a lot of code compared to just making the array in VB 6.
  • Mike GMike G Posts: 2,702
    edited 2011-10-04 21:29
    You're right, dragging controls around is much easier. Best of luck.
  • Rick_HRick_H Posts: 116
    edited 2011-10-04 21:50
    Thanks for trying to help me. Don't get me wrong I would love too use the new VB, the interface is way nice and a lot of the net framework is so much more powerful. But with just having learned how to write Spin, PASM and actually getting the I2C and Microwire protocols working I'm just really not looking forward to learning a completely new way of writing basic. I think I would loose my mind lmao.
  • localrogerlocalroger Posts: 3,452
    edited 2011-10-05 10:02
    Rick, you pass the settings to OpenPort as a string ("9600,n,8,1"). OpenPort contains the code to build the DCB from this.

    Some problems I've had with MSComm include returning a null string for a result after I've established that characters are waiting, and failing to call OnComm events reliably. These might be API issues. But the biggest nuisance is that MSComm must be registered by an installer, and it can be registered for runtime use only. What spurred me to write serapi was being at a customer site where he had his own copy of VB on the computer. I realized I needed to do some debugging, and when I went to run it in the development system it failed because he had the standard edition of VB which doesn't come with MSComm. The compiled app would run because I'd run the installer, but it had registered MSComm for runtime use only and I couldn't load it into the IDE.

    By eliminating that final dependency, my VB6 executables now run without being installed on any Windows 2000, XP, or 7 box, and on any NT box which has ever had a VB6 app installed (so that the VB6 runtime is registered). This means I can tell my customers to back up their installation by just copying the folder with the executable and all their data; if they drop that folder onto another machine it will run.
  • jblivelyjblively Posts: 6
    edited 2011-10-05 13:49
    So, in keeping with the VB6 and Propeller theme, I have a question about communicating with the Scribbler 2 and the FLUKE board from Visual Basic 6. For testing, I have everything setup in Python and am able to successfully communicate with FLUKE and Scribbler2 that way. Inside of VB6, using the MSComm tool, I can successfully create a connection the comm port and connect to the FLUKE via bluetooth. What my issue is, is syntax. See, I don't know the format of the commands to send to the FLUKE to get it to respond. I know it's receiving my data, as it send backs some characters when I send commands, but I'm not sure the format or what to send.

    In Python, I know it's using Myro to convert a command such as beep(3,1000) to a format that the FLUKE understands. I've been using this page as a reference: http://wiki.roboteducation.org/Scribbler_Wire_Protocol

    It shows the commands and what they should do, but i'm not getting any response from Scribbler.

    Maybe i'm not converting to hex correctly? I've been trying like this:
    MSComm1.Output = Hex$(Me.txt_chr.text) Where txt_chr.text is one of the numeric values from the page above. (IE 33)

    They talk on that page about the data having to be "a constant 9 bytes long." I'm not quite sure how to set it up like that.

    For example, they give this as a example string
    name values notes SOFT_RESET 33 Format: 33 0 0 0 0 0 0 0 0, Notes: Performs a software reset of robot. (turns name broadcast back on)
    But I don't know what to send to the comm port.

    Further down in that document, using the debug in Python, you can see that a simple forward command is represented like this:
    >>> forward(1, 1) _write: m
Sign In or Register to comment.