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
90
91
92
93
94
95
96
97
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/common.js"></script>
<script src="resources/manual.js"></script>
</head>
<body>
<p>
These tests require a connected serial device configured to act as a
"loopback" device, with the transmit and receive pins wired together.
</p>
<script>
manual_serial_test(async (t, port) => {
await port.open({baudRate: 115200, bufferSize: 1024});
// Create something much smaller than bufferSize above.
const data = new Uint8Array(64);
for (let i = 0; i < data.byteLength; ++i)
data[i] = i & 0xff;
const reader = port.readable.getReader();
for (let i = 0; i < 10; ++i) {
const writer = port.writable.getWriter();
writer.write(data);
const writePromise = writer.close();
const value = await readWithLength(reader, data.byteLength);
await writePromise;
compareArrays(value, data);
}
reader.releaseLock();
await port.close();
}, 'Can perform a series of small writes.');
manual_serial_test(async (t, port) => {
await port.open({baudRate: 115200, bufferSize: 1024});
// Create something much larger than bufferSize above.
const data = new Uint8Array(10 * 1024);
for (let i = 0; i < data.byteLength; ++i)
data[i] = (i / 1024) & 0xff;
const reader = port.readable.getReader();
for (let i = 0; i < 10; ++i) {
const writer = port.writable.getWriter();
writer.write(data);
const writePromise = writer.close();
const value = await readWithLength(reader, data.byteLength);
await writePromise;
compareArrays(value, data);
}
reader.releaseLock();
await port.close();
}, 'Can perform a series of large writes.');
manual_serial_test(async (t, port) => {
await port.open({baudRate: 115200, bufferSize: 64});
const writer = port.writable.getWriter();
// |data| is small enough to be completely transmitted.
let data = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
await writer.write(data);
// Wait a little bit for the device to process the incoming data.
await new Promise((resolve) => setTimeout(resolve, 100));
// ...before discarding the receive buffers.
await port.readable.cancel();
data = new Uint8Array([9, 10, 11, 12, 13, 14, 15, 16]);
const reader = port.readable.getReader();
const readPromise = readWithLength(reader, data.byteLength);
// The next block of data should be received successfully.
await writer.write(data);
writer.releaseLock();
const value = await readPromise;
reader.releaseLock();
compareArrays(value, data);
await port.close();
}, 'Canceling the reader discards buffered data.');
</script>
</body>
</html>
|