Results 1 to 10 of 10

Thread: [2005] Reading Multiple output from serial port

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    274

    [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

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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:
    1. Private iCmdSent As Integer 'Declared at class level
    2.  
    3. 'Then when you send a command, say strCommand1, you set
    4. iCmdSent = 1
    5.  
    6. 'Later, once you received your data, check:
    7. Select Case iCmdSent
    8.      Case 1 'Command1
    9.           TextBox1.Text = strDataReceived
    10.      Case 2 'Command2
    11.           TextBox2.Text = strDataReceived
    12. 'And so on....
    13. End Select

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    274

    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.

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Reading Multiple output from serial port

    Quote 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.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    274

    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:
    1. 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.

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Reading Multiple output from serial port

    Quote 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:
    1. 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.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    274

    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:
    1. Imports System.IO.Ports
    2. Imports System.IO
    3.  
    4.  
    5. Public Class Form1
    6.     Private iCmdSent As String
    7.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
    8.         'Event handler for the Connect button
    9.  
    10.         If SerialPort1.IsOpen() Then
    11.             SerialPort1.Close()
    12.         End If
    13.  
    14.  
    15.         Try
    16.             With SerialPort1
    17.                 .PortName = ComboBox1.SelectedItem
    18.                 .BaudRate = 9600
    19.                 .Parity = IO.Ports.Parity.None
    20.                 .DataBits = 8
    21.                 .StopBits = IO.Ports.StopBits.Two
    22.                 .ReadBufferSize = 64
    23.                 .ReadTimeout = 500
    24.                 .ReceivedBytesThreshold = 13
    25.                 .Handshake = IO.Ports.Handshake.None
    26.                 .RtsEnable = True
    27.                 .DtrEnable = True
    28.             End With
    29.  
    30.             SerialPort1.Open()
    31.  
    32.             Label1.Text = ComboBox1.Text & " Connected."
    33.  
    34.         Catch ex As Exception
    35.             MsgBox(ex.Message)
    36.         End Try
    37.  
    38.  
    39.     End Sub
    40.  
    41.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    42.         Try
    43.             'command for reading mark frequency
    44.             SendRequestCommand(Chr(76) & Chr(48) & Chr(13))
    45.             iCmdSent = (Chr(76) & Chr(48) & Chr(13))
    46.         Catch ex As Exception
    47.             MsgBox(ex.ToString)
    48.         End Try
    49.     End Sub
    50.  
    51.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    52.         Try
    53.             'command for reading step frequency
    54.             SendRequestCommand(Chr(76) & Chr(49) & Chr(13))
    55.             iCmdSent = (Chr(76) & Chr(49) & Chr(13))
    56.         Catch ex As Exception
    57.             MsgBox(ex.ToString)
    58.         End Try
    59.     End Sub
    60.  
    61.     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    62.         Try
    63.             'command for reading level data
    64.             SendRequestCommand(Chr(82) & Chr(49) & Chr(13))
    65.             iCmdSent = (Chr(82) & Chr(51) & Chr(13))
    66.         Catch ex As Exception
    67.             MsgBox(ex.ToString)
    68.         End Try
    69.     End Sub
    70.  
    71.     Private Sub OnDataReceived(ByVal sender As Object, _
    72.                         ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
    73.                         Handles SerialPort1.DataReceived
    74.         Try
    75.             TextBox1.Invoke(New myDelegate(AddressOf UpdateTextBox), New Object() {})
    76.         Catch ex As Exception
    77.             MsgBox(ex.Message)
    78.         End Try
    79.     End Sub
    80.  
    81.     Public Delegate Sub myDelegate()
    82.  
    83.     Public Sub UpdateTextBox()
    84.         Dim strInData As String = ""
    85.         While SerialPort1.BytesToRead > 0
    86.             strInData &= ChrW(SerialPort1.ReadByte())
    87.         End While
    88.  
    89.         'display output
    90.  
    91.         Select Case iCmdSent
    92.             Case (Chr(76) & Chr(48) & Chr(13))
    93.                 'display mark frequency
    94.                 TextBox1.Text = strInData.Substring(3, 9)
    95.                 TextBox1.Refresh()
    96.                 System.Threading.Thread.Sleep(1000)
    97.                 SendRequestCommand(Chr(76) & Chr(48) & Chr(13))
    98.  
    99.             Case (Chr(76) & Chr(49) & Chr(13))
    100.                 'display step frequency
    101.                 TextBox2.Text = strInData.Substring(3, 6)
    102.                 TextBox2.Refresh()
    103.                 System.Threading.Thread.Sleep(1000)
    104.                 SendRequestCommand(Chr(76) & Chr(49) & Chr(13))
    105.  
    106.             Case (Chr(82) & Chr(51) & Chr(13))
    107.                 'display level data
    108.                 TextBox3.Text = strInData
    109.                 TextBox1.Refresh()
    110.                 System.Threading.Thread.Sleep(1000)
    111.                 SendRequestCommand(Chr(82) & Chr(51) & Chr(13))
    112.  
    113.         End Select
    114.  
    115.     End Sub
    116.  
    117.     Private Sub SendRequestCommand(ByVal cmd As String)
    118.         If SerialPort1.IsOpen() Then
    119.             SerialPort1.DiscardInBuffer()
    120.             SerialPort1.Write(cmd)
    121.         Else
    122.             MsgBox("COM port is closed!")
    123.         End If
    124.     End Sub
    125.  
    126.  
    127.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    128.         For i As Integer = 0 To _
    129.                                            My.Computer.Ports.SerialPortNames.Count - 1
    130.             ComboBox1.Items.Add( _
    131.                My.Computer.Ports.SerialPortNames(i))
    132.         Next
    133.  
    134.     End Sub
    135.  
    136. 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?

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    274

    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?

  9. #9
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    274

    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:
    1. Private Shared sr As StreamWriter = New StreamWriter("c:\beacon.txt")
    2. 'this written before the sub

    VB Code:
    1. With TextBox9
    2.                     .AppendText((result) & vbCrLf)
    3.                     .Font = New Font("garamond", 12.0!, FontStyle.Bold)
    4.                     'wr.WriteLine(result)
    5.                     sr.WriteLine(result)
    6.                     System.Threading.Thread.Sleep(1000)
    7.                     SendRequestCommand(Chr(82) & Chr(51) & Chr(13))
    8.  
    9. 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
  •  



Click Here to Expand Forum to Full Width