Convert serial_onComm on Vb6 to Vb2010
Convert serial_onComm on Vb6 to Vb2010
Convert serial_onComm on Vb6 to Vb2010
Convert serial_onComm on Vb6 to Vb2010
Convert serial_onComm on Vb6 to Vb2010
Convert serial_onComm on Vb6 to Vb2010 Convert serial_onComm on Vb6 to Vb2010 Convert serial_onComm on Vb6 to Vb2010 Convert serial_onComm on Vb6 to Vb2010 Convert serial_onComm on Vb6 to Vb2010 Convert serial_onComm on Vb6 to Vb2010 Convert serial_onComm on Vb6 to Vb2010 Convert serial_onComm on Vb6 to Vb2010
Convert serial_onComm on Vb6 to Vb2010 Convert serial_onComm on Vb6 to Vb2010
Convert serial_onComm on Vb6 to Vb2010
Go Back  Xtreme Visual Basic Talk > > > Convert serial_onComm on Vb6 to Vb2010


Reply
 
Thread Tools Display Modes
  #1  
Old 12-31-2011, 01:38 AM
swadexi swadexi is offline
Newcomer
 
Join Date: Sep 2010
Posts: 6
Default Convert serial_onComm on Vb6 to Vb2010


Hi All, can anybody help me convert this code(vb6) to vb2010:

Code:
Private Sub Ser_OnComm()
chars = (Ser.Input)
If Not chars = "" Then
If Asc(chars) = 1 Then
txtExe.Text = Clear
Else
txtExe.Text = txtExe.Text + chars
End If
End If
End sub
because i'm so confuse the simple code on vb2010 is Like this
Code:
Private Sub Serial_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Serial.DataReceived
        TexBox.Invoke(New  _
                       myDelegate(AddressOf updateTextBox), _
                       New Object() {})
        
    End Sub
i dont know how add my code on vb6 to 2010..thanks so much
Reply With Quote
  #2  
Old 01-02-2012, 07:14 PM
passel's Avatar
passelConvert serial_onComm on Vb6 to Vb2010 passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

Since there hasn't been a reply yet I'll take a stab at it, but I really should be working on some other coding that I need to get done.
In .Net the SerialPort object will listen for port traffic on a secondary thread, so won't communicate directly with the main thread (which if you're running a GUI, is the GUI thread).
So, to access the GUI, the secondary thread would have to "Invoke" a routine (delegate) on the GUI thread that would do the actual update.
I can see that the code you've posted is invoking a delegate "updateTextBox", passing it a "New Object() {}". Unfortunately, I haven't had enough experience with various delegate methods to recognize exactly what that is doing, although I believe it is probably some shorthand to passing the info received in some manner to the updateTextBox delegate.

If I had to code this, I would probably take an approach that I currently understand, that would pass the data received in the secondary thread to a delegate where I could use code similiar to what I had in VB6 to process the data.
So, in my approach, I would:
Declare a Delegate template (probably not the correct term) with a parameter of the type I want to pass.

Private Delegate Sub ByteArrayDelegate(ByVal b() As Byte)

Create an object of that Delegate type, pointing to the routine for the delegate to access.

Dim onCommDelegate As New ByteArrayDelegate(AddressOf OnComm)

Write the routine OnComm

Private Sub OnComm(ByVal b() As Byte)
'...
End Sub

And in the DataReceived handler, read the serial data and invoke the delegate, passing on the data read.

Since I was pressed for time, I didn't have time to dig up the cables, etc.. necessary to do a computer to computer test, but I did have an old 9-pin serial mouse sitting in a box, so since I didn't have a serial mouse driver installed, I could just plug the mouse in the serial port and read data from it (at 1200 baud). This mouse sends 3-bytes at a time, so the code is kind of based on that, but not very robust. The SerialPort object does not promise to send bytes one at a time, or in groups that will align with the sender, so the data should be buffered and processed as a serial stream of bytes, rather than "packets" of bytes. But this should get you started.
I added a SerialPort object to the form, set its properties in the IDE, so you won't see that in the code. I had to set RtsEnable to True in the properties to get the mouse to communicate.
I tried reading the data as a string, and I was getting three character strings, but if I tried to use the Asc function to look at their byte value, I was always getting 63 (3F hex) for all three bytes. I don't have time to look at what I'm obviously doing wrong there, so the code just reads the data directly into a byte array, which worked fine.
If no button is pressed the first byte is C0 hex. If a button is pressed the first byte would be something different, so that is why I simulated kind of what you were doing by clearing the textbox if the first character was a certain value, otherwise printing some data in the textbox.
Hopefully, this can get you started on what you need to do.
Code:
Imports System.IO.Ports

Public Class Form1
  '  Private Delegate Sub StringDelegate(ByVal s As String)
  Private Delegate Sub ByteArrayDelegate(ByVal b() As Byte)

  '  Dim onCommDelegate As New StringDelegate(AddressOf OnComm)
  Dim onCommDelegate As New ByteArrayDelegate(AddressOf OnComm)

  'Private Sub OnComm(ByVal s As String)
  '  TextBox1.Text = Asc(s(0)).ToString("X")
  'End Sub

  Private Sub OnComm(ByVal b() As Byte)
    If b(0) = &HC0 Then
      TextBox1.Clear()
    Else
      TextBox1.Text = String.Format("Buttons: {0}, dX: {1}, dY: {2}", b(0).ToString("X"), b(1).ToString("X"), b(2).ToString("X"))
    End If
  End Sub

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    SerialPort1.Open()
  End Sub

  Private Sub Serialport1DataReceiveHandler(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    Dim sp As SerialPort = CType(sender, SerialPort)
    Dim indata(2) As Byte
    Dim s As String
    sp.Read(indata, 0, 3)
    s = sp.ReadExisting   'In my case I'm reading 3 byte data packets. If we start out of sync, this should discard extra bytes and get us into sync
    Me.Invoke(onCommDelegate, indata)  'Send the byte array to the main thread handler
  End Sub

End Class
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #3  
Old 01-06-2012, 11:13 PM
swadexi swadexi is offline
Newcomer
 
Join Date: Sep 2010
Posts: 6
Default

thanks so much...i'm very thank for yout help
Reply With Quote
Reply

Tags
converter, serial communication


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Convert serial_onComm on Vb6 to Vb2010
Convert serial_onComm on Vb6 to Vb2010
Convert serial_onComm on Vb6 to Vb2010 Convert serial_onComm on Vb6 to Vb2010
Convert serial_onComm on Vb6 to Vb2010
Convert serial_onComm on Vb6 to Vb2010
Convert serial_onComm on Vb6 to Vb2010 Convert serial_onComm on Vb6 to Vb2010 Convert serial_onComm on Vb6 to Vb2010 Convert serial_onComm on Vb6 to Vb2010 Convert serial_onComm on Vb6 to Vb2010 Convert serial_onComm on Vb6 to Vb2010 Convert serial_onComm on Vb6 to Vb2010
Convert serial_onComm on Vb6 to Vb2010
Convert serial_onComm on Vb6 to Vb2010
 
Convert serial_onComm on Vb6 to Vb2010
Convert serial_onComm on Vb6 to Vb2010
 
-->