-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlarm.cs
More file actions
63 lines (61 loc) · 2.28 KB
/
Copy pathAlarm.cs
File metadata and controls
63 lines (61 loc) · 2.28 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
namespace WirelessAlarm
{
// Console program for receiving the data
class Program
{
static void Main(string[] args)
{
// initialize the sensor port, mine was registered as COM8, you may check yours
// through the hardware devices from control panel
SerialPort sensor = new SerialPort("COM8", 9600, Parity.None, 8, StopBits.One);
int bytesToRead = 0;
bool isPlaying = false;
string message;
sensor.Open();
try
{
while (true)
{
// check if there are bytes incoming
bytesToRead = sensor.BytesToRead;
if (bytesToRead > 0)
{
byte[] input = new byte[bytesToRead];
// read the Xbee's input
sensor.Read(input, 0, bytesToRead);
// convert the bytes into string
message = System.Text.Encoding.UTF8.GetString(input);
// in our case "MOVE" is what we will expect
if ("MOVE".Equals(message))
{
if (!isPlaying)
{
// this is where the wmplayer through process.start
System.Diagnostics.Process pr;
System.Diagnostics.ProcessStartInfo ps;
// this is much like doing Start > Run > wmplayer <file> command
ps = new System.Diagnostics.ProcessStartInfo("wmplayer", "\"C:\\Alarm.mp3\"");
pr = new System.Diagnostics.Process();
pr.StartInfo = ps;
pr.Start(); // start it finally
isPlaying = true; // will not open another wmplayer anymore
}
// write something to the console
Console.WriteLine("Something moved!");
}
}
}
}
finally
{
// again always close the serial ports!
sensor.Close();
}
}
}
}