From 40e386a56778365bb05540b2a501f4cacce8431c Mon Sep 17 00:00:00 2001 From: flap1 Date: Wed, 8 Jun 2022 18:43:49 +0900 Subject: [PATCH] support linux --- .../src/src_user/IfWrapper/CMakeLists.txt | 2 + .../IfWrapper/Sils/ccsds_sils_sci_if.cpp | 94 +-------- .../IfWrapper/Sils/ccsds_sils_sci_if.hpp | 20 +- .../src_user/IfWrapper/Sils/sils_sci_if.cpp | 181 ++++++++++++++++++ .../src_user/IfWrapper/Sils/sils_sci_if.hpp | 42 ++++ .../IfWrapper/Sils/uart_sils_sci_if.cpp | 87 --------- .../IfWrapper/Sils/uart_sils_sci_if.hpp | 16 +- 7 files changed, 239 insertions(+), 203 deletions(-) create mode 100644 Examples/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/sils_sci_if.cpp create mode 100644 Examples/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/sils_sci_if.hpp diff --git a/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/CMakeLists.txt b/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/CMakeLists.txt index 89328307f..d080aeb73 100644 --- a/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/CMakeLists.txt +++ b/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/CMakeLists.txt @@ -29,6 +29,7 @@ if(USE_SCI_COM_WINGS AND NOT USE_SILS_MOCKUP) add_definitions(-DUSE_SCI_COM_WINGS) #target_sources(${PROJECT_NAME} PUBLIC list(APPEND C2A_SRCS + Sils/sils_sci_if.cpp Sils/ccsds_sils_sci_if.cpp ) message("USE SCI_COM_WINGS") @@ -38,6 +39,7 @@ if(USE_SCI_COM_UART AND NOT USE_SILS_MOCKUP) add_definitions(-DUSE_SCI_COM_UART) #target_sources(${PROJECT_NAME} PUBLIC list(APPEND C2A_SRCS + Sils/sils_sci_if.cpp Sils/uart_sils_sci_if.cpp ) message("USE SCI_COM_UART") diff --git a/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/ccsds_sils_sci_if.cpp b/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/ccsds_sils_sci_if.cpp index 6a40e5e53..e9a9cfa0f 100644 --- a/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/ccsds_sils_sci_if.cpp +++ b/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/ccsds_sils_sci_if.cpp @@ -3,15 +3,17 @@ * @file * @brief ccsds_sils_sci_if * @details WINGS TMTC IFとCCSDSのTransfer FrameをSCI COMでやりとりするIF - Windows上でcom0comを使うことを想定 - SCIComPort classは基本的にEQU ZEUSのコードを流用 */ #include "ccsds_sils_sci_if.hpp" // 最初だけ初期化して、プログラム終了時にポートを閉じるようにしたい -static SCIComPort sci_com_; +#ifdef WIN32 +static SCIComPortCcsds sci_com_(11); +#else +static SCIComPortCcsds sci_com_(1); +#endif int SILS_SIC_IF_init(void) { @@ -29,90 +31,4 @@ int SILS_SIC_IF_RX(unsigned char* data_v, int count) return sci_com_.Receive(data_v, 0, count); } - -SCIComPort::SCIComPort(void) -{ - // ビルド通らなかったので,ZEUSからちょっと変えた - myHComPort_ = CreateFile("\\\\.\\COM11", 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; - } -} - #pragma section diff --git a/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/ccsds_sils_sci_if.hpp b/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/ccsds_sils_sci_if.hpp index 5119340a0..0ae5f118e 100644 --- a/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/ccsds_sils_sci_if.hpp +++ b/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/ccsds_sils_sci_if.hpp @@ -2,28 +2,18 @@ * @file * @brief ccsds_sils_sci_if * @details WINGS TMTC IFとCCSDSのTransfer FrameをSCI COMでやりとりするIF - Windows上でcom0comを使うことを想定 - SCIComPort classは基本的にEQU ZEUSのコードを流用 */ #ifndef CCSDS_SILS_SCI_IF_HPP_ #define CCSDS_SILS_SCI_IF_HPP_ -#include +#include +#include "sils_sci_if.hpp" -// ZEUS SILSからのコピー -class SCIComPort +class SCIComPortCcsds : public SCIComPort { public: - SCIComPort(void); - ~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: - HANDLE myHComPort_; - DCB config_; - bool init_success; + SCIComPortCcsds(int port) : SCIComPort(port) {}; + ~SCIComPortCcsds(void) {}; }; int SILS_SIC_IF_init(); diff --git a/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/sils_sci_if.cpp b/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/sils_sci_if.cpp new file mode 100644 index 000000000..412b0fbcf --- /dev/null +++ b/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/sils_sci_if.cpp @@ -0,0 +1,181 @@ +#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_settings[15]; + snprintf(port_settings, 15, "%s%d", "\\\\.\\COM", port); + // ビルド通らなかったので,ZEUSからちょっと変えた + myHComPort_ = CreateFile(port_id, 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_settings[13]; + snprintf(port_settings, 13, "%s%d", "/dev/tnt", port); + if ((myHComPort_ = open(port_settings, 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/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/sils_sci_if.hpp b/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/sils_sci_if.hpp new file mode 100644 index 000000000..43dc9693c --- /dev/null +++ b/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/sils_sci_if.hpp @@ -0,0 +1,42 @@ +/** + * @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 + +// ZEUS SILSからのコピー +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/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/uart_sils_sci_if.cpp b/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/uart_sils_sci_if.cpp index 9bcb379d6..97245c391 100644 --- a/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/uart_sils_sci_if.cpp +++ b/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/uart_sils_sci_if.cpp @@ -3,7 +3,6 @@ * @file * @brief uart_sils_sci_if * @details SILSでDriverのテストをするように作った - ccsds_sils_sci_if.c/hのほぼコピー */ #include "uart_sils_sci_if.hpp" @@ -28,90 +27,4 @@ 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("\\\\.\\COM13", 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/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/uart_sils_sci_if.hpp b/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/uart_sils_sci_if.hpp index e6ff4550c..d58e33124 100644 --- a/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/uart_sils_sci_if.hpp +++ b/Examples/minimum_user_for_s2e/src/src_user/IfWrapper/Sils/uart_sils_sci_if.hpp @@ -7,21 +7,13 @@ #ifndef UART_SILS_SCI_IF_HPP_ #define UART_SILS_SCI_IF_HPP_ -#include +#include "sils_sci_if.hpp" -class SCIComPortUart +class SCIComPortUart : public SCIComPort { 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; + SCIComPortUart(int port) : SCIComPort(port) {}; + ~SCIComPortUart(void) {}; }; int SILS_SCI_UART_IF_init();