C2A_Core
ccsds_sils_sci_if.cpp
[詳解]
1 #pragma section REPRO
10 #include "ccsds_sils_sci_if.hpp"
11 
12 
13 // 最初だけ初期化して、プログラム終了時にポートを閉じるようにしたい
15 
17 {
18  return 0;
19 }
20 
21 int SILS_SIC_IF_TX(unsigned char* data_v, int count)
22 {
23  sci_com_.Send(data_v, 0, count);
24  return 0;
25 }
26 
27 int SILS_SIC_IF_RX(unsigned char* data_v, int count)
28 {
29  return sci_com_.Receive(data_v, 0, count);
30 }
31 
32 
34 {
35  // ビルド通らなかったので,ZEUSからちょっと変えた
36  myHComPort_ = CreateFile("\\\\.\\COM11", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
37 
38  if ((int)myHComPort_ == -1)
39  {
40  // 正常にポートオープンできていない場合は終了
41  CloseHandle(myHComPort_);
42  init_success = false;
43  return;
44  }
45 
46  // どうやら正常ポートopenにならないっぽく,これが必要
47  init_success = true;
48 
49  // ポートのボーレート、パリティ等を設定
50  config_.BaudRate = 115200;
51  config_.Parity = PARITY_NONE;
52  config_.ByteSize = 8;
53  config_.StopBits = STOPBITS_10;
54 
55  // Parity、StopBits、DataBitsも同様に設定
56  SetCommState(myHComPort_, &config_);
57 }
58 
60 {
61  if (init_success == true)
62  {
63  CloseHandle(myHComPort_);
64  }
65 }
66 
67 int SCIComPort::Send(unsigned char* buffer, size_t offset, size_t count)
68 {
69  DWORD toWriteBytes = count; // 送信したいバイト数
70  DWORD writeBytes; // 実際に送信されたバイト数
71 
72  if (init_success == true)
73  {
74  WriteFile(myHComPort_, buffer + offset, toWriteBytes, &writeBytes, NULL);
75 
76  return writeBytes;
77  }
78  else
79  {
80  return 0;
81  }
82 }
83 
84 int SCIComPort::Receive(unsigned char* buffer, size_t offset, size_t count)
85 {
86  DWORD fromReadBytes = count; // 受信したいバイト数
87  DWORD dwErrors;
88  COMSTAT ComStat;
89  DWORD dwCount; // 受信したバイト数
90 
91  if (init_success == true)
92  {
93  ClearCommError(myHComPort_, &dwErrors, &ComStat);
94  dwCount = ComStat.cbInQue;
95 
96  if (dwCount > 0)
97  {
98  if (dwCount < count)
99  {
100  fromReadBytes = dwCount;
101  ReadFile(myHComPort_, buffer + offset, fromReadBytes, &dwCount, NULL);
102  }
103  else
104  {
105  fromReadBytes = count; // 読み込みすぎるとデータが失われるので読み込む量を制御
106  ReadFile(myHComPort_, buffer + offset, fromReadBytes, &dwCount, NULL);
107  }
108  }
109 
110  return dwCount;
111  }
112  else
113  {
114  return 0;
115  }
116 }
117 
118 #pragma section
int SILS_SIC_IF_RX(unsigned char *data_v, int count)
static SCIComPort sci_com_
int SILS_SIC_IF_init(void)
int SILS_SIC_IF_TX(unsigned char *data_v, int count)
ccsds_sils_sci_if
int Send(unsigned char *buffer, size_t length, size_t offset)
int Receive(unsigned char *buffer, size_t length, size_t offset)