serial coms - again


Closed Thread
Results 1 to 40 of 58

Hybrid View

  1. #1
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    To be honest I'm not sure either - I thought if you sent A,B,C through the port it would just loop that over and over again... do PICs have buffers ? I thought they sent data straight out and in ? but then I'm really getting out of my depth with this.

  2. #2
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default VB Express 2008

    Whilst undertaking several hours googling I came across a forum that contained a simple example from a two year old post of using VB.net to communicate with a PIC micro using PBP. So far I'm managing to adapt it to include a combo box to choose the comm port and retrieve a set of data from my PIC.

    For reference here is the VB code
    Code:
    Imports System.IO
    Imports System.IO.Ports
    Imports System.Net
    Imports System.Data
    Imports System
    Imports System.Windows.Forms
    Imports System.Threading
    Imports System.Text
    
    
    Public Class mMain
    
        Dim WithEvents serPort As New IO.Ports.SerialPort
        Dim myStringBuilder As New StringBuilder
    
        Private Sub mMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            For i As Byte = 0 To My.Computer.Ports.SerialPortNames.Count - 1
                Me.ComboBox1.Items.Add(My.Computer.Ports.SerialPortNames(i))
            Next i
    
            serPort.BaudRate = 115200
            serPort.DataBits = 8
            serPort.Parity = Parity.None
            serPort.StopBits = StopBits.One
    
            butConnect.Enabled = True
            butDisConnect.Enabled = False
        End Sub
    
        Private Sub butConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butConnect.Click
            If (serPort.IsOpen = False) Then
                Try
                    serPort.Open()
    
                    lstData.Items.Clear()
                    myStringBuilder = New StringBuilder  'Clear any old data
    
                    butConnect.Enabled = False
                    butDisConnect.Enabled = True
                Catch ex As Exception
                    'Routine to process exceptions like port already open etc.
                    MsgBox("Exception error")
                End Try
            End If
        End Sub
    
        Private Sub butDisConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butDisConnect.Click
            If (serPort.IsOpen = True) Then
                serPort.Close()
                butDisConnect.Enabled = False
                butConnect.Enabled = True
            End If
        End Sub
    
        Private Sub serPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serPort.DataReceived
            Try
                myStringBuilder.Append(serPort.ReadExisting())
                Me.Invoke(New EventHandler(AddressOf update_Data))
            Catch ex As Exception
                MsgBox("Data Error")
            End Try
        End Sub
    
        Private Sub update_Data()
            lstData.Items.Add(myStringBuilder.ToString)
            myStringBuilder = New StringBuilder  'Flush the old data
        End Sub
    End Class
    I designed a form to suit the different buttons and combo boxes and this was the result.



    The data in the list box came from this section of PBP code

    Code:
    Hserout [DEC Temperatures(0)]
    Hserout [DEC Temperatures(1)]
    Hserout [DEC Temperatures(2)]
    Hserout [DEC Temperatures(3)]
    hserout [10]
    And as I only have one sensor out of four connected to the input associated with Temperature(2) in use the value of 002610, 002680 etc is right and reflects the 26.1C (rising).

    However I would like to split this and place the result into 4 separate text boxes, ie so textbox1 will be updated with just Temperature(0), Textbox2 with Temperature(1) etc. Is there any modification I can do to the PBP code to separate the string to do this.

    I know there has been some concerns about discussing VB code on this forum, but IMO when it's related to such a topic this thread covers I feel that it's justified.

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default

    Hi Malc,
    My VB is very rusty at the moment but have a look at the Split function in VB. It can be used to split a single string into separate strings (an array of strings actually) and you can specify the delimiter used. So if you send your values as text (which you are at the moment) separated by a comma you can use split the recived string into separate strings which you can the us to populate the textboxes.

    If your string as read from the serial port object (let's call it RxString) looks like:
    002610,002660,002680,002680
    Then something like MyArrayOfStrings = Split (RxString, ",") will return 002610 in the first element of MyArrayOfStrings, 002660 in the second and so on. You can then do something like myTextBox1.Text=MyArrayOfStrings(0)

    This is all untested but lookup Split in MSDN and string handling/parsing in general.

    /Henrik.

  4. #4
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks Henrik,

    I get the gist of what you have said, and I'll have a search for that expression. I must admit the syntax of VB is somewhat alien to me

  5. #5
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default formatting data sent to port

    Guys, one further thing... when there is no sensor connected the value for say Temperatures(0) will be 0. When present it sends the temperature as a three digit value, eg 25.7c would be sent as 257. I really need to send a zero value as 000 and not a single 0,

    any ideas ?

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default

    Hi,
    If you HSEROUT DEC3 Temperatures(0) it will write three digits, ie. 000 if the value is 0.

    /Henrik.

  7. #7
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Thankyou - works a treat

  8. #8
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default

    Hi malc-c;

    Hi use a function in VB, that does do the job, that you want, but you must send to VB more things;

    You are sending 4 temperatures, i supose from 4 diferent sensors, 1 temperatura measure for each sensor.
    So if you put something before sendig the temperatures to identify de sensor, like this for exemple;
    You are Sending;
    002610
    002660
    002680
    002680

    Ff you send;
    S1002610
    S2002660
    S3002680
    S4002680

    The 2 first chars, are the ID of the sensor, and the 6 next chars are the temperature.

    Now it's easy to separate them in VB, with the function "Mid",
    On the event Data Received, you must do something like this;
    Code:
     Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Dim buffer As String
    
            buffer = SerialPort1.ReadExisting
    
          If Mid(buffer, 1, 2) = "S1" Then
                Label1.Text = Mid(buffer, 3, 6) ' label to show the temperature of 1º sensor
           End If
    
          If Mid(buffer, 1, 2) = "S2" Then
                Label2.Text = Mid(buffer, 3, 6) ' label to show the temperature of 2º sensor
           End If
    
          If Mid(buffer, 1, 2) = "S3" Then
                Label3.Text = Mid(buffer, 3, 6) ' label to show the temperature of 3º sensor
           End If
    
          If Mid(buffer, 1, 2) = "S4" Then
                Label4.Text = Mid(buffer, 3, 6) ' label to show the temperature of 4º sensor
           End If
    End Sub
    You can use it too in a Select case, and you should put a try....cath too.

    If you don't understanding something, please tell me.

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts