Hi Guys,
this post is related to my previous post :
https://www.advancedhmi.com/forum/index.php?topic=2541.0I 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:
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?