kianyuen
kianyuen
CC#
Created by kianyuen on 8/14/2024 in #help
Controls from WPF.UI can't render properly on WPF, .NET Framework 4.8
No description
20 replies
CC#
Created by kianyuen on 8/6/2024 in #help
How to stream bytes from SerialPort (System.IO.Ports) into a chart.
I'm using WPF on .NET Framework 4.8. The packet that I'm trying to catch is sent at 250Hz. I was under the assumption My current basic implementation is as follow: On port.DataReceived, I will collect the data then store it to another buffer to further process it later (to deal with potential last unfinished packet by just queueing them).
SerialPort spL = (SerialPort)sender;
byte[] buf = new byte[spL.BytesToRead];
spL.Read(buf, 0, buf.Length);
_receiveBuffer.AddRange(buf);
SerialPort spL = (SerialPort)sender;
byte[] buf = new byte[spL.BytesToRead];
spL.Read(buf, 0, buf.Length);
_receiveBuffer.AddRange(buf);
I personally think this is hacky and non-efficient, is there a better existing approach that I'm not aware of?
13 replies
CC#
Created by kianyuen on 7/26/2024 in #help
How should Error Status Code returned from a DLLImport function be handled?
//Output: None
//return: 0 - success, <0 - failure(refer to error code definition)
//Note: None
[DllImport("./libraries/something.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int OpenDevice(int nPort, int nBaud);
//Output: None
//return: 0 - success, <0 - failure(refer to error code definition)
//Note: None
[DllImport("./libraries/something.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int OpenDevice(int nPort, int nBaud);
I am thinking defining a different function that wrap it and throw exception. Or should I just use the function as is?
8 replies
CC#
Created by kianyuen on 7/18/2024 in #help
ViewModel Command ran when I debugged, but the View is not updated
This is my ViewModel:
public class MainViewModel : ViewModelBase
{
private ViewModelBase _currentChildView;
private string _title;

public ViewModelBase CurrentChildView
{
get
{
return _currentChildView;
}
set
{
_currentChildView = value;
OnPropertyChanged(nameof(CurrentChildView));
}
}
public string Title
{
get => _title;
set
{
_title = value;
OnPropertyChanged(nameof(Title));
}
}

// Commands for user interactions
public ICommand ShowBloodPressureViewCommand { get; }
public ICommand ShowBMIViewCommand { get; }
public ICommand ShowHeightWeightViewCommand { get; }


public MainViewModel()
{
ShowBloodPressureViewCommand = new RelayCommand(ExecuteShowBloodPressureViewCommand);
ShowBMIViewCommand = new RelayCommand(ExecuteShowBMIViewCommand);
ShowHeightWeightViewCommand = new RelayCommand(ExecuteShowHeightWeightViewCommand);

ExecuteShowBloodPressureViewCommand(null);
}

private void ExecuteShowHeightWeightViewCommand(object obj)
{
CurrentChildView = new HeightWeightViewModel();
Title = "Height Weight";

}

private void ExecuteShowBMIViewCommand(object obj)
{
CurrentChildView = new BMIViewModel();
Title = "BMI";
}

private void ExecuteShowBloodPressureViewCommand(object obj)
{
CurrentChildView = new BloodPressureViewModel();
Title = "Blood Pressure";
}
}
public class MainViewModel : ViewModelBase
{
private ViewModelBase _currentChildView;
private string _title;

public ViewModelBase CurrentChildView
{
get
{
return _currentChildView;
}
set
{
_currentChildView = value;
OnPropertyChanged(nameof(CurrentChildView));
}
}
public string Title
{
get => _title;
set
{
_title = value;
OnPropertyChanged(nameof(Title));
}
}

// Commands for user interactions
public ICommand ShowBloodPressureViewCommand { get; }
public ICommand ShowBMIViewCommand { get; }
public ICommand ShowHeightWeightViewCommand { get; }


public MainViewModel()
{
ShowBloodPressureViewCommand = new RelayCommand(ExecuteShowBloodPressureViewCommand);
ShowBMIViewCommand = new RelayCommand(ExecuteShowBMIViewCommand);
ShowHeightWeightViewCommand = new RelayCommand(ExecuteShowHeightWeightViewCommand);

ExecuteShowBloodPressureViewCommand(null);
}

private void ExecuteShowHeightWeightViewCommand(object obj)
{
CurrentChildView = new HeightWeightViewModel();
Title = "Height Weight";

}

private void ExecuteShowBMIViewCommand(object obj)
{
CurrentChildView = new BMIViewModel();
Title = "BMI";
}

private void ExecuteShowBloodPressureViewCommand(object obj)
{
CurrentChildView = new BloodPressureViewModel();
Title = "Blood Pressure";
}
}
When initialized, ExecuteShowBloodPressureViewCommand ran normally, but when I use UI to run other commands, the Execute function still ran, but the View doesn't update.
18 replies
CC#
Created by kianyuen on 7/10/2024 in #help
Managed Debugging Assistant 'PInvokeStackImbalance'
I have a dll file that is originally used in a .NET Framework 3.5 Project. I am trying to port that module to .NET Framework 4.8, but Managed Debugging Assistant keeps throwing the error mentioned above. Turning off the MDA allowed the program to run, but is that recommended? This is how I import a function from the dll file
[DllImport("CsnPrinterLibs.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern uint Port_EnumCOM(byte[] buffer, int length);
[DllImport("CsnPrinterLibs.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern uint Port_EnumCOM(byte[] buffer, int length);
11 replies