-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql-usb1208LS.c
146 lines (114 loc) · 3.24 KB
/
mysql-usb1208LS.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
142
143
144
145
146
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/types.h>
#include <asm/types.h>
#include "pmd.h"
#include "usb-1208LS.h"
//mysql libraries
#include <my_global.h>
#include <mysql.h>
//mysql crap out function
void finish_with_error(MYSQL *con)
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
char *getPassword(char *password){
FILE *pFile;
//password is stored in a file called "password"
pFile=fopen("password","r");
fscanf(pFile,"%s",password);
fclose(pFile);
return password;
}
int main (int argc, char **argv)
{
//database password
char password[16];
//build MySQL connection
MYSQL *con = mysql_init(NULL);
if (con == NULL){
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}
if (mysql_real_connect(con, "localhost", "uva", getPassword(password),"slowcontrols", 0, NULL, 0) == NULL){
finish_with_error(con);
}
//everything between the asterix lines is crap you don't understand
//***************************************************
int flag;
signed short svalue;
__u8 gain;
HIDInterface* hid = 0x0;
hid_return ret;
int interface;
// Debug information. Delete when not needed
// hid_set_debug(HID_DEBUG_ALL);
// hid_set_debug_stream(stderr);
// hid_set_usb_debug(2);
ret = hid_init();
if (ret != HID_RET_SUCCESS) {
fprintf(stderr, "hid_init failed with return code %d\n", ret);
return -1;
}
if ((interface = PMD_Find_Interface(&hid, 0, USB1208LS_PID)) < 0) {
fprintf(stderr, "USB 1208LS not found.\n");
exit(1);
}else{
fprintf(stdout, "Acquiring data. Press 'x' then enter to stop.\n");
}
/* config mask 0x01 means all inputs */
usbDConfigPort_USB1208LS(hid, DIO_PORTB, DIO_DIR_IN);
usbDConfigPort_USB1208LS(hid, DIO_PORTA, DIO_DIR_OUT);
usbDOut_USB1208LS(hid, DIO_PORTA, 0x0);
usbDOut_USB1208LS(hid, DIO_PORTA, 0x0);
//***************************************************
//begin program here
gain = BP_20_00V;//set gain to 20 V
//these two lines make getchar() in the while loop non-blocking
flag = fcntl(fileno(stdin), F_GETFL);
fcntl(0, F_SETFL, flag | O_NONBLOCK);
//Voltage to flow rate requires fitting voltages to a line, y=mx+b. Values for m and b are different for each of the 4 cases:
//0: separator
//1: shield
//2: helium-3
//3: evaporator
//
//Each value of m and b was found empirically with a flow meter and gas bottle
float m[4]={
11.4418,//separator
11.2594,//shield
10.1275,//helium-3
60.7119 //evap
};
float b[4]={
0.1215, //separator
-0.0256, //shield
0.0219, //helium-3
0.1746 //evaporator
};
float calculated_flow=0.0;
int i;
char buffer[1000];
do {
for(i=0;i<4;++i){
svalue = usbAIn_USB1208LS(hid, i, gain);
//calculate measured value in SLPM
//y=mx+b
calculated_flow=m[i]*volts_LS(gain, svalue)+b[i];
//make and execute the db entry
sprintf(buffer, "INSERT INTO usb1208ls (device, raw_reading, measurement_reading) VALUES('d%d',%1f,%1f)",i,volts_LS(gain, svalue),calculated_flow);
if (mysql_query(con,buffer)) {
finish_with_error(con);
}
}
sleep(1);
//usleep(100000);
} while (!isalpha(getchar()));
return 0;
}