#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
int open_port(void)
{
int fd; /* File descriptor for the port */
// fd = ((open("/dev/ttyO1", O_RDWR | O_NOCTTY )) < 0);
fd = open("/dev/ttyO1", O_RDWR | O_NOCTTY );
printf("text openport\n");
if (fd == -1)
{
/* Could not open the port. */
perror("open_port: Unable to open /dev/ttyO1");
}
else
{
fcntl(fd, F_SETFL, 0);
printf("Port1 has been succesfully opened.\n");
}
return(fd);
} //open_port
int configure_port(int fd) // configure the port
{
struct termios port_settings; // structure to store the port settings in
cfsetispeed(&port_settings, B9600); // set baud rates
cfsetospeed(&port_settings, B9600);
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;
tcsetattr(fd, TCSANOW, &port_settings); // apply the settings to the port
printf("text configport\n");
return(fd);
}
int query_modem(int fd) // query modem with an AT command
{
int m,n,d,e,f,x;
char buff[100];
unsigned char rx_buffer[1];
unsigned char test[5];
unsigned char data1[10];
n=read(fd,buff,100);
//printf("Result %i\n",n);
printf("Buf = %s\n\n\n", buff);
sleep(1);
memset(buff, 0, 100);
printf("AT+CMGF=1\r\n");
d = write(fd, "AT+CMGF=1\r\n", 11);
if (d < 0)
fputs("write() of 4 bytes failed!\n", stderr);
else
printf("write succeed n0 %i\n",d);
sleep(2);
d=read(fd, buff, 100);
printf("Buf = %s\n\n\n", buff);
memset(buff, 0, 100);
/*
printf("AT+CMGS=\"9964129460\"\r\n"); // \r and \n CR n LF
e = write(fd, "AT+CMGS=\"9964129460\"\rhello\n\x1A", 30);
if (e < 0)
fputs("write() of 4 bytes failed!\n", stderr);
else
printf("write succeed n0 %i\n",e);
sleep(8);
e=read(fd, buff, 100);
printf("Buf = %s\n\n\n", buff);
*/
/*
printf("AT+CMGD=1\r\n"); // \r and \n CR n LF
e = write(fd, "AT+CMGD=1\r\n", 11);
if (e < 0)
fputs("write() of 4 bytes failed!\n", stderr);
else
printf("write succeed n0 %i\n",e);
sleep(8);
e=read(fd, buff, 100);
printf("Buf = %s\n\n\n", buff);
*/
//sleep(15);
printf("AT+CMGR=1\r\n"); // \r and \n CR n LF
f = write(fd, "AT+CMGR=1\r\n", 11);
while(1)
{
sleep(1);
for(int i=0;i<=3;i++)
{
read(fd, rx_buffer, (1)); //Filestream, buffer to store in, number of bytes to read (max)
test[i]=rx_buffer[0];
printf("%s\n", rx_buffer);
}
printf("end for data\n");
printf("%s\n", test);
}
/*
for ( x=0;x < 30;x++){
data[x]='\0';
}
x=0;
printf("hello\n");
for(x=0;x<30;x++)
{
data[x]=read(fd, rx_buffer, (30));
//x++;
if(data[x-1]==0x0D&&data[x-2]=='"'){
x=0;
}
}while(!(data[x-1]=='K'&&data[x-2]=='O'));
data[x-3]='\0'; //finish the string before the OK
*/
/*
if (f < 0)
fputs("write() of 4 bytes failed!\n", stderr);
else
printf("write succeed n0 %i\n",f);
sleep(8);
f=read(fd, buff, 100);
printf("Buf = %s\n\n\n", buff);
*/
return (fd);
}
int close_port(int fd)
{
close(fd);
printf("text close port\n");
}
int main(void) //main function
{
int fd = open_port();
sleep(5);
configure_port(fd);
query_modem(fd);
close_port(fd);
return(0);
} //main
No comments:
Post a Comment