Révision 03682d21
SICPA_Connexions/Model/BytesPrtc.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.Text; |
|
4 |
|
|
5 |
namespace SICPA_Connexions.Model |
|
6 |
{ |
|
7 |
public static class BytesPrtc |
|
8 |
{ |
|
9 |
public static byte STX = 0x02; |
|
10 |
public static byte DST_TX = 0x49; |
|
11 |
public static byte SRC_TX = 0x41; |
|
12 |
public static byte DST_RX = 0x41; |
|
13 |
public static byte SRC_RX = 0x49; |
|
14 |
public static byte ETX = 0x03; |
|
15 |
} |
|
16 |
} |
SICPA_Connexions/Model/CRC.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.Text; |
|
4 |
|
|
5 |
namespace SICPA_Connexions.Model |
|
6 |
{ |
|
7 |
public class CRC |
|
8 |
{ |
|
9 |
private List<byte> data = new List<byte>(); |
|
10 |
public CRC() { } |
|
11 |
public CRC(byte[] din) //trame entrée à la main |
|
12 |
{ |
|
13 |
foreach (byte b in din) { data.Add(b); } |
|
14 |
} |
|
15 |
public void putBytes(byte[] din) //ajout trame depuis fonction |
|
16 |
{ |
|
17 |
foreach (byte b in din) { data.Add(b); } |
|
18 |
} |
|
19 |
public byte getCRC() |
|
20 |
{ |
|
21 |
byte thatCRC = 0, current_byte = 0, count, i; |
|
22 |
for (i = 0; i < data.Count; i++) //calcul du CRC sur toute la trame excepté le CRC lui même et le ETX -> (frame.length-2) |
|
23 |
{ |
|
24 |
current_byte = data[i]; |
|
25 |
//Console.WriteLine("CRC byte calc : {0:X}", current_byte); |
|
26 |
for (count = 0; count < 8; count++) |
|
27 |
{ |
|
28 |
|
|
29 |
if (((thatCRC & 0x01) ^ (current_byte & 0x01)) != 0)//=1? |
|
30 |
{ |
|
31 |
//XOR based to abr200 polygon (X^8 + X^4 + X^3 + X^2 + X^1) |
|
32 |
thatCRC ^= 0x70; |
|
33 |
thatCRC >>= 1; //bit shifting on the right |
|
34 |
thatCRC |= 0x80; //set 1 to msb bit |
|
35 |
} |
|
36 |
else |
|
37 |
{ |
|
38 |
thatCRC >>= 1; //bit shifting on the right again :) |
|
39 |
thatCRC &= 0x7F; //set 0 to msb bit |
|
40 |
} |
|
41 |
//Console.WriteLine("current_crc : {0:X}", thatCRC); |
|
42 |
//bit shifting on the current byte... |
|
43 |
current_byte >>= 1; |
|
44 |
} |
|
45 |
} |
|
46 |
//Console.WriteLine(data.Count); |
|
47 |
/*for(i = 0; i < data.Count; i++) |
|
48 |
{ |
|
49 |
data.Remove(i); |
|
50 |
}*/ |
|
51 |
return thatCRC; |
|
52 |
} |
|
53 |
} |
|
54 |
} |
SICPA_Connexions/Model/GestionnaireBLE.cs | ||
---|---|---|
20 | 20 |
private List<ICharacteristic> charList; |
21 | 21 |
private ICharacteristic characRead; |
22 | 22 |
private ICharacteristic characWrite; |
23 |
private String retourBLE; |
|
24 |
public event EventHandler RetourBlEMofifiee; |
|
23 | 25 |
public IDevice DeviceConnected { get; set; } |
24 | 26 |
public IService SelectedService { get; set; } |
25 |
public String RetourBLE { get; set; } |
|
27 |
public String RetourBLE |
|
28 |
{ |
|
29 |
get |
|
30 |
{ |
|
31 |
return retourBLE; |
|
32 |
} |
|
33 |
set |
|
34 |
{ |
|
35 |
if (retourBLE != value) |
|
36 |
{ |
|
37 |
retourBLE = value; |
|
38 |
OnRetourBLEMofifiee(EventArgs.Empty); |
|
39 |
} |
|
40 |
} |
|
41 |
} |
|
42 |
|
|
43 |
public void OnRetourBLEMofifiee(EventArgs e) |
|
44 |
{ |
|
45 |
RetourBlEMofifiee?.Invoke(this, e); |
|
46 |
} |
|
26 | 47 |
|
27 | 48 |
public GestionnaireBLE() |
28 | 49 |
{ |
... | ... | |
173 | 194 |
//}); |
174 | 195 |
} |
175 | 196 |
|
197 |
public async Task LectureTag(IDevice deviceConnected) |
|
198 |
{ |
|
199 |
try |
|
200 |
{ |
|
201 |
String cmd = "READ"; |
|
202 |
|
|
203 |
List<byte> local_frame = new List<byte>(); |
|
204 |
CRC crcProcess = new CRC(); |
|
205 |
byte local_crc; |
|
206 |
|
|
207 |
local_frame.Add(BytesPrtc.STX); //add STX byte |
|
208 |
local_frame.Add(BytesPrtc.DST_TX); //add DST byte |
|
209 |
local_frame.Add(BytesPrtc.SRC_TX); //add SRC byte |
|
210 |
for (int i = 0; i < cmd.Length; i++) |
|
211 |
{ |
|
212 |
local_frame.Add((byte)cmd[i]); //add cmd string |
|
213 |
} |
|
214 |
crcProcess.putBytes(local_frame.ToArray()); //send that buffer (without "the CRC" and ETX bytes !) |
|
215 |
local_crc = crcProcess.getCRC(); //calculate CRC with that buffer |
|
216 |
|
|
217 |
local_frame.Add(local_crc); //add CRC byte |
|
218 |
local_frame.Add(BytesPrtc.ETX); //add ETX byte |
|
219 |
|
|
220 |
///////////////////////////////////////////// |
|
221 |
//And now send buffer ! |
|
222 |
await characWrite.WriteAsync(local_frame.ToArray()); |
|
223 |
await characRead.StartUpdatesAsync(); |
|
224 |
} |
|
225 |
catch |
|
226 |
{ |
|
227 |
await Application.Current.MainPage.DisplayAlert("Erreur de lecture", "Erreur de lecture du Tag", "OK"); |
|
228 |
} |
|
229 |
} |
|
230 |
|
|
176 | 231 |
} |
177 | 232 |
} |
SICPA_Connexions/View/AnimalView.xaml | ||
---|---|---|
2 | 2 |
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" |
3 | 3 |
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" |
4 | 4 |
x:Class="SICPA_Connexions.View.AnimalView" |
5 |
Title="Formulaire Vivant">
|
|
5 |
Title="Animal">
|
|
6 | 6 |
<Grid> |
7 | 7 |
<StackLayout BackgroundColor="{StaticResource InraePrimary}" Spacing="0"> |
8 | 8 |
<ScrollView> |
... | ... | |
17 | 17 |
<StackLayout> |
18 | 18 |
<Frame BorderColor="{StaticResource InraePrimary}" Margin="0,0,0,20"> |
19 | 19 |
<Label |
20 |
Text="Formulaire Vivant"
|
|
20 |
Text="Animal"
|
|
21 | 21 |
VerticalOptions="Center" |
22 | 22 |
HorizontalOptions="Center" |
23 | 23 |
Style="{StaticResource textTitre}" |
24 | 24 |
/> |
25 | 25 |
</Frame> |
26 |
<Picker Title="Liste des vivants" |
|
27 |
ItemsSource="{Binding Vivants}" |
|
28 |
SelectedItem="{Binding VivantSelectionne}" |
|
29 |
ItemDisplayBinding="{Binding VivantNom}"/> |
|
30 |
<Grid ColumnDefinitions="20*,80*" RowDefinitions="auto,auto"> |
|
26 |
<Grid ColumnDefinitions="15*,85*" > |
|
31 | 27 |
<Label Grid.Column="0" |
32 |
Grid.Row="0" |
|
33 |
Text="Id : " |
|
28 |
Text="Rfid" |
|
34 | 29 |
Style="{StaticResource textLabel}" /> |
35 | 30 |
<Label |
36 | 31 |
Grid.Column="1" |
37 |
Grid.Row="0" |
|
38 |
Text="{Binding VivantId}" |
|
32 |
Text="{Binding NumRfid}" |
|
39 | 33 |
VerticalOptions="Center" |
40 | 34 |
HorizontalOptions="Center" |
41 | 35 |
Style="{StaticResource textLabel}"/> |
42 |
<Label Grid.Column="0" |
|
43 |
Grid.Row="1" |
|
44 |
Text="Nom : " |
|
45 |
Style="{StaticResource textLabel}"/> |
|
46 |
<Label |
|
47 |
Grid.Column="1" |
|
48 |
Grid.Row="1" |
|
49 |
Text="{Binding VivantNom}" |
|
50 |
VerticalOptions="Center" |
|
51 |
HorizontalOptions="Center"/> |
|
52 | 36 |
</Grid> |
53 | 37 |
</StackLayout> |
54 | 38 |
</Frame> |
... | ... | |
62 | 46 |
VerticalOptions="FillAndExpand"> |
63 | 47 |
|
64 | 48 |
<Button |
65 |
Text="Edition"
|
|
66 |
Command="{Binding EditionCommand}"
|
|
49 |
Text="Scan BLE RFID"
|
|
50 |
Command="{Binding LectureBLECommand}"
|
|
67 | 51 |
Style="{StaticResource btnNormal}" |
68 | 52 |
VerticalOptions="End"/> |
69 | 53 |
</StackLayout> |
SICPA_Connexions/View/AnimalView.xaml.cs | ||
---|---|---|
10 | 10 |
InitializeComponent(); |
11 | 11 |
this.BindingContext = new AnimalViewModel(); |
12 | 12 |
} |
13 |
|
|
14 |
protected override void OnAppearing() |
|
15 |
{ |
|
16 |
base.OnAppearing(); |
|
17 |
AnimalViewModel viewModel = this.BindingContext as AnimalViewModel; |
|
18 |
if (viewModel != null) |
|
19 |
{ |
|
20 |
viewModel.ActualisePage(); |
|
21 |
} |
|
22 |
} |
|
23 |
|
|
13 | 24 |
} |
14 | 25 |
} |
SICPA_Connexions/ViewModel/AnimalViewModel.cs | ||
---|---|---|
19 | 19 |
#endregion |
20 | 20 |
|
21 | 21 |
#region Commandes |
22 |
public ICommand EditionCommand { get; private set; }
|
|
22 |
public ICommand LectureBLECommand { get; private set; }
|
|
23 | 23 |
#endregion |
24 | 24 |
|
25 | 25 |
#region Propriétés |
... | ... | |
55 | 55 |
#region Constructeurs |
56 | 56 |
public AnimalViewModel() |
57 | 57 |
{ |
58 |
EditionCommand = new Command(async () => await Edition());
|
|
58 |
LectureBLECommand = new Command(async () => await LectureBLE(), CheckDeviceConnected);
|
|
59 | 59 |
} |
60 | 60 |
#endregion |
61 | 61 |
|
62 | 62 |
#region Méthodes |
63 |
private async Task Edition()
|
|
63 |
private async Task LectureBLE()
|
|
64 | 64 |
{ |
65 | 65 |
IsBusy = true; |
66 |
await Task.Delay(1000); |
|
66 |
if (GlobalApplication.GestionnaireBLE.DeviceConnected != null) |
|
67 |
{ |
|
68 |
await GlobalApplication.GestionnaireBLE.LectureTag(GlobalApplication.GestionnaireBLE.DeviceConnected); |
|
69 |
} |
|
67 | 70 |
IsBusy = false; |
68 | 71 |
} |
72 |
|
|
73 |
private void HandleRetourBleModifiee(object sender, EventArgs e) |
|
74 |
{ |
|
75 |
if (GlobalApplication.GestionnaireBLE != null) |
|
76 |
{ |
|
77 |
NumRfid = GlobalApplication.GestionnaireBLE.RetourBLE; |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
81 |
private bool CheckDeviceConnected() |
|
82 |
{ |
|
83 |
return (GlobalApplication.GestionnaireBLE != null && GlobalApplication.GestionnaireBLE.DeviceConnected != null); |
|
84 |
} |
|
85 |
|
|
86 |
public void ActualisePage() |
|
87 |
{ |
|
88 |
((Command)LectureBLECommand).ChangeCanExecute(); |
|
89 |
if (GlobalApplication.GestionnaireBLE != null) |
|
90 |
{ |
|
91 |
GlobalApplication.GestionnaireBLE.RetourBlEMofifiee += HandleRetourBleModifiee; |
|
92 |
} |
|
93 |
} |
|
94 |
|
|
69 | 95 |
#endregion |
70 | 96 |
|
71 | 97 |
} |
SICPA_Connexions/ViewModel/SettingsViewModel.cs | ||
---|---|---|
73 | 73 |
ChangeConnexion(false); |
74 | 74 |
NomBLE = ""; |
75 | 75 |
} |
76 |
if (GlobalApplication.GestionnaireBLE != null && GlobalApplication.GestionnaireBLE.DeviceConnected != null)
|
|
76 |
if (GlobalApplication.GestionnaireBLE.DeviceConnected != null) |
|
77 | 77 |
{ |
78 | 78 |
ChangeConnexion(true); |
79 | 79 |
NomBLE = GlobalApplication.GestionnaireBLE.DeviceConnected.Name; |
... | ... | |
105 | 105 |
ConnexionNOK = !ok; |
106 | 106 |
} |
107 | 107 |
|
108 |
|
|
109 | 108 |
#endregion |
110 | 109 |
} |
111 | 110 |
} |
Formats disponibles : Unified diff