944,029 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 533
  • C++ RSS
Nov 10th, 2009
0

Error: write send data to comm port

Expand Post »
Quote ...
#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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
msofian3 is offline Offline
1 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: how randomise function works
Next Thread in C++ Forum Timeline: How to improve This 'card program' ??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC