ok, actually i imported some code from
https://github.com/bartashevich/serial-communicationI'm not very sure if my way of combining the PLC data (as you can see from 'SendTextBox' is correct. I actually just plus both data like this:
SendTextBox.Text = BasicLabel1.Text + BasicLabel2.Text
But so far the program is running fine. Here is the code for my DataPanelForm:
Public Class DataPanel
Dim Data_received As String
'*******************************************************************************
'* 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 ReturnToMainButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MainForm.Show()
' Me.Hide()
Me.Visible = False
End Sub
Private Sub MessageListByItem1_SelectedIndexChanged(sender As Object, e As EventArgs)
End Sub
Private Sub MessageListByValue1_SelectedIndexChanged(sender As Object, e As EventArgs)
End Sub
Private Sub SendTextBox_TextChanged(sender As Object, e As EventArgs) Handles SendTextBox.TextChanged
'combined data = data1 + data2 + data3 + .....
SendTextBox.Text = BasicLabel1.Text + BasicLabel2.Text
End Sub
Private Sub DataPanel_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Interval = 300
Timer1.Enabled = False
ModuleComPortParameters.configuration_is_valid = False
SendBtn.Enabled = False
End Sub
Private Sub ConfigurePortBtn_Click(sender As Object, e As EventArgs) Handles ConfigurePortBtn.Click
ConfigurationPort.StartPosition = FormStartPosition.CenterParent
ConfigurationPort.ShowDialog()
If ModuleComPortParameters.configuration_is_valid = True Then
With SerialPort1
.PortName = ModuleComPortParameters.Port
.BaudRate = ModuleComPortParameters.Baudrate
.Parity = ModuleComPortParameters.Parity
End With
End If
End Sub
Private Sub ExitBtn_Click(sender As Object, e As EventArgs) Handles ExitBtn.Click
End
End Sub
Private Sub OpenPortBtn_Click(sender As Object, e As EventArgs) Handles OpenPortBtn.Click
If ModuleComPortParameters.configuration_is_valid = True Then
If SerialPort1.IsOpen Then
SerialPort1.Close()
SendBtn.Enabled = False
OpenPortBtn.Text = "Open Port"
OpenPortBtn.BackColor = Color.LightGray
Timer1.Enabled = False
Else
SerialPort1.Open()
SendBtn.Enabled = True
OpenPortBtn.Text = "Close Port"
OpenPortBtn.BackColor = Color.Red
Timer1.Enabled = True
End If
Else
MsgBox("Cannot open port with an invalid configuration")
End If
End Sub
Private Sub SendBtn_Click(sender As Object, e As EventArgs) Handles SendBtn.Click
SerialPort1.Write(SendTextBox.Text)
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Data_received &= SerialPort1.ReadExisting
If Len(Data_received) > 0 Then
ReceiveTextBox.Text = Data_received
HistoryTextBox.Text = TimeOfDay.ToLongTimeString + " : " + Data_received + vbNewLine + HistoryTextBox.Text
Data_received = ""
End If
End Sub
End Class
and this the code for the ConfigurationPort:
Public Class ConfigurationPort
Private Sub Port_Conf_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim available_Ports As Array = IO.Ports.SerialPort.GetPortNames
Dim i As Integer
For i = 0 To UBound(available_Ports)
PortComboBox.Items.Add(available_Ports(i))
Next
BaudrateComboBox.Items.Clear()
BaudrateComboBox.Items.Add("4800")
BaudrateComboBox.Items.Add("9600")
BaudrateComboBox.Items.Add("19200")
BaudrateComboBox.SelectedIndex = 2
ParityComboBox.Items.Clear()
ParityComboBox.Items.Add(IO.Ports.Parity.None)
ParityComboBox.Items.Add(IO.Ports.Parity.Odd)
ParityComboBox.Items.Add(IO.Ports.Parity.Even)
ParityComboBox.SelectedIndex = 2
End Sub
Private Sub Cancel_Btn_Click(sender As Object, e As EventArgs) Handles CancelBtn.Click
ModuleComPortParameters.configuration_is_valid = False
Me.Close()
End Sub
Private Sub Ok_Btn_Click(sender As Object, e As EventArgs) Handles OkBtn.Click
ModuleComPortParameters.Port = PortComboBox.Text
ModuleComPortParameters.baudrate = BaudrateComboBox.Text
Select Case ParityComboBox.Text
Case "None"
ModuleComPortParameters.parity = IO.Ports.Parity.None
Case "Even"
ModuleComPortParameters.parity = IO.Ports.Parity.Even
Case "Odd"
ModuleComPortParameters.parity = IO.Ports.Parity.Odd
End Select
ModuleComPortParameters.configuration_is_valid = True
Me.Close()
End Sub
Private Sub Port_combo_SelectedIndexChanged(sender As Object, e As EventArgs) Handles PortComboBox.SelectedIndexChanged
End Sub
End Class
The results is shown in the attachment. I just need to know if the way i combined the data is correct. Currently i don't have my PLC data connected.