|
With the following code:
public SampleIndicator ()
{
_tradeManager = new(TradingManager ); <- Here the problem occurs !!!
}
I want to "inject" TradingManager into my own object _tradeManager which already works as expectet.
The problem is the errors message I get from the logging window of the app that uses my plugin dll.
Unfortunaltely I do not have access to the source code of that app, only to my dll.
|
|
|
|
|
That's not how you do dependency injection.
Your Indicator class is useless as it has no way of setting its TradingManager property. Also, the JsonIgnoreAttribute is only useful if you're serializing the Indicator class to a file. I'm leaving the Indicator out of your code.
class MyTradingManager : ITradingManager
{
... Code to implement the ITradingManager interface ...
}
internal class SampleIndicator : ExtendedIndicator
{
private ITradingManager _tradingManager;
public SampleIndicator(ITradingManager tradingManager)
{
_tradingManager = tradingManager;
}
... Code that uses the ITradingManager implementation ...
}
Note, for this to work, you cannot make SampleIndicator dependent on MyTradingManager. It should take an instance of some implementation of ITradingManager instead. MyTradingManager is that implementation. Whatever code is creating instances of this stuff has to create an instance of MyTradingManager and pass that to the constructor of SampleIndicator:
{
...
ITradingManager manager = new MyTradingManager();
SampleIndicator indicator = new SampleIndicator(manager);
...
}
|
|
|
|
|
public class Student
{
int id = 1234;
String name = "gustav";
bool state = true;
}
It should print:
Student:
1234
gustav
true
|
|
|
|
|
The Student class should NOT CARE AT ALL about outputting anything to the console or other output. It should be just limited to managing the data for a single Student.
One problem I see if your Student class should expose its data in public properties, not fields.
Having said that, whatever UI code is using this class in some way would be responsible for the presentation. To get the properties in the format you specified in your question, the easiest way to return the string would be to override the Student class ToString method and build the formatted string to return to the caller:
public class Student
{
public int ID { get; set; } = 1234;
public string Name { get; set; } = "gustav";
public bool State { get; set; } = true;
.
.
.
public override string ToString()
{
string result = $"{ID}\r\n{Name}\r\n{State}";
return result;
}
}
|
|
|
|
|
Hello,
thank you for the reply.
The underlying problem ist the following.
I want to save the property values of the objects I use in a file and I also want to be able to read it back from that file so that I have a kind of settings file for my app. For logging purposes I also want to be able to print the values to console or file. My understanding was that with serialization this could work.
|
|
|
|
|
Convert the private fields into public properties and you should be able to serialize these values as you see fit.
|
|
|
|
|
This should have been in your original question. What you're looking for is called "serialization". This is the process by which an object is written to a form that can transmitted and read by a matched deserializer and can be accomplished using JSON, XML, and Binary serializers.
My previous answer still stands for most serialization operations, you still need the public properties, but you do not need to override the ToString method.
You can read up on it at Serialization - .NET | Microsoft Learn[^]
|
|
|
|
|
Hello, I was able to identify a key press with a single key by the following code:
public override bool ProcessKeyDown(KeyEventArgs e)
{
if (e.IsRepeat) return false;
if (e.Key == Key.Escape)
{
}
}
But I was not able to find a solution that also identifies key combinations like:
CTRL + arrowup etc.
I also did not find anything that wokred with:
KeyEventArgs e
Any hints from your side ?
|
|
|
|
|
Try reading the documentation on KeyEventArgs Class[^].
You're looking for the Modifiers property.
|
|
|
|
|
public class Person : INotifyPropertyChanged
{
private string name;
public event PropertyChangedEventHandler PropertyChanged;
public string Name
{
get { return name; }
set
{
object before = Name;
name = value;
OnPropertyChanged("Name", before, Name);
}
}
void OnPropertyChanged(PropertyChangedEventArgs e, object? argvaluebefore = null, object? argnewvalue = null, [CallerMemberName] string argcallername = "", [CallerLineNumber] int argcallerline = 0)
{
PropertyChanged?.Invoke(this, e);
}
}
public class Master
{
Person _person = new();
public Master()
{
_person.PropertyChanged += MyHandler;
}
private void MyHandler(object? sender, PropertyChangedEventArgs e)
{
}
}
Hello,
I need to retrieve the propery values of class Person in MyHandler of Class Master.
Any ideas what I should do ?
Regards,
Chris
|
|
|
|
|
You already have this posted in QA: don't post the same question in two places - all you will do is duplicate work and annoy people. Pick either the C# forum or the Q&A and stick with it.
Annoyed people are less likely to be helpful than happy ones...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Yea, the following code was one of the examples I tried:
if (e.KeyCode == Keys.F1 && (e.Alt || e.Control || e.Shift))
{
}
It is from the source you mentioned, but i does not work.
Compiler says that
KeyEventArgs does not contain a definition for Alt etc.
Also there is no suggestion of how to fix it.
These are my usings:
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using System.Windows.Forms;
|
|
|
|
|
That's because they don't generate keycodes: ALT on it's own does nothing, it needs a keystroke to make it useful - ALT+F2, CTRL+F4 for example.
They are MODIFIER keys[^].
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hello I'm using this code from microsoft example but the information that I show from the red of the serial port is printed in ASCII, and I would like see in Mathlab.
Please could you help me.
THis is the code:
using System;
using System.IO.Ports;
class PortDataReceived
{
public static void Main()
{
SerialPort mySerialPort = new SerialPort("COM3");
mySerialPort.BaudRate = 56000;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.RtsEnable = true;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.Open();
Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
mySerialPort.Close();
}
private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.WriteLine("Data Received:" );
Console.Write(indata);
}
}
|
|
|
|
|
Please give an example of the data you are receiving, as well as samples of the data you want instead.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Data Received:
Data Received:
Data Received:
Data Received:
Data Received:
Data Received:
Data Received:
JData Received:
Data Received:
?Data Received:
?@Data Received:
Data Received:
Data Received:
Data Received:
Data Received:
LData Received:
Data Received:
?Data Received:
Data Received:
Data Received:
Data Received:
Data Received:
Data Received:
Data Received:
Data Received:
Data Received:
Data Received:
OData Received:
Data Received:
?
|
|
|
|
|
Victor Gonzalez 2024 wrote: string indata = sp.ReadExisting();
Absolutely no way that could be anything except a string. The encoding depends on various things but what is absolutely true is that binary data should not be accessed in that manner.
Binary data is not string data.
Victor Gonzalez 2024 wrote: and I would like see in Mathlab.
Binary data means you need to treat it like binary data.
Other than that if you are in fact reading data at all then either it is correct or the other end is sending it wrong (so nothing to do with your code.)
|
|
|
|
|
Then, how can I read binary data using c# code?
|
|
|
|
|
Victor Gonzalez 2024 wrote: how can I read binary data using c# code? That is not the right question to be asking. You are trying to get some information from a device attached to the serial port. So the first thing you need to do is to find out what form that information is presented in. If it is a sequence of ASCII characters then simple reads will do it. If it is variable length binary data then you need to read as a sequence of bytes which then need to be re-assembled into the correct form to match the sender. So you need to get that information before you can do anything else.
|
|
|
|
|
Hi Richard,
Thanks for you reply. I stand that you told me and I going to try to explai better.
I downloaded a third software that read information from the serial com, for example (open serial port monitor or serial port monitor). This sofware when I open the conection to the com port, start to read the information and print this information in the monitor.
The information that is printed, is in 3 type of data:
1st format:
à.....L.±......
...(.ëà.....L>¡
.........V.ëà..
...L.¢.........
..ëà.....L.£...
........ëà.....
L.§...........ë
2nd format:
e0 08 15 03 00 04 4c 00 b1 01 00 06 00 00 00
00 00 00 28 00 eb e0 09 15 03 00 04 4c 3e a1
00 06 00 00 00 00 00 00 00 56 00 eb e0 0a 15
03 00 04 4c 00 a2 00 00 00 00 00 00 00 00 00
14 00 eb e0 0b 15 03 00 04 4c 00 a3 00 00 00
00 00 00 00 00 00 16 00 eb e0 0c 15 03 00 04
4c 00 a7 00 00 00 00 00 00 00 00 00 1b 00 eb
3th format:
?
This 3 format that i copied, are the same information, but in different condification.
Then replying you "question": yes ,the device sent the information in different codification.
Then I would like to know how can I do for get the information in byte format, (2nd format of my example), because this information is the information that I need read and then work whit this.
I hope explained better.
Thanks!!
|
|
|
|
|
Victor Gonzalez 2024 wrote: the device sent the information in different codification. No, the device sends some information as a stream of bytes. The three different outputs are just your code printing it in different forms.
But my original response stands: the device (whatever that is) is sending some binary data. But you still need to find out what format that data is in. That is not something anyone here can do for you, as we have no idea what that device is or what information it provides.
|
|
|
|
|
Hi,
1.
to read binary data use the method SerialPort.Read(Byte[], Int32, Int32)[^]
2.
I seldom use the DataReceived event as it is the source of a number of problems. I'd rather use a separate thread (or a BackGroundWorker) that periodically calls a Read method.
3.
I am puzzled by the fact that your second data format shows 15 bytes per line, not 16 as is usual.
4.
I noticed your data seems to have a periodicity of 21 bytes; when reformatted it looks like this:
e0 08 15 03 00 04 4c 00 b1 01 00 06 00 00 00 00 00 00 28 00 eb
e0 09 15 03 00 04 4c 3e a1 00 06 00 00 00 00 00 00 00 56 00 eb
e0 0a 15 03 00 04 4c 00 a2 00 00 00 00 00 00 00 00 00 14 00 eb
e0 0b 15 03 00 04 4c 00 a3 00 00 00 00 00 00 00 00 00 16 00 eb
e0 0c 15 03 00 04 4c 00 a7 00 00 00 00 00 00 00 00 00 1b 00 eb
Hope this helps.
Luc Pattyn [My Articles]
The Windows 11 taskbar is a disgrace; a third-party add-on is needed to reverse the deterioration. I decline such a downgrade.
|
|
|
|
|
Hello all,
Yes periodicaly the data sended is 21 byte, but some times send 63 or 42 bytes.
I think that I found the solution and now I can red the data in decimanl and later I transofrm to "X".
Now I have another problem. When I start the program with out break point for debug, when I read the information i write to the screen, but this write not found, but if I put a break point and debug the progam, yes is writing the messages in the screen.
this is my code.
using System;
using System.IO.Ports;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
class PortDataReceived
{
public static void Main()
{
SerialPort mySerialPort = new SerialPort("COM3");
mySerialPort.BaudRate = 56000;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.RtsEnable = true;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.Open();
Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
mySerialPort.Close();
}
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
int count = sp.BytesToRead;
int count2 = 0;
byte[] data = new byte[count];
sp.Read(data, 0, data.Length);
switch (data.Length)
{
case 63:
Console.WriteLine("inicio carrera o pause off");
if (data[7].ToString("X").Equals("0") && data[8].ToString("X").Equals("A6"))
{
Console.WriteLine("pause off");
}
else
{
if (data[7].ToString("X").Equals("3E") && data[8].ToString("X").Equals("A1"))
{
Console.WriteLine("carrera go!");
}
}
break;
case 21:
if (data[7].ToString("X").Equals("1B") && data[8].ToString("X").Equals("A9"))
{
Console.WriteLine("contador vuelta y tiempo");
}
else
{
if (data[7].ToString("X").Equals("0") && data[8].ToString("X").Equals("A5"))
{
Console.WriteLine("pause on");
}
else
{
if (data[7].ToString("X").Equals("0") && data[8].ToString("X").Equals("A7"))
Console.WriteLine("stop forzado");
}
}
break ;
case 42:
Console.WriteLine(" fin carril");
break ;
}
sp.DiscardInBuffer();
}
|
|
|
|
|
Suggestions:
1.
don't use ToString("X"), use ToString("X2") instead, so you always get two characters
2.
don't use ToString at all, you can simply replace
data[8].ToString("X").Equals("A9") by
data[8]==0xA9
3. be warned, the DataReceived handler executes on a different thread and therefore cannot access GUI Controls directly. See for instance Invalid cross-thread operations[^]
Luc Pattyn [My Articles]
The Windows 11 taskbar is a disgrace; a third-party add-on is needed to reverse the deterioration. I decline such a downgrade.
|
|
|
|
|
Hi Luc Thanks!!
About my problem of the printing data, I detected that is because the program "it's more speed" than the reading of the COM port. I fixed temporaly using a sleep of 5 seconds, but I have another question.
This hardware have some sensors (now 2) and send information from the COM port, and if I "active" these two sensors at the same time, I receive the data in the same time and it's to difficult manipulate the data.
Could you help me to try to find a solution about these topic?
Thanks!!
|
|
|
|