CriusGUI::CriusGUI(QWidget *parent) : QMainWindow(parent), ui_(new Ui::CriusGUI), active_(false), connected_(false) { // Create SerialCommThread SerialCommThread* serial_thread = new SerialCommThread(); QThread* thread = new QThread; serial_thread->moveToThread(thread); connect(thread, SIGNAL(started()), serial_thread, SLOT(init())); connect(this, SIGNAL(loadFCConfig()), serial_thread, SLOT(requestConfig())); connect(this, SIGNAL(sendFCConfig(QByteArray)), serial_thread, SLOT(sendConfig(QByteArray))); connect(this, SIGNAL(sendSerialConfig(QString)), serial_thread, SLOT(connectSerial(QString))); connect(this, SIGNAL(sendSerialDisconnect()), serial_thread, SLOT(disconnectSerial())); connect(serial_thread, SIGNAL(sendData(QByteArray)), this, SLOT(getSerialData(QByteArray))); connect(serial_thread, SIGNAL(sendSerialPortInfo(QStringList)), this, SLOT(receiveSerialPortInfo(QStringList))); connect(this, SIGNAL(sendGotACK()), serial_thread, SLOT(receiveACK())); thread->start(); // Setup UI ui_->setupUi(this); // Initialize plots imu_plot_= new TimePlot(ui_->imu_plot, ui_->imu_plot->geometry().width()/2, 6); control_plot_ = new TimePlot(ui_->control_plot, ui_->control_plot->geometry().width()/2, 4); }
void SerialMonitor::disconnectedSerial(){ //Desconectado connect(ui->connectButton, SIGNAL(clicked()), this, SLOT(connectSerial())); disconnect(ui->connectButton, SIGNAL(clicked()), this, SLOT(disconnectSerial())); setSerialPort(serialPort); }
void SerialMonitor::connectedSerial(){ //Conectado disconnect(ui->connectButton, SIGNAL(clicked()), this, SLOT(connectSerial())); connect(ui->connectButton, SIGNAL(clicked()), this, SLOT(disconnectSerial())); ui->connectButton->setEnabled(true); ui->connectButton->setText(tr("Disconnect")); }
int CloudWatcherController::writeSerial(const char *buffer, int numberOfBytes) { connectSerial(); printMessage("writeSerial: writting %d bytes\n", numberOfBytes); int n = write(serialportFD, buffer, numberOfBytes); if (n < numberOfBytes) { printMessage("writeSerial: written of %d bytes failed!\n",numberOfBytes); return -1; } printMessage("writeSerial: wroten\n"); return n; }
int main (int argc, const char * argv[]) { bool connection; connection = connectSerial("/dev/tty.SmartBlueMiniGPSe644-GP"); if (connection) { printf("connected\n"); readNMEA(kReadCount); disconnectSerial(); } else { printf("not connected\n"); } return 0; }
int CloudWatcherController::readSerial(char *buffer, int numberOfBytes) { connectSerial(); int n = 0; while (n < numberOfBytes) { printMessage("readSerial: reading %d bytes\n", numberOfBytes - n); int readed = read(serialportFD, &(buffer[n]), numberOfBytes - n); if (readed <= 0) { printMessage("readSerial: read of %d bytes failed!\n", numberOfBytes); return(-1); } n += readed; } printMessage("readSerial: read %d bytes\n", n); return n; }
int main(int argc, char *argv[]) { fd_set rset; struct timeval timeout; char * IP; //IP address of socket char * PTY; //Pseudo Terminal Name unsigned char cBuff[BUFFER]; //char c[BUFFER]; int csize; int pty = -1; //Pseudo Terminal File Descriptor int sockfd1, sockfd2 = -1; int x; int args = 0; int tmp; char argSerial[] = "-serial"; char argHelp[] = "-help"; char argPort[] = "-port"; char argPTY[] = "-pty"; char argStrip[] = "-strip"; char argBaud[] = "-baud"; char argDebug[] = "-debug"; int SOCKET_PORT, BAUD, STRIP, DEBUG = 0; char SERIAL[100]; for (x = 0; x < argc; x++){//Cycle through the command line arguments if (!strcmp(argSerial,argv[x])) {//Look for the -serial option strcpy(SERIAL,argv[x+1]); //Copies the port to SERIAL if(BAUD>0){ //If the baud option has been passed sockfd2 = connectSerial(SERIAL, BAUD); //Open the serial port and return the file descriptor if (sockfd2 < 0) { close(sockfd2); if(sockfd1>=0) close(sockfd1); if(pty>=0) close(pty); return -1; }else{ args+=3; } }else{ args+=3; } } else if (!strcmp(argPort,argv[x])) { //Look for -port option SOCKET_PORT = atoi(argv[x+1]); //Convert string address into int sockfd1 = makeSocket(SOCKET_PORT); //Make the socket and return the file descriptor if (sockfd1 < 0) { close(sockfd1); if(sockfd2>=0) close(sockfd2); if(pty>=0) close(pty); return -1; }else{ args+=5; } } else if (!strcmp(argPTY,argv[x])) { //Look for -pty option pty = pseudoTY(&PTY); if(pty<0){ close(pty); if(sockfd2>=0) close(sockfd2); if(sockfd1>=0) close(sockfd1); return -1; }else{ args+=7; } } else if (!strcmp(argBaud,argv[x])) { //Look for -baud option tmp = atoi(argv[x+1]); //Convert string baud rate to int switch (tmp) { //Make sure the value is supported case 115200: BAUD = B115200; break; case 38400: BAUD = B38400; break; case 19200: BAUD = B19200; break; case 9600: BAUD = B9600; break; default: printf("ERROR!: Unknown baud rate.\n"); return -1; break; } if(strlen(SERIAL) != 0) //If we got the tag for a serial port, create the serial port sockfd2 = connectSerial(SERIAL, BAUD); //Open the serial port and return the file descriptor args+=1; } else if ( (args != 9 && args != 11 && args !=16 && x == argc-1) || (!strcmp(argHelp,argv[x]))) { //If not enough arguments, output usage directions and exit // Serial <=> Socket w/ baud = 9 // Serial <=> PTY w/ baud = 11 // Serial <=> Socket, Serial <=> PTY w/ baud = 16 printf("--------------------------------------\n"); printf("------------ SerialDaemon ------------\n"); printf("--------------------------------------\n"); printf("Usage:"); printf("\t./serialdaemon [options] [arguments]\n"); printf("\t-serial [Use to indicate which serial port to connect to. E.G. /dev/ttyS1]\n"); printf("\t-port [Use to indicate which TCP/IP port of the local host to connect to. E.G. 5000]\n"); printf("\t-pty [Create a pseudo terminal for the serial port to connect to.]\n"); printf("\t-baud [Serial port baudrate.]\n"); printf("\t\t115200\n"); printf("\t\t38400\n"); printf("\t\t19200\n"); printf("\t\t9600\n"); printf("\t-strip [Strip the endline character and replace with a space.]\n"); printf("\t-debug [Set the verbose debug mode for help.]\n"); printf("\t-help [For this help screen.]\n\n"); printf("Example Usage:\t./serialdaemon -serial /dev/ttyS1 -baud 115200 -pty -port 5000\n"); printf("This will link ttyS1 to localhost:5000 and ttyS1 to a pseudo terminal. The connection to ttyS1 will have a baudrate of 115200.\n"); return -1; } else if (!strcmp(argStrip,argv[x])){STRIP = 1;} //Look for the -strip option else if (!strcmp(argDebug,argv[x])){DEBUG = 1; printf ("DEBUG: debug mode on!\n");} //Look for the -debug option } signal(SIGINT, controlC); // catch ^C so we can close sockets & exit cleanly IP = getIP(); //Get the local IP address if(args == 9)//Serial to Socket printf("Connections made: \n\t\t\t%s < = > http://%s:%d\n",SERIAL,IP,SOCKET_PORT); else if(args == 11)//Serial to PTY printf("Connections made: \n\t\t\t%s < = > %s\n",SERIAL,PTY); else if(args == 16)//Serial to PTY & Serial to Socket printf("Connections made: \n\t\t\t%s < = > http://%s:%d\n\t\t\t%s < = > %s\n",SERIAL,IP,SOCKET_PORT,SERIAL,PTY); sd1 = waitOnSocket(sockfd1); //Check for a connection to the socket while(1){ /* Select on sockets */ if(sd1 > 0){ //If There is a connection to the socket then potentially set up these connections: tty < = > socket and tty < = > pty if (DEBUG) printf("DEBUG: New client socket opened.\n"); if (sd1 < 0) { //Error in creating connection close(sd1); return -1; } sd2 = sockfd2; //File descriptor of the serial port if (sd2 < 0) { //If an error close(sd1); close(sd2); return -1; } FD_ZERO(&rset); //Clear file descriptors in the rset set while(1) { FD_SET(sd1,&rset);//Set sd1 in rset FD_SET(sd2,&rset);//Set sd2 in rset if(pty!=-1) {//If a virtual port is requested FD_SET(pty,&rset); select(max(max(sd1,sd2),pty)+1,&rset,NULL,NULL,NULL); //Select specifies which of the file descriptors is ready for reading or writing }else { select(max(sd1,sd2)+1,&rset,NULL,NULL,NULL); //Select tests file descriptors in the range of 0 to nfds-1 or in this case 0 to max(sd1,sd2). } //----------------Check The Socket For Data ------------------ if (FD_ISSET(sd1,&rset)) { //Is there stuff to read from the socket /* There's stuff to read */ if ((csize= read(sd1, &cBuff, BUFFER)) >= 1) { //If there's something worth reading if (STRIP==1) { //Remove endline characters and replace with space for(x = 0 ; x < csize; x++) { if (cBuff[x] == '\n' ) { cBuff[x] = ' '; if (DEBUG) printf ("DEBUG: **STRIPPED**\n"); } } } if (DEBUG) { //Replace &cBuff and cBuff with c //cBuff[csize] = '\0'; printf("\nDEBUG: %s <== ",SERIAL); for(x=0; x<csize;x++){ printf("%#.2x ",cBuff[x]); } printf("\n"); } write(sd2, &cBuff, csize);//Write data from sd1 to sd2 }else{break;}// Failed -- port closed } //----------------Check The Serial Port For Data ------------------ if (FD_ISSET(sd2,&rset)) {//Is there stuff to read from the serial port if ((csize = read(sd2, &cBuff, BUFFER)) >= 1) {//If there is something worth reading from the serial port write(sd1, &cBuff, csize); //Write this data to the socket if(pty != -1){write(pty,&cBuff,csize);} //Write this data to the virtual com port if (DEBUG) { //Replace &cBuff and cBuff with c //cBuff[csize] = '\0'; printf("DEBUG: http://%s:%d <== ",IP,SOCKET_PORT); for(x=0; x<csize;x++){ printf("%#.2x ",cBuff[x]); } printf("\n"); if(pty !=-1){ printf("DEBUG: %s <== ",PTY); for(x=0; x<csize;x++){ printf("%#.2x ",cBuff[x]); } printf("\n"); } } } //else break; /* Failed */ } //----------------Check The PTY Port For Data ------------------ if (pty != -1 && FD_ISSET(pty,&rset)) {//If there is a virtual port, and data is ready, write data from if ((csize = read(pty, &cBuff, BUFFER)) >= 1) {//If there is something worth reading from the serial port write(sd2, &cBuff, csize); //Write this data to the serial port if (DEBUG) { //Replace &cBuff and cBuff with c //cBuff[csize] = '\0'; printf("\nDEBUG: %s <== ",SERIAL); for(x=0; x<csize;x++){ printf("%#.2x ",cBuff[x]); } printf("\n"); } } //else break; /* Failed */ } } printf("Restarting\n"); close(sd1);/* clean up */ sd1 = waitOnSocket(sockfd1); //Check for a connection to the socket }else if(pty != -1) {//Else, if there is a virtual port then tty <=> pty sd2 = sockfd2; //File descriptor of the serial port if (sd2 < 0) { //If an error close(sd2); close(pty); return -1; } FD_ZERO(&rset); //Clear file descriptors in the rset set while(1) { FD_SET(sd2,&rset);//Set sd2 in rset FD_SET(pty,&rset);//Set pty in rset sd1 = waitOnSocket(sockfd1); if(sd1 >= 0){break;} //Check for socket connection, if there is, break out of this loop. select(max(sd2,pty)+1,&rset,NULL,NULL,NULL); // Specifies which of the file descriptors is ready for reading or writing if (FD_ISSET(pty,&rset)) { //If there is a virtual port, and data is ready, write data from if ((csize = read(pty, &cBuff, BUFFER)) >= 1) {//If there is something worth reading from the serial port write(sd2, &cBuff, csize); //Write this data to the serial port if (DEBUG) { //Replace &cBuff and cBuff with c //cBuff[csize] = '\0'; printf("\nDEBUG: %s <== ",SERIAL); for(x=0; x<csize;x++){ printf("%#.2x ",cBuff[x]); } printf("\n"); } } //else break; // Failed } if (FD_ISSET(sd2,&rset)) {//Is there stuff to read from the serial port if ((csize = read(sd2, &cBuff, BUFFER)) >= 1) { //If there is something worth reading from the serial port write(pty, &cBuff, csize); //Write this data to the virtual com port if (DEBUG) { //Replace &cBuff and cBuff with c //cBuff[csize] = '\0'; printf("DEBUG: %s <== ",PTY); for(x=0; x<csize;x++){ printf("%#.2x ",cBuff[x]); } printf("\n"); } } //else break; /* Failed */ } } }else {//If there isn't a pty, then check the socket for a connection once a second if(DEBUG) printf("\rWaiting on the socket connection...\n"); sd1 = waitOnSocket(sockfd1); //Check for a connection to the socket sleep(1); } } }