Sida 3 av 3
Re: Läsa pulser från serieporten (led-detektor)
Postat: 11 april 2009, 13:08:22
av bearing
Ja just det, man måste skicka in NULL som argument i time().
Kul att det verkar funka.
Re: Läsa pulser från serieporten (led-detektor)
Postat: 15 september 2011, 00:04:30
av etholan
Hej!
Då man är nybörjare.....
Har försökt följa er tråd..i mitt fall försöker jag använda en serial to USB converter...men får ett seqmentation fault när jag kör igång scriptet...vad har jag missat
Server:~/Desktop$ lsusb
Bus 004 Device 002: ID 7545:1094
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 013: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
#include <stdio.h> /* Standard input/output definitions */
#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 */
#include <time.h>
#define interval 5
/*
* 'open_port()' - Open serial port 1.
*
* Returns the file descriptor on success or -1 on error.
*/
int open_port(void)
{
int fd; /* File descriptor for the port */
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{ /* Could not open the port */
fprintf(stderr, "open_port: Unable to open /dev/ttyUSB0 - %s\n",
strerror(errno));
}
return (fd);
}
int main()
{
int mainfd=0; /* File descriptor */
char chout;
struct termios options;
time_t start,earlier, now;
long pulseCount,pulseIntegral;
double currentPower,averagePower,ackumulatedEnergy;
char theTime[25];
mainfd = open_port();
fcntl(mainfd, F_SETFL, FNDELAY); /* Configure port reading */
/* Get the current options for the port */
tcgetattr(mainfd, &options);
cfsetispeed(&options, B110); /* Set the baud rates to 9600 */
cfsetospeed(&options, B110);
/* Enable the receiver and set local mode */
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB; /* Mask the character size to 8 bits, no parity */
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8; /* Select 8 data bits */
options.c_cflag &= ~CRTSCTS; /* Disable hardware flow control */
/* Enable data to be processed as raw input */
options.c_lflag &= ~(ICANON | ECHO | ISIG);
/* Set the new options for the port */
tcsetattr(mainfd, TCSANOW, &options);
pulseCount=0;
pulseIntegral=0;
start=earlier=time(NULL);
printf("Time\t\tCurrent Power(W)\tAverage Power(W)\tAckumulated Energy(kWh)\n");
while (1)
{
read(mainfd, &chout, sizeof(chout)); /* Read character from ABU */
if (chout != 0)
{
pulseCount++;
chout=0;
now=time(0);
if ((now-earlier)>=interval)
{
pulseIntegral+=pulseCount;
currentPower=(double)pulseCount*(3600/10)/(now-earlier);
averagePower=(double)pulseIntegral*(3600/10)/(now-start);
ackumulatedEnergy=(double)pulseIntegral/10000;
strftime(theTime,sizeof(theTime),"%d-%m-%y %H:%M:%S", NULL);
printf("%s\t%f\t%f\t%f\n",
theTime,currentPower,averagePower,ackumulatedEnergy);
earlier=now;
}
}
sleep(0); /* Give other processes some time */
}
return 0; /* Close the serial port */
close(mainfd);
}
Re: Läsa pulser från serieporten (led-detektor)
Postat: 15 september 2011, 00:09:26
av blueint
C == skript?
Använd:
printf("%u: Funkar\n", __LINE__ ); fflush(stdout);
Väldigt effektivt för att få fatt på enklare fel.
Re: Läsa pulser från serieporten (led-detektor)
Postat: 15 september 2011, 07:10:50
av etholan
Hej!
C Script it is..
Tack nu rullar det...så då åter bara resten:-))
//Mvh