AdvancedHMI Software

General Category => Support Questions => Topic started by: authorleon on April 19, 2018, 02:39:30 PM

Title: DF1Com1 in C# - Simple issue. I think
Post by: authorleon on April 19, 2018, 02:39:30 PM
Hello all,

I need to use C# because I need to use global hotkeys.

I am just setting up a basic program with one button, using an SLC 500.

Unfortunately I'm getting some errors and I don't know why.

I have had a look around but I don't know how to approach the problem.

Please have a look at the attached images.

Effectively I am just trying to get one button to work so it turns on output on the SLC 500.

I can get this working if I use .NET.  But fortunately I stated before I need to get it working in C#...

I hope I have explained it clearly. These have a look at the attached images
Title: Re: DF1Com1 in C# - Simple issue. I think
Post by: Archie on April 19, 2018, 10:08:29 PM
See this:

https://www.advancedhmi.com/forum/index.php?topic=2013.msg11296#msg11296
Title: Re: DF1Com1 in C# - Simple issue. I think
Post by: authorleon on April 21, 2018, 11:05:27 AM
Hello,

Fantastic... Right, I have done that. Also, I have VS 2017.

I have created a button, I am using DF1Com1 (AKA SerialDF1For5Com1). I assume that is the correct one. I run the program. the form comes up with the button. However, when I press the button, my AB SLC 500 does nothing. The relay does not toggle at all.

I am very close, thank you very much.

(https://i.imgur.com/wt78VRf.png)

(https://i.imgur.com/l0Z3aFA.png)

Title: Re: DF1Com1 in C# - Simple issue. I think
Post by: Archie on April 21, 2018, 11:24:08 AM
Is O:2/4 used in the ladder logic of the PLC?
Title: Re: DF1Com1 in C# - Simple issue. I think
Post by: authorleon on April 21, 2018, 11:27:56 AM
Nope..

I have an older application that does not include the C#
(https://i.imgur.com/nkbbLfO.png)

It works perfectly.

I have check that the com port is all good.

Thanks

Title: Re: DF1Com1 in C# - Simple issue. I think
Post by: authorleon on April 21, 2018, 11:28:38 AM
I have nothing in the PLC.. I.e. no code.. just 1 empty ladder

(https://i.imgur.com/DmzsJbe.png)
Title: Re: DF1Com1 in C# - Simple issue. I think
Post by: Archie on April 21, 2018, 11:38:51 AM
Try it on an internal bit such as B3/0. Also do you have RSLinx completely shutdown so the driver can get access to the com port?
Title: Re: DF1Com1 in C# - Simple issue. I think
Post by: authorleon on April 21, 2018, 11:47:09 AM
Hello, I gave it a go.. Still the same...

Yes, of course. I have closed down RSlinx 100%.

Would TeamV be an option... as I really would love to get this sorted. ?
Title: Re: DF1Com1 in C# - Simple issue. I think
Post by: Godra on April 21, 2018, 11:48:25 AM
The correct driver should be SerialDF1forSLCMicroCom.

You should also know all your COM1 settings and include them in the driver properties:
- BaudRate, change from AUTO to exact baud rate for your PLC
- TargetNode, is usually 1 but use RSLinx / RSLogix to confirm
- Parity
- ChecksumType

As Archie suggested, you should have 1 rung with contact set to B3:0/0 and coil set to O:2/4.
Then change the PLCAddressClick to B3:0/0 and maybe select Toggle as OutputType.
Title: Re: DF1Com1 in C# - Simple issue. I think
Post by: authorleon on April 21, 2018, 11:53:33 AM
The correct driver should be SerialDF1forSLCMicroCom.

You should also know all your COM1 settings and include them in the driver properties:
- BaudRate, change from AUTO to exact baud rate for your PLC
- TargetNode, is usually 1 but use RSLinx / RSLogix to confirm
- Parity
- ChecksumType

As Archie suggested, you should have 1 rung with contact set to B3:0/0 and coil set to O:2/4.
Then change the PLCAddressClick to B3:0/0 and maybe select Toggle as OutputType.

Hello,

I have all the config working:

(https://i.imgur.com/ZAY5IWl.png)

I am trying what you suggested now.

Title: Re: DF1Com1 in C# - Simple issue. I think
Post by: Godra on April 21, 2018, 12:06:28 PM
Can you post a picture of your RSLinx, just like the attached one?
Title: Re: DF1Com1 in C# - Simple issue. I think
Post by: Archie on April 21, 2018, 12:15:06 PM
You are not using the correct driver. The one you selected is for the PLC5, but I see you are using RSLogix500 which means you should be using the SLCMicro driver.
Title: Re: DF1Com1 in C# - Simple issue. I think
Post by: authorleon on April 21, 2018, 12:22:18 PM
Thank you both very much, it is now working so I just want to confirm a few things.

With the new version: X399 you cannot use a button with a direct output. e.g. O:1/1 etc. You have to use B3:0/0   (Make a ladder B3:0/0 > O:1/1 )

The second part is that I utterly have a piece of code which allows for the board shortcuts:

Code: [Select]
If (e.KeyCode And Not Keys.Modifiers) = Keys.T AndAlso e.Modifiers = Keys.Control Then
            DF1Com1.Write("O:1/0", "0")  ' (O:9/0) (R)
        End If

Could I have a little bit of help modifying this code so it would activate B3:0/0.

Once this has been achieved.

I will then focus on making global keyboard shortcuts. Thank you gents very much.

Title: Re: DF1Com1 in C# - Simple issue. I think
Post by: Godra on April 21, 2018, 12:45:35 PM
The code would be pretty much the same and for Toggle it could be like this:

Code: [Select]
    Private switch As Boolean
    Private Sub MainForm_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
        If (e.KeyCode And Not Keys.Modifiers) = Keys.T AndAlso e.Modifiers = Keys.Control Then
            If switch Then
                Me.SerialDF1forSLCMicroCom1.Write("B3:0/0", "0")
                switch = False
            Else
                Me.SerialDF1forSLCMicroCom1.Write("B3:0/0", "1")
                switch = True
            End If
        End If
    End Sub

or possibly this, which would be more accurate:

Code: [Select]
    Private Sub MainForm_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
        If (e.KeyCode And Not Keys.Modifiers) = Keys.T AndAlso e.Modifiers = Keys.Control Then
            'Read current B3:0/0 value
            Dim switch As Boolean = Me.SerialDF1forSLCMicroCom1.Read("B3:0/0")
            If switch Then
                Me.SerialDF1forSLCMicroCom1.Write("B3:0/0", "0")
            Else
                Me.SerialDF1forSLCMicroCom1.Write("B3:0/0", "1")
            End If
        End If
    End Sub
Title: Re: DF1Com1 in C# - Simple issue. I think
Post by: authorleon on April 21, 2018, 06:43:33 PM
Thank you very much... However I have realized that the code is in C#.

Here is the program had in the old application which worked. Of course I understand now that I have to use a B reference.

   
Code: [Select]
Private Sub MainForm_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown

        If (e.KeyCode And Not Keys.Modifiers) = Keys.T AndAlso e.Modifiers = Keys.Control Then
            DF1Com1.Write("O:1/0", "1")
        End If

    End Sub

    Private Sub MainForm_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp

        If (e.KeyCode And Not Keys.Modifiers) = Keys.T AndAlso e.Modifiers = Keys.Control Then
            DF1Com1.Write("O:1/0", "0")
        End If

    End Sub

Of course, I have now realized that the code is in C#.

Does anyone have the equivalent code in C# please. I have had to look online and consulted the Microsoft website but I tried that code and he did not work...

Much appreciated all.

Code: [Select]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AdvancedHMICS
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void basicButton1_Click(object sender, EventArgs e)
        {
         
        }
    }
}


Title: Re: DF1Com1 in C# - Simple issue. I think
Post by: Godra on April 21, 2018, 09:57:21 PM
You seem to be learning how to do programming in C#.
Maybe you should look up some tutorials online.

Currently you have a form with a driver and a BasicButton control on it.
Adding any more controls in the future will not make it any simpler for you.

There are some online code conversion tools which convert VB to C#, like this one:
https://www.carlosag.net/tools/codetranslator/

You could also use SharpDevelop 4.4 program which has code converter built-in.

As for C# in AdvancedHMI, you need to be using the MainForm inside the AdvancedHMIcs project.
You also need to get familiar with Properties window, like the attached picture shows.
Lightning Bolt icon will show all events associated with selected object, whether it's a form or a control.
Double-clicking any of the events will create a code for you and take you to the code window.

If you manually add a code that you find somewhere online then you still need to associate that code with the event by selecting it in the properties window.
Title: Re: DF1Com1 in C# - Simple issue. I think
Post by: authorleon on April 22, 2018, 06:12:37 AM
Fantastic !!!!!!!!!!!!!!! THANK YOU VERY MUCH!!!!!!!!!!

I love to learn and you have saved a lot of time!!!!!!!

Thank you again... A beer for you.
Title: Re: DF1Com1 in C# - Simple issue. I think
Post by: authorleon on April 28, 2018, 04:02:17 PM
Hello,

Right, I have some progress now. I have global hotkeys working which is fantastic....

I just need a little bit of help tweaking the code please.

 
Code: [Select]
public void hook_KeyPressed(object sender, KeyPressedEventArgs e)
        {
            if (e.Modifier == (Src.Hooks.ModifierKeys.Win | Src.Hooks.ModifierKeys.Alt) && e.Key == Keys.F12)
            {
                Me.SerialDF1forSLCMicroCom1.Write("B3:0/0", "0")
                switch = False
            }
            else if (e.Modifier == (Src.Hooks.ModifierKeys.Win | Src.Hooks.ModifierKeys.Alt) && e.Key == Keys.F11)
            {
                MessageBox.Show("Combinacion F11");
            }
            // show the keys pressed in a label.

        }


Please note, I do not want a toggle function. I would like you that when the button is pressed ("B3:0/0", "1"). And when the button is released ("B3:0/0", "0"). In other words the action only continues when the button is pressed and held down. Once released the bit is put back to zero.

Thank you very much...

Title: Re: DF1Com1 in C# - Simple issue. I think
Post by: authorleon on April 28, 2018, 04:22:18 PM
Right. I have the code working for toggle now....

For anyone, it if like this:

 
Code: [Select]
public void hook_KeyPressed(object sender, KeyPressedEventArgs e)
        {
            if (e.Modifier == (Src.Hooks.ModifierKeys.Win | Src.Hooks.ModifierKeys.Alt) && e.Key == Keys.F12)
            {
               
                serialDF1forSLCMicroCom1.Write ("B3:0/0", "1");
            }
            else if (e.Modifier == (Src.Hooks.ModifierKeys.Win | Src.Hooks.ModifierKeys.Alt) && e.Key == Keys.F11)
            {
                serialDF1forSLCMicroCom1.Write("B3:0/0", "0");

            }
            // show the keys pressed in a label.

        }


I jsut need help with keep up and release...

Thank you all very much
Title: Re: DF1Com1 in C# - Simple issue. I think
Post by: Godra on April 30, 2018, 05:54:41 PM
You seem to be looking to assign those functions to global hotkeys (which you mentioned in your other topic https://www.advancedhmi.com/forum/index.php?topic=2036.0).

There still doesn't seem to be any solution for that.