A question about programming RS-232

Сергей Собко bug2bug at bug2bug.tk
Mon Sep 4 09:31:30 PDT 2006


On Monday 04 September 2006 02:39, you wrote:
> --- stan <stanb at panix.com> wrote:
> > On Sun, Sep 03, 2006 at 11:26:04PM +0600, ??????
> >
> > ????? wrote:
> > > Hello.
> > > I have a question I can't deal myself.
> > > And nobody can help me in resolving my problem.
> > >
> > > Problem:
> > > I have a hand-made device, I want to control from
> >
> > FreeBSD 6.1
> >
> > > (I am porting this application from Windows
> >
> > equivalent).
> >
> > > But I don't know, in what device /dev/ I should
> >
> > write to get reults.
> >
> > > I tryed to write bytes into /dev/ttyd0,
> >
> > /dev/cuad0, but got nothing. :(
> >
> > Start off by using minicom (or cu) to talk to the
> > device. By doing
> > this you can sort through baud rate/parity,hardware
> > issues.
> >
> > Once you have that working, then move on to code.
> >
> > --
> > Unix is very simple, but it takes a genius to
> > understand the simplicity.
> > (Dennis Ritchie)
> > _______________________________________________
> > freebsd-questions at freebsd.org mailing list
>
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
>
> > To unsubscribe, send any mail to
> > "freebsd-questions-unsubscribe at freebsd.org"
>
> does your handmade device use RS-232? If its PIC or
> some such microcontroller based they claim to be
> RS-232 compliant but they do not always use +12V and
> -12V levels. MAX-232 chips can correct this. I assume
> if it worked in windows for you this might not the
> case, but you never know.
>
>
> -brian

Yes, you are partialy right, I'm using an old Atmel AT89C4051 microcontroller, 
I'm trying to control from through RS-232. The levels are correct as I know. 
But I don't know if the code is correct :(
I want to send 6 bytes through RS-232 with the following characteristics:

Port: COM1
Data Bits: 8
Stop Bits: 1
Parity: None
Flow Control: None

Please, say me if this code is incorrect:

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>

int main(void) {
 int t = 0, num = 10, fd, iOut; char *ch;
 struct termios my_termios;
 ch = (char *)malloc(6);
 memset(ch, 250, 6);
 fd = open("/dev/cuad0", O_RDWR | O_NONBLOCK);
 printf("Opened com port\n");
 if(fd < 0) return 0;
// tcflush(fd, TCIFLUSH);
 my_termios.c_cflag = CS8 | CLOCAL;
 if(cfsetspeed(&my_termios, B9600) < 0) return 0;
 if(tcsetattr(fd, TCSANOW, &my_termios) < 0) return 0;
 iOut = write(fd, ch, 6);
 if(iOut < 0) return 0;
 printf("Number of bytes = %d\n", iOut);
 printf("Writed %s!\n", ch);
 close(fd);
 printf("Closed!\n");
 return 0;
}

Thank you for any help.
With best regards, Sergei Sobko

P.S. Sorry for my bad English as I'm only 16 and I'm from Russia ;)


More information about the freebsd-questions mailing list