Serial Port from vb6 to c#

A

AngeloErre

Hi, i am converting my application from visual basic 6 to c#, but i have a
problem with serial port, in vb6 i used MSCOMM, now in c# there is SerialPort
componet.

My problem is to convert this code:

----------------------------------------------------------------------

Private Sub Form_Load()
MSComm1.CommPort = 8
MSComm1.Settings = "9600,n,8,1"
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
End Sub

Private Sub MSComm1_OnComm()
Dim Rx$
Rx$ = MSComm1.Input
If Len(Rx$) > 9 Then
Text2.text = ""
Text2.text = Text2.text & Rx$
If Rx$ = "0415151A74" Then
WindowsMediaPlayer1.URL = "C:\eduglow\suoni\asciugacapelli.wav"
schermo.Picture = LoadPicture("C:\eduglow\figure\asciugacapelli.jpg")
Call WindowsMediaPlayer1.Controls.Play

End If
End sub

How to convert it?
Thank's a lot!!
Angelo
 
P

Peter Duniho

Hi, i am converting my application from visual basic 6 to c#, but i have
a
problem with serial port, in vb6 i used MSCOMM, now in c# there is
SerialPort
componet.

My problem is to convert this code:

----------------------------------------------------------------------

Private Sub Form_Load()
MSComm1.CommPort = 8
MSComm1.Settings = "9600,n,8,1"
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
End Sub

Private Sub MSComm1_OnComm()
Dim Rx$
Rx$ = MSComm1.Input
If Len(Rx$) > 9 Then
Text2.text = ""
Text2.text = Text2.text & Rx$
If Rx$ = "0415151A74" Then
WindowsMediaPlayer1.URL = "C:\eduglow\suoni\asciugacapelli.wav"
schermo.Picture =
LoadPicture("C:\eduglow\figure\asciugacapelli.jpg")
Call WindowsMediaPlayer1.Controls.Play

End If
End sub

How to convert it?

Can you be more specific?

You already know that .NET has the SerialPort class. The documentation
clearly describes a variety of ways to get data from a SerialPort
instance, as well as how to initialize the instance. It's not clear from
your example whether you are reading ASCII from the serial port, but that
seems like a reasonable assumption. Since the default encoding for text
operations on the SerialPort class is already ASCII, you should be able to
use the SerialPort class as almost a direct "drop-in replacement" in the
code, at least in terms of features and functionality (obviously the
syntax will be a little different...the documentation can guide you
through that).

The rest of the code has very little to do with the serial port, but
rather what to do with the data after you get it. Assuming you've already
got a way to deal with the "WindowsMediaPlayer" object, it seems to me
that the translation there is even more straightforward.

I do note that the code you posted doesn't really seem correct. You can't
always count on a serial port delivering every byte all at once, depending
on the exact timing of the code. The fact that data read from the serial
port is simply thrown away if the length is insufficient can easily result
in a variety of problems, including missing entire messages, or having
some messages corrupted. And depending on what's connected at the other
end, it's not necessarily the case that you can count on getting _only_
the bytes that correspond to a single message or command.

But if the first step here is to port the code, it's difficult to
understand exactly what you need help with, given that you've already
found the appropriate class to use. The documentation should tell you
everything you need to know.

If it doesn't, then you should be more specific with respect to your
question, so we know exactly what answer to provide.

Pete
 
A

AngeloErre

Hi Pete, thank's a lot for you reply.

My application is usefull to connect a bluetooth RFID Reader.

I connect he RFID Reader, and when RFID read a "tag" send a string via
bluetooth (virtual serial port).

My application have to listen on specific serial port, so when arrive some
string have something to do.

I have found this code in vb.net but in c# Handles clauses are not
supported.

May you help me?

Public Class Form1

Dim Rx As String = ""
Public Event DataReceived As IO.Ports.SerialDataReceivedEventHandler

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
SerialPort1.PortName = "COM1"
SerialPort1.BaudRate = 9600
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.DataBits = 8
SerialPort1.StopBits = IO.Ports.StopBits.One
'SerialPort1.Handshake = IO.Ports.Handshake.None
SerialPort1.RtsEnable = True
SerialPort1.Open()

'If SerialPort1.IsOpen = True Then
' SerialPort1.Write("MicroCommand")
'End If
End Sub

Private Sub SerialPort1_DataReceived(ByVal sender As System.Object,
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles
SerialPort1.DataReceived
Rx = SerialPort1.ReadExisting 'or SerialPort1.ReadLine
Me.Invoke(New EventHandler(AddressOf DoUpdate))
End Sub

Public Sub DoUpdate()
If Len(Rx) > 9 Then
TextBox2.Text = ""
TextBox2.Text = TextBox2.Text & Rx
If Rx = "0415151A74" Then
WindowsMediaPlayer1.URL =
"C:\eduglow\suoni\asciugacapelli.wav"
PictureBox1.Image =
Image.FromFile("C:\eduglow\figure\asciugacapelli.jpg")
WindowsMediaPlayer1.Controls.Play()
End If
End If
End Sub

End Class
 
P

Peter Duniho

Hi Pete, thank's a lot for you reply.

My application is usefull to connect a bluetooth RFID Reader.

I connect he RFID Reader, and when RFID read a "tag" send a string via
bluetooth (virtual serial port).

My application have to listen on specific serial port, so when arrive
some
string have something to do.

I have found this code in vb.net but in c# Handles clauses are not
supported. [...]

The "Handles" keyword in VB.NET is a short-hand way of subscribing a
specific method to an event. There's no direct equivalent in C#, but you
can accomplish the same thing with an explicit event subscription. For
example, in the Form1 class constructor:

this.Load += Form1_Load;

Alternatively, and generally preferable IMHO, you can simply override the
OnLoad() method in the Form sub-class:

public class Form1 : Form
{
protected override void OnLoad(object sender, EventArgs e)
{
base.OnLoad(sender, e);

// call an initialization method here
}
}

For the SerialPort.DataReceived event handler, you'll have to use the
event subscription syntax, since you're not sub-classing a class where you
can write an override.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top