Serial communication between a PokectPC and hyperterminal using VB.NET

Asked By Indu mathy
27-May-09 01:13 PM
Earn up to 0 extra points for answering this tough question.
Hello Sir...
   I want to do serial communication between a PocketPC(Windows Mobile 5.0) and hyperterminal.
Plz give me sample coding in VB.NET  (windows or console application)
also is it necessary to use emulator from VS 2008?

Plz help me Sir
Thanking U in advance...

  re: Serial communication between a PokectPC and hyperterminal using VB.NET

Arun Kumar Ramesh replied to Indu mathy
30-Jun-10 01:48 PM
  Serial Communication with VB.Net

One of the thing I really miss while using VB.Net is the lack of serial communication support.Yes, you can use VB6's MsComm32.ocx  but if you, like me, have installed VB.Net on a separate hard disk you may experience some problems installing and using it in design mode.
Don't forget moreover that if you use that control, you must register it on user machine when you deploy your application, loosing the 'XCopy installation mode' provided by VB.Net.
With this class you can easily use serial communication using native VB.Net and API features.
Before describing you how to use the class, let me point out that I've just written it as a demonstration of VB.Net interface to serial communication, you'd probably need to customize it for your special 'Rs232' needs.

Initializing and Opening the Com port

Create an instance of CRs232 then set COM parameters before invoking the Open method
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 COM Port
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 COM Port
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

More details
The zip file that you can download here includes also a small Windows Form example that can help you understand how to use my class.

    

How to use the sample

    * Verify that COM1 or COM2 are not in use (sample uses COM1 or COM2 but class can handle more than 9 ports...)
    * Select default timeout (in milliseconds) and Baudrate (other parameters like parity... are fixed inside code)
    * Press Open COM Port
    * Type the string you wish to send to device (e.g ATDT12345) and press Tx
    * Verify on target device that bytes are sent.
    * If you want to read some bytes from COM port, type the number of char you wish to read into Bytes to read textbox and press Rx
    * Received data will be displayed inside Received data textbox

If not enough character are received inside allocated timeout, you will have an error and you will se what has been read from serial port.
If you want to test the status of Carrier Detect (CD) or RTS and other lines just seelct it from Status line combobox and press Check

RTS and DTR checkboxes will allow you to change the status of RTS and DTR lines respectively.
Automatically receive bytes allows you to read incoming bytes after you press Tx button simulating a client-server connection.

Enabling events
This is the most requested features i got from you and finally, here it is!
Check Enable events to start intercepting events coming from serial port (CD,RTS,Ring...) and to get characters as soon as they get into COM port.

Events are signaled by a CommEvent event, and you can use Mask parameter to detect what event has occurred, if you set a number of character into Bytes to read texbox, as soon as those character arrives into serial port you will get a CommEvent that you can use to get received characters.
Here's an extract from sample code:

If (mask And Rs232.EventMasks.RxChar) > 0 Then
 Dim s as String= source.InputStreamString) '// Here you have a string with received characters
End If

Please note that in order to get data asyncronously you have to set EnableEvents property to true (see code on sample Form)

Enjoy !
~ Arun
Create New Account