Created
October 24, 2023 03:26
-
-
Save jwinarske/a8a234272ed46cea558a70b0484c0331 to your computer and use it in GitHub Desktop.
[linux] serialport
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <unistd.h> | |
#include <libserialport.h> | |
const char* desired_port = "/dev/ttyUSB2"; | |
struct sp_port* port; | |
void list_ports() { | |
int i; | |
struct sp_port** ports; | |
enum 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); | |
enum 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, 115200); | |
for (;;) { | |
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; | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment