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