Blagodarq vi za linkovete. Programata se poluchi mnogo oprostena.
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
void sendCommand(int fd, char *command, char *result, int size)
{
int i = 0;
memset(result, 0, size);
write(fd, command, strlen(command));
char p;
char c;
do
{
p = c;
c = '\0';
int res = read(fd, &c, 1);
if(res > 0)
{
result[i] = c;
i++;
}
}while(p != 'K');
}
int main()
{
long BAUD = B4800; // derived baud rate from command line
long DATABITS = CS8;
long STOPBITS = 0;
long PARITYON = 00;
long PARITY = 00;
struct termios newtio;
int fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NONBLOCK);
if(fd < 0)printf("Error!!!\n");
fcntl(fd, F_SETOWN, getpid());
newtio.c_cflag = BAUD | CRTSCTS | DATABITS | STOPBITS | PARITYON | PARITY | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
newtio.c_lflag = 0; //ICANON;
newtio.c_cc[VMIN]=1;
newtio.c_cc[VTIME]=1;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
char result[128];
int i;
for(i=0; i<20; i++)
{
sendCommand(fd, "AON\r", result, sizeof(result));
printf("RESULT: %s\n", result);
sendCommand(fd, "BON\r", result, sizeof(result));
printf("RESULT: %s\n", result);
sendCommand(fd, "CON\r", result, sizeof(result));
printf("RESULT: %s\n", result);
sendCommand(fd, "DON\r", result, sizeof(result));
printf("RESULT: %s\n", result);
sendCommand(fd, "EON\r", result, sizeof(result));
printf("RESULT: %s\n", result);
sendCommand(fd, "FON\r", result, sizeof(result));
printf("RESULT: %s\n", result);
sendCommand(fd, "GON\r", result, sizeof(result));
printf("RESULT: %s\n", result);
sendCommand(fd, "STATE\r", result, sizeof(result));
printf("RESULT: %s\n", result);
sendCommand(fd, "AOFF\r", result, sizeof(result));
printf("RESULT: %s\n", result);
sendCommand(fd, "BOFF\r", result, sizeof(result));
printf("RESULT: %s\n", result);
sendCommand(fd, "COFF\r", result, sizeof(result));
printf("RESULT: %s\n", result);
sendCommand(fd, "DOFF\r", result, sizeof(result));
printf("RESULT: %s\n", result);
sendCommand(fd, "EOFF\r", result, sizeof(result));
printf("RESULT: %s\n", result);
sendCommand(fd, "FOFF\r", result, sizeof(result));
printf("RESULT: %s\n", result);
sendCommand(fd, "GOFF\r", result, sizeof(result));
printf("RESULT: %s\n", result);
sendCommand(fd, "STATE\r", result, sizeof(result));
printf("RESULT: %s\n", result);
sendCommand(fd, "AIN\r", result, sizeof(result));
printf("RESULT: %s\n", result);
sendCommand(fd, "DIN\r", result, sizeof(result));
printf("RESULT: %s\n", result);
sendCommand(fd, "ERROR\r", result, sizeof(result));
printf("RESULT: %s\n", result);
}
return 0;
}
Publicuvam coda ako nqkoi drug se sblyska s takyv problem nadqvam se da mu pomogne.