Monday, March 26, 2012

Serial port Class

I try to put a dropdownlist to specify COM port available on the computer by using the code below, however error occurs at My.Computer.Ports where it says that 'ports' is not a member of my.mycomputer

Imports System.IO

PartialClass _Default

Inherits System.Web.UI.Page

PrivateShared portAs System.IO.Ports.SerialPort =New System.IO.Ports.SerialPort()

ProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)HandlesMe.Load

For iAsInteger = 0ToMy.Computer.Ports.SerialPortNames.Count - 1

DropDownList1.Items.Add( _

My.Computer.Ports.SerialPortNames(i))

Next

EndSub

End class

Try this:

For iAsInteger = 0To _My.Computer.Ports.SerialPortNames.Count - 1

cbbCOMPorts.Items.Add( _

My.Computer.Ports.SerialPortNames(i))

cbbCOMPorts.Select(1, 1)

Next


Error still happens atMy.Computer.Portswhich says 'ports' is not a member of my.mycomputer..


I've some problems regarding receiving data from the serial port.

PrivateShared portAs System.IO.Ports.SerialPort =New System.IO.Ports.SerialPort()

'event handler for data_received

PrivateSub OnDataReceived(ByVal senderAsObject, _

ByVal eAs System.IO.Ports.SerialDataReceivedEventArgs) _

Handles port.DataReceived

1)At port.DataReceived, the port has an error which says port handles clause requires a WithEvents variable defined in the containing type of one of its base type

Try

TextBox1.Invoke(New myDelegate(AddressOf UpdateTextBox),NewObject() {})

Catch exAs Exception

MsgBox(ex.Message)

EndTry

EndSub

2)TextBox1.Invoke, error occurs which says 'invoke' is not a member of 'system.Web.UI,webcontrols.textbox'.


I imagine that you've probably solved this, but for anyone else who's searching (i've done this so many times myself), i think you need to set the line

PrivateSharedWithEvents portAs System.IO.Ports.SerialPort

HTH.


Yes if you are going the event driven route. I am not sure I did that in the app I built last year. I think I tried, but the device I was reading did not like that method.Big Smile

0 comments:

Post a Comment