Automatic VB6 to pic serial connection


Closed Thread
Results 1 to 14 of 14
  1. #1
    Join Date
    Aug 2005
    Posts
    57

    Default Automatic VB6 to pic serial connection

    The attached project files demonstrate how to make an automatic serial connection between a VB6 interface and a pic. This relieves the user from having to know what port the pic is connected to and then manually connect . This is very useful when using a USB to serial converter, as the virtual port numbers can be from com 5 on up and change depending on the USB port being used. The VB6 application functions as follows: As the application loads, the program attempts to open port 1.If this fails, the program errors, leaves the routine, increments the port number and attempts to open the next port. If the opening is successful, the program transmits a character that the pic will recognize, and in turn the pic will begin sending data. The VB6 program will wait for a period of time for data to arrive, if successful the application will finish loading and data will be displayed. If unsuccessful, the program will try all the ports until exhausted, displaying no connection. When the application is shut down, the program transmits a character that the pic will recognize, and in turn the pic will stop sending data. The pic used was a 16F873A. At the very least its another example for serial communication between a pc and a pic. Some of the code was found on VBnet.com and modified.
    Attached Files Attached Files

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,796


    Did you find this post helpful? Yes | No

    Default

    Hi Arnie,

    nice example! Thanks for posting.

    Ioannis

  3. #3
    T.Jackson's Avatar
    T.Jackson Guest


    Did you find this post helpful? Yes | No

    Default Poor'ol slow & obsolete VB6

    Suggest using timegettime or gettickcount API's for the delay. This approach will ensure that it runs at the same speed on every machine. Plus, it will also make the process much more efficient, with no dead time - other processes can be processed in the background. A for next loop with an irritation of 10,000 is a big ask for poor'ol slow & obsolete VB6.

  4. #4
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,796


    Did you find this post helpful? Yes | No

    Default

    Hi Trent. How do you use the API's? I am really new at the VB things and appreciate an example if you can.

    Thanks,
    Ioannis

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    The usual way is to call it in a module and use the function in your code.

    Add a module to your project and past the following ones...
    Code:
            Option Explicit
    
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    
    Public Function GetTicks() As Long
            GetTicks = GetTickCount()
            End Function
    In your Form(s), you just need to call it..
    Code:
            Option Explicit
            Dim TickValue As Long
    
    Private Sub Form_Load()
            Command1.Caption = "Start"
            Command2.Caption = "Stop"
            Text1.Text = ""
            End Sub
    
    Private Sub Command1_Click()
            TickValue = GetTicks
            End Sub
    
    Private Sub Command2_Click()
            TickValue = GetTicks - TickValue
            Text1.Text = TickValue
            End Sub
    HTH
    Last edited by mister_e; - 7th January 2008 at 22:33. Reason: Long instead of double :o)
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    Windows supports 256 COM ports. While few PCs will have a large number of ports, with the proliferation of USB/serial adapters and ethernet/serial adapters, there may be several ports and they usually do not use contiguous numbers. Testing all 256 possibilities can be quite time consuming. I think it's best to first enumerate the ports, eliminate those that are infrared, modems, etc. and then test the remainder. This is much faster and more reliable. Do a Google search using "VB enumerate COM ports" and you will find lots of example code. Many examples check the registry but this is not an infallible method as users can edit the registry.

    Here's an example written in PureBasic which uses the Windows API function EnumPorts. The backslash is PB's equivalent to VB's dot for referencing structure members (VB Type).
    Code:
    ;--------------------------------------------------------
    ;
    ; SerialPorts.pbi
    ;
    ;--------------------------------------------------------
    
    Structure API_PORT_INFO_2
      PortName.s
      MonitorName.s
      Description.s
      PortType.l
      Reserved.l
    EndStructure
    
    Global NewList Portinfos.API_PORT_INFO_2()
    Global ports.s
    Global NumPorts.l
    
    Procedure.l GetAvailablePorts(ServerName.s="")
      Protected res.l, pcbNeeded.l,pcReturned .l,i.l,ret.l
      Protected *TempBuff,*pName,*strPortinfos.PORT_INFO_2
     
      If ServerName=""
        *pName=0
      Else
        *pName=@ServerName
      EndIf
      
      ;Get the number of bytes needed To contain the Data returned by the API call
      ret = EnumPorts_(*pName, 2, 0, 0, @pcbNeeded, @pcReturned)
      If pcbNeeded
        *TempBuff = AllocateMemory(pcbNeeded) ; Allocate the Buffer
        ret = EnumPorts_(*pName, 2, *TempBuff, pcbNeeded, @pcbNeeded, @pcReturned)
        If ret
          For i = 0 To pcReturned - 1
            ;set structure over the memory area
            *strPortinfos.PORT_INFO_2=*TempBuff+(i*SizeOf(PORT_INFO_2))
            AddElement(Portinfos())
            Portinfos()\PortName=PeekS(*strPortinfos\pPortName)
            Portinfos()\MonitorName=PeekS(*strPortinfos\pMonitorName)
            Portinfos()\Description=PeekS(*strPortinfos\pDescription)
            Portinfos()\PortType=*strPortinfos\fPortType
            If Not FindString(LCase(Portinfos()\Description),"infrared",1)
              If Not FindString(LCase(Portinfos()\Description),"modem",1)
                If Not FindString(ports,Portinfos()\PortName,1)
                  If Left(Portinfos()\PortName,3)="COM" And FindString(Portinfos()\Description," Port",1)
                    ;test each here by trying to open it 
                    ports+Portinfos()\PortName
                    numPorts+1
                  EndIf  
                EndIf
              EndIf
            EndIf
          Next
        EndIf
        ;Free the Heap Space allocated for the Buffer
        If *TempBuff
          FreeMemory(*TempBuff)
        EndIf
      EndIf
     
      ProcedureReturn pcReturned
    EndProcedure
    Here's a link to a VB example. http://vbnet.mvps.org/index.html?cod.../enumports.htm

    EnumPorts returns all of the printer ports on the PC. The COM ports are a subset of the printer ports.
    Last edited by dhouston; - 7th January 2008 at 23:18. Reason: added VB link

  7. #7
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Yup, that's one method, i already did it but, using CreateFile.. but some says it doesn't work with Me/2000

    However, there's no good reason to stick with VB6. All the above is 90% standard in .NET... the problem is just to move on .NET syntax
    Last edited by mister_e; - 7th January 2008 at 22:53.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  8. #8
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,796


    Did you find this post helpful? Yes | No

    Default

    Thanks all for the tips.

    Steve: About .NET and newer, one must have a super-duper machine to install the new IDE...

    Tried the 2005 and waited about 20 min to install!

    Then to open it about 40-50 seconds...

    Until I can have a quad core PC, VB6 is OK for me!

    Ioannis

  9. #9
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    yeah it sucks, even worst with 2008. You can speed it a little bit if you disable all web features in.. but, it's still really slow on most of my machines here. BUt once the code is compiled and works, that's nice.

    The major point i guess is more about Vista compatibility. Not sure enough how bad/good most VB6 program will behave with. Still good in XP in most case. .NET is not bad, it's just a bit had to move on and to understand the whole new syntax/traps.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  10. #10
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,796


    Did you find this post helpful? Yes | No

    Default

    I think that the new IDE can accept the old DLL's from the VB6 for example and use the old controls too. A friend of mine said he just open the libraries and workd ok.

    I cannot test this as I got rid off the monster!

    Also said that the old syntax is acceptable too (may be with some trick though).

    By the way, Steve, is there a way to access a bit in a variable array in PBP? I meen for example like this: array[7].4=1

    I know the link of Melanies that is accessing as an array of bits the whole byte array, but is not easy to remember the 267th bit!

    I overcome this by reloading the array byte to a variable and then back to array again.

    Ioannis

  11. #11
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Ioannis View Post
    is there a way to access a bit in a variable array in PBP? I meen for example like this: array[7].4=1
    If you're really using constants as shown, then you can alias the location and access the individual bit
    Code:
    TheByte  VAR array(7)
    
    TheByte.4 = 1
    But you probably want variables for both the byte and bit index's ...
    Code:
    ByteIDX  VAR BYTE
    BitIDX   VAR BYTE
    
    ByteIDX = 7
    BitIDX = 4
    
    array.0((ByteIDX<<3)+BitIDX) = 1
    DT

  12. #12
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,796


    Did you find this post helpful? Yes | No

    Default

    Hey, look ma, how we can things more complicate!!!

    OK, thanks for the tips Darrel. The second is clever, but I suppose it is cpu power hungry.

    I 'll take the first, thank you!

    Ioannis

  13. #13
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Ioannis View Post
    I know the link of Melanies that is accessing as an array of bits the whole byte array, but is not easy to remember the 267th bit!
    It's simple enough to keep track of bytes & bits using the Division (/) and Modulus (//) operators.

    267 / 8 = 33
    267 // 8 = 3

    33 * 8 + 3 = 267

  14. #14
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,796


    Did you find this post helpful? Yes | No

    Default

    Thanks for the tip Dave.

    Ioannis

Similar Threads

  1. Midi, Interrupts and Pic better than SX28?
    By Lajko in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 27th September 2008, 01:26
  2. PIC to PIC "wired" serial one-way communication - SERIN2/SEROUT2
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 14th April 2008, 21:02
  3. PIC to serial with visual basic
    By mbw123 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 31st March 2007, 17:06
  4. PIC to PIC serial resistor?
    By RYTECH in forum Serial
    Replies: 0
    Last Post: - 5th September 2006, 16:46
  5. serial comm from Pic to STAMP
    By d1camero in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 5th April 2004, 00:58

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts