diff --git a/README.MD b/README.MD index 8c496bd..025de2e 100644 --- a/README.MD +++ b/README.MD @@ -1,6 +1,8 @@ # serial for ip2 TODO: -- [ ] create read function -- [ ] create write function + +- [x] create read function +- [x] create write function - [x] how to be aware of data arriving - [ ] other +- [ ] test read and write functions \ No newline at end of file diff --git a/communication.c b/communication.c index c636a02..9475525 100644 --- a/communication.c +++ b/communication.c @@ -9,84 +9,128 @@ #include -struct sp_port ** port_list; +//TODO: Add descriptive enum for the different return values +//TODO: ADD DOCUMENTATION struct sp_port * port; -struct sp_event_set *event_set; +struct sp_event_set *event_data_ready; +struct sp_event_set *event_ready_for_tx; -void comm_init_communication() { - /* +/** * Initializes the serial communication for the usb -> uart board */ - port_list=comm_get_port_list(); +void comm_init_communication() { + struct sp_port **port_list; + comm_get_port_list(&port_list); int portnr=0; comm_list_serial_ports(port_list); printf("Chose a port: "); scanf("%d",&portnr); sp_copy_port(port_list[portnr], &port); sp_free_port_list(port_list); - comm_open_port(port); + comm_open_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(port); - //sp_free_port_list(port_list); -} - -int comm_await() { - // Await data from the robot - return (sp_wait(event_set, 50) == SP_OK); -} - -int comm_read_byte(byte *data) { - //read available data - //TODO: IMPLEMENT - return 0; -} - - -int comm_write_byte(byte 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); +void 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++) { + 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) { +void comm_open_port() { /* * Opens communication to the selected serial port and sets the correct parameters for the zigbee carrier board. * Also add event to see when data arrives. */ - 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)); + 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)); - check(sp_new_event_set(&event_set)); - sp_add_port_events(event_set, port, SP_EVENT_RX_READY); + check(sp_new_event_set(&event_data_ready)); + check(sp_new_event_set(&event_ready_for_tx)); + + sp_add_port_events(event_data_ready, port, SP_EVENT_RX_READY); + sp_add_port_events(event_ready_for_tx, port, SP_EVENT_TX_READY); +} + +/** + * Ends the serial communication for the usb -> uart board + */ +void comm_end_communication() { + check(sp_close(port)); + sp_free_port(port); +} + +int comm_await_data_ready(int timeout) { + // Await data from the robot + return (sp_wait(event_data_ready, timeout) == SP_OK); +} + +int comm_await_ready_for_tx(int timeout) { + return (sp_wait(event_ready_for_tx, timeout) == SP_OK); +} + +int comm_nonblocking_read(byte *data, int amount_of_bytes) { + //read available data + //TODO: TEST ACTUAL READ LOGIC + int recv_bytes = sp_nonblocking_read(port, data, amount_of_bytes); + if (recv_bytes == amount_of_bytes) { + return 1; + } + if (recv_bytes >= 0) { + return 0; + } + return -1; +} + +int comm_blocking_read(byte *data, int amount_of_bytes, int timeout) { + //read available data + //TODO: TEST ACTUAL READ LOGIC + int recv_bytes = sp_blocking_read(port, data, amount_of_bytes, timeout); + if (recv_bytes == amount_of_bytes) { + return 1; + } + if (recv_bytes >= 0) { + return 0; + } + return -1; +} + +int comm_blocking_write(byte *data, int amount_of_bytes,int timeout) { + // 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: TEST ACTUAL WRITE LOGIC + int sent_bytes = sp_blocking_write(port, data, amount_of_bytes, timeout); + if (sent_bytes == amount_of_bytes) { + return 1; + } + if (sent_bytes >= 0) { + return 0; + } + return -1; +} + +int comm_nonblocking_write(byte *data, int amount_of_bytes) { + // 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: TEST ACTUAL WRITE LOGIC + int sent_bytes = sp_nonblocking_write(port, data, amount_of_bytes); + if (sent_bytes == amount_of_bytes) { + return 1; + } + if (sent_bytes >=0) { + return 0; + } + return -1; } /** Helper function for error handling. Taken from libserialport examples */ diff --git a/communication.h b/communication.h index f533de3..3e2984b 100644 --- a/communication.h +++ b/communication.h @@ -1,7 +1,6 @@ // // Created by nano on 4/26/25. // - #ifndef COMMS_H #define COMMS_H #include @@ -14,22 +13,33 @@ typedef u_int8_t byte; */ void comm_init_communication(); -/** - * Ends 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_get_port_list(struct sp_port ***port_list); void comm_list_serial_ports(struct sp_port **port_list); /** * Opens communication to the selected serial port and sets the correct parameters for the zigbee carrier board. */ -void comm_open_port(struct sp_port *port); +void comm_open_port(); +/** + * Ends the serial communication for the usb -> uart board + */ +void comm_end_communication(); -int comm_await(); +int comm_blocking_read(byte *data, int amount_of_bytes, int timeout); + +int comm_await_data_ready(int timeout); + +int comm_await_ready_for_tx(int timeout); + +int comm_nonblocking_read(byte *data, int amount_of_bytes); + +int comm_blocking_read(byte *data, int amount_of_bytes, int timeout); + +int comm_nonblocking_write(byte *data, int amount_of_bytes); + +int comm_blocking_write(byte *data, int amount_of_bytes, int timeout); int check(enum sp_return result); diff --git a/docs/readme.md b/docs/readme.md index bbe65df..c05375c 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -1,3 +1,9 @@ +## Compiling libserialport caused a false positive* by windows defender + +###### *at least so i think + +# !!! THIS IS OUTDATED + # Serial Communication Module Documentation ## Overview diff --git a/main.c b/main.c index 2451801..9a1eae6 100644 --- a/main.c +++ b/main.c @@ -7,29 +7,34 @@ #include "utils.h" #include "communication.h" -extern struct sp_port *port; - int main(int argc, char *argv[]) { - comm_init_communication(); + comm_init_communication(); //initialize communication with zigbee board + while (1) { - if (comm_await()) { - byte *buffer = malloc(sizeof(byte)); - if (sp_nonblocking_read(port, buffer, 1) == 1) { - byte *bits = malloc(sizeof(byte) * 8); - byte_to_bit_array(bits, *buffer); - printf("Data received: "); + if (comm_await_data_ready(50)) { + //await in 50ms increments for data to be ready + + byte *buffer = malloc(sizeof(byte)); //allocate enough space to store incoming data + + if (comm_blocking_read(buffer, 1, 50) == 1) { + // read data from buffer and continue only if the correct nr of bytes is received + + byte *bits = malloc(sizeof(byte) * 8); // + byte_to_bit_array(bits, *buffer); // convert the incoming data to + printf("Data received: "); // an array of bits and print said bits for (int i = 7; i; i--) { - printf("%d", bits[i]); - } - printf("\n"); + // + printf("%d", bits[i]); // THIS IS JUST TESTING CODE + } // + printf("\n"); // free(buffer); if (bits[7] == 1) { - free(bits); - break; + free(bits); //exit if the MSB is 1, so that we don't loop infinitely + break; // again, test code } } } } - comm_end_communication(port); + comm_end_communication(); return 0; } diff --git a/utils.c b/utils.c index 1397a3e..562a258 100644 --- a/utils.c +++ b/utils.c @@ -3,6 +3,9 @@ // #include "utils.h" + +#include + #include "communication.h" // Helper functions to convert from bits to bytes @@ -20,4 +23,5 @@ void bit_array_to_byte(byte bit_array[], byte *data) { data+=bit_array[i]*currentpow; currentpow*=2; } //2's complement notation -} \ No newline at end of file +} +