我正在使用SerialPort
从LoRa扫描仪阅读数据,该扫描仪的信号强度(RSSI)范围在-10和-110之间。我想仪器自动更新每次一个新的RSSI值读取。
我创建了一个serialPortConn
类,在其中我连接到扫描仪并使用名为SerialPort_DataReceived
的函数读取数据:
public void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
while (serialPort.BytesToRead > 0)
{
string DataIn = serialPort.ReadLine();
Debug.WriteLine("Data received from the scanner: " + DataIn);
// Process the received data and extract the fields
Network data = ProcessReceivedData(DataIn);
MainThread.BeginInvokeOnMainThread(() =>
{
// Check if the data name is not empty or null
if (!string.IsNullOrEmpty(data.Name))
{
// Find the existing network with the same name, if it exists
Network existingNetwork = NetworkNames.FirstOrDefault(network => network.Name == data.Name);
if (existingNetwork != null)
{
// Add the RSSI value to the existing network's RSSIList
// NetworkNames.Add(new Network { RSSI = data.RSSI });
// strength = data.RSSI;
Strength = data.RSSI;
existingNetwork.RSSI = data.RSSI;
existingNetwork.Name = data.Name;
Debug.WriteLine("strength: " + strength);
}
else
{
// Create a new network with the name and initialize its RSSIList
NetworkNames.Add(new Network { Name = data.Name, RSSI = data.RSSI });
// strength = data.RSSI;
Strength = data.RSSI;
}
// Print the network name and its RSSIList for debugging purposes
foreach (Network network in NetworkNames)
{
Debug.WriteLine("NetworkName: " + network.Name + " " + network.RSSI);
}
}
});
}
}
catch (OperationCanceledException)
{
// Handle the cancellation if needed
Debug.WriteLine("Reading data from the scanner was canceled.");
}
catch (Exception ex)
{
// Handle other exceptions
Debug.WriteLine($"Failed to read data from the scanner: {ex.Message}");
}
}
字符串
我将新的RSSI值保存到SerialPortConn
开头定义的strength
属性中,如下所示:
public class SerialPortConn : INotifyPropertyChanged
{
//public ObservableCollection<Network> NetworkNames { get; set; }
private SerialPort serialPort;
Network networkNames = new Network();
public ObservableCollection<Network> NetworkNames { get; set; } = new ObservableCollection<Network>();
private int strength;
public int Strength
{
get { return strength; }
//get => strength;
set
{
if (strength != value)
{
strength = value;
OnPropertyChanged(nameof(Strength));
//OnPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
型
在后面的内容页面代码中,我尝试将Strength
属性分配给NeedlePointer
值,如下所示:
namespace Scanner_MAUI.Pages;
public partial class HistoricalData : ContentPage, INotifyPropertyChanged
{
//public ObservableCollection<Network> NetworkNames { get; set; }
private SerialPortConn scannerConn;
public HistoricalData()
{
InitializeComponent();
needlePointer.Value = scannerConn.Strength;
Number.Text = scannerConn.Strength.ToString();
scannerConn.PropertyChanged += ScannerConn_PropertyChanged;
//..More Code
}
private void ScannerConn_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(scannerConn.Strength))
{
//MainThread.BeginInvokeOnMainThread(() =>
//{
new HistoricalData();
needlePointer.Value = scannerConn.Strength;
Number.Text = scannerConn.Strength.ToString();
Debug.WriteLine("test1 " + needlePointer.Value);
Debug.WriteLine("test2 " + Number.Text);
//});
}
}
型
但是, Jmeter 值不变。注意:我对一个显示网络名称的列表做了类似的处理,在下面的代码行后面添加了代码:NetworkListView.ItemsSource = scannerConn.NetworkNames;
不同之处在于名称保存在ObservableCollection
中。这将在扫描运行时在listView中显示新名称。
最后是xaml:
<Frame Grid.Row="1">
<Grid>
<gauge:SfRadialGauge x:Name="radialGauge">
<!-- more stuff -->
<gauge:RadialAxis.Pointers>
<gauge:NeedlePointer x:Name="needlePointer" />
</gauge:RadialAxis.Pointers>
<gauge:RadialAxis.Annotations>
<gauge:GaugeAnnotation x:Name="annotation" DirectionUnit="Angle" DirectionValue="90" PositionFactor="0.5">
<gauge:GaugeAnnotation.Content>
<Label x:Name="Number" FontSize="25" FontAttributes="Bold" TextColor="Black"/>
</gauge:GaugeAnnotation.Content>
</gauge:GaugeAnnotation>
<!-- more stuff -->
型
**更新!**这是我得到的数据的控制台输出:
strength: -24
15:24:50:061 existingNetwork: device_1_dummy -24
15:24:50:061 NetworkName: device_1_dummy -24
15:24:50:061 NetworkName: device_1_dummy -24
15:24:50:061 NetworkName: device_1_dummy -25
15:24:52:309 Data received from the scanner: message: datastearm, name: b'device_1_dummy', type: LoRaRAW, lat: None, lon: None, rssi: -25, snr: 6.0, time: (2023, 6, 15, 20, 32, 39, 775777, None)
strength: -25
....and so on
型
负数是我所追求的RSSI值。与虚拟数据,我得到的只有一个网络名称,但RSSI值的变化所有的时间。我尝试使用点击和项目选择事件。有了这些我只得到一个RSSI值,它显示在 Jmeter 上。请不要我仍然是一个菜鸟:)
1条答案
按热度按时间mitkmikd1#
在
SerialPort_DataReceived
中,您正在设置strength
,它是一个私有字段,而不是Strength
,它是一个公共属性。后者触发PropertyChanged
事件,而前者不触发