Author Topic: using multiple serial port  (Read 2855 times)

joko markono

  • Full Member
  • ***
  • Posts: 132
    • View Profile
using multiple serial port
« on: November 04, 2019, 03:53:27 AM »
Hi Guys,

this post is related to my previous post : https://www.advancedhmi.com/forum/index.php?topic=2541.0

I have multiple pages (4 in exact) on my AHMI. One of them having a link between PLC and other device. i.e, I will read certain data from the PLC (hostlink serial protocol) com 4, and send them out to third party device as discuss here: https://www.advancedhmi.com/forum/index.php?topic=2541.0 using serial port com 7 .
FYI, i'm using usb to serial converters for both com.

I can run the program no problem. Only thing when I start to open the corresponding page (I call it "SendDataPage"), and give some analog input to the PLC, communication between PLC and HMI will stop after some times. It happen before I sent out the data that I receive from PLC to com 7 (means before i open com 7 port). I think there is a problem with this page because if I don't open this page, my PLC com has no issue. here is the code for this SendDataPage:
Code: [Select]
Public Class SendDataPage

    '*******************************************************************************
    '* Stop polling when the form is not visible in order to reduce communications
    '* Copy this section of code to every new form created
    '*******************************************************************************
    Private NotFirstShow As Boolean

    Private Sub Form_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.VisibleChanged
        '* Do not start comms on first show in case it was set to disable in design mode
        If NotFirstShow Then
            AdvancedHMIDrivers.Utilities.StopComsOnHidden(components, Me)
        Else
            NotFirstShow = True
        End If
    End Sub

    Private Sub BasicButton1_Click(sender As Object, e As EventArgs) Handles BasicButton1.Click
        MainForm.Show()
        ' Me.Hide()
        Me.Visible = False
    End Sub

    '------------------------------------------------
    Dim myPort As Array
    Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
    '------------------------------------------------

    Private Sub SendDataPage_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load


        Timer1.Start() 'Timer starts functioning

        myPort = IO.Ports.SerialPort.GetPortNames()
        ComboBox1.Items.AddRange(myPort)

        ComboBox2.Items.Clear()
        ComboBox2.Items.Add("4800")
        ComboBox2.Items.Add("9600")
        ComboBox2.Items.Add("19200")
        ComboBox2.SelectedIndex = 2



    End Sub
    '------------------------------------------------

    Private Sub ComboBox1_Click(sender As System.Object, e As System.EventArgs) Handles ComboBox1.Click
    End Sub
    '------------------------------------------------
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        SerialPort1.PortName = ComboBox1.Text
        SerialPort1.BaudRate = ComboBox2.Text

        SerialPort1.Open()

        Button1.Enabled = False

        Button3.Enabled = True

    End Sub
    '------------------------------------------------

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


        If SerialPort1.IsOpen Then

            SerialPort1.Write(Chr(&H15) & Chr(&H13) & "Date    :" & DateString & vbNewLine &
                            "Time    :" & TimeString & vbNewLine &
                            "Head    :" & Space(9 - Strings.Len("Head    :")) & BasicLabel1.Text & " deg" & Space(10 - Strings.Len(BasicLabel1.Text + " deg")) &
                            "Pitch   :" & Space(9 - Strings.Len("Pitch   :")) & BasicLabel2.Text & " deg" & Space(10 - Strings.Len(BasicLabel2.Text + " deg")) &
                            "Roll    :" & Space(9 - Strings.Len("Roll    :")) & BasicLabel3.Text & " deg" & Space(10 - Strings.Len(BasicLabel3.Text + " deg")) &
                             vbNewLine &
                            "Pt Bury :" & Space(9 - Strings.Len("Pt Bury :")) & BasicLabel10.Text & " cm" & Space(10 - Strings.Len(BasicLabel10.Text + " cm")) &
                            "Stb Bury:" & Space(9 - Strings.Len("Stb Bury:")) & BasicLabel9.Text & " cm" & Space(10 - Strings.Len(BasicLabel9.Text + " cm")) &
                            "Jet Pres:" & Space(9 - Strings.Len("Jet Pres:")) & BasicLabel8.Text & " psi" & Space(10 - Strings.Len(BasicLabel8.Text + " psi")) &
                            vbNewLine &
                            "Alt     :" & Space(9 - Strings.Len("Alt     :")) & BasicLabel4.Text & " mtr" & Space(10 - Strings.Len(BasicLabel4.Text + " mtr")) &
                            "Depth   :" & Space(9 - Strings.Len("Depth   :")) & BasicLabel5.Text & " mtr" & Space(10 - Strings.Len(BasicLabel5.Text + " mtr")) &
                            vbNewLine &
                            RichTextBox3.Text)
 

        End If

    End Sub



    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        SerialPort1.Close()
        Button1.Enabled = True
        Button3.Enabled = False
    End Sub

    Private Sub SerialPort1_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs)
        ReceivedText(SerialPort1.ReadExisting())
    End Sub

    Private Sub ReceivedText(ByVal [text] As String) 'input from ReadExisting
        If Me.RichTextBox2.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})
        Else
            Me.RichTextBox2.Text &= [text] 'append text
        End If
    End Sub


End Class


note that the DtrEnable in SerialPort1 is actually set to False.
If someone can advice me what could be the problem?
« Last Edit: November 04, 2019, 06:52:37 AM by joko markono »

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: using multiple serial port
« Reply #1 on: November 04, 2019, 08:26:48 PM »
Your SerialPort1 shows port name as COM6.

Are these paired ports of some kind?

joko markono

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: using multiple serial port
« Reply #2 on: November 04, 2019, 09:44:01 PM »
I think there is a conflict between the two com (PLC com 4 and Serial port com 7 in this case).
it will close both hostlink connection on main page and SendDataPage or it will close either one of them. But the serial port (com 7) out to the device is still open.

joko markono

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: using multiple serial port
« Reply #3 on: November 04, 2019, 09:54:42 PM »
I can select the com port during running the program. it's written in the code.
I can send out the data through desired com port with no issue.
the only problem is communication of the hostlink will stop as I mentioned in previous reply.
there is a timer in the SendDataPage. does it affect the hostlink com?

joko markono

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: using multiple serial port
« Reply #4 on: November 05, 2019, 04:32:34 AM »
i tried using basic AHMI main page (with 3 PLC input). get the data from PLC (omron hostlink com 4) and sent them out to other serial port (com 7). No fancy setting on serial port (all default). Except both databit is set to 8 and stop bit to one.
So far after 20 minutes, program never stop.

Same thing i tried on page 2 of the AHMI (at same time, main page receive same data). works fine.

Is there a possibility because of many data input from PLC? and number of pages of the AHMI? that cause my hostlink com to stop function while receiving from PLC and sending out at same time?

joko markono

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: using multiple serial port
« Reply #5 on: November 06, 2019, 02:06:16 AM »
alright guys, I've figured out what was the problem.  :)
I finally ended up combining the sendDataPage into the main page.  :(
not my first preference but I have no other choice now.
it happen that if too many PLC addresses on the main page (in my case maybe I have around 200), and I try to make new page with the same com driver as the main page, and send some of the input data out to other port, I will face traffic issue I guess. that's why either com on main or the SendDataPage will stop or on both pages.
Note that I'm using Omron Serial Hostlink driver.
I'm not sure about other driver.

If I have less PLC addresses, opening multiple page at same time is not  an issue.