Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,592 members, 7,820,132 topics. Date: Tuesday, 07 May 2024 at 10:12 AM

Easy Com Port On Vb - Software/Programmer Market - Nairaland

Nairaland Forum / Science/Technology / Programming / Software/Programmer Market / Easy Com Port On Vb (3683 Views)

I Need Proejcts On Vb.net Or C#, Business Applications That Can Bring Money / Need Ebooks On Vb / Vb Programmers Wanted Urgently In A Leading Software Company In Lagos (2) (3) (4)

(1) (Reply) (Go Down)

Easy Com Port On Vb by Andrew3(m): 4:09pm On Sep 24, 2011
Working with windows components could be a rigmarole a times and still could be fun if  later achieve your target.

Since I discovered the free download of Visual Studio .Net Express 2008 over at the microsoft site, I have had a small play bringing some of my VB 6 code over to the new standards.

Attached is a small sample I just wrote to communicate with the Serial Port.

The s/w enumerates all available ports automatically and data entred into the upper text box is transmitted to the port on clicking the Tx button. The data sent is terminated by vbCr character (chr(13)) but this can be modified in code.

Pauses are used to wait for any recieved data to be filled in the buffer, and the Rx data is then added to the RichTextBox. Again these can be modified or replaced by Do, While loops depending on what device you communicate with.

This is not the best way of working with VB.NET, I am reading a lot about threading at the moment. However this very simple code works in my application 100%, and hopefully it can give others a small head start.

As a gsm programmer, serial port and usb ports are my major priority. getting my new blackberry crypto algorithm to communicate with my newly developed blackberry tool has taken me almost 3 MONTHS but at last. I had to read the blackberry bsn dump via local brutheforcing and all cable schematics to the chip failed even via jtag.

I simply ended up being able to communicate via serial with this.

If anyone need this code in case.

 Dim comPorts As Array   'Com ports enumerated into here
    Dim rxBuff As String    'Buffer for receievd data

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'When the form loads
        'Enumerate available Com ports and add to ComboBox1
        comPorts = IO.Ports.SerialPort.GetPortNames()

        For i = 0 To UBound(comPorts)
            ComboBox1.Items.Add(comPorts(i))
        Next

        'Set ComboBox1 text to first available port
        ComboBox1.Text = ComboBox1.Items.Item(0)
        'Set SerialPort1 portname to first available port
        SerialPort1.PortName = ComboBox1.Text

        'Set remaining port attributes
        SerialPort1.BaudRate = 19200
        SerialPort1.Parity = IO.Ports.Parity.None
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.DataBits = 8


    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'When Tx button clicked
        'Clear buffer
        rxBuff = ""

        'If port is closed, then open it
        If SerialPort1.IsOpen = False Then SerialPort1.Open()

        'Write this data to port
        SerialPort1.Write(TextBox1.Text & vbCr)

        'Pause for 800ms
        System.Threading.Thread.Sleep(800)

        'If the port is open, then close it
        If SerialPort1.IsOpen = True Then SerialPort1.Close()

        'If the buffer is still empty then no data. End sub
        If rxBuff = "" Then GoTo ends

        'Else display the recieved data in the RichTextBox
        RichTextBox1.Text = rxBuff

ends:
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        'When ComboBox1 selection is changed, we need to update SerialPort1 with the new choise
        'But only if the port is closed!

        If SerialPort1.IsOpen = False Then
            SerialPort1.PortName = ComboBox1.Text
        Else : MsgBox("Operation only valid when port is closed.", MsgBoxStyle.Exclamation, "Error"wink

        End If

    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        'This sub gets called automatically when the com port recieves some data

        'Pause while all data is read
        System.Threading.Thread.Sleep(300)

        'Move recieved data into the buffer
        rxBuff = (SerialPort1.ReadExisting)

    End Sub

End Class


attached is the com exe

http://www.4shared.com/file/lGN2OpsU/SerialPort_Test.html.

Re: Easy Com Port On Vb by Andrew3(m): 4:11pm On Sep 24, 2011
,

Re: Easy Com Port On Vb by Andrew3(m): 4:25pm On Sep 24, 2011
AT LAST ALL THOSE WITH MEP 0 LEFT CAN CONTACT ME FOR A STANDALONE SOLUTION.






Uploaded with ImageShack.us
Re: Easy Com Port On Vb by THABOSS1: 8:27pm On Oct 09, 2011
hi there wat operating system did you install your vb 2008 on??
Re: Easy Com Port On Vb by Andrew3(m): 2:15pm On Oct 10, 2011
THA BOSS:

hi there wat operating system did you install your vb 2008 on??
XP SP3
Re: Easy Com Port On Vb by CristianM: 7:53am On Apr 06, 2012
hello, about that standalone solution? thank you

(1) (Reply)

I Need 2 Of The Best Programmers For A Project In January / A Ready Made Project Report On Hospital Management (information) System / sold

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 22
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.