Added test cpp Project
Signed-off-by: Bernardo Carvalho <bernardo.carvalho@tecnico.ulisboa.pt>
This commit is contained in:
5
DataSources/PSUCommunicator/testSerial/CMakeLists.txt
Normal file
5
DataSources/PSUCommunicator/testSerial/CMakeLists.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
cmake_minimum_required(VERSION 3.25)
|
||||
|
||||
project(serial_psu)
|
||||
add_executable(psuSendReceive psuSendReceive.cpp)
|
||||
74
DataSources/PSUCommunicator/testSerial/PSUMessages.h
Normal file
74
DataSources/PSUCommunicator/testSerial/PSUMessages.h
Normal file
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* @file PSUMessages.h
|
||||
* @brief Header file
|
||||
* @date 25/10/2025
|
||||
* @author Bernardo Carvalho
|
||||
*
|
||||
*
|
||||
* @copyright Copyright 2025 European Joint Undertaking for ITER and
|
||||
* the Development of Fusion Energy ('Fusion for Energy').
|
||||
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved
|
||||
* by the European Commission - subsequent versions of the EUPL (the "Licence")
|
||||
* You may not use this work except in compliance with the Licence.
|
||||
* You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl
|
||||
*
|
||||
* @warning Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the Licence is distributed on an "AS IS"
|
||||
* basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
||||
* or implied. See the Licence permissions and limitations under the Licence.
|
||||
|
||||
* @details This header file contains the declaration of the class AtcaIopADC
|
||||
* with all of its public, protected and private members. It may also include
|
||||
* definitions for inline methods which need to be visible to the compiler.
|
||||
*/
|
||||
#define FA_CHARGE_MESSAGE_1 0x6C
|
||||
#define FA_CHARGE_MESSAGE_2 0x6D
|
||||
#define FA_SHUTDOWN_MESSAGE_1 0x92
|
||||
#define FA_SHUTDOWN_MESSAGE_2 0x93
|
||||
#define FA_STARTOP_MESSAGE_1 0xFE
|
||||
#define FA_STARTOP_MESSAGE_2 0xFF
|
||||
#define FA_STOPOP_MESSAGE_1 0x00
|
||||
#define FA_STOPOP_MESSAGE_2 0x01
|
||||
|
||||
#define FA_STARTED_MESSAGE_1 0xFE
|
||||
#define FA_STARTED_MESSAGE_2 0xFF
|
||||
#define FA_STOPPED_MESSAGE_1 0x00
|
||||
#define FA_STOPPED_MESSAGE_2 0x01
|
||||
#define FA_STOP_ERROR_MESSAGE_1 0x24
|
||||
#define FA_STOP_ERROR_MESSAGE_2 0x25
|
||||
#define FA_COMM_ERROR_MESSAGE_1 0xDA
|
||||
#define FA_COMM_ERROR_MESSAGE_2 0xDB
|
||||
|
||||
#define FA_COMMUNICATION_MAX_PACKETS 4
|
||||
#define FA_FRAMING_BIT_MASK 0x01
|
||||
|
||||
#define FA_SCALE_MIN 0
|
||||
#define FA_SCALE_MAX 1023
|
||||
|
||||
#define LOG_CHARGE 1
|
||||
#define LOG_SHUTDOWN 2
|
||||
#define LOG_STARTOP 3
|
||||
#define LOG_STOPOP 4
|
||||
#define LOG_TEMPERATURE_FAULT 5
|
||||
#define LOG_24V_FAULT 6
|
||||
#define LOG_CHARGED 7
|
||||
#define LOG_NOT_CHARGED 8
|
||||
#define LOG_STARTED 9
|
||||
#define LOG_STOPPED 10
|
||||
#define LOG_STOP_FAULT 11
|
||||
#define LOG_COMMUNICATION_FAULT 12
|
||||
#define LOG_CURRENT_VALUE 13
|
||||
|
||||
// Logging #defines
|
||||
// #define __FA_COM_LOG_RECEIVED_MESSAGES
|
||||
// #define ___FA_COM_LOG_SENT_MESSAGES
|
||||
#define __FA_COM_LOG_LEVEL InitialisationError
|
||||
|
||||
// Communicator Online Stages
|
||||
#define FA_COMMUNICATOR_ONLINE_IDLE 0
|
||||
#define FA_COMMUNICATOR_ONLINE_WAIT_CODAC_TRIGGER 1
|
||||
#define FA_COMMUNICATOR_ONLINE_DISCHARGE 2
|
||||
#define FA_COMMUNICATOR_ONLINE_STOP_OPERATION 3
|
||||
#define FA_COMMUNICATOR_ONLINE_ERROR 4
|
||||
|
||||
#define FA_COMMUNICATOR_MAXIMUM_ATTEMPTS 5
|
||||
174
DataSources/PSUCommunicator/testSerial/psuSendReceive.cpp
Normal file
174
DataSources/PSUCommunicator/testSerial/psuSendReceive.cpp
Normal file
@@ -0,0 +1,174 @@
|
||||
// C library headers
|
||||
// setserial /dev/ttyS0 spd_cust
|
||||
// setserial /dev/ttyS0 divisor 16
|
||||
// stty -F /dev/ttyS0 921600
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// Linux headers
|
||||
#include <errno.h> // Error integer and strerror() function
|
||||
#include <fcntl.h> // Contains file controls like O_RDWR
|
||||
#include <termios.h> // Contains POSIX terminal control definitions
|
||||
#include <unistd.h> // write(), read(), close()
|
||||
|
||||
#include "PSUMessages.h"
|
||||
|
||||
uint8_t packet[2];
|
||||
|
||||
int serial_port;
|
||||
|
||||
bool CreateCurrentPacket(unsigned int current) {
|
||||
bool ok = true;
|
||||
|
||||
// Calculate the point in the scale of the current
|
||||
int16_t pointOfCurrent = current;
|
||||
|
||||
// Saturate current
|
||||
if (pointOfCurrent < FA_SCALE_MIN)
|
||||
pointOfCurrent = FA_SCALE_MIN;
|
||||
if (pointOfCurrent > FA_SCALE_MAX)
|
||||
pointOfCurrent = FA_SCALE_MAX;
|
||||
|
||||
// Build packets
|
||||
uint16_t pc = (uint16_t)pointOfCurrent;
|
||||
uint16_t nc = ~pc;
|
||||
packet[0] = (uint8_t)(0x0000 | ((nc & 0x03C0) >> 5) | ((pc & 0x0007) << 5));
|
||||
packet[1] = (uint8_t)(0x0001 | ((pc & 0x03F8) >> 2));
|
||||
nc = (uint16_t)packet[1];
|
||||
nc <<= 8;
|
||||
nc &= 0xFF00;
|
||||
nc |= packet[0];
|
||||
printf("Current: %d, Packet 0x%02X 0x%02X\n", current, packet[0], packet[1]);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int read_msg() {
|
||||
// Allocate memory for read buffer, set size according to your needs
|
||||
char read_buf[4];
|
||||
|
||||
// Normally you wouldn't do this memset() call, but since we will just receive
|
||||
// call printf() easily.
|
||||
memset(&read_buf, '\0', sizeof(read_buf));
|
||||
// wait 1 ms
|
||||
usleep(1000);
|
||||
|
||||
// Read bytes. The behaviour of read() (e.g. does it block?,
|
||||
// how long does it block for?) depends on the configuration
|
||||
// settings above, specifically VMIN and VTIME
|
||||
int num_bytes = read(serial_port, &read_buf, sizeof(read_buf));
|
||||
|
||||
// n is the number of bytes read. n may be 0 if no bytes were received, and
|
||||
// can also be -1 to signal an error.
|
||||
if (num_bytes < 0) {
|
||||
printf("Error reading: %s", strerror(errno));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// Here we assume we received ASCII data, but you might be sending raw bytes
|
||||
// (in that case, don't try and print it to the screen like this!)
|
||||
printf("Read %i bytes. \t", num_bytes);
|
||||
for (int i = 0; i < num_bytes; i++) {
|
||||
unsigned int val = read_buf[i] & 0xFF;
|
||||
printf("%i: 0x%02X \t", i, val);
|
||||
// printf("%i: %u \n", i, val);
|
||||
}
|
||||
printf("\n");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Open the serial port. Change device path as needed (currently set to an
|
||||
// standard FTDI USB-UART cable type device)
|
||||
serial_port = open("/dev/ttyS2", O_RDWR);
|
||||
if (serial_port <= 0) {
|
||||
printf("Error %i from open: %s\n", serial_port, strerror(errno));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
const speed_t baud_rate = B921600;
|
||||
// B115200; // B576000; // B460800; OK// B230400; // B9600;B921600;
|
||||
// const speed_t baud_rate = B115200; // B9600;B921600;
|
||||
|
||||
// Create new termios struct, we call it 'tty' for convention
|
||||
struct termios tty;
|
||||
|
||||
// Read in existing settings, and handle any error
|
||||
if (tcgetattr(serial_port, &tty) != 0) {
|
||||
printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity (most common)
|
||||
tty.c_cflag |= PARENB;
|
||||
tty.c_cflag |= PARODD;
|
||||
tty.c_cflag |= CSTOPB;
|
||||
// tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in
|
||||
// communication (most common)
|
||||
tty.c_cflag &= ~CSIZE; // Clear all bits that set the data size
|
||||
tty.c_cflag |= CS8; // 8 bits per byte (most common)
|
||||
tty.c_cflag &=
|
||||
~CRTSCTS; // Disable RTS/CTS hardware flow control (most common)
|
||||
tty.c_cflag |=
|
||||
CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)
|
||||
|
||||
tty.c_lflag &= ~ICANON;
|
||||
tty.c_lflag &= ~ECHO; // Disable echo
|
||||
tty.c_lflag &= ~ECHOE; // Disable erasure
|
||||
tty.c_lflag &= ~ECHONL; // Disable new-line echo
|
||||
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
|
||||
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
|
||||
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR |
|
||||
ICRNL); // Disable any special handling of received bytes
|
||||
// write(serial_port, packet, sizeof(packet));
|
||||
// printf("Sent : %lu bytes\n", sizeof(packet));
|
||||
|
||||
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g.
|
||||
// newline chars)
|
||||
tty.c_oflag &=
|
||||
~ONLCR; // Prevent conversion of newline to carriage return/line feed
|
||||
// tty.c_oflag &= ~OXTABS; // Prevent conversion of tabs to spaces (NOT
|
||||
// PRESENT ON LINUX) tty.c_oflag &= ~ONOEOT; // Prevent removal of C-d chars
|
||||
// (0x004) in output (NOT PRESENT ON LINUX)
|
||||
|
||||
tty.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soon
|
||||
// as any data is received.
|
||||
tty.c_cc[VMIN] = 0;
|
||||
|
||||
// Set in/out baud rate
|
||||
cfsetispeed(&tty, baud_rate);
|
||||
cfsetospeed(&tty, baud_rate);
|
||||
|
||||
// Save tty settings, also checking for error
|
||||
if (tcsetattr(serial_port, TCSANOW, &tty) != 0) {
|
||||
printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
// clear input port
|
||||
read_msg();
|
||||
// Write to serial port
|
||||
// unsigned char msg[] = {FA_STARTOP_MESSAGE_1, 0xFF};
|
||||
packet[0] = FA_STARTOP_MESSAGE_1;
|
||||
packet[1] = FA_STARTOP_MESSAGE_2;
|
||||
write(serial_port, packet, sizeof(packet));
|
||||
printf("Sent : %lu bytes\n", sizeof(packet));
|
||||
read_msg();
|
||||
CreateCurrentPacket(100);
|
||||
write(serial_port, packet, sizeof(packet));
|
||||
printf("Sent : %lu bytes\n", sizeof(packet));
|
||||
read_msg();
|
||||
|
||||
// Halting the execution for 100000 Microseconds (0.1 seconds)
|
||||
usleep(100000);
|
||||
packet[0] = FA_STOPOP_MESSAGE_1;
|
||||
packet[1] = FA_STOPOP_MESSAGE_2;
|
||||
write(serial_port, packet, sizeof(packet));
|
||||
printf("Sent : %lu bytes\n", sizeof(packet));
|
||||
read_msg();
|
||||
close(serial_port);
|
||||
return 0; // success
|
||||
};
|
||||
;
|
||||
Reference in New Issue
Block a user