Skip to content

Instantly share code, notes, and snippets.

@Nixes
Last active February 25, 2021 02:55
Show Gist options
  • Save Nixes/78e401234e66aa131547d7b78135271c to your computer and use it in GitHub Desktop.
Save Nixes/78e401234e66aa131547d7b78135271c to your computer and use it in GitHub Desktop.
libserialport example
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <unistd.h> // for sleep function
#include <libserialport.h> // cross platform serial port lib
const char* desired_port = "COM8";
struct sp_port *port;
void list_ports() {
int i;
struct sp_port **ports;
sp_return error = sp_list_ports(&ports);
if (error == SP_OK) {
for (i = 0; ports[i]; i++) {
printf("Found port: '%s'\n", sp_get_port_name(ports[i]));
}
sp_free_port_list(ports);
} else {
printf("No serial devices detected\n");
}
printf("\n");
}
void parse_serial(char *byte_buff, int byte_num) {
for(int i = 0; i < byte_num;i++){
printf("%c", byte_buff[i]);
}
printf("\n");
}
int main() {
list_ports();
printf("Opening port '%s' \n", desired_port);
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,57600);
while(true) {
sleep(0.5); // can do something else in mean time
int bytes_waiting = sp_input_waiting(port);
if (bytes_waiting > 0) {
printf("Bytes waiting %i\n", bytes_waiting);
char byte_buff[512];
int byte_num = 0;
byte_num = sp_nonblocking_read(port,byte_buff,512);
parse_serial(byte_buff,byte_num);
}
fflush(stdout);
}
sp_close(port);
} else {
printf("Error opening serial device\n");
}
} else {
printf("Error finding serial device\n");
}
return 0;
}
@hu55a1n1
Copy link

hu55a1n1 commented Feb 19, 2017

Please note that sp_set_baudrate(port,57600); MUST be called after opening the port.
i.e. the correct code should read ->

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,57600);    // <----
        // ...

@Nixes
Copy link
Author

Nixes commented Feb 19, 2017

Correction made, thanks for the feedback.

@ntd
Copy link

ntd commented Jan 22, 2018

I just forked to add some cosmetic changes and let your example compile successfully with stock gcc 7.2.1..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment