00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <unistd.h>
00013 #include <string.h>
00014 #include <errno.h>
00015 #include <sys/types.h>
00016 #include <sys/stat.h>
00017 #include <fcntl.h>
00018 #include <termios.h>
00019 #include <stdio.h>
00020
00021 int fd = 0;
00022 char sCmd[254];
00023 char sResult[254];
00024 QString serialport = "/dev/ttyS0";
00025 QString aoid = "08";
00026 QString doid = "02";
00027 QString diid = "04";
00028
00029 int MainWindow::init(){
00030 CommunicationLog->append("Starting program up");
00031 AOid->setText(aoid);
00032 DIid->setText(diid);
00033 DOid->setText(doid);
00034 if (OpenPort() < 0) {
00035 return 0;
00036 }
00037 CommunicationLog->append("Serial port open");
00038 UpdateDigitalInput();
00039 UpdateAnalogInput();
00040 UpdateDigitalOutput();
00041 UpdateAnalogOutput();
00042 return 0;
00043 }
00044
00045 void MainWindow::destroy(){
00046 CommunicationLog->append("Closing program down");
00047 }
00048
00049 QString MainWindow::GetInput( QString command ){
00050 CommunicationLog->append(command);
00051 QString toreturn = "";
00052 QString tempstr = "";
00053 WritePort(command);
00054 usleep(100000);
00055 if (ReadPort(sResult,254) > 0) {
00056 toreturn = sResult;
00057 CommunicationLog->append(toreturn);
00058 }
00059 for(unsigned int i = 0; i<=toreturn.length() ;i++){
00060 if(toreturn[i] != 'x'){
00061 tempstr+=toreturn[i];
00062 }
00063 }
00064 toreturn = tempstr.simplifyWhiteSpace();
00065 return toreturn;
00066 }
00067
00068 void MainWindow::UpdateAnalogInput(){
00069 QString hexstr;
00070 QString outputstr;
00071 hexstr = GetInput("k");
00072 hexstr = hexstr.left( 2 );
00073 hexstr = hexstr.prepend("0x");
00074 bool ok;
00075 int voltage1 = hexstr.toInt(&ok, 16);
00076 double voltage2 = (double) voltage1 * 0.007843137;
00077 voltage2 *= 100.0;
00078 double param, fractpart, intpart;
00079 param = voltage2;
00080 fractpart = modf (param , &intpart);
00081 if((fractpart*10.0)>4){
00082 intpart++;
00083 }
00084 voltage2 = (double) intpart * 0.01;
00085 outputstr.setNum(voltage2);
00086 outputstr.append(" V");
00087 LineEditAI->setText(outputstr);
00088 }
00089
00090 void MainWindow::UpdateAnalogOutput(){
00091 bool ok;
00092 QString output = "s";
00093 output.append(aoid);
00094
00095 QString voltage = spinBoxAO->text();
00096 QString stripvoltage = voltage.left(voltage.find(" "));
00097 stripvoltage = stripvoltage.simplifyWhiteSpace();
00098 int ivoltage = stripvoltage.toInt(&ok, 10);
00099 double dvoltage = ((double)ivoltage / 7.81640625);
00100 ivoltage = (int)dvoltage;
00101 QString str = QString( "%1" ).arg( ivoltage, 0, 16 );
00102 output.append("o");
00103 if(str.length() == 1){
00104 output.append("0");
00105 }
00106 output.append(str);
00107 GetInput(output);
00108 UpdateAnalogInput();
00109 }
00110
00111 int MainWindow::OpenPort(){
00112 CommunicationLog->append(serialport.ascii());
00113 ClosePort();
00114 fd = open(serialport.ascii(), O_RDWR | O_NOCTTY | O_NDELAY);
00115 if (fd < 0) {
00116 printf("open error %d %s\n", errno, strerror(errno));
00117 } else {
00118 struct termios my_termios;
00119 tcgetattr(fd, &my_termios);
00120 tcflush(fd, TCIFLUSH);
00121 my_termios.c_cflag = B9600 | CS8 |CREAD | CLOCAL | HUPCL;
00122 cfsetospeed(&my_termios, B57600);
00123 tcsetattr(fd, TCSANOW, &my_termios);
00124 }
00125 return fd;
00126 }
00127
00128 void MainWindow::ClosePort(){
00129 if (fd > 0) {
00130 close(fd);
00131 }
00132 }
00133
00134 int MainWindow::ReadPort( char * psResponse, int iMax ){
00135 int iIn;
00136 if (fd < 1) {
00137 printf(" port is not open\n");
00138 return -1;
00139 }
00140 strncpy (psResponse, "N/A", iMax<4?iMax:4);
00141 iIn = read(fd, psResponse, iMax-1);
00142 if (iIn < 0) {
00143 if (errno == EAGAIN) {
00144 return 0;
00145 } else {
00146 printf("read error %d %s\n", errno, strerror(errno));
00147 }
00148 } else {
00149 psResponse[iIn<iMax?iIn:iMax] = '\0';
00150 }
00151 return iIn;
00152 }
00153
00154 int MainWindow::WritePort( QString psOutput ){
00155 int iOut;
00156 if (fd < 1) {
00157 printf("Port is not open\n");
00158 return -1;
00159 }
00160 iOut = write(fd, psOutput.ascii(), strlen(psOutput));
00161 if (iOut < 0) {
00162 printf("write error %d %s\n", errno, strerror(errno));
00163 } else {
00164 }
00165 return iOut;
00166 }
00167
00168 void MainWindow::UpdateDigitalInput(){
00169 QString hexstr;
00170 QString str = "s";
00171 str.append(diid);
00172 str.append("i");
00173 hexstr = GetInput(str);
00174
00175 hexstr = hexstr.left( 2 );
00176 hexstr = hexstr.prepend("0x");
00177 bool ok;
00178 int value = hexstr.toInt(&ok, 16);
00179
00180 if(value >= 128){
00181 DI8->setChecked(1);
00182 value -= 128;
00183 } else {
00184 DI8->setChecked(0);
00185 }
00186 if(value >= 64){
00187 DI7->setChecked(1);
00188 value -= 64;
00189 } else {
00190 DI7->setChecked(0);
00191 }
00192 if(value >= 32){
00193 DI6->setChecked(1);
00194 value -= 32;
00195 } else {
00196 DI6->setChecked(0);
00197 }
00198 if(value >= 16){
00199 DI5->setChecked(1);
00200 value -= 16;
00201 } else {
00202 DI5->setChecked(0);
00203 }
00204 if(value >= 8){
00205 DI4->setChecked(1);
00206 value -= 8;
00207 } else {
00208 DI4->setChecked(0);
00209 }
00210 if(value >= 4){
00211 DI3->setChecked(1);
00212 value -= 4;
00213 } else {
00214 DI3->setChecked(0);
00215 }
00216 if(value >= 2){
00217 DI2->setChecked(1);
00218 value -= 2;
00219 } else {
00220 DI2->setChecked(0);
00221 }
00222 if(value >= 1){
00223 DI1->setChecked(1);
00224 value -= 1;
00225 } else {
00226 DI1->setChecked(0);
00227 }
00228 }
00229
00230 void MainWindow::UpdateDigitalOutput(){
00231 QString str = "s";
00232 str.append(doid);
00233 str.append("o");
00234 int value = 0;
00235 if(DO8->isChecked()){
00236 value += 128;
00237 }
00238 if(DO7->isChecked()){
00239 value += 64;
00240 }
00241 if(DO6->isChecked()){
00242 value += 32;
00243 }
00244 if(DO5->isChecked()){
00245 value += 16;
00246 }
00247 if(DO4->isChecked()){
00248 value += 8;
00249 }
00250 if(DO3->isChecked()){
00251 value += 4;
00252 }
00253 if(DO2->isChecked()){
00254 value += 2;
00255 }
00256 if(DO1->isChecked()){
00257 value += 1;
00258 }
00259 QString tempstr = QString( "%1" ).arg( value, 0, 16 );
00260 if(tempstr.length() == 1){
00261 tempstr.prepend("0");
00262 }
00263 str.append(tempstr);
00264 GetInput(str);
00265 }
00266
00267 void MainWindow::changeAOid(){
00268 aoid = AOid->text();
00269 CommunicationLog->append("Analog Output id changed");
00270 CommunicationLog->append(aoid);
00271 }
00272
00273 void MainWindow::changeDIid(){
00274 diid = DIid->text();
00275 CommunicationLog->append("Digital Input id changed");
00276 CommunicationLog->append(diid);
00277 }
00278
00279 void MainWindow::changeDOid(){
00280 doid = DOid->text();
00281 CommunicationLog->append("Digital Output id changed");
00282 CommunicationLog->append(doid);
00283 }