CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2001
    Location
    USA
    Posts
    298

    Serial port in VB.NET

    I need help using the serial port under VB.NET. I found a control called AxMSComm which I assume is the comm control, but there's no documentation in my MSDN help. Where can I find descriptions of the methods and properties of this control? And why doesn't MSDN have it?

    Thanks

  2. #2
    Join Date
    Nov 2005
    Posts
    281

    Re: Serial port in VB.NET

    Here is an example that might possibly help

    http://www.microsoft.com/downloads/d...DisplayLang=en


    Code:
    Here's an example: 
    Dim moRS232 as New Rs232()
    With moRs232
    		   .Port = 1													  '// Uses COM1 
    		   .BaudRate = 2400									    ' // 2400 baud rate 
    		   .DataBit = 8												 ‘// 8 data bits	 											  
    		   .StopBit = Rs232.DataStopBit.StopBit_1		 '// 1 Stop bit 
    		   .Parity = Rs232.DataParity.Parity_None		   '// No Parity 
    		   .Timeout = 500											 '// 500 ms of timeout admitted to get all required bytes 
    End With 
    '// Initializes and Open 
    moRS232.Open () 
    You can, optionally control the state of DTR/RTS lines after the Port is open 
     '// Set state of RTS / DTS
    moRS232.Dtr = True
    moRS232.Rts = True


    In case of an error/problems an exception is raised, so i suggest you to enclose the code within a Try...Catch block.

    Transmitting data to a COM port:
    Code:
    The class has 2 buffers one for Tx and one for Rx, to transmit data just set the TxData property with the informations you wish to send then invoke the Tx method. 
    example: 
    moRS232.Write(txtTx.Text)


    Receiving data from a COM port:
    Code:
     
    Just invoke the Rx method passing it the number of bytes you want to read from COM port, then read the Rxdata property. 
    example:
    moRS232.Read(10)					   '// Gets 10 bytes from serial communication buffer 
    Dim sRead as String=moRs232.InputStreamString
    Please note that thread is blocked for the period of time set by Timeout property and that in case the class cannot read all required bytes from COM buffer a Timeout exception is raised.
    If the number of bytes to read is omitted, the class assumes 512 bytes have to be read from buffer.
    
    The class is very simple and surely misses some error control, but as prefaced I just wanted to give you an example of what you can do with VB.Net without resorting to ocx'es or other 3rdy parties controls 
    And this may help too:
    http://support.microsoft.com/default...79&Product=vb6

    I hope I helped with this issue. I will be around here more to help the moderators with all of the questions being asked here.


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