commit 0b3608c6ba0e0d61c9fdc4523d755fb45db8b87c Author: Nicholas Stănescu Date: Sat Apr 26 16:56:27 2025 +0200 initial commit diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..4a7ee9d --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.30) +project(serial C) +set(CMAKE_C_STANDARD 11) +include_directories(.) + +add_executable(serial + communication.c + communication.h + main.c) + +target_link_libraries(serial serialport) diff --git a/communication.c b/communication.c new file mode 100644 index 0000000..e7ba785 --- /dev/null +++ b/communication.c @@ -0,0 +1,102 @@ +// +// Created by nano on 4/26/25. +// + +#include "communication.h" + +#include +#include +#include + +struct sp_port ** port_list; +struct sp_port * port; + + +void comm_init_communication() { + /* + * Initializes the serial communication for the usb -> uart board + */ + port_list=comm_get_port_list(); + int portnr=0; + comm_list_serial_ports(port_list); + printf("Chose a port: "); + scanf("%d",&portnr); + port=port_list[portnr]; + comm_open_port(port); +} + +void comm_end_communication(struct sp_port *port) { + /* + * Ends the serial communication for the usb -> uart board + */ + check(sp_close(port)); + sp_free_port_list(port_list); +} + +int comm_write(u_int8_t data) { + // we are writing 8 bit bytes to the fpga anyways and pc -> fpga only needs to tell what do to next, can be fit in 8 bits + //TODO: IMPLEMENT + return 0; +} + +struct sp_port **comm_get_port_list() { + struct sp_port **port_list; + enum sp_return result = sp_list_ports(&port_list); + if (result != SP_OK) { + printf("Error listing serial ports\n"); + exit(-1); + } + return port_list; +} + +void comm_list_serial_ports(struct sp_port **port_list) { + printf("Port list:\n"); + for (int i=0;port_list[i]!= NULL; i++) { + char *port_name = sp_get_port_name(port_list[i]); + printf("Port %d: %s\n", i, port_name); + } +} + +void comm_open_port(struct sp_port *port) { + /* + * Opens communication to the selected serial port and sets the correct parameters for the zigbee carrier board. + */ + check(sp_open(port,SP_MODE_READ_WRITE)); + check(sp_set_baudrate(port,19200)); + check(sp_set_bits(port,8)); + check(sp_set_parity(port,SP_PARITY_NONE)); + check(sp_set_stopbits(port,1)); +} + +/* Helper function for error handling. Taken from libserialport examples */ +int check(enum sp_return result) +{ + /* For this example we'll just exit on any error by calling abort(). */ + char *error_message; + + switch (result) { + case SP_ERR_ARG: + printf("Error: Invalid argument.\n"); + abort(); + case SP_ERR_FAIL: + error_message = sp_last_error_message(); + printf("Error: Failed: %s\n", error_message); + sp_free_error_message(error_message); + abort(); + case SP_ERR_SUPP: + printf("Error: Not supported.\n"); + abort(); + case SP_ERR_MEM: + printf("Error: Couldn't allocate memory.\n"); + abort(); + case SP_OK: + default: + return result; + } +} + +void uint8_to_bit_array(u_int8_t bit_array[], u_int8_t data) { + for (int i = 0; i < 8; i++) { + bit_array[i] = (data >> i) & 1; + } +} diff --git a/communication.h b/communication.h new file mode 100644 index 0000000..c20dd67 --- /dev/null +++ b/communication.h @@ -0,0 +1,27 @@ +// +// Created by nano on 4/26/25. +// + +#ifndef COMMS_H +#define COMMS_H +#include +#include + + +void comm_init_communication(); + +/** + * Initializes the serial communication for the usb -> uart board + */ +void comm_end_communication(struct sp_port *port); + +struct sp_port **comm_get_port_list(); + +void comm_list_serial_ports(struct sp_port **port_list); + +void comm_open_port(struct sp_port *port); + +int check(enum sp_return result); + +void uint8_to_bit_array(u_int8_t *bit_array, u_int8_t data); +#endif //COMMS_H diff --git a/main.c b/main.c new file mode 100644 index 0000000..7f61644 --- /dev/null +++ b/main.c @@ -0,0 +1,41 @@ +// +// Created by nano on 4/26/25. +// +#include +#include +#include + +#include "communication.h" +extern struct sp_port *port; + +int main(int argc, char *argv[]) { + comm_init_communication(); + + u_int8_t data = 205; + int size = strlen(&data); + printf("size: %d\n", size); + unsigned int timeout = 50 * size; //50 ms timeout per byte + + check(sp_nonblocking_write(port, &data, size)); + char *buf = malloc(size + 1); + + int result = check(sp_blocking_read(port, buf, size, timeout)); + + if (result == size) { + printf("Received the same amount of bytes back \n"); + } + buf[result] = '\0'; + printf("%s\n", buf); + + u_int8_t bits[8]; + uint8_to_bit_array(bits, buf[0]); + free(buf); + + for (int i = 0; i < 8; i++) { + printf("%d ", bits[i]); + } + printf("\n"); + + comm_end_communication(port); + return 0; +}