forked from nanoframework/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
89 lines (75 loc) · 2.9 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using nanoFramework.Device.Can;
using System;
using System.Diagnostics;
using System.Threading;
using System.Device.Gpio;
namespace Can.TestApp
{
public class Program
{
static GpioPin _led;
static CanController CanController1;
static CanController CanController2;
public static void Main()
{
// PJ5 is LD2 in STM32F769I_DISCO
//_led = new GpioController().OpenPin(PinNumber('J', 5), PinMode.Output);
// PG14 is LEDLD4 in F429I_DISCO
//_led = new GpioController().OpenPin(PinNumber('G', 14), PinMode.Output);
// PD13 is LED3 in DISCOVERY4
_led = new GpioController().OpenPin(PinNumber('D', 13), PinMode.Output);
// set settings for CAN controller
CanSettings canSettings = new CanSettings(6, 8, 1, 0);
// get controller for CAN1
CanController1 = CanController.FromId("CAN1", canSettings);
// get controller for CAN2
CanController2 = CanController.FromId("CAN2", canSettings);
//CanController1.MessageReceived += CanController_DataReceived;
CanController2.MessageReceived += CanController_DataReceived;
while (true)
{
CanController1.WriteMessage(new CanMessage(0x01234567, CanMessageIdType.EID, CanMessageFrameType.Data, new byte[] { 0xCA, 0xFE }));
//CanController2.WriteMessage(new CanMessage(0x01234567, false, true, new byte[] { 0xFE, 0xCA }));
Thread.Sleep(2000);
}
}
private static void CanController_DataReceived(object sender, CanMessageReceivedEventArgs e)
{
CanController canCtl = (CanController)sender;
while (true)
{
// try get the next message
var msg = canCtl.GetMessage();
if(msg == null)
{
Debug.WriteLine("*** No more message available!!!");
break;
}
// new message available, output message
if (msg.Message != null)
{
Debug.Write($"Message on {canCtl.ControllerId}: ");
for (int i = 0; i < msg.Message.Length; i++)
{
Debug.Write(msg.Message[i].ToString("X2"));
}
new Thread(BlinkLED).Start();
}
Debug.WriteLine("");
}
}
static void BlinkLED()
{
// blink led for each message received
_led.Write(PinValue.High);
Thread.Sleep(500);
_led.Toggle();
}
static int PinNumber(char port, byte pin)
{
if (port < 'A' || port > 'J')
throw new ArgumentException();
return ((port - 'A') * 16) + pin;
}
}
}