pretty much done
needs documentation update and testing # Conflicts: # README.MD
This commit is contained in:
parent
d968ce22be
commit
0eb5ac6de9
@ -1,6 +1,8 @@
|
|||||||
# serial for ip2
|
# serial for ip2
|
||||||
TODO:
|
TODO:
|
||||||
- [ ] create read function
|
|
||||||
- [ ] create write function
|
- [x] create read function
|
||||||
|
- [x] create write function
|
||||||
- [x] how to be aware of data arriving
|
- [x] how to be aware of data arriving
|
||||||
- [ ] other
|
- [ ] other
|
||||||
|
- [ ] test read and write functions
|
||||||
140
communication.c
140
communication.c
@ -9,84 +9,128 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
struct sp_port ** port_list;
|
//TODO: Add descriptive enum for the different return values
|
||||||
|
//TODO: ADD DOCUMENTATION
|
||||||
struct sp_port * port;
|
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
|
* 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;
|
int portnr=0;
|
||||||
comm_list_serial_ports(port_list);
|
comm_list_serial_ports(port_list);
|
||||||
printf("Chose a port: ");
|
printf("Chose a port: ");
|
||||||
scanf("%d",&portnr);
|
scanf("%d",&portnr);
|
||||||
sp_copy_port(port_list[portnr], &port);
|
sp_copy_port(port_list[portnr], &port);
|
||||||
sp_free_port_list(port_list);
|
sp_free_port_list(port_list);
|
||||||
comm_open_port(port);
|
comm_open_port();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void comm_get_port_list(struct sp_port ***port_list) {
|
||||||
void comm_end_communication(struct sp_port *port) {
|
enum sp_return result = sp_list_ports(port_list);
|
||||||
/*
|
|
||||||
* 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);
|
|
||||||
if (result != SP_OK) {
|
if (result != SP_OK) {
|
||||||
printf("Error listing serial ports\n");
|
printf("Error listing serial ports\n");
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
return port_list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void comm_list_serial_ports(struct sp_port **port_list) {
|
void comm_list_serial_ports(struct sp_port **port_list) {
|
||||||
printf("Port list:\n");
|
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]);
|
char *port_name = sp_get_port_name(port_list[i]);
|
||||||
printf("Port %d: %s\n", i, port_name);
|
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.
|
* 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.
|
* Also add event to see when data arrives.
|
||||||
*/
|
*/
|
||||||
check(sp_open(port,SP_MODE_READ_WRITE));
|
check(sp_open(port, SP_MODE_READ_WRITE));
|
||||||
check(sp_set_baudrate(port,19200));
|
check(sp_set_baudrate(port, 19200));
|
||||||
check(sp_set_bits(port,8));
|
check(sp_set_bits(port, 8));
|
||||||
check(sp_set_parity(port,SP_PARITY_NONE));
|
check(sp_set_parity(port, SP_PARITY_NONE));
|
||||||
check(sp_set_stopbits(port,1));
|
check(sp_set_stopbits(port, 1));
|
||||||
|
|
||||||
check(sp_new_event_set(&event_set));
|
check(sp_new_event_set(&event_data_ready));
|
||||||
sp_add_port_events(event_set, port, SP_EVENT_RX_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 */
|
/** Helper function for error handling. Taken from libserialport examples */
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
//
|
//
|
||||||
// Created by nano on 4/26/25.
|
// Created by nano on 4/26/25.
|
||||||
//
|
//
|
||||||
|
|
||||||
#ifndef COMMS_H
|
#ifndef COMMS_H
|
||||||
#define COMMS_H
|
#define COMMS_H
|
||||||
#include <libserialport.h>
|
#include <libserialport.h>
|
||||||
@ -14,22 +13,33 @@ typedef u_int8_t byte;
|
|||||||
*/
|
*/
|
||||||
void comm_init_communication();
|
void comm_init_communication();
|
||||||
|
|
||||||
/**
|
void comm_get_port_list(struct sp_port ***port_list);
|
||||||
* 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_list_serial_ports(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.
|
* 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);
|
int check(enum sp_return result);
|
||||||
|
|||||||
@ -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
|
# Serial Communication Module Documentation
|
||||||
|
|
||||||
## Overview
|
## Overview
|
||||||
|
|||||||
35
main.c
35
main.c
@ -7,29 +7,34 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "communication.h"
|
#include "communication.h"
|
||||||
|
|
||||||
extern struct sp_port *port;
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
comm_init_communication();
|
comm_init_communication(); //initialize communication with zigbee board
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
if (comm_await()) {
|
if (comm_await_data_ready(50)) {
|
||||||
byte *buffer = malloc(sizeof(byte));
|
//await in 50ms increments for data to be ready
|
||||||
if (sp_nonblocking_read(port, buffer, 1) == 1) {
|
|
||||||
byte *bits = malloc(sizeof(byte) * 8);
|
byte *buffer = malloc(sizeof(byte)); //allocate enough space to store incoming data
|
||||||
byte_to_bit_array(bits, *buffer);
|
|
||||||
printf("Data received: ");
|
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--) {
|
for (int i = 7; i; i--) {
|
||||||
printf("%d", bits[i]);
|
//
|
||||||
}
|
printf("%d", bits[i]); // THIS IS JUST TESTING CODE
|
||||||
printf("\n");
|
} //
|
||||||
|
printf("\n"); //
|
||||||
free(buffer);
|
free(buffer);
|
||||||
if (bits[7] == 1) {
|
if (bits[7] == 1) {
|
||||||
free(bits);
|
free(bits); //exit if the MSB is 1, so that we don't loop infinitely
|
||||||
break;
|
break; // again, test code
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
comm_end_communication(port);
|
comm_end_communication();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
4
utils.c
4
utils.c
@ -3,6 +3,9 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "communication.h"
|
#include "communication.h"
|
||||||
|
|
||||||
// Helper functions to convert from bits to bytes
|
// Helper functions to convert from bits to bytes
|
||||||
@ -21,3 +24,4 @@ void bit_array_to_byte(byte bit_array[], byte *data) {
|
|||||||
currentpow*=2;
|
currentpow*=2;
|
||||||
} //2's complement notation
|
} //2's complement notation
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user