From 5d0f3dec8cdf14382fc11588379a5f0d0a46309d Mon Sep 17 00:00:00 2001 From: Ryo Suzumoto Date: Sun, 30 Oct 2022 17:59:28 +0900 Subject: [PATCH] fix for 2nd obc --- .../src_user/IfWrapper/Sils/sils_sci_if.cpp | 180 ++++++++++++++++++ .../src_user/IfWrapper/Sils/sils_sci_if.hpp | 41 ++++ .../IfWrapper/Sils/sils_sci_uart_if.cpp | 35 ++++ .../IfWrapper/Sils/sils_sci_uart_if.hpp | 23 +++ .../src/src_user/IfWrapper/Sils/uart_sils.cpp | 6 +- .../IfWrapper/Sils/uart_sils_sci_if.cpp | 117 ------------ .../IfWrapper/Sils/uart_sils_sci_if.hpp | 31 --- .../2nd_obc_user/sync_with_minimum_user.bat | 4 +- 8 files changed, 285 insertions(+), 152 deletions(-) create mode 100644 Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/sils_sci_if.cpp create mode 100644 Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/sils_sci_if.hpp create mode 100644 Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/sils_sci_uart_if.cpp create mode 100644 Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/sils_sci_uart_if.hpp delete mode 100644 Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/uart_sils_sci_if.cpp delete mode 100644 Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/uart_sils_sci_if.hpp diff --git a/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/sils_sci_if.cpp b/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/sils_sci_if.cpp new file mode 100644 index 000000000..37b218820 --- /dev/null +++ b/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/sils_sci_if.cpp @@ -0,0 +1,180 @@ +#pragma section REPRO +/** + * @file + * @brief sils_sci_if + * @details SCI COMでやりとりするIF + */ + +#include "sils_sci_if.hpp" + +#ifdef _WIN32 +SCIComPort::SCIComPort(int port) +{ + char port_name[15]; + snprintf(port_name, 15, "%s%d", "\\\\.\\COM", port); + myHComPort_ = CreateFile(port_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + + if ((int)myHComPort_ == -1) + { + // 正常にポートオープンできていない場合は終了 + CloseHandle(myHComPort_); + init_success = false; + return; + } + + // どうやら正常ポートopenにならないっぽく,これが必要 + init_success = true; + + // ポートのボーレート、パリティ等を設定 + config_.BaudRate = 115200; + config_.Parity = PARITY_NONE; + config_.ByteSize = 8; + config_.StopBits = STOPBITS_10; + + // Parity、StopBits、DataBitsも同様に設定 + SetCommState(myHComPort_, &config_); +} + +SCIComPort::~SCIComPort(void) +{ + if (init_success == true) + { + CloseHandle(myHComPort_); + } +} + +int SCIComPort::Send(unsigned char* buffer, size_t offset, size_t count) +{ + DWORD toWriteBytes = count; // 送信したいバイト数 + DWORD writeBytes; // 実際に送信されたバイト数 + + if (init_success == true) + { + WriteFile(myHComPort_, buffer + offset, toWriteBytes, &writeBytes, NULL); + return writeBytes; + } + else + { + return 0; + } +} + +int SCIComPort::Receive(unsigned char* buffer, size_t offset, size_t count) +{ + DWORD fromReadBytes = count; // 受信したいバイト数 + DWORD dwErrors; + COMSTAT ComStat; + DWORD dwCount; // 受信したバイト数 + + if (init_success == true) + { + ClearCommError(myHComPort_, &dwErrors, &ComStat); + dwCount = ComStat.cbInQue; + + if (dwCount > 0) + { + if (dwCount < count) + { + fromReadBytes = dwCount; + ReadFile(myHComPort_, buffer + offset, fromReadBytes, &dwCount, NULL); + } + else + { + fromReadBytes = count; // 読み込みすぎるとデータが失われるので読み込む量を制御 + ReadFile(myHComPort_, buffer + offset, fromReadBytes, &dwCount, NULL); + } + } + + return dwCount; + } + else + { + return 0; + } +} + +#else + +SCIComPort::SCIComPort(int port) +{ + char port_name[13]; + snprintf(port_name, 13, "%s%d", "/dev/tnt", port); + if ((myHComPort_ = open(port_name, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0) + { + close(myHComPort_); + init_success = false; + return; + } + + // どうやら正常ポートopenにならないっぽく,これが必要 + init_success = true; + + cfsetispeed(&config_, 115200); + cfsetospeed(&config_, 115200); + config_.c_cflag &= ~PARENB; // No Parity + config_.c_cflag &= ~CSTOPB; // 1 Stop Bit + config_.c_cflag &= ~CSIZE; + config_.c_cflag |= CS8; // 8 Bits + tcsetattr(myHComPort_, TCSANOW, &config_); +} + +SCIComPort::~SCIComPort(void) +{ + if (init_success == true) + { + close(myHComPort_); + } +} + +int SCIComPort::Send(unsigned char* buffer, size_t offset, size_t count) +{ + unsigned long toWriteBytes = count; // 送信したいバイト数 + unsigned long writeBytes; // 実際に送信されたバイト数 + + if (init_success == true) + { + writeBytes = write(myHComPort_, buffer + offset, toWriteBytes); + return writeBytes; + } + else + { + return 0; + } +} + +int SCIComPort::Receive(unsigned char* buffer, size_t offset, size_t count) +{ + unsigned long fromReadBytes = count; // 受信したいバイト数 + unsigned long dwErrors; + unsigned long ComStat_cbInQue; + unsigned long dwCount; // 受信したバイト数 + + if (init_success == true) + { + dwCount = ComStat_cbInQue; + + if (dwCount > 0) + { + if (dwCount < count) + { + fromReadBytes = dwCount; + dwCount = read(myHComPort_, buffer + offset, fromReadBytes); + } + else + { + fromReadBytes = count; // 読み込みすぎるとデータが失われるので読み込む量を制御 + dwCount = read(myHComPort_, buffer + offset, fromReadBytes); + } + } + + return dwCount; + } + else + { + return 0; + } +} + +#endif + +#pragma section diff --git a/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/sils_sci_if.hpp b/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/sils_sci_if.hpp new file mode 100644 index 000000000..568bf6099 --- /dev/null +++ b/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/sils_sci_if.hpp @@ -0,0 +1,41 @@ +/** + * @file + * @brief sils_sci_if + * @details SCI COMでやりとりするIF + */ +#ifndef SILS_SCI_IF_HPP_ +#define SILS_SCI_IF_HPP_ + +#ifdef _WIN32 +#include +#else +#include +#include +#include +#endif + +#include +#include + +class SCIComPort +{ +public: + SCIComPort(int port); + virtual ~SCIComPort(void); + + int Send(unsigned char* buffer, size_t length, size_t offset); + int Receive(unsigned char* buffer, size_t length, size_t offset); + +private: +#ifdef _WIN32 + HANDLE myHComPort_; + DCB config_; +#else + int myHComPort_; + struct termios config_; +#endif + bool init_success; +}; + +#endif + diff --git a/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/sils_sci_uart_if.cpp b/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/sils_sci_uart_if.cpp new file mode 100644 index 000000000..1dd3c65ac --- /dev/null +++ b/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/sils_sci_uart_if.cpp @@ -0,0 +1,35 @@ +#pragma section REPRO +/** + * @file + * @brief sils_sci_uart_if + * @details SILSでDriverのテストをするように作った + */ + +#include "sils_sci_uart_if.hpp" + + +// 最初だけ初期化して、プログラム終了時にポートを閉じるようにしたい +#ifdef _WIN32 +static SCIComPortUart SILS_SCI_UART_IF_sci_com_(14); +#else +static SCIComPortUart SILS_SCI_UART_IF_sci_com_(4); +#endif + + +int SILS_SCI_UART_IF_init(void) +{ + return 0; +} + +int SILS_SCI_UART_IF_tx(unsigned char* data_v, int count) +{ + SILS_SCI_UART_IF_sci_com_.Send(data_v, 0, count); + return 0; +} + +int SILS_SCI_UART_IF_rx(unsigned char* data_v, int count) +{ + return SILS_SCI_UART_IF_sci_com_.Receive(data_v, 0, count); +} + +#pragma section diff --git a/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/sils_sci_uart_if.hpp b/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/sils_sci_uart_if.hpp new file mode 100644 index 000000000..f2c32132a --- /dev/null +++ b/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/sils_sci_uart_if.hpp @@ -0,0 +1,23 @@ +/** + * @file + * @brief sils_sci_uart_if + * @details SILSでDriverのテストをするように作った + sils_sci_ccsds_if.c/hのほぼコピー + */ +#ifndef SILS_SCI_UART_IF_HPP_ +#define SILS_SCI_UART_IF_HPP_ + +#include "sils_sci_if.hpp" + +class SCIComPortUart : public SCIComPort +{ +public: + SCIComPortUart(int port) : SCIComPort(port) {}; + ~SCIComPortUart(void) {}; +}; + +int SILS_SCI_UART_IF_init(); +int SILS_SCI_UART_IF_tx(unsigned char* data_v, int count); +int SILS_SCI_UART_IF_rx(unsigned char* data_v, int count); + +#endif diff --git a/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/uart_sils.cpp b/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/uart_sils.cpp index a8e7e0635..5bb7a46d0 100644 --- a/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/uart_sils.cpp +++ b/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/uart_sils.cpp @@ -3,7 +3,7 @@ #include "../../Settings/port_config.h" #ifdef USE_SCI_COM_WINGS -#include "uart_sils_sci_if.hpp" +#include "sils_sci_urat_if.hpp" #endif int OBC_C2A_SendFromObc(int port_id, unsigned char* buffer, int offset, int count); @@ -24,7 +24,7 @@ int UART_rx(void* my_uart_v, void* data_v, int buffer_size) } #ifdef USE_SCI_COM_WINGS - return SILS_SCI_UART_IF_RX((unsigned char*)data_v, buffer_size); + return SILS_SCI_UART_IF_rx((unsigned char*)data_v, buffer_size); #else return OBC_C2A_ReceivedByObc(my_uart->ch, (unsigned char*)data_v, 0, buffer_size); #endif @@ -46,7 +46,7 @@ int UART_tx(void* my_uart_v, void* data_v, int data_size) } } #ifdef USE_SCI_COM_WINGS - SILS_SCI_UART_IF_TX((unsigned char*)data_v, data_size); + SILS_SCI_UART_IF_tx((unsigned char*)data_v, data_size); #else if (OBC_C2A_SendFromObc(my_uart->ch, (unsigned char*)data_v, 0, data_size) < 0) { diff --git a/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/uart_sils_sci_if.cpp b/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/uart_sils_sci_if.cpp deleted file mode 100644 index 6fb389fe6..000000000 --- a/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/uart_sils_sci_if.cpp +++ /dev/null @@ -1,117 +0,0 @@ -#pragma section REPRO -/** - * @file - * @brief uart_sils_sci_if - * @details SILSでDriverのテストをするように作った - ccsds_sils_sci_if.c/hのほぼコピー - */ - -#include "uart_sils_sci_if.hpp" - - -// 最初だけ初期化して、プログラム終了時にポートを閉じるようにしたい -static SCIComPortUart sci_com_uart_; - -int SILS_SCI_UART_IF_init(void) -{ - return 0; -} - -int SILS_SCI_UART_IF_TX(unsigned char* data_v, int count) -{ - sci_com_uart_.Send(data_v, 0, count); - return 0; -} - -int SILS_SCI_UART_IF_RX(unsigned char* data_v, int count) -{ - return sci_com_uart_.Receive(data_v, 0, count); -} - - -SCIComPortUart::SCIComPortUart(void) -{ - // ビルド通らなかったので,ZEUSからちょっと変えた - myHComPort_ = CreateFile("\\\\.\\COM14", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - - if ((int)myHComPort_ == -1) - { - // 正常にポートオープンできていない場合は終了 - CloseHandle(myHComPort_); - init_success = false; - return; - } - - // どうやら正常ポートopenにならないっぽく,これが必要 - init_success = true; - - // ポートのボーレート、パリティ等を設定 - config_.BaudRate = 115200; - config_.Parity = PARITY_NONE; - config_.ByteSize = 8; - config_.StopBits = STOPBITS_10; - - // Parity、StopBits、DataBitsも同様に設定 - SetCommState(myHComPort_, &config_); -} - -SCIComPortUart::~SCIComPortUart(void) -{ - if (init_success == true) - { - CloseHandle(myHComPort_); - } -} - -int SCIComPortUart::Send(unsigned char* buffer, size_t offset, size_t count) -{ - DWORD toWriteBytes = count; // 送信したいバイト数 - DWORD writeBytes; // 実際に送信されたバイト数 - - if (init_success == true) - { - WriteFile(myHComPort_, buffer + offset, toWriteBytes, &writeBytes, NULL); - - return writeBytes; - } - else - { - return 0; - } -} - -int SCIComPortUart::Receive(unsigned char* buffer, size_t offset, size_t count) -{ - DWORD fromReadBytes = count; // 受信したいバイト数 - DWORD dwErrors; - COMSTAT ComStat; - DWORD dwCount; // 受信したバイト数 - - if (init_success == true) - { - ClearCommError(myHComPort_, &dwErrors, &ComStat); - dwCount = ComStat.cbInQue; - - if (dwCount > 0) - { - if (dwCount < count) - { - fromReadBytes = dwCount; - ReadFile(myHComPort_, buffer + offset, fromReadBytes, &dwCount, NULL); - } - else - { - fromReadBytes = count; // 読み込みすぎるとデータが失われるので読み込む量を制御 - ReadFile(myHComPort_, buffer + offset, fromReadBytes, &dwCount, NULL); - } - } - - return dwCount; - } - else - { - return 0; - } -} - -#pragma section diff --git a/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/uart_sils_sci_if.hpp b/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/uart_sils_sci_if.hpp deleted file mode 100644 index e6ff4550c..000000000 --- a/Examples/2nd_obc_user/src/src_user/IfWrapper/Sils/uart_sils_sci_if.hpp +++ /dev/null @@ -1,31 +0,0 @@ -/** - * @file - * @brief uart_sils_sci_if - * @details SILSでDriverのテストをするように作った - ccsds_sils_sci_if.c/hのほぼコピー - */ -#ifndef UART_SILS_SCI_IF_HPP_ -#define UART_SILS_SCI_IF_HPP_ - -#include - -class SCIComPortUart -{ -public: - SCIComPortUart(void); - ~SCIComPortUart(void); - - int Send(unsigned char* buffer, size_t length, size_t offset); - int Receive(unsigned char* buffer, size_t length, size_t offset); - -private: - HANDLE myHComPort_; - DCB config_; - bool init_success; -}; - -int SILS_SCI_UART_IF_init(); -int SILS_SCI_UART_IF_TX(unsigned char* data_v, int count); -int SILS_SCI_UART_IF_RX(unsigned char* data_v, int count); - -#endif diff --git a/Examples/2nd_obc_user/sync_with_minimum_user.bat b/Examples/2nd_obc_user/sync_with_minimum_user.bat index 787d5623f..9367f197a 100644 --- a/Examples/2nd_obc_user/sync_with_minimum_user.bat +++ b/Examples/2nd_obc_user/sync_with_minimum_user.bat @@ -14,7 +14,9 @@ call :sync_dir ".\src\src_user\Script\" "..\minimum_user\src\src_user\Script\" call :sync_dir ".\src\src_user\Settings\TlmCmd\Ccsds\" "..\minimum_user\src\src_user\Settings\TlmCmd\Ccsds\" call :sync_file ".\src\src_user\Applications\UserDefined\debug_apps.h" "..\minimum_user\src\src_user\Applications\UserDefined\debug_apps.h" -call :sync_file ".\src\src_user\IfWrapper\Sils\uart_sils_sci_if.hpp" "..\minimum_user\src\src_user\IfWrapper\Sils\uart_sils_sci_if.hpp" +call :sync_file ".\src\src_user\IfWrapper\Sils\sils_sci_uart_if.hpp" "..\minimum_user\src\src_user\IfWrapper\Sils\sils_sci_uart_if.hpp" +call :sync_file ".\src\src_user\IfWrapper\Sils\sils_sci_if.cpp" "..\minimum_user\src\src_user\IfWrapper\Sils\sils_sci_if.cpp" +call :sync_file ".\src\src_user\IfWrapper\Sils\sils_sci_if.hpp" "..\minimum_user\src\src_user\IfWrapper\Sils\sils_sci_if.hpp" call :sync_file ".\src\src_user\IfWrapper\Sils\wdt_sils.cpp" "..\minimum_user\src\src_user\IfWrapper\Sils\wdt_sils.cpp" call :sync_file ".\src\src_user\IfWrapper\SilsMockup\README.md" "..\minimum_user\src\src_user\IfWrapper\SilsMockup\README.md" call :sync_file ".\src\src_user\IfWrapper\SilsMockup\uart_sils.c" "..\minimum_user\src\src_user\IfWrapper\SilsMockup\uart_sils.c"