forked from ms-iot/samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainPage.xaml.cpp
More file actions
86 lines (73 loc) · 2.25 KB
/
MainPage.xaml.cpp
File metadata and controls
86 lines (73 loc) · 2.25 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) Microsoft. All rights reserved.
//
// MainPage.xaml.cpp
// Implementation of the MainPage class.
//
#include "pch.h"
#include "MainPage.xaml.h"
using namespace DeviceIoControlUwp;
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
void AccessComPort ()
{
using namespace Microsoft::WRL::Wrappers;
// Open the COM port synchronously for simplicity.
// You'll almost always want to use FILE_FLAG_OVERLAPPED
// for COM ports.
FileHandle fileHandle(CreateFile(
L"\\\\.\\COM1",
GENERIC_READ | GENERIC_WRITE,
0,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
nullptr));
if (!fileHandle.IsValid()) {
DWORD lastError = GetLastError();
throw ref new Exception( // set a breakpoint here
HRESULT_FROM_WIN32(lastError),
L"Failed to open COM port.");
}
// Set baud rate
SERIAL_BAUD_RATE inputBuffer = { 115200 };
DWORD information;
if (!DeviceIoControl(
fileHandle.Get(),
IOCTL_SERIAL_SET_BAUD_RATE,
&inputBuffer,
sizeof(inputBuffer),
nullptr,
0,
&information,
nullptr)) {
throw ref new Exception(
HRESULT_FROM_WIN32(GetLastError()),
L"Failed to set baud rate.");
}
// Write out a string over the serial port
const char message[] = "Hello serial!\n";
if (!WriteFile(
fileHandle.Get(),
message,
sizeof(message),
&information,
nullptr)) {
throw ref new Exception(
HRESULT_FROM_WIN32(GetLastError()),
L"Failed to write data to COM port.");
}
}
MainPage::MainPage()
{
InitializeComponent();
AccessComPort();
}