-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_sample.cpp
More file actions
203 lines (175 loc) · 4.82 KB
/
Copy pathupdate_sample.cpp
File metadata and controls
203 lines (175 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <time.h>
#include "goplt_if.h"
/////////// シリアル通信用
#define SERIAL_PORT "/dev/ttyACM0" // ファイルディスクリプタ
static int fd=0;
BOOL Serial_Init(){
struct termios tio; // シリアル通信設定
int baudRate = B115200;
int i;
int len;
int ret;
int size;
//シリアルポートの初期化
fd = open(SERIAL_PORT, O_RDWR); // デバイスをオープンする
if (fd < 0) {
printf("open error\n");
return -1;
}
//非カノニカルモードで動作させるための設定
tio.c_cflag += CREAD; // 受信有効
tio.c_cflag += CLOCAL; // ローカルライン(モデム制御なし)
tio.c_cflag += CS8; // データビット:8bit
tio.c_cflag += 0; // ストップビット:1bit
tio.c_cflag += 0; // パリティ:None
cfsetispeed( &tio, baudRate ); //USB接続では設定に影響ない
cfsetospeed( &tio, baudRate );
tio.c_iflag=IGNPAR | ICRNL;
cfmakeraw(&tio); // RAWモード
//受信タイムアウトを待たないよう設定
tio.c_cc[VTIME]=0;
tio.c_cc[VMIN]=0;
tcsetattr( fd, TCSANOW, &tio ); // デバイスに設定を行う
ioctl(fd, TCSETS, &tio); // ポートの設定を有効にする
}
void Serial_Close()
{
close(fd);
}
//#define LOG_OUTPUT //コンソールに通信内容出力する場合はコメントアウト
static int isbinary=FALSE;
void debug_binary(int mode){
isbinary=mode;
}
#ifdef LOG_OUTPUT
void debug_put_console(int c)
{
if(c!=-1){
if(isbinary){
// printf("%02x ",(uint8_t)c);
// if(c==0x0d){
// printf("\n");
// }
}else{
if(c==0x0d){
printf("\n");
}else if(c==0x02){
printf("[stx]");
}else if(c==0x03){
printf("[etx]");
}else if(c==0x06){
printf("[ack]");
}else if(c==0x15){
printf("[nak]");
}else{
printf("%c",c);
}
}
}
}
#else
#define debug_put_console(a) //
#endif
//ポートから1バイト取得 データない場合は-1を返す
int uart_getc(){
char buf[1];
int c;
if(read(fd,buf,1)){
c=buf[0];
}else{
c=-1;
}
debug_put_console(c);
return c;
}
//ポートから1バイト出力
void uart_putc(char c){
char buf[1];
buf[0]=c;
write(fd,buf,1);
debug_put_console(c);
}
//システム時刻をms単位で取得
uint32_t get_syscount(){
uint32_t _systime=0;
static struct timespec _sttime={0,0};
if(_sttime.tv_sec==0&&_sttime.tv_nsec==0){
clock_gettime(CLOCK_REALTIME, &_sttime);
}else{
struct timespec _nowtime;
clock_gettime(CLOCK_REALTIME, &_nowtime);
_systime=(_nowtime.tv_sec-_sttime.tv_sec)*1000+(_nowtime.tv_nsec-_sttime.tv_nsec)/1000000;
}
return _systime;
}
//////GOPアプリケーション動作用
int32_t sv_1,pv_1;
BOOL run_flag=FALSE; //運転状態のフラグ
//メッセージ"START"受信時 運転フラグをセット
void fnSTART()
{
run_flag=TRUE;
}
//メッセージ"STOP"受信時 運転フラグをリセット
void fnSTOP()
{
run_flag=FALSE;
}
//メッセージ"SET"受信時 GOPのメモリーSV_1の値をホスト側の変数sv_1に読み込み
void fnSET()
{
LtMemRead("SV_1",&sv_1);
}
void fnUPDATE()
{
DoTransfarDataSer("../update_data/");
}
//メッセージ対するハンドラー関数の設定
ltMes_Callback tbl[]={
{"START",fnSTART},
{"STOP",fnSTOP},
{"SET",fnSET},
{"UPDATE",fnUPDATE},
{NULL,NULL} //ハンドラーテーブルの終端を示すため、最後の行に{NULL,NULL}を登録
};
//運転フラグセット時の動作
//pv_1をsv_1に近づけるよう値を増減
//増減した結果をGOPのメモリーPV_1に書き込み
void run()
{
int d=sv_1-pv_1;
if(d<=10){
pv_1-=3;
}else{
if(d>100){
d=10;
}else {
d/=10;
}
pv_1+=d;
}
LtMemWrite("PV_1",pv_1);
}
int main(int argc, char *argv[])
{
//シリアル通信の初期化
Serial_Init();
//メッセージハンドラーを登録
LtSetMessageCallback(tbl);
// メインループ
while(1) {
if(run_flag){
run();
}
LtEnq(NULL); //メッセージの受信処理 この関数をメインループで呼ぶとメッセージに応じたハンドラ関数を呼び出します。
}
close(fd); // デバイスのクローズ
return 0;
}