Sto cercando di instaurare una comunicazione seriale con una scheda esterna, la comunicazione si basa sull'invio di byte. ad esempio per accendere una luce devo inviare il byte 0xFD.
non sono sicuro di impostare bene la scheda o cmq i parametri.
codice:
giovanni@giovanni-desktop:~/Desktop/Progetto$ setserial -a /dev/ttyS0
/dev/ttyS0, Line 0, UART: 16550A, Port: 0x03f8, IRQ: 4
Baud_base: 115200, close_delay: 50, divisor: 0
closing_wait: 3000
Flags: spd_normal skip_test
Ho questo codice, ma la scheda non da segni di vita. Grazie in anticipo per le risposte e gli aiuti.
codice:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
#include <errno.h>
static int fd = 0;
int OpenAdrPort(char* sPortNumber);
void CloseAdrPort();
int main(int argc, char *argv[])
{
OpenAdrPort(argv[1]);
unsigned char str = 0xfd;
printf("%x\n", str);
write(fd,&str,1); // Lo spedisco sulla seriale
CloseAdrPort(fd); // Chiudo il canale seriale
return 0;
}
// Apre la porta seriale
// return code:
// > 0 = fd for the port
// -1 = open failed
int OpenAdrPort(char* sPortNumber)
{
char sPortName[64];
printf("in OpenAdrPort port#=%s\n", sPortNumber);
sprintf(sPortName, "/dev/ttyS%s", sPortNumber);
printf("sPortName=%s\n", sPortName);
//controllo che la porta sia chiusa
CloseAdrPort(fd);
fd = open(sPortName, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd < 0)
{
printf("open error %d %s\n", errno, strerror(errno));
}
else
{
struct termios my_termios;
printf("fd is %d\n", fd);
tcgetattr(fd, &my_termios);
// NOTE: salva i vecchi attributi, in modo da poterli
// ripristinare in caso in seguito
printf("old cflag=%08x\n", my_termios.c_cflag);
printf("old oflag=%08x\n", my_termios.c_oflag);
printf("old iflag=%08x\n", my_termios.c_iflag);
printf("old lflag=%08x\n", my_termios.c_lflag);
printf("old line=%02x\n", my_termios.c_line);
tcflush(fd, TCIFLUSH);
my_termios.c_cflag = B9600 | CS8 |CREAD | CLOCAL | HUPCL;
cfsetospeed(&my_termios, B9600);
tcsetattr(fd, TCSANOW, &my_termios);
printf("new cflag=%08x\n", my_termios.c_cflag);
printf("new oflag=%08x\n", my_termios.c_oflag);
printf("new iflag=%08x\n", my_termios.c_iflag);
printf("new lflag=%08x\n", my_termios.c_lflag);
printf("new line=%02x\n", my_termios.c_line);
} // end if
return fd;
} // end OpenAdrPort
void CloseAdrPort()
{
if (fd > 0)
{
close(fd);
} // end if
} // end CloseAdrPort