serial port communication (linux)

C

code

Hi folks,

I would like to read and write to a ISO-Reader card (RFID) connected to
/dev/ttyS0 (COM1). Writing works fine, all commands I write to the
device will be executed.

To every command there is a response. For example:

#001 LED 1 1 CR (command led1=1)

the response should be:
001 01h 00h CR

but there's only gibberish in the read buffer. :(

there's some source code:

printf("Trying to open COM1 ... ");
fp = open(DEVICE, O_RDWR | O_NOCTTY);
if(fp == -1) {
perror("failed.\n");
return IOCTL_FAIL;
} else {
printf("success.\n");
return fp;
}
send_data(fp,"#001LED10");
read(fp,rxbuf,1024);
printf("%s",rxbuf);

any suggestions?

Btw. There's something about FIFO Buffering in the documentation ...
Anybody know what this is and how I turn it off?

thanks a lot
 
D

dandelion

Hi folks,

send_data(fp,"#001LED10");
read(fp,rxbuf,1024);

<snip>

If that 'read' call is non-blocking, your serial device just did not get
time to put anything in any buffer. This would explain the garbage, but the
read-call seems to be blocking. (unless you use O_NONBLOCK somewhere outside
your snippet.

This leaves open other sources of a possible error:

What is the definition of rxbuf? You snippet does not tell.

What (if any) errrors does read return? Is errno set? You do not check. The
result may be enlightening.
Btw. There's something about FIFO Buffering in the documentation ...
Anybody know what this is and how I turn it off?

FIFO : first in, first out. A simple queue to detach producers from
consumers so timing
is less critical. If I were you, i'd leave it ON.

In any case, this is not C-related, but Linux-related, so you'd be better
off in a Linux specific newgroup.
 
J

Joona I Palaste

(e-mail address removed) scribbled the following:
Hi folks,
I would like to read and write to a ISO-Reader card (RFID) connected to
/dev/ttyS0 (COM1). Writing works fine, all commands I write to the
device will be executed.

Can't be done in ISO standard C. Please ask in a Linux newsgroup.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
(e-mail address removed) scribbled the following:



Can't be done in ISO standard C. Please ask in a Linux newsgroup.

Sure it can, Joona. The OP just has to conform to standard C rather than use C
with Posix extensions.

To the OP: to make your code (and your problem) answerable here, you're going
to have to rewrite it to use standard C I/O calls, such as
fopen(),
fread(),
fwrite(), and
fclose()

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFBzDkkagVFX4UWr64RAkLIAJoCcIcWhnpu5NLDInS/l0F3od+iRwCghOjc
Xgj/aotMj/zBwBJPKnXtvFc=
=IWn3
-----END PGP SIGNATURE-----
 
G

Gordon Burditt

I would like to read and write to a ISO-Reader card (RFID) connected to
/dev/ttyS0 (COM1). Writing works fine, all commands I write to the
device will be executed.

To every command there is a response. For example:

#001 LED 1 1 CR (command led1=1)

the response should be:

Is that binary or ASCII? How many bytes are there in that response?
Is it "\001\001\000\015" or the text string "001 01h 00h\r" or
something else?
but there's only gibberish in the read buffer. :(

Raw binary bytes, perhaps?
there's some source code:

printf("Trying to open COM1 ... ");
fp = open(DEVICE, O_RDWR | O_NOCTTY);
if(fp == -1) {
perror("failed.\n");
return IOCTL_FAIL;
} else {
printf("success.\n");
return fp;
}
send_data(fp,"#001LED10");
read(fp,rxbuf,1024);
printf("%s",rxbuf);

any suggestions?

read() does not necessarily return a nul-terminated string, so
printing it with %s is not a good idea. read() is also not guaranteed
to return a whole response in one read(), either, so you should
check whether you got a whole response (somehow) and read more if
necessary.

USE the return value of read() to see how much data you got, if
any.

Also realize that there is a potential sync problem here. If you
leave bytes of a response unread, then send a command, the bytes
unread are still there and appear to be a response to the command
you just sent, but they aren't. And turning off the FIFO alone
will NOT solve the problem.

Gordon L. Burditt
 
S

Stan Milam

Hi folks,

I would like to read and write to a ISO-Reader card (RFID) connected to
/dev/ttyS0 (COM1). Writing works fine, all commands I write to the
device will be executed.

To every command there is a response. For example:

#001 LED 1 1 CR (command led1=1)

the response should be:




but there's only gibberish in the read buffer. :(

there's some source code:

printf("Trying to open COM1 ... ");
fp = open(DEVICE, O_RDWR | O_NOCTTY);
if(fp == -1) {
perror("failed.\n");
return IOCTL_FAIL;
} else {
printf("success.\n");
return fp;
}
send_data(fp,"#001LED10");
read(fp,rxbuf,1024);
printf("%s",rxbuf);

any suggestions?

Well, for starters you are using a file pointer of type FILE with read()
system call. The read() system call uses a *file descriptor*. If you
used fread() instead you might get a better result. Besides, read() is
a UNIX system call and has nothing to do with the C language.
 
K

Keith Thompson

Stan Milam said:
(e-mail address removed) wrote: [...]
there's some source code:
printf("Trying to open COM1 ... ");
fp = open(DEVICE, O_RDWR | O_NOCTTY);
if(fp == -1) {
perror("failed.\n");
return IOCTL_FAIL;
} else {
printf("success.\n");
return fp;
}
send_data(fp,"#001LED10");
read(fp,rxbuf,1024);
printf("%s",rxbuf);
any suggestions?

Well, for starters you are using a file pointer of type FILE with
read() system call. The read() system call uses a *file
descriptor*. If you used fread() instead you might get a better
result. Besides, read() is a UNIX system call and has nothing to do
with the C language.

I don't see anything of type FILE or FILE* in the OP's source code.
As far as I can tell, he's consistently using Unix-style file
descriptors. The code is incomplete, so it impossible to be sure, but
chances are fp is declared as an int (the type Unix uses for file
descriptors).

Whether Unix-style file descriptors or standard C FILE* pointers are
more appropriate to this problem is another question for another
newsgroup.
 
L

Lawrence Kirby

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Sure it can, Joona. The OP just has to conform to standard C rather than use C
with Posix extensions.

To the OP: to make your code (and your problem) answerable here, you're going
to have to rewrite it to use standard C I/O calls, such as
fopen(),
fread(),
fwrite(), and
fclose()

That will only work if by some freak accident the serial port is
configured correctly to talk to the device and read/write data in an
appropriate cooked or raw way. In practice the OP is likely to need to
use platform specific APIs such as POSIX Termios. A good place to discuss
that is comp.unix.programmer.

Lawrence
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top