Gordon Cook

I am writing a program in VB2005. I had a similar program in VB6 using MSCOMM that worked ok. I would like to rewrite it in VB2005. I have most of it working ok except that when the incoming data triggers the datareceived event all I can do is append the data.

The unit that I am using the uses the following convention a + sign to signal the start of an event and vbCR to indicate the end of the event. I am trying to output the data to a text box but like I said it will only append.

Here is what I have for code.

If I replace .AppendText(SerialPort1.ReadExisting) wtih .text = serialport1.readexisting I generally only get random parts of the data.

Public Class Form1

Private Enum IDENT_FEATURES

WATCHDOG = 64

DTMFDIGITS = 16

HOOKSTATUS = 8

RINGSTOP = 4

RINGSTART = 2

CALLERID = 1

End Enum

Public Delegate Sub myDelegate()

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If SerialPort1.IsOpen Then

SerialPort1.Close()

End If

Try

With SerialPort1

.PortName = "Com3"

.BaudRate = 4800

.Parity = IO.Ports.Parity.None

.DataBits = 8

.StopBits = IO.Ports.StopBits.One

.ReceivedBytesThreshold = 1

End With

SerialPort1.Open()

SerialPort1.Write(vbCr + "ATSN" + vbCr)

Dim intFeatures As Integer

intFeatures = 0

intFeatures = intFeatures + IDENT_FEATURES.WATCHDOG

intFeatures = intFeatures + IDENT_FEATURES.DTMFDIGITS

intFeatures = intFeatures + IDENT_FEATURES.HOOKSTATUS

intFeatures = intFeatures + IDENT_FEATURES.RINGSTOP

intFeatures = intFeatures + IDENT_FEATURES.RINGSTART

intFeatures = intFeatures + IDENT_FEATURES.CALLERID

SerialPort1.Write("ATF" + Str(intFeatures) + vbCr) 'Turn on selected events

SerialPort1.Write("ATD1" + vbCr) 'Turn on North American Dialing

SerialPort1.Write("ATX,5,7500" + vbCr) 'Set DTMF timeout to 7.5 seconds

Catch ex As Exception

MsgBox(ex.ToString)

End Try

End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

Try

SerialPort1.Close()

Catch ex As Exception

MsgBox(ex.ToString)

End Try

End Sub

Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _

Handles SerialPort1.DataReceived

Dim Phone As String = SerialPort1.ReadExisting

TextBox1.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {Phone})

End Sub

Public Sub updateTextBox()

With TextBox1

.Font = New Font("Garamond", 12.0!, FontStyle.Bold)

.AppendText(SerialPort1.ReadExisting)

.ScrollToCaret()

End With

End Sub

Private Sub SerialPort1_PinChanged(ByVal sender As Object, ByVal e As System.IO.Ports.SerialPinChangedEventArgs) Handles SerialPort1.PinChanged

If e.EventType = IO.Ports.SerialPinChange.Ring Then

TextBox1.Text = ""

End If

End Sub

End Class

Thanks.



Re: Visual Basic Language SerialPort

Bruno Yu - MSFT

Gordon Cook,

According to the SerialPort problem, I would like to provide you an article on the issue below:

Programming Serial Ports Using Visual Basic 2005

http://www.devx.com/dotnet/Article/31001 type=kbArticle&trk=MSCP

While serial port programming was absent in .NET version 1.1, Visual Basic developers who grew accustomed to the MSCOMM control in VB6 will be glad to know that this functionality is supported again in .NET 2.0. Learn to use the SerialPort class to make two computers talk to one another or even to manipulate a mobile device from your computer using Bluetooth.

You can also take a look at the Documentation guide for Serial Port Class in the following thread:

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=141201&SiteId=1

A SerialPort class provides I/O capabilities over a serial communications resource (e.g. ¡°COM1¡±). It can be used to control serial hardware or effect port-to-port communication over any standard serial port, through any RS-232 or null-modem cable. The SerialPort component can be placed directly onto a form, and using the properties available at both design- and run-time, you can determine the internal settings of the serial driver, including baud rate, parity, handshaking, etc. Calling Open() on a properly set SerialPort establishes connection over the specified serial resource file.

This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.






Re: Visual Basic Language SerialPort

Silvan

Hi,

the baud rate 4800 could be a problem since no more supported in several chips.

Another problem that I have seen is when you send a char 128...255 (decimal). On the output of the RS232 the char that appear is only a 63d. I have seen this with a logic analyzer and I still don't have a solution! With VB6 is OK!

Bye

Silvan





Re: Visual Basic Language SerialPort

OmegaDan

To eliminate the spurious data when NOT using appendData, you'll need either a global variable or a static variable and concatenate incoming bytes to build-up your data string:

Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim aChar As String

Static Phone As String
aChar = SerialPort1.ReadChar
if aChar <> vbCr then
Phone = Phone & aChar
else
TextBox1.Text = Phone
Phone = ""
end if
End Sub

- OR -

You could set the Serialport Newline to vbCr:
SerialPort1.NewLine = vbCr
and subsequently use
Phone = SerialPort1.ReadLine






Re: Visual Basic Language SerialPort

D wiseman

Did you ever solve the problem of sending char 128..255 (decimal). I need a solution to this problem also.

Thanks, D





Re: Visual Basic Language SerialPort

MattJW

I'm pretty sure you need the following:

SerialPort1.Encoding = System.Text.Encoding.Default





Re: Visual Basic Language SerialPort

Lina María Villa

Hi!
I'm trying to make a project that consists in communicate an hp IPAQ hx 2700 with serial port to my pc. This Ipaq works with windows mobile 5.0. I communicated them by making an application in vb 2005 for the pda and communicating with the hyperterminal in the pc. I'm having troubles because i can write things in the pda to the pc, but i can't write things in the pc and view them on the pda.
I think that maybe i have to add a library or i have to configure some propierties, but i dont know what to do. Here's the code i used:

Imports System.IO.Ports.SerialPort

Public Class Form1

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

SerialPort1.Open()

End Sub

Private Sub CmdEnviar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdEnviar.Click

SerialPort1.WriteLine(TxtTX.Text)

TxtTX.Text = ""

End Sub

Private Sub DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

Dim Returnstr As String = ""

Do

Dim Incoming As String = SerialPort1.ReadExisting

If Incoming Is Nothing Then

Exit Do

Else

Returnstr &= Incoming & vbCrLf

End If

Loop

TxtRX.Text = TxtRX.Text + Returnstr

End Sub

Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click

Application.Exit()

End Sub

Private Sub Form1_Close()

SerialPort1.Close()

End Sub

End Class

The propierties i used in the serial port are:

Name: SerialPort1

Generate Member: True

Modifiers: Friend

Baud Rate: 9600

Data bits: 8

Discard Null: False

DTR Enable: True

Handshake: None

Parity: None

Parity replace: 63

Port Name: COM 1

ReadBufferSize: 4096

Read Timeout: -1

Received bytes threshold: 1

RTS Enable: True

Stop bits: 1

Write buffer size: 2048

Write timeout: -1

Hope you can help me please.....

Thank you VERY MUCH!!!!!