A question about programming RS-232

2006-09-16 Thread Сергей Собко
Andrew Falanga wrote:
> I am by no means the worlds best serial programmer, but recently I have
> done some work on this subject and I noticed one thing in the code sample
> above that should be avoided.  However, I'll give you what I saw in-line:
>
>
> #include 
>
> > #include 
> > #include 
> > #include 
> >
> > 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);
>
> Ok, great, we've opened our serial device.  Unless you need this to be a
> controlling terminal, you should open with open( "/dev/cuad0", O_RDWR |
> O_NONBLOCK | O_NOCTTY );  Check with the open man page to make sure I've
> given you the correct constant for opening as a non-controlling terminal.
>
>
> 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;
>
> You've set the attributes you want to use in the structure you defined,
> my_termios.  However, you should call tcgetattr() before changing what you
> want to change (and make sure you always turn things on as you have done
> above with bitwise or).  So, your code should look something like,
>
> // assume an open file descriptor named fd
> struct termios my_termios;
>
> if( tcgetattr( fd, &my_termios ) < 0 ) {
>fprintf( stderr, "error in getting termios properties\n" );
>return AN_ERROR;
> }
>
> // turn on what you want
> my_termios.c_cflag = CS8 | CLOCAL;
>
> if( tcsetattr( fd, &my_termios ) < 0 } {
>fprintf( stderr, "error in setting new properties to serial port\n" );
>return AN_ERROR;
> }
>
>
> I don't know if this will solve your problems but I do know I read that you
> should always get the current settings because the serial driver may use
> certain bits and you don't want to turn them off.  Also, if you're going to
> return the port settings to the state before you took hold of it, make two
> termios structures and stuff the original settings away to be restored upon
> exit or close of the port.
>
> Lastly, here is a link to a serial programming guide that I found quite
> helpful.  The info is probably dated to some degree, but it is non the less
> useful.
>
> http://www.easysw.com/~mike/serial/serial.html
>
> Andy

I have corrected, what you say, but it doesn't work at all!
The code I tryed:

#include 
#include 
#include 
#include 

int main(void) {
 int t = 0, num = 10, fd, i, iOut; char *ch;
 struct termios my_termios;
 ch = (char *)malloc(6);
 memset(ch, 50, 6);
 fd = open("/dev/cuad0", O_RDWR | O_NOCTTY | O_NDELAY);
 fcntl(fd, F_SETFL, 0);
 printf("Opened com port\n");
 if(fd < 0) return 0;
 if(tcgetattr(fd, &my_termios) < 0) return 0;
 printf("Got my_termios struct\n");
 if(cfsetspeed(&my_termios, B9600) < 0) return 0;
 printf("Set speed B9600\n");
 my_termios.c_cflag &= ~PARENB;
 my_termios.c_cflag &= ~CSTOPB;
 my_termios.c_cflag &= ~CSIZE;
 my_termios.c_cflag |= CS8;
 my_termios.c_cflag |= CLOCAL;
 my_termios.c_cflag |= CREAD;
 my_termios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
 my_termios.c_oflag &= ~OPOST;
 if(tcsetattr(fd, TCSANOW, &my_termios) < 0) return 0;
 printf("Setting my_termios attr\n");
 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;
}

After executing it writes:

Opened com port
Got my_termios struct
Set speed B9600
Setting my_termios attr
Number of bytes = 6
Writed 22!
Closed!

But! No effect ;(
I'm in shock! How can it work under Windows if it doesn't work in UNIX? May be 
I'm a lamer or I'm doing stupid things? But such code in Delphi using 
component TComPort works perfectly:



library cportio;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  SysUtils,
  Classes,
  CPort,
  Forms,
  Windows,
  IniFiles;

var Com: TComPort;
const crlf: String = #10 + #13;
const Name: String = 'Com';
const xPort: String = 'COM1';
const xDataBits: TDataBits = dbEight;
const xStopBits: TStopBits = sbOneStopBit;
const xParityBits: TParityBits = prNone;
const xFlowControl: TFlowControl = fcNone;
const xBaudRate: TBaudRate = br9600;

{$R *.res}

function WriteComPort(xData: Integer): Integer; stdcall;
begin
Com := TComPort.Create(Application);
Com.Port := xPort;
Co

Re: A question about programming RS-232

2006-09-06 Thread Andrew Falanga

I am by no means the worlds best serial programmer, but recently I have done
some work on this subject and I noticed one thing in the code sample above
that should be avoided.  However, I'll give you what I saw in-line:


#include 

#include 
#include 
#include 

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);



Ok, great, we've opened our serial device.  Unless you need this to be a
controlling terminal, you should open with open( "/dev/cuad0", O_RDWR |
O_NONBLOCK | O_NOCTTY );  Check with the open man page to make sure I've
given you the correct constant for opening as a non-controlling terminal.


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;



You've set the attributes you want to use in the structure you defined,
my_termios.  However, you should call tcgetattr() before changing what you
want to change (and make sure you always turn things on as you have done
above with bitwise or).  So, your code should look something like,

// assume an open file descriptor named fd
struct termios my_termios;

if( tcgetattr( fd, &my_termios ) < 0 ) {
  fprintf( stderr, "error in getting termios properties\n" );
  return AN_ERROR;
}

// turn on what you want
my_termios.c_cflag = CS8 | CLOCAL;

if( tcsetattr( fd, &my_termios ) < 0 } {
  fprintf( stderr, "error in setting new properties to serial port\n" );
  return AN_ERROR;
}


I don't know if this will solve your problems but I do know I read that you
should always get the current settings because the serial driver may use
certain bits and you don't want to turn them off.  Also, if you're going to
return the port settings to the state before you took hold of it, make two
termios structures and stuff the original settings away to be restored upon
exit or close of the port.

Lastly, here is a link to a serial programming guide that I found quite
helpful.  The info is probably dated to some degree, but it is non the less
useful.

http://www.easysw.com/~mike/serial/serial.html

Andy
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: A question about programming RS-232

2006-09-04 Thread Сергей Собко
On Monday 04 September 2006 02:39, you wrote:
> --- stan <[EMAIL PROTECTED]> 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@freebsd.org mailing list
>
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
>
> > To unsubscribe, send any mail to
> > "[EMAIL PROTECTED]"
>
> 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 
#include 
#include 
#include 

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 ;)
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Fwd: Re: A question about programming RS-232

2006-09-03 Thread backyard


--- stan <[EMAIL PROTECTED]> wrote:

> Date: Sun, 3 Sep 2006 16:49:51 -0400
> From: stan <[EMAIL PROTECTED]>
> To: backyard <[EMAIL PROTECTED]>
> Subject: Re: A question about programming RS-232
> 
> On Sun, Sep 03, 2006 at 01:39:00PM -0700, backyard
> wrote:
> > 
> > 
> > --- stan <[EMAIL PROTECTED]> 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@freebsd.org mailing list
> > >
> >
>
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> > > To unsubscribe, send any mail to
> > > "[EMAIL PROTECTED]"
> > > 
> > 
> > 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.
> 
> I'm not the orignal poster on this.
> 
> -- 
> Unix is very simple, but it takes a genius to
> understand the simplicity.
> (Dennis Ritchie)
> 
oops must have clicked a little too quick on this one

-brian
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: A question about programming RS-232

2006-09-03 Thread backyard


--- stan <[EMAIL PROTECTED]> 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@freebsd.org mailing list
>
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to
> "[EMAIL PROTECTED]"
> 

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
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: A question about programming RS-232

2006-09-03 Thread stan
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@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


A question about programming RS-232

2006-09-03 Thread Сергей Собко
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. :(

Code:

#include 
#include 
#include 
#include 

int main(void) {
 int t = 0, num = 10, fd, i, iOut; char *ch;
 struct termios my_termios;
 ch = (char *)malloc(6);
 memset(ch, 250, 6);
 fd = open("/dev/ttyd0", 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;
}

P.S. Please, help me. 
P.P.S. Sorry for such stupid and annoying questions
P.P.P.S. Sorry for my bad English as I'm from Russia and I'm only 16 ;)
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"