Results 1 to 9 of 9

Thread: reading data via rs232

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    9

    reading data via rs232

    hello guys !
    i'm new here , i would like to know a method that can read data via rs232 ( i use pic 18f452 to send 5 values(pression, temperature, current, voltage and solar radiation ) of Adc conversion )
    i successed to read one value at first ( sending one value via pic) , but i couldn't read more than one at end

    so, question is how to do that ?
    how to separate between the data?
    show pression in textbox1
    // // in textbox2
    .
    .
    ..
    // // in textbox5
    thanks

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: reading data via rs232

    You are reading through a serial port, right? If so, the general pattern is the handle the Data Received event of the port. You may get one message per input item, or you may get a whole bundle, depending on how the output works. The data is probably coming across as fairly small packets of bytes, with one or more values in that array. There are plenty of examples around here of reading serial ports, but you may have to make up some test code so that you can look at the bytes being received before you really know what is coming in, and in what order, and when. I was doing something like this a couple years ago, and may be doing it again in a year, or so. I remember that there was a fair amount of trial and error seeing what byte packets were coming in, and figuring out how the data was composed in those bytes. However, the first step is to set up something that reads the incoming bytes, and searching on DataReceived, or Serial Port Read, should give you plenty of examples.
    My usual boring signature: Nothing

  3. #3
    Addicted Member
    Join Date
    Nov 2011
    Posts
    229

    Re: reading data via rs232

    Hi, what is the format of the five values that the Pic is sending to your PC, is the format an ASCII string of characters representing each value or a packet of bytes where each byte or pair of bytes represent the values.

    I assume you have access to programming the Pic, at the very least I would include a header for the data. The header could be something simple like "PIC",10 this would denote to the VB application the start of the data packet and help with synchronization.

    You will need two routines to collect and display the data, the DataReceived event handler to collect the data and a Delegate SubRoutine for display.

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    9

    Re: reading data via rs232

    i use float values , but when send them i use this function
    Printf which convert them to string values

    could you give me a simple code example ?

  5. #5
    Addicted Member
    Join Date
    Nov 2011
    Posts
    229

    Re: reading data via rs232

    Hi, there are many possible ways to transmit/receive and display your data, for simplicity and hopefully so you will see some immediate results you can try the following.

    The first thing is to ensure that the data from the Pic is formatted exactly how we want it for this test. I'm not a C programmer but you can perhaps figure our exactly what is required from this. The objective is to transmit 5 data strings with a space character between each and finally the newline. The newline character (binary value 10) is important because that is the default value the Visual Basic ReadLine instruction looks for as a termination character. If the Newline character is not suitable for your purpose then it can be modified, eg. SerialPort1.NewLine=somecharacter

    Code:
    printf("%f %f %f %f %f \n",dat1,dat2,dat3,dat4,dat5);
    The VB code uses the DataReceived event handler to detect data arriving at the ports input buffer, the timeout is there to give 100 mS for a valid string to arrive and help prevent the application from hanging. Once the string is read by the SerialPort1.ReadLine it is passed as a parameter to the delegate Show_sData. This sub routine uses Split to split the sData string into an array of 5 smaller strings using the spaces between data as the delimiter then each string in the array is displayed in its respective text box.

    Code:
     
    
    Private Delegate Sub mydel(ByVal s As String)
    
    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    
            Dim sData As String = ""
            Try
    
                SerialPort1.ReadTimeout = 100
                sData = SerialPort1.ReadLine()
                Me.Invoke(New mydel(AddressOf Show_sData), sData)
    
            Catch ex As Exception
    
            End Try
    
        End Sub
    
    
    
        Private Sub Show_sData(ByVal s As String)
    
            Dim valArray() As String
            valArray = s.Split(" ")
    
            TextBox1.Text = valArray(0)
            TextBox2.Text = valArray(1)
            TextBox3.Text = valArray(2)
            TextBox4.Text = valArray(3)
            TextBox5.Text = valArray(4)
    
        End Sub

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    9

    Re: reading data via rs232

    thanks for your idea i appreciate your help , but i solved it with another(own) way last two days
    if its possible now
    how can i plot graph of variation for each variable ??

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    9

    Re: reading data via rs232

    and save those data (variables ) in text file .

  8. #8
    Fanatic Member
    Join Date
    Sep 2009
    Location
    Lakewood, Colorado
    Posts
    621

    Re: reading data via rs232

    For plotting, you can use the Chart control in the Toolbox (System.Windows.Forms.DataVisualization.Charting.Chart). There is example code on http://archive.msdn.microsoft.com/mschart.

    Dick
    Richard Grier, Consultant, Hard & Software
    Microsoft MVP (Visual Basic)

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    9

    Re: reading data via rs232

    as you know my variables changing evrytime , i need to save them in text file or create a database
    how can do that ?

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