using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.PointOfService;
using System.Collections;
namespace ScalePOS
{
class ScalePOS
{
static void Main(string[] args)
{
// Create a new instance of PosExplorer and use it to
// collect device information.
PosExplorer explorer = new PosExplorer();
DeviceCollection devices = explorer.GetDevices();
// Search all connected devices for an Scale, print its service object name
foreach (DeviceInfo device in devices)
{
if (device == null)
{
Console.WriteLine("device is null");
}
Console.WriteLine(device.ServiceObjectName);
Console.WriteLine(device.Type);
Console.WriteLine(device.HardwareId);
Console.ReadLine();
// It is important that applications close all open
}
}
}
}
I am trying to interface with a USB Scale and PosExplorer seems to not pick it up. When I run this code I get a bunch of Microsoft MSR,Scanner,Keylock simulators, but my scale is not picked up. Does anyone here know why not?
You can check for installed Service Objects through Visual Studio by opening the Server Explorer (View menu, then Server Explorer).
Once in the Server Explorer (which is presented as a tree), expand the "Servers" node, then your computer name node, then you can check for your particular device in either the "LogicalDevice", "POSDevice" or "ServiceObject" nodes.
I'd start with the "ServiceObject" node first!
Figured out what the issue was, I needed an OPOS Driver or a service object associated with the scale. As the manufacturer did not provide one I needed to create my own.
Related
I get up to a certain point in a desktop app that I'm automating and I need to click a link and continue automating in a browser. The link automatically goes to internet explorer.(suggestions on how to copy and paste that into chrome would be appreciated). I need to know how to switch from the desktop to the webview, automate the web view and go back to the desktop view.
So far this is the closest thing I've found that solves the problem. I'm newer to c# so I know the theory of what I'd like to do, just not how to implement it. http://appium.io/docs/en/writing-running-appium/web/hybrid/. I've got so far as to log the context to my output. I haven't been able to set or reset it yet.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Remote;
namespace UnitTestProject2
{
[TestClass]
public class GoldTrakPCTest
{
[TestMethod]
public void TestMethod1()
{
AppiumOptions options = new AppiumOptions();
options.AddAdditionalCapability("deviceName", "WindowsPC");
options.AddAdditionalCapability("platformName", "Windows");
options.AddAdditionalCapability("app", "XXXX -Path to desktop app");
WindowsDriver<WindowsElement> windowsDriver = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723/"), options);
Thread.Sleep(1500);
windowsDriver.FindElementByAccessibilityId("1006").SendKeys("Username");
windowsDriver.FindElementByAccessibilityId("1003").SendKeys("Password");
windowsDriver.FindElementByAccessibilityId("1001").SendKeys("354 - B - Mariner");
windowsDriver.FindElementByAccessibilityId("1").Click();
var myVar = windowsDriver.FindElementByAccessibilityId("59393");
Thread.Sleep(4200);
//Trace.WriteLine(windowsDriver.FindElementByName("WILLY WONKA"));
windowsDriver.FindElementByName("WILLY WONKA").Click();
Thread.Sleep(1000);
windowsDriver.FindElementByAccessibilityId("1310").Click();
Thread.Sleep(4000);
windowsDriver.FindElementByName("Documents").Click();
Thread.Sleep(4000);
windowsDriver.FindElementByAccessibilityId("1011").Click();
windowsDriver.FindElementByAccessibilityId("1011").Click();
windowsDriver.FindElementByName("Doc Set - WI Esign Documents").Click();
windowsDriver.FindElementByAccessibilityId("4750").Click();
Thread.Sleep(4000);
windowsDriver.FindElementByName("OK").Click();
windowsDriver.FindElementByAccessibilityId("2034").Click();
Thread.Sleep(4000);
string Context = windowsDriver.Context;
Trace.WriteLine(Context);
/*List<string> AllContexts = new List<string>();
foreach (var context in (windowsDriver.Contexts))
{
AllContexts.Add(context);
Trace.WriteLine(context);
}*/
//Trace.WriteLine(AllContexts, "Hello");
//options.AddAdditionalCapability("")
//windowsDriver.FindElementByName("WILLY WONKA").Click();
//windowsDriver.FindElementByName("Next").Click();
Thread.Sleep(6000);
//windowsDriver.Close();
}
}
}
I need to know how to change context and if it's possible to switch between native windows desktop apps and browsers and how to do this.
It is possible. I have 2 desktop application and 1 web application all running in tandem. However, I have one question regarding "The link automatically goes to internet explorer.(suggestions on how to copy and paste that into chrome would be appreciated)."
is it because Internet Explorer is the default explorer in windows?
Switching context between apps.
As soon as u click the windowselement that opens Web (IE or chrome), you'll need to create Webdriver instance (IE/chrome)and attach it to the browser process
A test initialize method which launches/attaches to an existing session :
How to use/attach an existing browser using Selenium?
I'm trying to automate configuring remote hosts, we have hundreds of these devices, we normally do it through USB programming, but if I could get a script to connect to these devices and do it programmatically, it would free up time.
These devices run some type of linux os, i'm not sure exactly, but they do have SSH enabled and confirm server host keys when you first connect to them via utility like PuTTY.
For now, i'm just trying to initiate an SSH session with the device. I've done quite a bit of research, and have come up with this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Renci.SshNet;
using Renci.SshNet.Common;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//Connection information
string user = "admin";
string pass = "********";
string host = "IP Address";
//Set up the SSH connection
using (var client = new SshClient(host, user, pass))
{
//Accept Host key
client.HostKeyReceived += delegate (object sender, HostKeyEventArgs e)
{
e.CanTrust = true;
};
//Start the connection
client.Connect();
var output = client.RunCommand("show device details");
client.Disconnect();
Console.WriteLine(output.ToString());
Console.ReadLine();
}
}
}
}
The problem is this doesn't seem to execute the command listed. The console window comes up, and I can access the same device by WebGUI and see the log file, it shows a connection being made, but when I break the execution and see the variable values the output variable shows null.
If I let the execution sit, with the console window open (just shows a blinking cursor in the upper left), the connection times out after 10 minutes and connection is lost, which I also see happen in the device log.
Why would does this not seem to execute the runcommand and store the results in the output variable?
When you execute the RunCommand() method on an object of type Renci.SshNet.SshClient, it does not return the result as a variable.
Instead, it returns an object of the Renci.SshNet.SshCommand type.
The issue is that, it looks like you can't fit this resultant SshCommand object into a var.
This Renci.SshNet.SshCommand, returned when you execute RunCommand(), will contain several properties and methods.
The properties are:
CommandText
CommandTimeout
ExitStatus
OutputStream
ExtendedOutputStream
Result
Error
They're all useful, but as everything else seems to be working, the only relevant one you want is "Result".
The "Result" property will contain a String, which will be the host stream result of the command you provided to RunCommand().
As you mention the device's logfile has logged a successful connection being made, it looks like the connection is successful. So you'd just have to make the proper tweak to grab the Result, as described above, and you should be good to go.
Addendum:
The following line in the original post's code:
var output = client.RunCommand("show device details");
Should be replaced with this code:
var output = client.RunCommand("show device details").Result;
This will assign the Result property (which is a String) to the output var, which will give the desired outcome.
I wrote a program using C# and make exe file using advanced installer and it work good but i want to make this exe file work in one computer, because some clints
take exe and give this exe to another and i want to privint that and protect my works
run the code below on the machine that you want your .exe. file to work on (it will give you this machines MAC address).
2. Add this MAC address to the code below
3. Add the code below to your C# code to ensure that it only runs on the machine with the correct MAC address (unique)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
namespace OneMachine
{
class Program
{
static void Main(string[] args)
{
string clientMAC = "XX-XX-XX-XX-XX-XX"; // put the correct value here when you know it
string thisComputerMAC = GetMACAddress2();
Console.WriteLine("MAC:" + thisComputerMAC); // remove this when necessary
if (clientMAC == thisComputerMAC)
{
Console.WriteLine("this is the right computer");
} else
{
Console.WriteLine("PROGRAM WONT RUN ON THIS COMPUTER");
}
}
public static string GetMACAddress2()
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
String sMacAddress = string.Empty;
foreach (NetworkInterface adapter in nics)
{
if (sMacAddress == String.Empty)// only return MAC Address from first card
{
//IPInterfaceProperties properties = adapter.GetIPProperties(); Line is not required
sMacAddress = adapter.GetPhysicalAddress().ToString();
}
} return sMacAddress;
}
}
}
reference: C# Get Computer's MAC address "OFFLINE"
I think what you want would be some sort of licence key and an authorization method.
A quick google turned up this article which you may find useful.
http://www.codeproject.com/Articles/28678/Generating-Unique-Key-Finger-Print-for-a-Computer
u can create the security constrain for exe like
by Giving unique password
By Entering the serial Key i.e. Licence Key which will know to u only or create random serial key generator based on the system.
You can allow only one user run your program by using a hardware ID to identify the user using the application or you can use a licensing system.
I am executing the example code of LibUsbDotNet which will return me the information of all connected usb devices. You can find this code below.
using System;
using LibUsbDotNet;
using LibUsbDotNet.Info;
using LibUsbDotNet.Main;
using System.Collections.ObjectModel;
namespace Examples
{
internal class ShowInfo
{
public static UsbDevice MyUsbDevice;
public static void Main(string[] args)
{
// Dump all devices and descriptor information to console output.
UsbRegDeviceList allDevices = UsbDevice.AllDevices;
foreach (UsbRegistry usbRegistry in allDevices)
{
if (usbRegistry.Open(out MyUsbDevice))
{
Console.WriteLine(MyUsbDevice.Info.ToString());
for (int iConfig = 0; iConfig < MyUsbDevice.Configs.Count; iConfig++)
{
UsbConfigInfo configInfo = MyUsbDevice.Configs[iConfig];
Console.WriteLine(configInfo.ToString());
ReadOnlyCollection<UsbInterfaceInfo> interfaceList = configInfo.InterfaceInfoList;
for (int iInterface = 0; iInterface < interfaceList.Count; iInterface++)
{
UsbInterfaceInfo interfaceInfo = interfaceList[iInterface];
Console.WriteLine(interfaceInfo.ToString());
ReadOnlyCollection<UsbEndpointInfo> endpointList = interfaceInfo.EndpointInfoList;
for (int iEndpoint = 0; iEndpoint < endpointList.Count; iEndpoint++)
{
Console.WriteLine(endpointList[iEndpoint].ToString());
}
}
}
}
}
// Free usb resources.
// This is necessary for libusb-1.0 and Linux compatibility.
UsbDevice.Exit();
// Wait for user input..
Console.ReadKey();
}
}
}
My problem is that the second line executed in the code:
UsbRegDeviceList allDevices = UsbDevice.AllDevices;
does not not return any device at all, while I do have the device I want to find connected, my keyboard and mouse.
Has anyone encountered this problem before? And/or does anyone know how to solve it?
Thanks in Advance!
Milan van Dijck
Does libusb support HID devices?
On Windows, the native Windows HID driver is supported by libusb, but there are some limitations, such as not being able to access HID mice and keyboards, as they are system reserved, as well as getting a direct read of HID report descriptors. Apart from that, you should be communicate with an HID device as you would with any other USB device.
If your application will revolve around HID access, you are encouraged to try to use the ​HIDAPI library by Signal 11 Software, which is also cross-platform. It uses native HID API under Windows and Mac OS X and can use libusb or hidraw as the backend under Linux.
The documentation says that
Gets a list of all available USB devices (WinUsb, LibUsb, Linux LibUsb v1.x).
and
Use this property to get a list of USB device that can be accessed by LibUsbDotNet.
If you are using the standard HID driver for your mouse and keyboard and haven't replaced that with the libusb.sys driver, then LibUsbDotNet can't access those devices and therefore doesn't list them.
I am trying to create a little helper application, one scenario is "file duplication finder". What I want to do is this:
I start my C# .NET app, it gives me an empty list.
Start the normal windows explorer, select a file in some folder
The C# app tells me stuff about this file (e.g. duplicates)
How can I monitor the currently selected file in the "normal" windows explorer instance. Do I have to start the instance using .NET to have a handle of the process. Do I need a handle, or is there some "global hook" I can monitor inside C#. Its a little bit like monitoring the clipboard, but not exactly the same...
Any help is appreciated (if you don't have code, just point me to the right interops, dlls or help pages :-) Thanks, Chris
EDIT 1 (current source, thanks to Mattias)
using SHDocVw;
using Shell32;
public static void ListExplorerWindows()
{
foreach (InternetExplorer ie in new ShellWindowsClass())
DebugExplorerInstance(ie);
}
public static void DebugExplorerInstance(InternetExplorer instance)
{
Debug.WriteLine("DebugExplorerInstance ".PadRight(30, '='));
Debug.WriteLine("FullName " + instance.FullName);
Debug.WriteLine("AdressBar " + instance.AddressBar);
var doc = instance.Document as IShellFolderViewDual ;
if (doc != null)
{
Debug.WriteLine(doc.Folder.Title);
foreach (FolderItem item in doc.SelectedItems())
{
Debug.WriteLine(item.Path);
}
}
}
You can do this with the shell automation interfaces. The basic process is to
Run Tlbimp on Shdocwv.dll and
Shell32.dll (or directly add a
reference from VS).
Create an
instance of the ShellWindows
collection and iterate. This will
contain both Windows Explorer and
Internet Explorer windows.
For
Windows Explorer windows, the
IWebBrowser2.Document property will
return a IShellFolderViewDual
reference.
The IShellFolderViewDual
has a SelectedItems method you can
query and an event for changes you
can handle.