Results 1 to 8 of 8

Thread: serial comm vb6 migration from vb 2012 (.net)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    104

    serial comm vb6 migration from vb 2012 (.net)

    guy,
    The Serial with mscomm in vb6 for me works fully perfect. But as switching to win10 and also as switching to vb.net, i need to convert it... how?? let's say it:

    Here is my old vb 6 code:

    Code:
    Private Sub cmdSend_Click()
        MSComm1.Output = txtInput.Text + vbLf
        txtInput.Text = ""
    End Sub
    
    Private Sub cmndClear_Click()
        txtInput.Text = ""
        txtInput.SetFocus
    End Sub
    
    Private Sub cmndCommConnnect_Click()
        MSComm1.CommPort = txtCom.Text
        MSComm1.Settings = "9600,n,8,1"
        MSComm1.PortOpen = True
        MSComm1.RThreshold = 1
        cmndDisconnCom.Visible = True
        cmndCommConnnect.Visible = False
        txtOutput.Text = ""
    End Sub
    
    Private Sub cmndDisconnCom_Click()
        MSComm1.PortOpen = False
        cmndDisconnCom.Visible = False
        cmndCommConnnect.Visible = True
    End Sub
    
    Private Sub Form_Load()
        cmndDisconnCom.Visible = False
    End Sub
    
    Private Sub MSComm1_OnComm()
        Dim InBuffer As String
        InBuffer = MSComm1.Input
        If MSComm1.CommEvent = comEvReceive Then
            txtOutput.Text = txtOutput.Text & InBuffer        ' Put received data in Text2
            txtOutput.SelStart = Len(txtOutput.Text)
        End If
    End Sub
    
    Private Sub txtInput_KeyPress(KeyAscii As Integer)
        If KeyAscii = 13 Then                   ' If user presses Enter don't send it.
            'Exit Sub
            MSComm1.Output = txtInput.Text + vbLf
            txtInput.Text = ""
        Else
            'MSComm1.Output = Chr(KeyAscii)   ' Output each character when user presses the key.
        End If
    End Sub
    Now,
    Code:
    Private Sub MSComm1_OnComm()
        Dim InBuffer As String
        InBuffer = MSComm1.Input
        If MSComm1.CommEvent = comEvReceive Then
            txtOutput.Text = txtOutput.Text & InBuffer        ' Put received data in Text2
            txtOutput.SelStart = Len(txtOutput.Text)
        End If
    End Sub
    
    Private Sub txtInput_KeyPress(KeyAscii As Integer)
        If KeyAscii = 13 Then                   ' If user presses Enter don't send it.
            'Exit Sub
            MSComm1.Output = txtInput.Text + vbLf
            txtInput.Text = ""
        Else
            'MSComm1.Output = Chr(KeyAscii)   ' Output each character when user presses the key.
        End If
    End Sub
    is the part where i'm stuck. The same application that I need. Just with .net version. I made he code like below:

    Code:
    Imports System.IO.Ports
    
    Public Class frmMain
        Public mySerialPort As New SerialPort
        Dim port As String
    
        Private Sub Form1_Load() Handles MyBase.Load
            btnDisconnect.Visible = False
            ' Show all available COM ports.
            For Each sp As String In My.Computer.Ports.SerialPortNames
                cmbPort.Items.Add(sp)
            Next
            AddHandler mySerialPort.DataReceived, AddressOf SerialDataReceivedEventHandler
        End Sub
    
        Private Sub btnExit_Click() Handles btnExit.Click
            End
        End Sub
    
        Private Sub btnConnect_Click() Handles btnConnect.Click
            If (mySerialPort.IsOpen) Then
                mySerialPort.Close()
            End If
            port = cmbPort.Text
            txtOutput.Text = port
            Call CommPortSetup()
            'My.Computer.Ports.OpenSerialPort(port)
            Try
                mySerialPort.Open()
                btnDisconnect.Visible = True
                btnConnect.Visible = False
            Catch ex As Exception
                mySerialPort.Close()
                cmbPort.ResetText()
                btnDisconnect.Visible = False
                btnConnect.Visible = True
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        Private Sub btnClear_Click() Handles btnClear.Click
            txtInput.Text = ""
            txtInput.Focus()
        End Sub
    
        Private Sub btnDisconnect_Click() Handles btnDisconnect.Click
            Try
                mySerialPort.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            cmbPort.ResetText()
            btnDisconnect.Visible = False
            btnConnect.Visible = True
        End Sub
    
        Private Sub CommPortSetup()
            With mySerialPort
                .PortName = port
                .BaudRate = 9600
                .DataBits = 8
                .Parity = Parity.None
                .StopBits = StopBits.One
                .Handshake = Handshake.None
            End With
        End Sub
    
        Private Sub btnSend_Click() Handles btnSend.Click
            Dim data As String
            txtOutput.Text = "$ " + txtInput.Text + vbCrLf
            data = txtInput.Text + vbLf
            mySerialPort.Write(data)
            btnClear.Focus()
        End Sub
    
        Private Sub SerialDataReceivedEventHandler(sender As Object, e As SerialDataReceivedEventArgs)
            Dim sport As SerialPort = CType(sender, SerialPort)
            Dim indata As String = sport.ReadExisting()
            txtOutput.Text = txtOutput.Text + indata
            txtOutput.SelectionStart = Len(txtOutput.Text)
        End Sub
    
    End Class
    and lo luck. I think I'm sending to serial, but not receiving. Any guide?


    Basically I need a terminal software for one of my arduino project. VB.NET is a new for me... though I started to like, but someplaces it's harder than VBB6.

    Mishu~

  2. #2

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    104

    Re: serial comm vb6 migration from vb 2012 (.net)

    Hi,
    After updating the code in to this:

    Code:
    Imports System.IO.Ports
    
    Public Class frmMain
        Public mySerialPort As New SerialPort
        Dim comPort As String
        Dim receivedData As String = ""
    
        Private Sub Form1_Load() Handles MyBase.Load
            Timer1.Enabled = False
            btnDisconnect.Visible = False
            comPort = ""
            ' Show all available COM ports.
            For Each sp As String In My.Computer.Ports.SerialPortNames
                cmbPort.Items.Add(sp)
            Next
    
        End Sub
    
        Private Sub cmbPort_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbPort.SelectedIndexChanged
            If (cmbPort.SelectedItem <> "") Then
                comPort = cmbPort.SelectedItem
            End If
        End Sub
    
        Private Sub btnExit_Click() Handles btnExit.Click
            End
        End Sub
    
        Private Sub btnClear_Click() Handles btnClear.Click
            txtInput.Text = ""
            txtInput.Focus()
        End Sub
    
        Private Sub btnSend_Click() Handles btnSend.Click
            Dim data As String
            data = txtInput.Text + vbLf
            mySerialPort.Write(data)
            btnClear.Focus()
        End Sub
    
        Private Sub btnConnect_Click() Handles btnConnect.Click
            If (mySerialPort.IsOpen) Then
                mySerialPort.Close()
            End If
            comPort = cmbPort.Text
            txtOutput.Text = comPort
            Call CommPortSetup()
            'My.Computer.Ports.OpenSerialPort(port)
            Try
                mySerialPort.Open()
                btnDisconnect.Visible = True
                btnConnect.Visible = False
                Timer1.Enabled = True
            Catch ex As Exception
                mySerialPort.Close()
                Timer1.Enabled = False
                cmbPort.ResetText()
                btnDisconnect.Visible = False
                btnConnect.Visible = True
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        Private Sub btnDisconnect_Click() Handles btnDisconnect.Click
            Try
                mySerialPort.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            cmbPort.ResetText()
            btnDisconnect.Visible = False
            btnConnect.Visible = True
            Timer1.Enabled = False
        End Sub
    
        Private Sub CommPortSetup()
            With mySerialPort
                .PortName = comPort
                .BaudRate = 9600
                .DataBits = 8
                .Parity = Parity.None
                .StopBits = StopBits.One
                .Handshake = Handshake.None
            End With
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            receivedData = ReceiveSerialData()
            txtOutput.Text &= receivedData
        End Sub
    
        Function ReceiveSerialData() As String
            Dim Incoming As String
            Try
                Incoming = mySerialPort.ReadExisting()
                If Incoming Is Nothing Then
                    Return "nothing" & vbCrLf
                Else
                    Return Incoming
                End If
            Catch ex As TimeoutException
                Return "Error: Serial Port read timed out."
            End Try
        End Function
    
    End Class
    Noow I can see some readouts... but a bit different way... it's not putting the CRLF while output...

    User Name:adminPassword:*****

    authentication failed


    press ENTER key to retry authentication

    it should show

    User Name:admin

    Password:*****

    authentication failed


    press ENTER key to retry authentication

    Ideas?????

  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: serial comm vb6 migration from vb 2012 (.net)

    Quote Originally Posted by aq_mishu View Post
    Hi,
    Noow I can see some readouts... but a bit different way... it's not putting the CRLF while output...

    [...]

    Ideas?????
    You didn't tell it to send CRLF. You told it to send LF:
    Code:
    data = txtInput.Text + vbLf
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    104

    Re: serial comm vb6 migration from vb 2012 (.net)

    Quote Originally Posted by Sitten Spynne View Post
    You didn't tell it to send CRLF. You told it to send LF:
    Code:
    data = txtInput.Text + vbLf
    Actually I was working on a different one... it was taken from cisco but my rduino sends a bit different way may be...

    It was a strictly customized terminal for a specific arduino project. Anyway, after a lot of digging, solved most of the part. Except that, I wish to press "enter" and then the data will be sent, instead of clicking "send" button. As usual, send by Enter... I will update my code soon here, and then you guys can get the better hint.

  5. #5
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: serial comm vb6 migration from vb 2012 (.net)

    Yes, this time when you post, please post the code that is actually causing the problem. It's not very nice to post different code, then have someone else waste time pointing out a problem, then say, "Well, actually... that wasn't the right code."
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    104

    Re: serial comm vb6 migration from vb 2012 (.net)

    Quote Originally Posted by Sitten Spynne View Post
    Yes, this time when you post, please post the code that is actually causing the problem. It's not very nice to post different code, then have someone else waste time pointing out a problem, then say, "Well, actually... that wasn't the right code."
    uhu!! The code I posted was right... and made a lot of corrections as needed after lots of research... What i said was is:

    This code was supposed to work for a specific job and I tested it on another item (i wanted to use with arduino but tested it on a cisco switch). For arduino i need a terminal, but for cisco there is putty and that will be enough. [yep there is a reason why i need terminal for arduino]

    So no time waste of anyone... it was the same code that i was working on... and here is the finally updated code:
    Code:
    Imports System.IO.Ports
    
    Public Class frmMain
        Public mySerialPort As New SerialPort
        Dim comPort As String
        Dim receivedData As String = ""
    
        Private Sub frmMain_Load() Handles MyBase.Load
            Timer1.Enabled = False
            btnDisconnect.Visible = False
            comPort = ""
            ' Show all available COM ports.
            For Each sp As String In My.Computer.Ports.SerialPortNames
                cmbPort.Items.Add(sp)
            Next
    
        End Sub
    
        Private Sub cmbPort_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbPort.SelectedIndexChanged
            If (cmbPort.SelectedItem <> "") Then
                comPort = cmbPort.SelectedItem
            End If
        End Sub
    
        Private Sub btnExit_Click() Handles btnExit.Click
            End
        End Sub
    
        Private Sub btnClear_Click() Handles btnClear.Click
            txtInput.Text = ""
            txtInput.Focus()
        End Sub
    
        Private Sub btnSend_Click() Handles btnSend.Click
            Dim data As String
            txtOutput.AppendText("[command sent] " + txtInput.Text + vbCrLf)
            data = txtInput.Text + vbLf
            mySerialPort.Write(data)
            txtInput.Text = ""
            txtInput.Focus()
        End Sub
    
        Private Sub txtOutput_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtOutput.TextChanged
            Dim ln As Integer = txtOutput.Text.Length
            txtOutput.SelectionStart = ln
            txtOutput.ScrollToCaret()
        End Sub
    
        Private Sub btnConnect_Click() Handles btnConnect.Click
            If (mySerialPort.IsOpen) Then
                mySerialPort.Close()
            End If
            comPort = cmbPort.Text
            Call CommPortSetup()
            'My.Computer.Ports.OpenSerialPort(port)
            Try
                mySerialPort.Open()
                btnDisconnect.Visible = True
                btnConnect.Visible = False
                txtOutput.Text = ""
                Timer1.Enabled = True
            Catch ex As Exception
                mySerialPort.Close()
                Timer1.Enabled = False
                cmbPort.ResetText()
                btnDisconnect.Visible = False
                btnConnect.Visible = True
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        Private Sub btnDisconnect_Click() Handles btnDisconnect.Click
            Try
                mySerialPort.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            cmbPort.ResetText()
            btnDisconnect.Visible = False
            btnConnect.Visible = True
            Timer1.Enabled = False
        End Sub
    
        Private Sub CommPortSetup()
            With mySerialPort
                .PortName = comPort
                .BaudRate = 9600
                .DataBits = 8
                .Parity = Parity.None
                .StopBits = StopBits.One
                .Handshake = Handshake.None
            End With
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            receivedData = ReceiveSerialData()
            txtOutput.Text &= receivedData
        End Sub
    
        Function ReceiveSerialData() As String
            Dim Incoming As String
            Try
                Incoming = mySerialPort.ReadExisting()
                If Incoming Is Nothing Then
                    Return "nothing" & vbCrLf
                Else
                    Return Incoming
                End If
            Catch ex As TimeoutException
                Return "Error: Serial Port read timed out."
            End Try
        End Function
    
    End Class
    and now lastly all i need is when i press enter in txtInput, it will work for the same way of "Send" button.

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: serial comm vb6 migration from vb 2012 (.net)

    I would normally put the code that is in the Send button in its own sub and call the sub from the send button.
    Then if I needed to "send" from other places, I would call the same sub the Send button calls.
    Check for the Enter key "Keys.Enter" in the keypressed event of txtInput and call the Send routine. You may also want to set the Handled flag if you don't want to pass the Enter key on to be handled as it normally would by the textbox (usually causes an obnoxious beep if not a multi-lined textbox).
    Code:
        Private Sub txtInput_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtInput.KeyPress
            If e.KeyChar = ChrW(Keys.Enter) Then
                SendText()
                e.Handled = True
            End If
        End Sub

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    104

    Thumbs up Re: serial comm vb6 migration from vb 2012 (.net)

    Perfect!! Liked the idea of making a sub routine and then call it in everywhere needed... and Enter Press worked!! Thanks a lot!!

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