|
-
Dec 23rd, 2008, 03:40 AM
#1
New Member
Re: Serial Port
Ok, I am still having problems. Here is exactly what I am doing:
I am sending the following command to the serial port:
SerialPort1.Write("FA;")
The above is in a timer and sent every second, because this is the frequency display of my radio.
The data that is sent back to me over the serial port is:
;00059270000AF;00059270000AF;00059270000AF;00059270000AF;00059270000AF;00059270000AF;00059270000AF;0 0059270000AF;00059270000AF;00059270000AF;00059270000AF;00059270000AF;00059270000AF;00059270000AF
If you look closely at the above data, you will see the following number backwards "7295". That is the radios frequency in Mhz, what I need to do is get that data somehow and display it in a textbox that is updated with the timer. Below is code that was borrowed and is what is giving me the above data.
Private Sub butOC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butOC.Click
'open / close button
butOC.Enabled = False : butOC.Refresh()
If butOC.Text = "Open" Then
'open Port
'Port Setup
'defaults
'SerialPort1.ReadBufferSize = 4096
'SerialPort1.DataBits = 8
'SerialPort1.Parity = IO.Ports.Parity.None
'SerialPort1.StopBits = IO.Ports.StopBits.One
'SerialPort1.ReadTimeout = -1 'forever
SerialPort1.ReadTimeout = 0
'SerialPort1.WriteTimeout = -1 'forever
SerialPort1.WriteTimeout = 2
'SerialPort1.ReceivedBytesThreshold = 1 'don't change! unless you really need to
'SerialPort1.Encoding = Encoding.GetEncoding(20127) ' byte < 128, bytes > 127 equal question mark
SerialPort1.Encoding = AllByteEnc 'so byte > 127 aren't question marks
If cbSPD.SelectedItem Is Nothing Then
SerialPort1.BaudRate = Convert.ToInt32(defSPD) '<<<<<<<<<<<<<<<<<<<<<<< my default speed - Change
Else
SerialPort1.BaudRate = Convert.ToInt32(cbSPD.SelectedItem.ToString) '<<<<<<<<<<<<<<<<<<<<<<< my default speed - Change
End If
'determine port to open
If cbPorts.SelectedItem Is Nothing Then
SerialPort1.PortName = defPort 'use port default if nothing selected
Else
SerialPort1.PortName = cbPorts.SelectedItem.ToString 'use selected item
End If
If zSPopen(SerialPort1, True) Then 'zSPopen opens the port and sets up handler
'port opened succesfully
butOC.Text = "Close"
butOC.Enabled = True : butOC.Refresh()
butSend.Enabled = True
tbSend.ReadOnly = False
'loop while there are bytes in the queue
Do While SerialPort1.IsOpen OrElse zSPNumBytesInQ() > 0
bytesFromQ = New Byte() {}
bytesFromQ = zSPGetBytes(1) 'get one byte if available
If bytesFromQ.Length > 0 Then 'did we get data
'yes
' the rich text box shows newest byte on left
rbRcv.Checked = True : rbRcv.Refresh()
thBytes.Insert(0, " ", 1) 'insert new byte on left in the RTB
thBytes.Insert(0, bytesFromQ(0).ToString, 1)
rtbBytes.Text = thBytes.ToString
'don't show cr lf in the following textbox
If Not (bytesFromQ(0) = 10 OrElse bytesFromQ(0) = 13) Then
rtbCHAR.Text = Chr(bytesFromQ(0)).ToString & rtbCHAR.Text
End If
If thBytes.Length > 512 Then thBytes.Length = 128 'don't let rtb get to big
If rtbCHAR.TextLength > 512 Then rtbCHAR.Text = "" 'don't let rtb get to big
Else 'no data received
rbRcv.Checked = False : rbRcv.Refresh()
End If
Application.DoEvents() 'needed so event handler fires
Loop
End If
Else
'close Port
FreqTime.Enabled = False 'disable frequency display
butSend.Enabled = False
tbSend.ReadOnly = True
zSPclose(SerialPort1) 'close serial port, remove handler
butOC.Text = "Open"
butOC.Enabled = True : butOC.Refresh()
rbRcv.Checked = False
End If
End Sub
rtbCHAR is the textbox that I would like to send the proper data and format to. I guess I just do not quite understand what the program is doing. The program was downloaded here . I am just trying to adapt his serial program to my needs until I understand what is going on.
How would I write the code within the above code to display the data that I need? Remember the timer is sending the command once every second.
Tags for this Thread
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
|
Click Here to Expand Forum to Full Width
|