00001
00013 #include "serie.h"
00014
00015 #include <QtDebug>
00016
00017 #include <string.h>
00018 #include <stdio.h>
00019
00020 static bool connection_ready = false;
00021 static QextSerialPort *port;
00022
00023
00041 bool serie_openConnection(const char *port_name) {
00042
00043 static bool port_created = false;
00044
00045 if (port_created == false) {
00046 port = new QextSerialPort();
00047 port->setQueryMode(QextSerialPort::Polling);
00048 port_created = true;
00049 }
00050
00051
00052 if (connection_ready) {
00053 serie_closeConnection();
00054 connection_ready = false;
00055 }
00056
00057
00058 port->setPortName(port_name);
00059
00060 port->setBaudRate (BAUD9600);
00061 port->setDataBits(DATA_8);
00062 port->setStopBits(STOP_1);
00063 port->setParity(PAR_NONE);
00064 port->setFlowControl(FLOW_OFF);
00065
00066 if (port->open(QextSerialPort::ReadWrite)) {
00067 connection_ready = true;
00068 qDebug("abierto!!!\n");
00069 } else {
00070 connection_ready = false;
00071 qDebug("vaya, esto CASCA\n");
00072 }
00073
00074 return(connection_ready);
00075 }
00076
00077
00087 void serie_closeConnection(void) {
00088
00089 if (connection_ready) {
00090 port->close();
00091 }
00092
00093 connection_ready = false;
00094
00095 }
00096
00097
00111 void serie_sendStr(const char *cad) {
00112
00113 if (connection_ready) {
00114 port->write(cad);
00115 } else {
00116 qDebug("Intentando transmitir sin el puerto listo");
00117 }
00118 }
00119
00120
00136 void serie_sendData(const char *dat, int len) {
00137
00138 if (connection_ready) {
00139 port->write(dat, len);
00140 } else {
00141 qDebug("Intentando transmitir sin el puerto listo");
00142 }
00143 }
00144
00145
00168 int serie_receiveData(char *destination, int max_data) {
00169
00170
00171
00172
00173 int num_bytes;
00174
00175 if (!connection_ready) {
00176 return(0);
00177 }
00178
00179 num_bytes = port->bytesAvailable();
00180
00181
00182 if (num_bytes > 0) {
00183 if (num_bytes > max_data) {
00184 num_bytes = max_data;
00185 }
00186 port->read((char *)destination, num_bytes);
00187 }
00188
00189 return(num_bytes);
00190 }
00191
00192