CRS232 i krzaki w terminalu - połączenie Debian, Windows
: 17 stycznia 2009, 11:57
Witam
System to Debian podłączony kablem null-modem z Windowsem. W terminalu Windowsa ustawione 19200 8-N-1. Po wysłaniu jakiegoś tekstu za pomocą tego kodu w terminalu dostaje same krzaki. Kompiluje poleceniem: gcc - o serial serial.c
W internecie znalazłem taki kod:
Macie jakieś propozycje co jest źle?
System to Debian podłączony kablem null-modem z Windowsem. W terminalu Windowsa ustawione 19200 8-N-1. Po wysłaniu jakiegoś tekstu za pomocą tego kodu w terminalu dostaje same krzaki. Kompiluje poleceniem: gcc - o serial serial.c
W internecie znalazłem taki kod:
Kod: Zaznacz cały
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <string.h>
#include <termios.h>
#include <fcntl.h>
static int serial_fd;
void serial_set_dtr(int s) // pin 4
{
unsigned int v;
if (serial_fd<0) return;
ioctl(serial_fd,TIOCMGET,&v);
if (s) v|=TIOCM_DTR; else v&=~TIOCM_DTR;
ioctl(serial_fd,TIOCMSET,&v);
}
int serial_open(char *dev_name, int speed)
{
struct termios t;
int fd;
int spd;
switch(speed)
{
case 115200: spd=B115200; break;
case 57600: spd=B57600; break;
case 38400: spd=B38400; break;
case 19200: spd=B19200; break;
case 9600: spd=B9600; break;
default: return -2;
}
fd = open (dev_name, O_RDWR);
if(fd<0) return -1;
tcgetattr (fd, &t);
t.c_iflag = IGNBRK | IGNPAR;
t.c_oflag = t.c_lflag = t.c_line = 0;
t.c_cflag = CS8 | CREAD | CLOCAL | HUPCL | spd;
tcsetattr (fd, TCSAFLUSH, &t);
serial_fd = fd;
return 0;
}
void serial_close()
{
close(serial_fd);
}
int serial_write(char *data, int len)
{
return write(serial_fd, data, len);
}
int serial_read(char *data, int len)
{
int nbytes=0;
while(len)
{
if(read(serial_fd, data, 1)==1) { len--;data++; nbytes++; }
}
return nbytes;
};
void serial_write_byte(unsigned char b)
{
write (serial_fd,&b,1);
}
unsigned char serial_read_byte()
{
unsigned char b;
serial_read(&b,1);
// fprintf(stderr,"%02x ", b);
return b;
}
int serial_data_avail()
{
fd_set set;
struct timeval tv;
FD_ZERO(&set);
FD_SET(serial_fd,&set);
tv.tv_sec = 0;
tv.tv_usec = 0;
return select(serial_fd+1, &set, NULL, NULL, &tv)>0;
}
unsigned int sys_get_clock_usec()
{
struct timezone tz={0,0};
struct timeval tv;
gettimeofday(&tv,&tz);
return tv.tv_usec + tv.tv_sec * 1000000;
}
void sys_delay(int msecs)
{
usleep(msecs*1000);
}
int main()
{
serial_open("/dev/ttyS1", 19200);
serial_write("tekst", 5);
serial_close();
return 0;
}