Results 1 to 8 of 8

Thread: MSComm woes

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    5

    MSComm woes

    Hi,

    I'm trying to write some code to open a serial port and send data to a motorised x-y stage.

    i'm trying to do this in a language called ADL (specific to varian scientific instruments) which is based on Sax Basic, which is VB compatible.

    I have referenced the MSCommLib .ocx and am using the following code to open the port ready for various bits of data to be sent

    Code:
    Dim OldHandShake As Integer
    Dim OldSettings As String
    Dim OldMode As Integer
    Dim OldInputLen As Integer
    
    
    
    Sub PortControl
     
            Dim myCom As MSCommLib.MSComm
              
            With myCom
              
              .CommPort = 1
              .PortOpen = True
              'save current port settings
              OldHandShake = .Handshaking
              OldSettings = .Settings
              OldMode = .InputMode
              OldInputLen = .InputLen
              'set the new settings
              .Handshaking = comNone
              .Settings = "9600,N,8,1"
              .InputMode = comInputModeText
              .InputLen = 1
              .RThreshold = 1
              
            End With
         
    End Sub
    
         
    
    Sub Main
    	PortControl
    End Sub
    when i run the script i get the following error

    Error in file C:\Documents...etc..... at line 15 position 0 (10094) ActiveX Automation: Object var is "Nothing". ......

    and it highlights the line .CommPort = 1

    Sadly im unsure what this actually means. I'm a chemist, not a programmer, thats been thrown in the deepend without a float. I'm learning fast but this has me a bit a stumped.

    Any help would be greatly appreciated. Thanks

    EDIT: i should also mention that this script has no form/frontend, it is a macro that runs within the Varian Software
    Last edited by renzokuken; Jun 18th, 2007 at 09:05 AM.

  2. #2
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: MSComm woes

    Try this.

    Replace "Dim myCom As MSCommLib.MSComm"

    with

    Dim myCom As Object
    Set myCom = CreateObject("MSCommLib.MSComm")

    In normal VB you would have a MSComm sitting on your form. In your case you are having to create a new one.
    This world is not my home. I'm just passing through.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    5

    Re: MSComm woes

    wow, thanks trisuglow, that worked a charm

    i sent a command to my stage and it did what it was supposed to......amazing....thanks

    heres the code now (including a command to reset both units to their "home" position

    Code:
    Dim OldHandShake As Integer
    Dim OldSettings As String
    Dim OldMode As Integer
    Dim OldInputLen As Integer
    
    
    
    
    Sub PortControl
     
            Dim myCom As Object
            Set myCom = CreateObject("MSCommLib.MSComm")
            
            With myCom
              
              .CommPort = 1
              .PortOpen = True
              'save current port settings
              OldHandShake = .Handshaking
              OldSettings = .Settings
              OldMode = .InputMode
              OldInputLen = .InputLen
              'set the new settings
              .Handshaking = comNone
              .Settings = "9600,N,8,1"
              .InputMode = comInputModeText
              .InputLen = 1
              .RThreshold = 1
              Wait 1
              .Output = Chr$(0) + Chr$(20) + Chr$(0) + Chr$(0) + Chr$(0) + Chr$(0)
            End With
         
    End Sub
    
    
    Sub Main
    	PortControl
    
    End Sub
    now i want to add a set of predefined instructions to move units to set positions, how does that effect the two lines you advised me to add. they are not global variables or anything and restricted to the "Portcontrol" sub if i understand correctly.

    so would the following work

    Code:
    Dim OldHandShake As Integer
    Dim OldSettings As String
    Dim OldMode As Integer
    Dim OldInputLen As Integer
    
    
    
    
    Sub PortControl
     
            Dim myCom As Object
            Set myCom = CreateObject("MSCommLib.MSComm")
            
            With myCom
              
              .CommPort = 1
              .PortOpen = True
              'save current port settings
              OldHandShake = .Handshaking
              OldSettings = .Settings
              OldMode = .InputMode
              OldInputLen = .InputLen
              'set the new settings
              .Handshaking = comNone
              .Settings = "9600,N,8,1"
              .InputMode = comInputModeText
              .InputLen = 1
              .RThreshold = 1
              Wait 1
              .Output = Chr$(0) + Chr$(2) + Chr$(0) + Chr$(0) + Chr$(0) + Chr$(0)     'renumber all units
            End With
         
    End Sub
    
    Sub PositionOne
             .Ouput = Chr$(0) + Chr$(20) + Chr$(85) + Chr$(23) + Chr$(7) + Chr$(0)
    
    End Sub
    
    Sub PositionTwo
         .Output = Chr$(0) + Chr$(20) + Chr$(45) + Chr$(3) + Chr$(34) + Chr$(0)
    
    End Sub
    
    
    
    Sub Main
    	PortControl
            PositionOne
            PositionTwo
            ......etc
    End Sub

  4. #4
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: MSComm woes

    No.

    You'll need to put Dim myCom at the top of your file (along with Dim OldInputLen and the others). Leave the CreateObject line where it is though.

    In your PositionOne sub you will need to add "With myCom" and "End With" (also elsewhere).
    This world is not my home. I'm just passing through.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2007
    Posts
    5

    Re: MSComm woes

    yup, thats nailed it, thanks for your help

    in 2 minutes you've helped with weeks of frustration.

  6. #6
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: MSComm woes

    Great!

    Glad to be of help. I wish I was programming robots instead of databases...
    This world is not my home. I'm just passing through.

  7. #7
    New Member
    Join Date
    Jun 2007
    Posts
    6

    Re: MSComm woes

    hi
    i also need your help regarding to my probs.

    I have to transfer a text file through serial port (COM1 ) to other connected end system.

    Give me some idea about this

  8. #8
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: MSComm woes

    Hi,

    I've been on holiday so I'm only just seeing this now. Did you start a new thread and get an answer from somebody else?

    The simplest way (in principle) is to open a DOS window and type COPY file.txt COM1:

    Also in the DOS window, use MODE COM1: to get/set the port parameters.
    This world is not my home. I'm just passing through.

Posting Permissions

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



Click Here to Expand Forum to Full Width