Anoniem
Hallo allemaal,
Ik ben bezig met het goed werkend krijgen van rs232 onder linux. Nu heb ik de volgende 2 stukken code:
int open_usart(void){
struct sigaction saio; /* definition of signal action */
/*open the uart channel ttyUSB0*/
/*O_RDWR Open for reading and writing. The result is undefined if this flag is applied to a FIFO.
O_NOCTTY If set and path identifies a terminal device, open() will not cause the terminal device to become the controlling terminal for the process.
O_NONBLOCK When opening a FIFO with O_RDONLY or O_WRONLY set: If O_NONBLOCK is set: An open() for reading only will return without delay. An open() for writing only will return an error if no process currently has the file open for reading. */
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NONBLOCK);
/*if opening of the usart fails it will return -1*/
if (fd == -1){
return -1;
}
else {
tcgetattr(fd,&oldtio);
bzero(&options, sizeof(options)); /* clear struct for new port settings */
/*flush both data received but not read and data written but not transmitted*/
tcflush(fd, TCIOFLUSH);
/* install the signal handler before making the device asynchronous */
saio.sa_handler = signal_handler_IO;
sigemptyset(&saio.sa_mask);
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO,&saio,NULL);
fcntl(fd, F_SETOWN, getpid());
fcntl(fd, F_SETFL, FASYNC);
/*Get the current port options*/
tcgetattr(fd, &options);
/*set Baudrate to 19200*/
cfsetospeed(&options, B19200); /*output baudrate*/
cfsetispeed(&options, B19200); /*input baudrate*/
/*set the new port options*/
/*control mode flags*/
options.c_cflag = (options.c_cflag & ~CSIZE) | CS8; /*mask for the number per bits and set bits per byte to 8*/
options.c_cflag |= CLOCAL; /*terminal connected localy*/
options.c_cflag |= CREAD; /*read input from terminal*/
options.c_cflag &= ~(PARENB | PARODD); /*parity*/
options.c_cflag &= ~CRTSCTS; /*disable hardware flow control*/
options.c_cflag &= ~CSTOPB; /*1 stop bit*/
/*Input options*/
options.c_iflag=IGNBRK;
options.c_iflag &= ~(IXON|IXOFF|IXANY); /*no handshake*/
/*Line options*/
options.c_oflag=0;
options.c_lflag=0;
/*Control characters*/
options.c_cc[VTIME]=0;
options.c_cc[VMIN]=49;
/*change attributes immediatly*/
tcsetattr(fd, TCSANOW, &options);
}
return (fd);
}
char *curchar;
char buffer[255];
nbytes = read(fd, curchar, buffer + sizeof(buffer) - curchar - 1);
Wanneer ik nu de compoort uitlees lees ik veel te veel uit. De microcontroller die op de com poort hangt stuur elke 100 milliseconden een waarde. Maar ik ontvang elke keer meerde van deze waarden achter elkaar.
Kan iemand mij vertellen of ik mijn poort verkeerd heb geïnitialiseerd? of gebruik ik de read functie verkeerd?
Alvast bedankt.
Ik mis her en der wat declaraties.
Zoals ik het doe:
int fd;
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<termios.h>
#include<unistd.h>
struct termios oldio;
struct termios options;
fd = open(PORT, O_RDWR | O_NONBLOCK | O_NOCTTY);// | O_NOCTTY | O_NDELAY);
if (fd == -1) {
printf("Error: Unable to open %s\n",PORT);
return 1;
}
tcgetattr(fd,&oldio);
bzero(&options,sizeof(options));
tcgetattr(fd,&options);
options.c_cflag = baud | CS8 | CLOCAL | CREAD; // no handshake, 8 databits, allow other input.
options.c_iflag = 0;
options.c_oflag = 0;
options.c_lflag = TOSTOP; // no echo , non canonical
options.c_cc[VTIME] = 0; // no timeout
options.c_cc[VMIN] = 1; // read minimal 1 char.
tcflush(fd,TCIOFLUSH);
tcsetattr(fd,TCSANOW,&options);
declaraties natuurlijk naar keuze global maken of in een functie
en de routine in een functie of main plaatsen
Anoniem
ok,
Op dit moment stuur ik het volgende:
0x04,0x04,30.0
0x05,0x04,24.2
0x06,0x03,4.8
0x07,0x04,1000
0x08,0x01,3
tussen elk bericht zit 100 milliseconde.
Wanneer ik nu dit inlees slaat mijn programma een aantal berichten over. De berichten die hij overslaat zijn random. Ook krijg ik af en toe 2 keer achter elkaar de zelfde waarde binnen. Ligt dit dan aan mijn initialisatie?
rew
four NANDS do make a NOR . Kijk ook eens in onze shop: http://www.bitwizard.nl/shop/
Ik denk dat je buffer handling niet helemaal jofel is.
Ik definieer altijd een "myread":
myread (int fd, char *buf, int n)
{
int nn;
while (n > 0) {
nn = read (fd, buf, n);
if (nn <= 0) return nn;
buf += nn;
n -= nn;
}
return iets;
}
Ik zou eerst eens met bijvoorbeeld "kermit" (in het pakket ckermit) eens met de hand kijken of wat de microcontroller stuurt of dat juist is. Dan weet je m.i. zeker dat dat ok is.
Verder zie ik je iets doen met een io handler functie. Ik heb dat nog nooit gedaan, en volgens mij heb jij dat niet nodig.
Anoniem
Ik heb nu alles even aangepast en krijg nu voortaan hetvolgende binnen:
0x07,0x04,22.2
0x08,0x04,21.1
0x04,0x04,21.1
0x05,0x04,21.1
0x06,0x04,18.8
0x07,0x04,17.7
0x08,0x04,16.6
0x04,0x04,16.6
0x05,0x04,29.9
0x06,0x04,28.8
0x07,0x04,27.7
0x08,0x04,26.6
0x04,0x04,25.5
0x05,0x04,24.4
0x06,0x04,24.4
0x07,0x04,22.2
0x08,0x04,21.1
0x04,0x04,20.0
0x05,0x04,19.9
0x06,0x04,19.9
0x07,0x04,19.9
Ik stuurde het volgende:
0x07,0x04,22.2
0x08,0x04,21.1
0x04,0x04,20.0
0x05,0x04,19.9
0x06,0x04,18.8
0x07,0x04,17.7
0x08,0x04,16.6
0x04,0x04,15.5
0x05,0x04,29.9
0x06,0x04,28.8
0x07,0x04,27.7
0x08,0x04,26.6
0x04,0x04,25.5
0x05,0x04,24.4
0x06,0x04,23.3
0x07,0x04,22.2
0x08,0x04,21.1
0x04,0x04,20.0
0x05,0x04,19.9
0x06,0x04,18.8
0x07,0x04,17.7
Het gaat dus sowieso fout op de laatste karakters. De eerste karakters ontvangt hij nu goed.
Ik heb gekeken met een terminal programma of ik alle waardes goed binnenkreeg en deze gaf aan dat alles goed was. Het gaat dus ergens in mijn software fout.
@rew: Jij zegt dat ik mijn handling niet goed heb. Wat zou hier dan niet aan kloppen volgens jou?
Lonewolf
If at first you don't succeed, call it version 1.0
Picdev e.d. ook geen optie voor linux ?
Of gaat het echt om het zelf maken van progjes onder linux ?
Werk zelf met een spinsel van Fedora ( Fel 12 ) hier zit ook vrij veel 'hobbysoftware' bij
Weet niet of je deze pagina doorgenomen hebt: Serial Programming Guide
Ben er zelf niet mee bezig, maar staat zo te zien wel veel info in.
Ben zelf ook een LINUX gebruiker, altijd leuk om te weten of je het werkend krijgt.
Anoniem
Na een aantal tests heb ik het volgende gevonden.
Wanneer ik c_cc[VMIN] te hoog maak zorgt deze er voor dat ik een aantal dingen niet door krijg.
Wanneer ik hem te laag maak krijg ik dubbele data.
Iemans enig idee wat ik hier mee aan moet?