|
-
Oct 4th, 2006, 12:14 PM
#1
Thread Starter
Hyperactive Member
[2005] Reading Multiple output from serial port
Is that dataReceived event can only be used once inside my program? For example, i have to send out multiple commands to request for multiple outputs from the device im working on. and i then want to display the outputs received from device seperately in different textbox. Is that possible?
So far im only managed to send out a command requesting certain output fr the device (with the help fr VB forum), and receive the output wif datareceived event n display it at a text box.
thanks
-
Oct 4th, 2006, 04:08 PM
#2
Re: [2005] Reading Multiple output from serial port
Yes, it is quite possible. When you send a request command, you know what to expect in the return data (i.e if you request to read the frequency, then you should expect the data returned contains frequency info...). Based on this assumption, when you send a command, set another class level variable to some value associate to the command, then when you receive the data, check the value of this variable and put your data to appropriate textbox. Something like:
VB Code:
Private iCmdSent As Integer 'Declared at class level
'Then when you send a command, say strCommand1, you set
iCmdSent = 1
'Later, once you received your data, check:
Select Case iCmdSent
Case 1 'Command1
TextBox1.Text = strDataReceived
Case 2 'Command2
TextBox2.Text = strDataReceived
'And so on....
End Select
-
Oct 4th, 2006, 10:05 PM
#3
Thread Starter
Hyperactive Member
Re: [2005] Reading Multiple output from serial port
Thanks stanav for taking the time to answer my question, but i wonder:
Why is that iCmdSent has to be set to 1?
and Setting iCmdSent as integer to indicate the receiving data is in integer value rite?
My data would be frequency in Hz as well as signal value in dB.
-
Oct 5th, 2006, 07:23 AM
#4
Re: [2005] Reading Multiple output from serial port
 Originally Posted by wence
Thanks stanav for taking the time to answer my question, but i wonder:
Why is that iCmdSent has to be set to 1?
and Setting iCmdSent as integer to indicate the receiving data is in integer value rite?
My data would be frequency in Hz as well as signal value in dB.
iCmdSent is just a variable of type Integer used to identify what command had just been sent. It can be set to whatever value you want it to be, however, for simplicity, I would just choose:
1 - indicates command1 (whatever command1 is) was sent
2 - indicates command2 (whatever command2 is) was sent
3 - indicates command3 (whatever command3 is) was sent
.... And so on ....
So later, after you receive the data, you can check the value of iCmdSent and know that this data was a result from which command. And that's all iCmdSent is: giving you an indication of which command had been sent prior to the data received event, no more, no less. You can use other variable types (string, char...) for the same purpose instead of integer... It's up to you as the programmer.
Once you have indentified the data received was the result from which command, you can easily display the data in the appropriate textbox.
-
Oct 5th, 2006, 07:32 AM
#5
Thread Starter
Hyperactive Member
Re: [2005] Reading Multiple output from serial port
OKay thanks, I think its better that i try it on my device first then ask if i hav any question. but ive another question now which is the invoke command, is it still applicable in this case. normally, we hav
VB Code:
TextBox3.Invoke(New myDelegate(AddressOf UpdateTextBox), New Object() {})
which the received data will be directed to textbox3. In this case invoke will still be used? and is that invoke can only be used once?
Thanks lot for being patient wif my question, as im still a beginner in vb05 programming.
-
Oct 5th, 2006, 11:40 AM
#6
Re: [2005] Reading Multiple output from serial port
 Originally Posted by wence
OKay thanks, I think its better that i try it on my device first then ask if i hav any question. but ive another question now which is the invoke command, is it still applicable in this case. normally, we hav
VB Code:
TextBox3.Invoke(New myDelegate(AddressOf UpdateTextBox), New Object() {})
which the received data will be directed to textbox3. In this case invoke will still be used? and is that invoke can only be used once?
Thanks lot for being patient wif my question, as im still a beginner in vb05 programming.
Your received data won't be automatically directed to TextBox3 just because you use it to invoke something. If you look closely inside your UpdateTextBox sub, you will see that the data is manually assigned to TextBox3.Text ... So now, instead of assigning the data to TextBox3, you can check to find out which textbox should receive the data and assign the data to that textbox instead.
-
Oct 13th, 2006, 07:40 AM
#7
Thread Starter
Hyperactive Member
Re: [2005] Reading Multiple output from serial port
Stanav, you're rite. With your help I'm now able to to display certain output inside certain textbox. However i hav a few questions:
This is my source codes:
VB Code:
Imports System.IO.Ports
Imports System.IO
Public Class Form1
Private iCmdSent As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
'Event handler for the Connect button
If SerialPort1.IsOpen() Then
SerialPort1.Close()
End If
Try
With SerialPort1
.PortName = ComboBox1.SelectedItem
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.Two
.ReadBufferSize = 64
.ReadTimeout = 500
.ReceivedBytesThreshold = 13
.Handshake = IO.Ports.Handshake.None
.RtsEnable = True
.DtrEnable = True
End With
SerialPort1.Open()
Label1.Text = ComboBox1.Text & " Connected."
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
'command for reading mark frequency
SendRequestCommand(Chr(76) & Chr(48) & Chr(13))
iCmdSent = (Chr(76) & Chr(48) & Chr(13))
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Try
'command for reading step frequency
SendRequestCommand(Chr(76) & Chr(49) & Chr(13))
iCmdSent = (Chr(76) & Chr(49) & Chr(13))
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Try
'command for reading level data
SendRequestCommand(Chr(82) & Chr(49) & Chr(13))
iCmdSent = (Chr(82) & Chr(51) & Chr(13))
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub OnDataReceived(ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles SerialPort1.DataReceived
Try
TextBox1.Invoke(New myDelegate(AddressOf UpdateTextBox), New Object() {})
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Delegate Sub myDelegate()
Public Sub UpdateTextBox()
Dim strInData As String = ""
While SerialPort1.BytesToRead > 0
strInData &= ChrW(SerialPort1.ReadByte())
End While
'display output
Select Case iCmdSent
Case (Chr(76) & Chr(48) & Chr(13))
'display mark frequency
TextBox1.Text = strInData.Substring(3, 9)
TextBox1.Refresh()
System.Threading.Thread.Sleep(1000)
SendRequestCommand(Chr(76) & Chr(48) & Chr(13))
Case (Chr(76) & Chr(49) & Chr(13))
'display step frequency
TextBox2.Text = strInData.Substring(3, 6)
TextBox2.Refresh()
System.Threading.Thread.Sleep(1000)
SendRequestCommand(Chr(76) & Chr(49) & Chr(13))
Case (Chr(82) & Chr(51) & Chr(13))
'display level data
TextBox3.Text = strInData
TextBox1.Refresh()
System.Threading.Thread.Sleep(1000)
SendRequestCommand(Chr(82) & Chr(51) & Chr(13))
End Select
End Sub
Private Sub SendRequestCommand(ByVal cmd As String)
If SerialPort1.IsOpen() Then
SerialPort1.DiscardInBuffer()
SerialPort1.Write(cmd)
Else
MsgBox("COM port is closed!")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To _
My.Computer.Ports.SerialPortNames.Count - 1
ComboBox1.Items.Add( _
My.Computer.Ports.SerialPortNames(i))
Next
End Sub
End Class
My questions:
1)If i want to multiply data received by 10. I do this, TextBox1.Text =( strInData.Substring(3, 9))*10, but an error wil occur saying that input string was not in a correct format.
2)I can read that frequency output, but when i try to read the level data ouput, they appear as boxes and zeros. Referring back to the manual, it says:
Frequency output format:
ASC DATA + CD(ODH)
Level data output format:
8BIT DATA + CR (0DH)
LEVEL DATA
0.0dB 30H "0"
0.5dB 31H "1"
1.0dB 32H "2"
.
.
.
15.0dB 4FH
.
.
49.5dB 93H
50.0dB 94H
I wonder when it says 8 bit data, does it mean that the level data output are not in asc form? What posibility could it be then? in hex?
-
Oct 13th, 2006, 07:46 AM
#8
Thread Starter
Hyperactive Member
Re: [2005] Reading Multiple output from serial port
LEVEL refers to the value in dB while DATA refers to value in H, seems they're too close..
I think it most probably in hex form, how am i going to convert it to a form which can be read?
-
Oct 13th, 2006, 08:04 AM
#9
Re: [2005] Reading Multiple output from serial port
Hello Wence,
I've never worked with anything similar to your particular device, so I really can't give you an answer on it. I suggest you to contact the manufacturer to find out what these data output format specifications really mean for that specific device.
-
Oct 16th, 2006, 08:54 AM
#10
Thread Starter
Hyperactive Member
Re: [2005] Reading Multiple output from serial port
Hi Stanav,im able to retrieve data in value after converting it to hex(need to convert later), but ive yet to confirm whether the value after conversion is correction.
I need to do manipulation with my data n need have a look-up table starting from 0.0dB and 30H data. For every increment of hex value, the level increases by 0.5dB. Means i will convert my incoming data to hex, and then based on the look-up table, convert hex to level data in dB. What command should i use for that purpose?
Besides, i try to use writeline method to write every data read to a txt file(for the use of graph plotting later), but it didnt really work. Whats the mistakes?
VB Code:
Private Shared sr As StreamWriter = New StreamWriter("c:\beacon.txt")
'this written before the sub
VB Code:
With TextBox9
.AppendText((result) & vbCrLf)
.Font = New Font("garamond", 12.0!, FontStyle.Bold)
'wr.WriteLine(result)
sr.WriteLine(result)
System.Threading.Thread.Sleep(1000)
SendRequestCommand(Chr(82) & Chr(51) & Chr(13))
End With
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
|