Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
serial_cpp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Erik Strand
serial_cpp
Commits
22d6fd49
Commit
22d6fd49
authored
Nov 28, 2018
by
Erik Strand
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Echo characters from the serial device
parent
b8400081
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
0 deletions
+40
-0
main.cpp
main.cpp
+40
-0
No files found.
main.cpp
View file @
22d6fd49
...
...
@@ -22,6 +22,46 @@ int main() {
std
::
cout
<<
"Searching for serial ports.
\n
"
;
list_ports
();
const
char
*
desired_port
=
"/dev/cu.usbserial-AC01YB5P"
;
constexpr
uint32_t
baud_rate
=
115200
;
constexpr
uint32_t
byte_buffer_size
=
512
;
char
byte_buffer
[
byte_buffer_size
];
struct
sp_port
*
port
;
std
::
cout
<<
"Opening port "
<<
desired_port
<<
'\n'
;
sp_return
error
=
sp_get_port_by_name
(
desired_port
,
&
port
);
if
(
error
==
SP_OK
)
{
error
=
sp_open
(
port
,
SP_MODE_READ
);
if
(
error
==
SP_OK
)
{
sp_set_baudrate
(
port
,
baud_rate
);
while
(
true
)
{
int
bytes_waiting
=
sp_input_waiting
(
port
);
if
(
bytes_waiting
>
0
)
{
std
::
cout
<<
bytes_waiting
<<
" bytes in the buffer: "
;
int
byte_num
=
0
;
byte_num
=
sp_nonblocking_read
(
port
,
byte_buffer
,
byte_buffer_size
);
bool
button_pressed
=
false
;
for
(
decltype
(
byte_num
)
i
=
0
;
i
<
byte_num
;
++
i
){
if
(
byte_buffer
[
i
]
==
'1'
)
{
button_pressed
=
true
;
}
std
::
cout
<<
byte_buffer
[
i
];
}
if
(
button_pressed
)
{
std
::
cout
<<
" the button is pressed!"
;
}
std
::
cout
<<
std
::
endl
;
}
}
// Execution can never make it here, but I'll close the port on principle.
sp_close
(
port
);
}
else
{
std
::
cout
<<
"Error opening serial device
\n
"
;
}
}
else
{
std
::
cout
<<
"Error finding serial device
\n
"
;
}
return
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment