Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
serial_cpp
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Erik Strand
serial_cpp
Commits
22d6fd49
Commit
22d6fd49
authored
6 years ago
by
Erik Strand
Browse files
Options
Downloads
Patches
Plain Diff
Echo characters from the serial device
parent
b8400081
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.cpp
+40
-0
40 additions, 0 deletions
main.cpp
with
40 additions
and
0 deletions
main.cpp
+
40
−
0
View file @
22d6fd49
...
@@ -22,6 +22,46 @@ int main() {
...
@@ -22,6 +22,46 @@ int main() {
std
::
cout
<<
"Searching for serial ports.
\n
"
;
std
::
cout
<<
"Searching for serial ports.
\n
"
;
list_ports
();
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
;
return
0
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment