Author Topic: SEND PLC DATA TO OTHER COM PORT  (Read 3675 times)

joko markono

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: SEND PLC DATA TO OTHER COM PORT
« Reply #15 on: September 12, 2019, 01:42:22 AM »
Sorry for late reply. At the moment, i bypass the JAVA terminal exchange program and connect straight to the device (in this case it is a PC which run a JAVA program that has multiple data). i can read the data as can be seen on the attachment. so i have few questions:
1. is there a way i can read the updated data only in a single line.
2. can i separate the data into individual? i have tried someone code from https://www.electroniclinic.com/how-to-split-a-string-message-and-access-the-sensors-values/. here is the code:
Code: [Select]
Imports System.IO
Imports System.IO.Ports

Public Class Form1

    Dim value1 As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Close()
        SerialPort1.PortName = "com15"
        SerialPort1.BaudRate = "9600"
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default
        SerialPort1.Open()
    End Sub



    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim s As String

        s = TextBox1.Text + "," + "," + "," + ","

        Dim somestring() As String
        ' Split string based on comma
        somestring = s.Split(New Char() {","c})

        TextBox2.Text = somestring(0)
        ' value1 = Convert.ToDecimal(TextBox2.Text)
        TextBox3.Text = somestring(1)
        TextBox4.Text = somestring(2)
        TextBox1.Text = ""


    End Sub

    Private Sub DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Try
            Dim mydata As String = ""
            mydata = SerialPort1.ReadExisting()

            If TextBox1.InvokeRequired Then
                TextBox1.Invoke(DirectCast(Sub() TextBox1.Text &= mydata, MethodInvoker))
            Else
                TextBox1.Text &= mydata
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class
but it doesn't work.

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: SEND PLC DATA TO OTHER COM PORT
« Reply #16 on: September 13, 2019, 02:02:35 PM »
Can you provide specific answers for these 2 questions:

1) What is the exact device you are using (brand name, type and model)?
2) Besides for this device, will you be using any other devices?

joko markono

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: SEND PLC DATA TO OTHER COM PORT
« Reply #17 on: September 16, 2019, 01:55:03 AM »
I'm sorry maybe I didn't give full info.
1. the device is actually a desktop PC. it is having a ROV (remotely operated underwater vehicle) software made by JAVA.
    in the software, there are many input data such as altitude, depth, heading, pitch, roll, etc...
    the reason I choose this method to get the incoming data is because I don't have any other device that can send serial data at the moment.
2. yes I will but it will be the same situation. I may need to read the data from specific software such as TSS cable tracker.
« Last Edit: September 16, 2019, 02:14:12 AM by joko markono »

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: SEND PLC DATA TO OTHER COM PORT
« Reply #18 on: September 16, 2019, 03:56:28 PM »
So, all together it looks like you want to use AHMI as a graphical interface for the simulation.

Maybe something similar to this:  https://www.youtube.com/watch?v=FRxoemOd5F0

From your picture, the data seems to be coming in this format:

"┴Yes   20:37:00 12/09/19 0.0  0.0    0.0  0.0  -90.0 -90.0 -20.0  (followed by 20+ spaces)"

If this is continuous format and it doesn't change much, besides for numbers contained within the string, then you could try using a trick to display it within a TextBox as a single line (so set the TextBox's Multiline property to False):

Code: [Select]
    Private Sub DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Try
            Dim mydata As String = SerialPort1.ReadExisting()

            If mydata.StartsWith("┴") Then
                'Trim all the trailing spaces and remove ┴ character from the string.
                Dim resultingString As String = mydata.TrimEnd(New Char() {" "c}).Substring(1)

                'Trim all the excess spaces from the string.
                Do
                    resultingString = resultingString.Replace("  ", " "c)
                Loop Until resultingString.IndexOf("  ") = -1

                'Compare the current text in the TextBox to the resultingString and replace it if different.
                'This will update the values only when they change.
                'This should be every second due to the clock value but might be different due to speed of communication.
                If TextBox1.Text <> resultingString Then
                    If TextBox1.InvokeRequired Then
                        TextBox1.Invoke(DirectCast(Sub() TextBox1.Text = resultingString, MethodInvoker))
                    Else
                        TextBox1.Text = resultingString
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

I didn't test the code above but it should theoretically give you a string that looks like this:

"Yes 20:37:00 12/09/19 0.0 0.0 0.0 0.0 -90.0 -90.0 -20.0"

Now you could split it and pass the values to AHMI controls, similar to this:

Code: [Select]
    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        If Not String.IsNullOrEmpty(TextBox1.Text) Then
            Dim somestring() As String
            'Split string based on space
            somestring = TextBox1.Text.Split(New Char() {" "c})

            'This should give us the following substrings:
            'somestring(0) = Yes
            'somestring(1) = 20:37:00
            'somestring(2) = 12/09/19
            'somestring(3) = 0.0
            'somestring(4) = 0.0
            'somestring(5) = 0.0
            'somestring(6) = 0.0
            'somestring(7) = -90.0
            'somestring(8) = -90.0
            'somestring(9) = -20.0

            AttitudeIndicatorInstrumentControlHMI1.PitchAngle = CDbl(somestring(6))
            AttitudeIndicatorInstrumentControlHMI1.RollAngle = CDbl(somestring(7))
            AirSpeedIndicatorInstrumentControlHMI1.AirSpeed = CInt(somestring(3))
            AltimeterInstrumentControlHMI1.Altitude = CInt(somestring(5))
            HeadingIndicatorInstrumentControlHMI1.Heading = CInt(somestring(4))
            TurnCoordinatorInstrumentControlHMI1.TurnQuality = CSng(somestring(6))
            TurnCoordinatorInstrumentControlHMI1.TurnRate = CSng(somestring(9))
            VerticalSpeedIndicatorInstrumentControlHMI1.VerticalSpeed = CInt(somestring(9))
            'etc
        End If
    End Sub

I didn't test the code above either.

The whole code could skip a beat at certain times because of the serial communication, meaning how packets are received, or could even show different strings.


Resource:  https://stackoverflow.com/questions/26593520/scan-a-file-for-a-string-of-words-ignoring-extra-whitespaces-using-vb-net
« Last Edit: September 17, 2019, 01:07:14 PM by Godra »

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: SEND PLC DATA TO OTHER COM PORT
« Reply #19 on: September 17, 2019, 12:51:04 PM »
If this works then you can eventually eliminate the TextBox and try to do all updating within the DataReceived sub, something like this:

Code: [Select]
    Private Sub DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Try
            Dim mydata As String = SerialPort1.ReadExisting()

            If mydata.StartsWith("┴") Then
                'Trim all the trailing spaces and remove ┴ character from the string.
                Dim resultingString As String = mydata.TrimEnd(New Char() {" "c}).Substring(1)

                'Trim all the excess spaces from the string.
                Do
                    resultingString = resultingString.Replace("  ", " "c)
                Loop Until resultingString.IndexOf("  ") = -1

                Dim somestring() As String
                'Split string based on space
                somestring = resultingString.Split(New Char() {" "c})

                'If Invoke is required then add it for each control (the same as it was done for the TextBox)
                AttitudeIndicatorInstrumentControlHMI1.PitchAngle = CDbl(somestring(6))
                AttitudeIndicatorInstrumentControlHMI1.RollAngle = CDbl(somestring(7))
                AirSpeedIndicatorInstrumentControlHMI1.AirSpeed = CInt(somestring(3))
                AltimeterInstrumentControlHMI1.Altitude = CInt(somestring(5))
                HeadingIndicatorInstrumentControlHMI1.Heading = CInt(somestring(4))
                TurnCoordinatorInstrumentControlHMI1.TurnQuality = CSng(somestring(6))
                TurnCoordinatorInstrumentControlHMI1.TurnRate = CSng(somestring(9))
                VerticalSpeedIndicatorInstrumentControlHMI1.VerticalSpeed = CInt(somestring(9))
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

Just a possibility.
« Last Edit: September 17, 2019, 01:06:29 PM by Godra »

joko markono

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: SEND PLC DATA TO OTHER COM PORT
« Reply #20 on: September 26, 2019, 04:27:20 AM »
I'm truly sorry haven't had change to give a reply. i will try the code that you show and will let you know later.