Skip to content

Commit

Permalink
Support Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
conjikidow committed Jun 2, 2023
1 parent c98e8a7 commit ff92fb6
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 213 deletions.
2 changes: 2 additions & 0 deletions src/src_user/IfWrapper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(C2A_SRCS
if(USE_SCI_COM)
add_definitions(-DUSE_SCI_COM)
list(APPEND C2A_SRCS
SILS/sils_sci_if.c
SILS/CCSDS_SILS_SCI_IF.c
)
message("USE SCI_COM")
Expand All @@ -26,6 +27,7 @@ endif()
if(USE_SCI_COM_UART)
add_definitions(-DUSE_SCI_COM_UART)
list(APPEND C2A_SRCS
SILS/sils_sci_if.c
SILS/uart_sils_sci_if.cpp
)
message("USE SCI_COM")
Expand Down
93 changes: 2 additions & 91 deletions src/src_user/IfWrapper/SILS/CCSDS_SILS_SCI_IF.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,109 +12,20 @@
#include "CCSDS_SILS_SCI_IF.h"


// 最初だけ初期化して、プログラム終了時にポートを閉じるようにしたい
static SCIComPort sci_com;

int SILS_SIC_IF_init()
{
return 0;
}

int SILS_SIC_IF_TX(unsigned char* data_v, int count)
{
sci_com.Send(data_v, 0, count);
SILS_SCI_IF_sci_com_.Send(data_v, 0, count);
return 0;
}

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;
}
return SILS_SCI_IF_sci_com_.Receive(data_v, 0, count);
}

#pragma section
23 changes: 8 additions & 15 deletions src/src_user/IfWrapper/SILS/CCSDS_SILS_SCI_IF.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
#ifndef CCSDS_SILS_SCI_IF_H_
#define CCSDS_SILS_SCI_IF_H_

#include <Windows.h>
#include <stddef.h>
#include "sils_sci_if.h"

// ZEUS SILSからのコピー
class 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;
};
// 最初だけ初期化して、プログラム終了時にポートを閉じるようにしたい
#ifdef WIN32
static SCIComPort SILS_SCI_IF_sci_com_(11);
#else
static SCIComPort SILS_SCI_IF_sci_com_(1);
#endif

int SILS_SIC_IF_init();
int SILS_SIC_IF_TX(unsigned char* data_v, int count);
Expand Down
175 changes: 175 additions & 0 deletions src/src_user/IfWrapper/SILS/sils_sci_if.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
#pragma section REPRO

#include "sils_sci_if.h"

#ifdef WIN32
SCIComPort::SCIComPort(int port)
{
char port_settings[15];
snprintf(port_settings, 15, "%s%d", "\\\\.\\COM", port);
myHComPort_ = CreateFile(port_settings, 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
35 changes: 35 additions & 0 deletions src/src_user/IfWrapper/SILS/sils_sci_if.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef SILS_SCI_IF_H_
#define SILS_SCI_IF_H_

#ifdef WIN32
#include <Windows.h>
#else
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#endif

#include <stddef.h>
#include <stdio.h>

class SCIComPort
{
public:
SCIComPort(int port);
~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
Loading

0 comments on commit ff92fb6

Please sign in to comment.