-
Notifications
You must be signed in to change notification settings - Fork 1
/
FastCTD.c
executable file
·141 lines (105 loc) · 3.75 KB
/
FastCTD.c
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
//
// main.m
// FastCTD
// May 4th, 2004: Put the code from "FASTCATCTD" written by ANSI-C
// Created by Mai Bui on Mon May 03 2004.
// Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#include "Globals.h"
#include "FileUtils.h"
#include "SerialPortUtils.h"
#include "FastCat_CTDdaq.h"
#include "PThreadUtils.h"
#include <time.h>
// Allocate main Fast CTD structure
int InitApp(FastCTDStructPtr *FCTDPtr, char SPList[][MAXNAMELEN])
{
FastCTDStructPtr newCTDPtr = NULL;
char temp[4][MAXNAMELEN] = {"\0"};
// Allocate for CTD Structure
newCTDPtr = (FastCTDStruct*)malloc(sizeof(FastCTDStruct));
if (newCTDPtr == NULL)
return 0;
*FCTDPtr = newCTDPtr;
memcpy(SPList,temp,sizeof(temp));
// Initialize CTD's data structure
FastCTDInit(*FCTDPtr);
// Get all coefficiences from configure file over write defaults set above for CTD's data structure
GetCoef_fromFile(*FCTDPtr);
// Create data file and setup file
CreateNewFile(&((*FCTDPtr)->fpData[0]));
// find and open serial ports
// ****** Start for Find_OpenSerialPort(...)
FindSerialPortName(SPList);
MakeSerialPortList(SPList);
// Initialize Serial Ports
InitSerialPort(&((*FCTDPtr)->SerialPort[0]), SPList[0]);
// Now open the serial port we want, if that serial was open, close it and open it again.
// if ((*FCTDPtr)->SerialPort[0].spd!=0) CloseSerialPort((*FCTDPtr)->SerialPort[0]); // close port 1
if ((OpenSerialPort(&((*FCTDPtr)->SerialPort[0])))!=0) // != 0 -> failed
return EX_IOERR;
//***** Find_OpenSerialPort(SPList, &((*FCTDPtr)->SerialPort[0]));
return 1;
}
void ReleaseAllMemory(FastCTDStructPtr *FastCTDPtr)
{
// Close all files
CloseCurrFile(&(*FastCTDPtr)->fpData[0]);
// close all serial ports
CloseSerialPort((*FastCTDPtr)->SerialPort[0]);
printf("All serial ports closed.\n");
// free memory for FastCTDPtr
if(*FastCTDPtr){ free(*FastCTDPtr); *FastCTDPtr = NULL;}
}
void RunApp(FastCTDStructPtr FastCTDPtr, pthread_t ThList[])
{
// THREADS TASKS: HAPPEN HERE ... *************************
// Start background tasks that doo all the realtime work.
// i.e. 1) read serial port data, 2) write data, and 3)thread timeout control (three threads)
FastCTDPtr->Done = false;
// Create all threads for CTD
FastCTDCreateThreads(ThList, FastCTDPtr);
sleep(15); // acquire data for 15 secs, for now
}
void StopApp(FastCTDStructPtr *FastCTDPtr, pthread_t ThList[])
{
(*FastCTDPtr)->Done = true;
// Handle read() is locked
RealeaseSPRead(&(*FastCTDPtr)->SerialPort[0]);
// Close all threads
FastCTDJoinThreads(ThList);
}
int main(int argc, const char *argv[])
{
pthread_t threadList[NUM_THREADS];
FastCTDStructPtr FastCTDPtr = NULL;
char SerialPortListName[4][MAXNAMELEN] = {"\0"};
// INITIALIZATION *************************************
if (InitApp(&FastCTDPtr, SerialPortListName)==0)
return 1;
RunApp(FastCTDPtr, threadList);
StopApp(&FastCTDPtr, threadList);
// CLEANING ********************************* ... need to clean all threads
// ****** start for ReleaseAllMemory(...)
CloseCurrFile(&(FastCTDPtr->fpData[0]));
// close all serial ports
if(FastCTDPtr->SerialPort[0].spd)
{
//¥¥¥¥¥ start for CloseSerialPort(...)
if (tcsetattr(FastCTDPtr->SerialPort[0].spd, TCSANOW, &(FastCTDPtr->SerialPort[0]).gOriginalTTYAttrs) == -1)
{
printf("Error resetting tty attributes - %s(%d).\n",
strerror(errno), errno);
}
close(FastCTDPtr->SerialPort[0].spd);
FastCTDPtr->SerialPort[0].spd = 0;
}
//¥¥¥¥¥ CloseSerialPort((SerialPortData)FastCTDPtr->SerialPort[0]);
printf("All serial ports closed.\n");
// free memory for FastCTDPtr
if(FastCTDPtr){ free(FastCTDPtr); FastCTDPtr = NULL;}
//***** ReleaseAllMemory(&FastCTDPtr);
return 0;
// return NSApplicationMain(argc, argv);
}