I need some help. I'm programming a Win 10 App in C++/CX. I am using two USB to RS485 devices, both of which have the same VID number. In days of old, I could write a bit software and connect to ports using good old COMx etc.
I'm now following the example here Serial Sample which uses the approach gathering the device info so when looking for connected devices, what I see in the list of available devices is the following.
\?\FTDIBUS#VID_0403+PID_6001
Both devices have the same VID and PID. This leads to the problem of me being cable to connect to the correct USB device. I think my app is trying to connect to both devices at the same time? Does anyone have any ideas about how I can resolve this hitch?
void MainPage::Get_Serial_Devices() {
cancellationTokenSource_Port1 = new Concurrency::cancellation_token_source();
cancellationTokenSource_Port2 = new Concurrency::cancellation_token_source();
// THIS USES ASYNCRONOUS OPERATION. GET A LIST OF SERIAL DEVICES AND POPULATE THE COMBO BOX
Concurrency::create_task(ListAvailablePortsAsync()).then([this](DeviceInformationCollection^ serialDeviceCollectioin)
{
// serialDeviceCollection CONTAINS ALL SERIAL DEVICES FOUND, COPY INTO _deviceCollection
DeviceInformationCollection^ _deviceCollection = serialDeviceCollectioin;
// CLEAR EXISTING DEVICES FOR OUR OBJECT COLLECTION
_availableDevices->Clear();
// FOR EVERY DEVICE IN _deviceCollection
for (auto &&device : _deviceCollection) {
if (device->Name->Equals("USB-RS485 Cable")) {
// CREATE A NEW DEVICE TYPE AND APPEND TO OUR OBJECT COLLECTION
_availableDevices->Append(ref new Device(device->Id, device));
Total_Ports++;
this->DeviceLists->Items->Append(device->Id);
}
}
});
void MainPage::ConnectButton_Click(Object^ sender, RoutedEventArgs^ e) {
if (Port1_Connected == false) {
// CAST INDEX TO CORRELATING Device IN _availableDevices
Device^ selectedDevice = static_cast<Device^>(_availableDevices->GetAt(Port_1_ID));
// GET THE DEVICE INFO
DeviceInformation^ entry = selectedDevice->DeviceInfo;
Concurrency::create_task(ConnectToSerialDeviceAsync_Port1(entry, cancellationTokenSource_Port1->get_token())).then([this]( ) {
Get_Echo();
Waiting_For_Ack = true;
});
}
Concurrency::task<void> MainPage::ConnectToSerialDeviceAsync_Port1(DeviceInformation^ device, Concurrency::cancellation_token cancellationToken) {
// CREATE A LINKED TOKEN WHICH IS CANCELLED WHEN THE PROVIDED TOKEN IS CANCELLED
auto childTokenSource = Concurrency::cancellation_token_source::create_linked_source(cancellationToken);
// GET THE TOKEN
auto childToken = childTokenSource.get_token();
// CONNECT TO ARDUINO TASK
return Concurrency::create_task(SerialDevice::FromIdAsync(device->Id), childToken).then([this](SerialDevice^ serial_device) {
try {
_serialPort_Port1 = serial_device;
TimeSpan _timeOut; _timeOut.Duration = 10;
// CONFIGURE SERIAL PORT SETTINGS
_serialPort_Port1->WriteTimeout = _timeOut;
_serialPort_Port1->ReadTimeout = _timeOut;
_serialPort_Port1->BaudRate = 57600;
_serialPort_Port1->Parity = Windows::Devices::SerialCommunication::SerialParity::None;
_serialPort_Port1->StopBits = Windows::Devices::SerialCommunication::SerialStopBitCount::One;
_serialPort_Port1->DataBits = 8;
_serialPort_Port1->Handshake = Windows::Devices::SerialCommunication::SerialHandshake::None;
// CREATE OUR DATA READER OBJECT
_dataReaderObject_Port1 = ref new DataReader(_serialPort_Port1->InputStream);
_dataReaderObject_Port1->InputStreamOptions = InputStreamOptions::None;
// CREATE OUR DATA WRITE OBJECT
_dataWriterObject_Port1 = ref new DataWriter(_serialPort_Port1->OutputStream);
this->ConnectButton->IsEnabled = false;
this->DisconnectButton->IsEnabled = true;
// KICK OF THE SERIAL PORT LISTENING PROCESS
Listen_Port1();
}
catch (Platform::Exception^ ex) {
this->Error_Window->Text = (ex->Message);
CloseDevice(PORT_1);
}
});
FT_PROG is a free EEPROM programming utility for use with FTDI devices. It is used for modifying EEPROM contents that store the FTDI device descriptors to customize designs.
The full FT_PROG User Guide can be downloaded here.
Related
How do I use VirtualMachineConfigSpec to set device change and configure my new clone to another distributed virtual port? My cloned VM network adapter type is VirtualVmxnet3, I would like to configure the 10.xx.xxx.xx-x_vm_ddc (dv port name) dv switch upon successful clone.
Following clone method takes a source vm (powered off) as template for new clone:
public ResponseEntity<?> cloneRhcosVm(#PathVariable String vcenter, #RequestBody VmClone vmClone) {
VirtualMachine vm = null;
VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
VirtualMachineConfigSpec configSpec = new VirtualMachineConfigSpec();
VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec();
VirtualMachineBootOptions boot = new VirtualMachineBootOptions();
VirtualMachineBootOptionsBootableCdromDevice isoBoot = new
VirtualMachineBootOptionsBootableCdromDevice();
VirtualMachineBootOptionsBootableDevice[] bootOrder = new
VirtualMachineBootOptionsBootableDevice[]{isoBoot};
List<VirtualDeviceConfigSpec> vdSpecAll = new ArrayList<VirtualDeviceConfigSpec>();
VirtualDeviceConfigSpec[] vdSpecArray = new VirtualDeviceConfigSpec[]{};
try {
String sourceVmName = vmClone.getSourceVmName();
String cloneVmName = vmClone.getTargetVmName();
vm = vmService.getVirtualMachine(vcenter, sourceVmName);
int cloneVmCPUs = vmClone.getTargetVmCPUs();
long cloneVmMemoryMB = (long) vmClone.getTargetVmMemoryGB() * Constants.MB_TO_GB;
String cloneVmNicName = vmClone.getTargetVmNicName();
boot.setBootOrder(bootOrder);
// Get target nic (dv switch) virutal device
VirtualDeviceConfigSpec nicSpec = getTargetNicVDConfigSpec(vm, cloneVmNicName);
if (nicSpec != null) {
vdSpecAll.add(nicSpec);
}
vdSpecArray = vdSpecAll.toArray(new VirtualDeviceConfigSpec[vdSpecAll.size()]);
configSpec.setDeviceChange(vdSpecArray);
configSpec.setName(cloneVmName);
configSpec.setNumCPUs(cloneVmCPUs);
configSpec.setNumCoresPerSocket(cloneVmCPUs);
configSpec.setMemoryMB(cloneVmMemoryMB);
configSpec.setBootOptions(boot);
cloneSpec.setConfig(configSpec);
cloneSpec.setLocation(relocSpec);
cloneSpec.setPowerOn(true);
cloneSpec.setTemplate(false);
Task task = vm.cloneVM_Task((Folder) vm.getParent(), cloneVmName, cloneSpec);
TaskInfo taskInfo = task.getTaskInfo();
...
}
Following method used to configure new nic virtual device: targetNicName is the new 10.xx.xxx.xx-x_vm_ddc I want to set to after cloning
private VirtualDeviceConfigSpec getTargetNicVDConfigSpec(VirtualMachine vm, String targetNicName) {
VirtualDevice[] vds = vm.getConfig().getHardware().getDevice();
VirtualDeviceConfigSpec nicSpec = new VirtualDeviceConfigSpec();
VirtualDeviceConnectInfo deviceConnInfo = new VirtualDeviceConnectInfo();
String adapter1 = "Network adapter 1";
for (int i = 0; i < vds.length; i++) {
if ((vds[i] instanceof VirtualEthernetCard) && (vds[i].getDeviceInfo().getLabel().equalsIgnoreCase(adapter1))) {
VirtualEthernetCard nic = (VirtualEthernetCard) vds[i];
VirtualEthernetCardNetworkBackingInfo nicBacking = (VirtualEthernetCardNetworkBackingInfo) nic.getBacking();
deviceConnInfo.setConnected(true);
nicBacking.setDeviceName(targetNicName);
nic.setBacking(nicBacking);
nic.setConnectable(deviceConnInfo);
nicSpec.setOperation(VirtualDeviceConfigSpecOperation.edit);
nicSpec.setDevice(nic);
return nicSpec;
}
}
return null;
}
Above code works but the new network adapter is not being configured as a dvSwitch as I was expecting by setting the new device name.
I found that my network adapter virtual device (vd) is of class VirtualVmxnet3 and the vd's backing info is of class VirtualEthernetCardDistributedVirtualPortBackingInfo, however these classes do not have any method that works like .setDevice(nicName) to configure the network adapter to new dvSwitch by passing name 10.xx.xxx.xx-x_vm_ddc.
Any clues if it's possible to clone and configure to another dv port?
I am trying to write bool value to PLC Tag (turn relay on or off) using OPC UA as Client writing to OPC UA Server running on Siemens S7-1512 PLC. The client must be implemented in c/c++
I have tried a few different GUI clients with no issues.
Also, I have tried Python SDK including freeopcua. I had slight issues but was able to write value after setting an attribute in the write request. But with open62541 I can't find any solution to that (status code is good but value is not changed I am able to read values):
Python working request:
node.set_attribute(ua.AttributeIds.Value, ua.DataValue(not node.get_value()))
C not working request code below:
UA_WriteRequest request;
UA_WriteRequest_init(&request);
request.nodesToWrite = UA_WriteValue_new();
request.nodesToWriteSize = 1;
request.nodesToWrite[0].nodeId = UA_NODEID_STRING_ALLOC(3, "\"VALUE\"");
request.nodesToWrite[0].attributeId = UA_ATTRIBUTEID_VALUE;
request.nodesToWrite[0].value.hasValue = true;
request.nodesToWrite[0].value.value.type = &UA_TYPES[UA_TYPES_BOOLEAN];
request.nodesToWrite[0].value.value.storageType = UA_VARIANT_DATA_NODELETE;
request.nodesToWrite[0].value.hasServerTimestamp = true;
request.nodesToWrite[0].value.hasSourceTimestamp = true;
request.nodesToWrite[0].value.sourceTimestamp = UA_DateTime_now();
request.nodesToWrite[0].value.value.data = val;
request.requestHeader.timestamp = UA_DateTime_now();
request.requestHeader.authenticationToken = UA_NODEID_NUMERIC(0, UA_NS0ID_SESSIONAUTHENTICATIONTOKEN);
//write to client service
UA_WriteResponse wResp = UA_Client_Service_write(client, request);
I expect value for PLC tag to be changed to the opposite value or provide me with info on why it won't work.
Better use the client high level api:
UA_NodeId nodeid = UA_NODEID_STRING_ALLOC(3, "\"VALUE\"");
UA_Boolean value = true;
UA_Variant *var= UA_Variant_new();
UA_Variant_setScalarCopy(var, &value, &UA_TYPES[UA_TYPES_BOOLEAN]);
UA_StatusCode ret = UA_Client_writeValueAttribute(client, nodeid, var);
....
UA_Variant_delete(var);
The reason it is rejected, is that you trie to set the Timestamp in your write request. Most server refuse that.
I am trying to query a list of available video capture devices (webcams) on windows using gstreamer 1.0 in c++.
I am using ksvideosrc as source and i am able to capture the video input but i can't query a list of available devices (and their caps).
On gstreamer 0.10 it has been possible through GstPropertyProbe which is removed in gstreamer 1.0. The documentation suggests using GstDeviceMonitor. But i have no luck using that either.
Has anyone succeeded in acquiring a list of device names? Or can you suggests another way of retrieving the available device names and their caps?
You can use GstDeviceMonitor and gst_device_monitor_get_devices () function.
First initialize GstDeviceMonitor by gst_device_monitor_new().
Second start the monitor by gst_device_monitor_start(pMonitor).
Third, get devices list by gst_device_monitor_get_devices(pMonitor).
Code would be like this:
GstDeviceMonitor* monitor= gst_device_monitor_new();
if(!gst_device_monitor_start(monitor)){
printf("WARNING: Monitor couldn't started!!\n");
}
GList* devices = gst_device_monitor_get_devices(monitor);
My references:
https://gstreamer.freedesktop.org/data/doc/gstreamer/head/gstreamer/html/gstreamer-GstDeviceMonitor.html#gst-device-monitor-get-devices
Although I haven't figured out how to enumerate the device names, I've come up with a workaround to at least get the available ksvideosrc device indexes. Below is the code in Python, but you should be able to port it to C++ fairly easily, thanks to the GObject introspection bindings.
from gi.repository import Gst
def get_ksvideosrc_device_indexes():
device_index = 0
video_src = Gst.ElementFactory.make('ksvideosrc')
state_change_code = None
while True:
video_src.set_state(Gst.State.NULL)
video_src.set_property('device-index', device_index)
state_change_code = video_src.set_state(Gst.State.READY)
if state_change_code != Gst.StateChangeReturn.SUCCESS:
video_src.set_state(Gst.State.NULL)
break
device_index += 1
return range(device_index)
if __name__ == '__main__':
Gst.init()
print get_ksvideosrc_device_indexes()
Note that the video source device-name property is None as of GStreamer version 1.4.5.0 on Windows for the ksvideosrc.
It's very late, but for the future...
The Gst.DeviceMonitor can be used to enumerate devices, and register an addition or removal of a device.
Here's how to get device names in C# with GStreamer 1.14
static class Devices
{
public static void Run(string[] args)
{
Application.Init(ref args);
GtkSharp.GstreamerSharp.ObjectManager.Initialize();
var devmon = new DeviceMonitor();
// to show only cameras
// var caps = new Caps("video/x-raw");
// var filtId = devmon.AddFilter("Video/Source", caps);
var bus = devmon.Bus;
bus.AddWatch(OnBusMessage);
if (!devmon.Start())
{
"Device monitor cannot start".PrintErr();
return;
}
Console.WriteLine("Video devices count = " + devmon.Devices.Length);
foreach (var dev in devmon.Devices)
DumpDevice(dev);
var loop = new GLib.MainLoop();
loop.Run();
}
static void DumpDevice(Device d)
{
Console.WriteLine($"{d.DeviceClass} : {d.DisplayName} : {d.Name} ");
}
static bool OnBusMessage(Bus bus, Message message)
{
switch (message.Type)
{
case MessageType.DeviceAdded:
{
var dev = message.ParseDeviceAdded();
Console.WriteLine("Device added: ");
DumpDevice(dev);
break;
}
case MessageType.DeviceRemoved:
{
var dev = message.ParseDeviceRemoved();
Console.WriteLine("Device removed: ");
DumpDevice(dev);
break;
}
}
return true;
}
}
I have been looking all over, and couldn't find any example for an RTD CLIENT (many RTD server samples, though).
My goal is to 'pull' data from an RTD server into my application for algo-trading purposes.
If possible, without using C# / .Net, as I am looking for a lightweight, deploy-able solution.
Can you give me any tips?
Here is a C# client I built as a test harness for Excel RTD servers (both in-process DLL and out-of-process EXE):
using System;
using System.Reflection;
using System.Threading;
namespace MyRTD
{
class Program
{
// ProgIDs for COM classes.
private const String RTDProgID = "MyRTD.RTD";
private const String RTDUpdateEventProgID = "MyRTD.UpdateEvent";
private const String RTDEXEProgID = "MyRTDEXE.RTD";
private const String RTDEXEUpdateEventProgID = "MyRTDEXE.UpdateEvent";
// Dummy topic.
private const int topicID = 12345;
private const String topic = "topic";
static void Main(string[] args)
{
Console.WriteLine("Test in-process (DLL) RTD server.");
TestMyRTD(RTDProgID,RTDUpdateEventProgID);
Console.WriteLine("Test out-of-process (EXE) RTD server.");
TestMyRTD(RTDEXEProgID,RTDEXEUpdateEventProgID);
Console.WriteLine("Press enter to exit ...");
Console.ReadLine();
}
static void TestMyRTD(String rtdID, String eventID)
{
try
{
// Create the RTD server.
Type rtd;
Object rtdServer = null;
rtd = Type.GetTypeFromProgID(rtdID);
rtdServer = Activator.CreateInstance(rtd);
Console.WriteLine("rtdServer = {0}", rtdServer.ToString());
// Create a callback event.
Type update;
Object updateEvent = null;
update = Type.GetTypeFromProgID(eventID);
updateEvent = Activator.CreateInstance(update);
Console.WriteLine("updateEvent = {0}", updateEvent.ToString());
// Start the RTD server.
Object[] param = new Object[1];
param[0] = updateEvent;
MethodInfo method = rtd.GetMethod("ServerStart");
Object ret; // Return value.
ret = method.Invoke(rtdServer, param);
Console.WriteLine("ret for 'ServerStart()' = {0}", ret.ToString());
// Request data from the RTD server.
Object[] topics = new Object[1];
topics[0] = topic;
Boolean newData = true; // Request new data, not cached data.
param = new Object[3];
param[0] = topicID;
param[1] = topics;
param[2] = newData;
method = rtd.GetMethod("ConnectData");
ret = method.Invoke(rtdServer, param);
Console.WriteLine("ret for 'ConnectData()' = {0}", ret.ToString());
// Loop and wait for RTD to notify (via callback) that
// data is available.
int count = 0;
do
{
count++;
// Check that the RTD server is still alive.
Object status;
param = null;
method = rtd.GetMethod("Heartbeat");
status = method.Invoke(rtdServer, param);
Console.WriteLine("status for 'Heartbeat()' = {0}", status.ToString());
// Get data from the RTD server.
int topicCount = 0;
param = new Object[1];
param[0] = topicCount;
method = rtd.GetMethod("RefreshData");
Object[,] retval = new Object[2, 1];
retval = (Object[,])method.Invoke(rtdServer, param);
Console.WriteLine("retval for 'RefreshData()' = {0}", retval[1,0].ToString());
// Wait for 2 seconds before getting
// more data from the RTD server.
Thread.Sleep(2000);
} while (count < 5); // Loop 5 times.
// Disconnect from data topic.
param = new Object[1];
param[0] = topicID;
method = rtd.GetMethod("DisconnectData");
method.Invoke(rtdServer, param);
// Shutdown the RTD server.
param = null;
method = rtd.GetMethod("ServerTerminate");
method.Invoke(rtdServer, param);
}
catch (Exception e)
{
Console.WriteLine("Error: {0} ", e.Message);
}
}
}
}
You can indeed create RTD "clients" outside Excel by emulating the calls that Excel would make to the RTD server. The RTD server is, after all, just a COM component that implements IRtdServer (and IRTDUpdateEvent for the callback).
You must follow the call sequence that Excel itself uses when interacting with the RTD. But once you do that, the RTD should quite happily pump data into your "client". Indeed, there might be an advantage to doing this because whereas Excel will only pull data from the RTD about every two seconds, your client can pull data as fast as it wants. This is certainly an advantage for algorithmic trading.
Whether such a client can work side-by-side with Excel is something I have not tested.
You would use RTD because RTD is normally free and the API access adds $100/month/cient or more to the datafeed cost for the dataservice we are using
I have an issue regarding WMI connection through asp.net from Computer A (windows 2003 server) to Computer B (Windows XP)..
The error is as follows:
RPC server is unavailable..
There are a few steps that you must take in order to successfully leverage WMI connectivity. The basics are you must allow remote management on the target box of course. If you can’t RDP into it, chances are, you can’t remote manage anything else. This can also include Windows firewall issues too. Make sure your request can even get in at all.
Next, start simple. Can you even poll for the running processes on that box? Try to output all the running processes on the target box with System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetProcesses("machine-name"). If you can at least get some information on the box then the RPC message you are getting has to do with incorrect arguments being passed in, perhaps?
Anyways, I recently wrote a web application that allowed the users to find a server on the LAN and kill a target process there or start a new one. I did it in C# so the code snippet below is just what I used. It's not the best but its working in production right now:
public static class RemoteProcessAccess
{
public static void KillProcessByProcessID(string NameOfServer, string DomainName, string LogIn, string Password, int processID)
{
//#1 The vars for this static method
#region /// <variables> ...
string userName;
string password;
string machineName;
string myDomain;
Hashtable hs = new Hashtable();
ManagementScope mScope;
ConnectionOptions cnOptions;
ManagementObjectSearcher objSearcher;
ManagementOperationObserver opsObserver;
ManagementClass manageClass;
DirectoryEntry entry;
DirectorySearcher searcher;
DirectorySearcher userSearcher;
#endregion
//#2 Set the basics sent into the method
machineName = NameOfServer;
myDomain = DomainName;
userName = LogIn;
password = Password;
cnOptions = new ConnectionOptions();
cnOptions.Impersonation = ImpersonationLevel.Impersonate;
cnOptions.EnablePrivileges = true;
cnOptions.Username = myDomain + "\\" + userName;
cnOptions.Password = password;
mScope = new ManagementScope(#"\\" + machineName + #"\ROOT\CIMV2", cnOptions);
//#3 Begin Connection to Remote Box
mScope.Connect();
objSearcher = new ManagementObjectSearcher(String.Format("Select * from Win32_Process Where ProcessID = {0}", processID));
opsObserver = new ManagementOperationObserver();
objSearcher.Scope = mScope;
string[] sep = { "\n", "\t" };
//#4 Loop through
foreach (ManagementObject obj in objSearcher.Get())
{
string caption = obj.GetText(TextFormat.Mof);
string[] split = caption.Split(sep, StringSplitOptions.RemoveEmptyEntries);
// Iterate through the splitter
for (int i = 0; i < split.Length; i++)
{
if (split[i].Split('=').Length > 1)
{
string[] procDetails = split[i].Split('=');
procDetails[1] = procDetails[1].Replace(#"""", "");
procDetails[1] = procDetails[1].Replace(';', ' ');
switch (procDetails[0].Trim().ToLower())
{
//You could look for any of the properties here and do something else,
case "processid":
int tmpProc = Convert.ToInt32(procDetails[1].ToString());
//if the process id equals the one passed in....
//(this is redundant since we should have limited the return
//by the query where above, but we're paranoid here
if (tmpProc.Equals(processID))
{
obj.InvokeMethod(opsObserver, "Terminate", null);
}
break;
}//end process ID switch...
}//end our if statement...
}//end our for loop...
}//end our for each loop...
}//end static method
}
Look at KB875605 ("How to troubleshoot WMI-related issues in Windows XP SP2")
You could enable the RPC server on any target machine by running this on the target's command prompt:
[/code]
netsh firewall set service RemoteAdmin
[/code]
Worked for me at least. :)
Try to use wmic command line to get information from the remote computer, also you can install the code of Services+ and try to connect and debug your connection to the server, most likely it is firewall problem or RPC services is down or disabled.