beatboxer05
Gebruiker
- Lid geworden
- 9 nov 2009
- Berichten
- 5
Hallo,
Ik heb een programma gemaakt die een antwoord inleest dat via de serieele port binnen komt als er op een schakelaarknop wordt gedrukt. Hieronder is het script te zien. Ik heb alleen het probleem dat als ik op de knop druk, het antwoord 4 keer wordt weergeven terwijl dit maar 1 keer moet gebeuren. Ik denk dat ik dit op moet lossen door de filediscriptor leeg te maken. Alleen dat krijg ik niet werkend, dus ik vraag me af of hier iemand het antwoord op weet. Alvast bedankt
script:
[cpp]
//Headers//
#include <fcntl.h>
#include <cstdio>
#include <vector>
#include <sstream>
#include <istream>
#include <iostream>
#include <stdio.h> /* Standard input/output definitions */
#include <stdlib.h>
#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 definitions */
using namespace std;
int uitlezen();
unsigned char antwoordschakelaar[22];
int fd;
int uitlezen()
{
usleep(50000);
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY );
struct termios portterm;
tcflush(fd, TCIOFLUSH);
portterm.c_cflag = CS8 | CREAD| CLOCAL;
portterm.c_cc[VMIN] = 1;
portterm.c_cc[VTIME] = 0;
cfsetispeed(&portterm, B115200);
cfsetospeed(&portterm, B115200);
portterm.c_lflag = 0;
portterm.c_iflag = IGNBRK;
portterm.c_oflag = 0;
read(fd,antwoordschakelaar,22);
usleep(100000);
for(int k = 0; k< 22; k++)
{
cout<<hex<<(int)antwoordschakelaar[k]<<" ";
}
if(antwoordschakelaar[0]==0x01)
{
cout<<"test"<<"\n";
}
cout<<"\n";
return(0);
}
int main()
{
while(true)
{
uitlezen();
}
}
[/cpp]
Ik heb een programma gemaakt die een antwoord inleest dat via de serieele port binnen komt als er op een schakelaarknop wordt gedrukt. Hieronder is het script te zien. Ik heb alleen het probleem dat als ik op de knop druk, het antwoord 4 keer wordt weergeven terwijl dit maar 1 keer moet gebeuren. Ik denk dat ik dit op moet lossen door de filediscriptor leeg te maken. Alleen dat krijg ik niet werkend, dus ik vraag me af of hier iemand het antwoord op weet. Alvast bedankt
script:
[cpp]
//Headers//
#include <fcntl.h>
#include <cstdio>
#include <vector>
#include <sstream>
#include <istream>
#include <iostream>
#include <stdio.h> /* Standard input/output definitions */
#include <stdlib.h>
#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 definitions */
using namespace std;
int uitlezen();
unsigned char antwoordschakelaar[22];
int fd;
int uitlezen()
{
usleep(50000);
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY );
struct termios portterm;
tcflush(fd, TCIOFLUSH);
portterm.c_cflag = CS8 | CREAD| CLOCAL;
portterm.c_cc[VMIN] = 1;
portterm.c_cc[VTIME] = 0;
cfsetispeed(&portterm, B115200);
cfsetospeed(&portterm, B115200);
portterm.c_lflag = 0;
portterm.c_iflag = IGNBRK;
portterm.c_oflag = 0;
read(fd,antwoordschakelaar,22);
usleep(100000);
for(int k = 0; k< 22; k++)
{
cout<<hex<<(int)antwoordschakelaar[k]<<" ";
}
if(antwoordschakelaar[0]==0x01)
{
cout<<"test"<<"\n";
}
cout<<"\n";
return(0);
}
int main()
{
while(true)
{
uitlezen();
}
}
[/cpp]
Laatst bewerkt door een moderator: