CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2015
    Posts
    3

    Help Please - Visual Basic and Com Ports

    Hi
    I am really after someone help as I am starting to pull my hair out.
    I am a Visual Basic newbie and struggling to find a solution to my problem.
    I have recently purchased a few MDrive Stepper Motors. I can control them by going to a terminal emulation program like Putty and typing SL 100000 which in effect starts the motor turning. SL 0 stops the motor.
    I would like to create a Visual Basic application where I have a form with two buttons, Start which has the command SL 100000 associated with it and Stop which has SL 0 associated with it. I am using Com 2 to do this.
    I just cannot get this to work and was hoping that someone may be willing to offer some advice.
    Many thanks
    Barrie

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Help Please - Visual Basic and Com Ports

    Well it would help if we had some idea what you have done and what problem you are having. Just telling us that you are using VB and can't get your program to work could mean a million different things. The better info you provide the more likely someone can help you right away.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Mar 2015
    Posts
    3

    Re: Help Please - Visual Basic and Com Ports

    Hi
    At the minute I have created a form which prompts for a Com port and then a connect button. That all seems to work. I then have a field to enter some text / commands but if I enter SL 100000 it seems to send but does not do anything with the motor. I have monitored the port and it defiantly is sending information but the information seems different than the information within Putty or other terminal emulation programs. The code I am using is as below:-

    Code:
    'Code Starts here ….
    'Import Systems which we are gonna use in our code
    Imports System
    Imports System.ComponentModel
    Imports System.Threading
    Imports System.IO.Ports
    
    'frmMain is the name of our form ….
    'Here starts our main form code …..
    Public Class frmMain
        Dim myPort As Array
        Delegate Sub SetTextCallback(ByVal [text] As String)
    
        'Page Load Code Starts Here….
        Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            myPort = IO.Ports.SerialPort.GetPortNames()
            cmbBaud.Items.Add(9600)
            cmbBaud.Items.Add(19200)
            cmbBaud.Items.Add(38400)
            cmbBaud.Items.Add(57600)
            cmbBaud.Items.Add(115200)
            For i = 0 To UBound(myPort)
                cmbPort.Items.Add(myPort(i))
            Next
            cmbPort.Text = cmbPort.Items.Item(0)
            cmbBaud.Text = cmbBaud.Items.Item(0)
            btnDisconnect.Enabled = False
        End Sub
        'Page Load Code Ends Here ….
    
        'Connect Button Code Starts Here ….
        Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
            SerialPort1.PortName = cmbPort.Text
            SerialPort1.BaudRate = cmbBaud.Text
            SerialPort1.Parity = IO.Ports.Parity.None
            SerialPort1.StopBits = IO.Ports.StopBits.One
            SerialPort1.DataBits = 8
            SerialPort1.Open()
            btnConnect.Enabled = False
            btnDisconnect.Enabled = True
        End Sub
        'Connect Button Code Ends Here ….
    
        'Disconnect Button Code Starts Here ….
        Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
            SerialPort1.Close()
            btnConnect.Enabled = True
            btnDisconnect.Enabled = False
        End Sub
        'Disconnect Button Code Ends Here ….
    
        'Send Button Code Starts Here ….
        Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
            SerialPort1.Write(txtTransmit.Text)
        End Sub
        'Send Button Code Ends Here ….
    
        'Serial Port Receiving Code Starts Here ….
        Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            ReceivedText(SerialPort1.ReadExisting())
        End Sub
        'Serial Port Receiving Code Ends Here ….
    
        'Serial Port Receiving Code(Invoke) Starts Here ….
        Private Sub ReceivedText(ByVal [text] As String)
            If Me.rtbReceived.InvokeRequired Then
                Dim x As New SetTextCallback(AddressOf ReceivedText)
                Me.Invoke(x, New Object() {(text)})
            Else
                Me.rtbReceived.Text &= [text]
            End If
        End Sub
        'Serial Port Receiving Code(Invoke) Ends Here ….
    
        'Com Port Change Warning Code Starts Here ….
        Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged
            If SerialPort1.IsOpen = False Then
                SerialPort1.PortName = cmbPort.Text
            Else
                MsgBox("Valid only if port is Closed", vbCritical)
            End If
        End Sub
        'Com Port Change Warning Code Ends Here ….
    
        'Baud Rate Change Warning Code Starts Here ….
        Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaud.SelectedIndexChanged
            If SerialPort1.IsOpen = False Then
                SerialPort1.BaudRate = cmbBaud.Text
            Else
                MsgBox("Valid only if port is Closed", vbCritical)
            End If
        End Sub
        'Baud Rate Change Warning Code Ends Here ….
    
    End Class
    'Whole Code Ends Here ….
    Last edited by DataMiser; March 29th, 2015 at 04:29 PM.

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Help Please - Visual Basic and Com Ports

    Could it be something simple like it wanting a CR after the data? If using putty [I'm not familiar with that] do you type in the code and hit enter? If so then you probably to to send that from your program as well. Most com routines rely on a terminator character in order for them to know the data is complete. Have you tried using the WriteLine method rather than Write?

    Other than that then port settings could be off. Really not much to it.

    Edit: I also see that you are not actually using VB6 but some version of VB.Net so I will move this to the correct forum for Vb.Net
    Last edited by DataMiser; March 29th, 2015 at 04:32 PM.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Mar 2015
    Posts
    3

    Re: Help Please - Visual Basic and Com Ports

    Thanks for moving to the correct group.

    The GUI Form I have created has a send button, so I enter SL 100000 and click the send button. I have installed a COM port sniffer so when I press send I can see data being sent to the COM port but it does not move the motor and I am not convinced that the data being sent is correct. Putty is just a simple ASCII terminal program, very similar to HyperTerminal. For the MDrive motor I am using they have their own program (SEM Terminal). I can type SL 100000 into the terminal emulator and it works fine but when I try to call it from within VB it just does not seem to work.

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Help Please - Visual Basic and Com Ports

    Did you try sending the line terminator using either writeline or by appending a carriage return to the end of your text i.e. CHR(13) ?

    If you have a sniffer attached then all you have to do is look at what the program which works sends and compare that to what you are sending. If they do not match then that would be a problem.

    These things are usually simple, Baud, parity, data bits, stop bits all have to match and you must send the expected data in the format expected including any start or stop characters.
    Last edited by DataMiser; March 29th, 2015 at 06:27 PM.
    Always use [code][/code] tags when posting code.

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

Featured