Error: write send data to comm port

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2009
Posts: 1
Reputation: msofian3 is an unknown quantity at this point 
Solved Threads: 0
msofian3 msofian3 is offline Offline
Newbie Poster

Error: write send data to comm port

 
0
  #1
Nov 10th, 2009
#include <stdio.h> // standard input / output functions
#include <string.h> // string function definitions
#include <unistd.h> // UNIX standard function definitions
#include <fcntl.h> // File control definitions
#include <errno.h> // Error number definitions
#include <termios.h> // POSIX terminal control definitionss
#include <time.h> // time calls
#include <conio.h>
#include <iomanip.h>
#include <sys/time.h> //for timeout
#include <sys/select.h>
#include <fstream.h>




int open_port(void)
{
int fd; // file description for the serial port
fd = open("/dev/ser2", O_RDWR | O_APPEND | O_SYNC );


if(fd == -1) // if open is unsucessful
{
printf("\nopen_port: Unable to open /dev/ttyS0. \n");
}
else
{
fcntl(fd, F_SETFL, 0);
printf("port is open.\n");
}
return(fd);
}


int configure_port(int fd) // configure the port
{
struct termios port_settings; // structure to store the port settings in


port_settings.c_cflag = ( CS8 | CREAD | CLOCAL );
port_settings.c_cflag &= ~PARENB; // set no parity, stop bits, data bits
port_settings.c_cflag &= ~CSTOPB;
port_settings.c_cflag &= ~CSIZE;
port_settings.c_cflag |= CS8;

cfsetispeed(&port_settings, B9600); // set baud rates
cfsetospeed(&port_settings, B9600);


tcsetattr(fd, TCSANOW, &port_settings); // apply the settings to the port
return(fd);


} //configure_port

int query_modem(int fd) // query modem with an AT command
{

int m, n;
fd_set rdfs;
struct timeval timeout;

// initialise the timeout structure
timeout.tv_sec = 5; // ten second timeout
timeout.tv_usec = 0;

/

// const char* test="1234";


int success=0;

success=write(fd, test, strlen(test)); //Send data


//n = select(fd + 1, &rdfs, NULL, NULL, &timeout);
n = select(1, &rdfs, NULL, NULL, &timeout);


printf("n before if ------------------> = %d\n", n); //0

if( n==-1)
{
perror("select failed\n");

}

else if (n)
{
printf("\nBytes detected on the port!\n");

}

else
printf("n else------------------> = %d\n", n); //0

printf("\nNo data!\n");
return 0;
}



int main(void)
{
int fd = open_port();
cout<<"\n*********** test ******************"<<endl;
printf("\n________________________________________________________\n");
printf("\n Integrated TVM with Printer (5)\n");
printf("\________________________________________________________\n");
configure_port(fd);
query_modem(fd);
printf("\____________________dah ok______________________\n");
return(0);

} //main
i try to send the data to printer using comm port. I can open the port, but the printer still no response. I'm confuse why the system display No data (refer printf("\nNo data!\n"). Pls help me..
Last edited by msofian3; Nov 10th, 2009 at 4:53 am.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C++ Forum


Views: 192 | Replies: 0
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC