.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Problems with getting data from serial port.
 

Problems with getting data from serial port.

Hi, all!
Running the code below, I have the problem that when exiting the For loop, the received string disappear from the strTotal variable. Can anyone please guide me on how to solve this? Thank you in advance.

Imports System
Imports System.IO.Ports
Imports System.Threading

Public
Class Form1
Private serialport1 As New SerialPort
Public Shared strtotal As String

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

Dim
strData As String
Dim array1() As Byte
Dim nr As Integer

With
serialport1
.PortName =
"COM1"
.BaudRate = 1200
.Parity = Parity.None
.DataBits = 8
.StopBits = 2
.DtrEnable =
True
End With

'Somehow the 4 lines below cannot be a part of the With block. Can anyone tell me why?

serialport1.Open()
serialport1.Write("A07E") 'codes to trigger the reception of data from the serial device
serialport1.Write(
"G")

serialport1.RtsEnable = True

ReDim
array1(23)' Should preferably have been ReDim array1(serialport1.BytesToRead) but this doesn't work, apparently

nr = 0
strtotal =""

For nr = 0 To array1.Length
serialport1.BaseStream.Read(array1, nr, 1)
strData =
CStr(ChrW(array1(nr)))
strtotal = strtotal + strData

Debug.Print(strtotal) 'For testing that the loop works. The string is printed for every loop as expected, one chr more per loop. End Sub

Next nr

Debug.Print(strtotal & "total") 'To test if the total string is stored. IT IS NOT???

serialport1.BaseStream.Close()
serialport1.Close()

End sub
End class

Svein Vargan
The for loop should be
For nr = 0 To array1.Length - 1
SamCPP

You can use google to search for other answers

Custom Search

More Threads

• Attributes on interface members not visible on implementation class
• ASP.Net 2.0
• SerialPort to Receive Image Data
• Error:Wrong Local header signature-918826653
• How do I get a user's domain name?
• ACTIVEX WITH VS05
• GetManifestResourceStream("UBViews.xml.EbeddedData.xml") returns null
• Type.GetType method
• Databing to a single object instead of a collection
• Exception in "How to: Envelope a Message for One Recipient"