Serialport question

  • Thread starter Thread starter Eric
  • Start date Start date
E

Eric

Hi,

I'm trying to read data from a device connected by SerialPort van MS CF2.0

I connect to the device and the send an INIT command to it.
The device then responds with some data.

Each time the datarecieved event is handled, I get the following
(translated) error:

NotSupportedException
No error message can be shown because an optional source-assembly can not be
found at
Miscrosoft.AGL.Common.MISC.HandleAR() at
System.Windows.Forms.Control.get_Text() at
MyProgram.Form1.prt_DataRecieved() at
System.IO.Ports.SerialPort.CatchRecievedEvents() at
EventLoopRunner.CallRecieveEvents() at
WorkItem.doWork() at
System.Threading.Timer.ring()


Can anyone tell me what this means, please, and how can I solve this?

rg.
Eric
 
I don't know exactly what you mean, here is part of my code:

Public WithEvents prt As SerialPort

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
SendInit()
End Sub

Private Sub SendInit()
If prt.IsOpen = False Then
prt.Open()
End If

Dim buffer(4) As Byte
Dim ac As Integer
buffer(0) = &H3F
buffer(1) = &H3
buffer(2) = &H7F
buffer(3) = &H9E
buffer(4) = CheckSum(buffer, 3)
prt.Write(buffer, 0, buffer.Length)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
prt = New SerialPort("COM1", 9600, Parity.Even, 8, 1)
prt.WriteTimeout = 10000
prt.ReadTimeout = 50000
prt.ReceivedBytesThreshold = 3
End Sub

Private Sub prt_DataReceived(ByVal sender As Object, ByVal e As
SerialDataReceivedEventArgs) Handles prt.DataReceived
Dim ab, ac
ab = Me.TextBox1.Text
ab += "data recieved..." + vbCrLf
Me.TextBox1.Text = ab

For ac = 0 To prt.BytesToRead - 1
ab += Hex(prt.ReadByte) + " "
Next
Me.TextBox1.Text = ab
End Sub

Please help me some more?

rg,
Eric
 
Did you read the link I sent? Please go and read it. The only other piece I
can clarify is that your Data event handler is running on a worker thread.
It is in there where you must Control.Invoke (as my blog entry describes).

Cheers
Daniel
 
yes, I did read it, but I'm not an enough advanced programmer to understand
it, sorry.

Thanks, for your help, by the way.

rg,
Eric
 
Eric, spend more than 5 minutes looking at the code and you will work it
out. I cannot explain it further. All I (and others) can do here is actually
write the function for you. I prefer to let you work it out since you will
appreciate my help more like that in the long run.

Anyway, here is a skeleton starter

Sub OnUIThread(...)End Sub

Good luck!

Cheers
Daniel
 
Thanks again for you help, but since I don't understand the part about
"invoke", I'll go and take a look at OpenNetCF's version.

rg,
Eric
 
While retyping my little project to OpenNetCF's serialport, I found an
example:

I think what you wanted me to do was:

Private Sub prt_DataReceived(ByVal sender As Object, ByVal e As
SerialDataReceivedEventArgs) handles prt.DataReceived
Me.Invoke( new Eventhandler(addressof ShowData)
End Sub

Sub ShowData(ByVal sender as object, byVal e as EventArgs)
Dim ab, ac
ab = Me.TextBox1.Text
ab += "data recieved..." + vbCrLf
Me.TextBox1.Text = ab

For ac = 0 To prt.BytesToRead - 1
ab += Hex(prt.ReadByte) + " "
Next
Me.TextBox1.Text = ab
End Sub

So basicly, as I understand it, using Invoke will create a new thread and if
the DataRecieved event is hit several times after eachother, it will create
a new thread each time and they will be dealt with by the program in the
order they came?

Is this right?

rg,
Eric
 
OpenNETCF's won't help as the event handler also runs in a worker thread.
You'll still have to use Invoke. Trying to get past that is a disservice to
yourself. Learn how invoke works - this is not the only time you're going
to need it.

-Chris
 
No, Invoke does not create a thread at all. It marshals the call back to
the primary (UI) thread, which is the only process thread that can tough a
UI element. If you get multiple calls to Invoke while others are running,
they get serialized (and you should sync lock them if necessary).

-Chris
 
Chris covered your misinterpretation of what is going on. I'll repeat, go
read my blog entry *and* the links it points to.

As for your code, yes that should work for you and you could have also
created a different delegate (not eventhandler) so you can pass the e
argument to the method that runs on the UI thread.

Cheers
Daniel
 
Back
Top