-
RS232 - Reading Serial Port
I'm facing problems with reading data from serial port.
Im sending out a command to the spectrum analyzer, and start reading. characters appears continously but i still cant read as its in ascii.
this is what i wrote:
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
SerialPort1.WriteLine(Chr(82) & Chr(51) & Chr(13) & vbCrLf)
End Sub
Private Sub DataReceived( _
ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles SerialPort1.DataReceived
TextBox3.Invoke(New _
myDelegate(AddressOf updateTextBox3), _
New Object() {})
End Sub
Public Delegate Sub myDelegate()
Public Sub updateTextBox3()
With TextBox3
.Font = New Font("Garamond", 12.0!, FontStyle.Bold)
.AppendText(SerialPort1.ReadExisting & vbCrLf)
.ScrollToCaret()
End With
End Sub
here're my questions:
1)how to convert my data read from port which is in ascii into decimal?
2)the incoming data is running such as in an overflow state,
like,
43
44
43
..
..
..
it goes on...
but i want it to appears as a continuously changing figure.
like XX.XX (keeps changing)
How to write this command?
3) How to write the data read from the serial port into a txt file inside certain folder?
-
Give this a shot
This sample will grab a hex data steam from com1 that looks something like this:
ff 30 31 32 33 34 35 36 37 38 39 31 0d
then turn it into the acsii equivalent:
?01234567891[]
where [] is a little rectangle "junk" character
In this sample the data in the receive event is grabbed in the "grabSerialData"
subroutine in this line of code:
comportData &= SerialPort1.ReadExisting()
I'll bet that your overflow state can be cured with the values in the
.ReadBufferSize and .ReceivedBytesThreshold parameters
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
With SerialPort1
.PortName = "Com1"
.BaudRate = 300
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = 1
.ReadBufferSize = 14 'inbound data stream + 1
.ReadTimeout = 100
.ReceivedBytesThreshold = 13 'the length of your inbound data stream
.Handshake = IO.Ports.Handshake.None
.RtsEnable = True
.DtrEnable = True
'.Encoding = System.Text.Encoding.ASCII (when I leave this out the inbound hex stream turns into ascii)
End With
SerialPort1.Open()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Delegate Sub myDelegate()
Private Sub OnDataReceived(ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles SerialPort1.DataReceived
grabSerialData()
End Sub
Public Sub grabSerialData()
' declare variables
Dim comportData
Dim intBytesAvailable As Integer = SerialPort1.BytesToRead
If intBytesAvailable > 0 Then
comportData &= SerialPort1.ReadExisting()
End If
The comportData variable now holds "?01234567891[]" where [] is a little rectangle.
You would use something like Mid$ to parse out what you want and throw away the ?
and [], i.e. the "junk" characters.
Dim arg0, arg1
arg0 = Mid$(comportData, 12, 1) 'strips out 0123456789 and puts it into arg0
arg1 = Mid$(comportData, 2, 10) 'strips out 1 and puts it in arg1
'the ? and [] are not parsed so they just get thrown away
Then you would use code that looks something something like this to write to
a flat file on your c: drive:
Dim objFSO, objFolder, objShell, objTextFile, objFile
Dim strDirectory, strFile
strDirectory = "c:\dir"
strFile = "\file.txt"
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Check that the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "Just created " & strDirectory
End If
If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
Wscript.Echo "Just created " & strDirectory & strFile
End If
'set objFile = nothing
'set objFolder = nothing
' OpenTextFile Method needs a Const value
' ForAppending = 8 ForReading = 1, ForWriting = 2
Const ForAppending = 8
Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForAppending, True)
' Writes a new line into open file
objTextFile.WriteLine(arg0 & strComma & arg1)
objTextFile.Close
It would be best to make sure C:\dir\file.txt already exists. Also make SURE
that you have permission to write to file.txt
Good luck,
Ike
Similar Threads
-
Replies: 1
Last Post: 03-21-2006, 01:48 PM
-
Replies: 1
Last Post: 02-21-2006, 02:23 AM
-
By sashi1977 in forum .NET
Replies: 4
Last Post: 11-14-2005, 07:48 AM
-
By deeps_chennai in forum Security
Replies: 1
Last Post: 10-06-2005, 02:36 PM
-
By newbie29 in forum Java
Replies: 0
Last Post: 05-24-2005, 01:36 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|