Ask a Question related to UNIX Programming, Design and Development.
-
gkk290778 #1
Serial port programming
Hallo,
this is my first mail to the list. Here i have a program which
communicates with the modem (/dev/ttyS0) and gets the output 'OK' on to
the terminal screen. Iam reading a value from a file called temp and
concatenating with /dev/ttyS and i am using this file for communicating
with the modem.
When i open the serial_device with the command
serial_fd = open("serial_device", O_RDWR | O_NOCTTY);
i get /dev/ttyS0 no such file or directory error. But when i
replace with
serial_fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY); i get proper output.
Actually serial_device contains the value /dev/ttyS0 but why it is not
opening when i try to access it through serial_fd.
#include<stdio.h>
#include<sys/ioctl.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include <termios.h>
#include <string.h>
extern int errno;
int main(void){
int comvalue, serial_fd; char buf[10]; int numdevice; int
modemvalue;
int r_num, at_num; char w_buf[4], r_buf[100], at_buf[100];
struct termios options;
char serial_device[10]="/dev/ttyS";
if((comvalue=open("temp",O_RDONLY,0))==-1)
{perror("Unable to open tty device"); return 1; }
fflush(stdout);
numdevice=read(comvalue,buf,100);
printf("Got Back %d bytes\n",numdevice);
numdevice[buf]=0; printf("You Entered: %s\n",buf);
modemvalue=atoi(buf);
if((modemvalue < 0) || (modemvalue > 3)) {
printf("problem\n");
exit(1); }
strcat(serial_device,buf);
printf("Reading from %s\n", serial_device);
// serial_fd = open("serial_device", O_RDWR | O_NOCTTY);
serial_fd=open("/dev/ttyS0", O_RDWR | O_NOCTTY);
if(serial_fd < 0) {
perror(serial_device);
exit(-1);
}
memset(r_buf,0,sizeof(r_buf));
write(serial_fd,"AT\r",3);//reset modem
sleep(1);
r_num=read(serial_fd,r_buf,7);
printf("value of r_num is %d r_buf is %s\n",r_num,r_buf);
close(comvalue);
close(serial_fd);
return 0;
}
--
Posted via [url]http://dbforums.com[/url]
gkk290778 Guest
-
[Q] carbon, IO kit and serial port programming
i'm looking for some OSX code that talks to a serial port. I've found some Apple sample code but that's for a modem and I wondered anyone could... -
Serial Port Problems
I'm trying to send data over the serial port using PHP and Serproxy. I downloaded Serproxy from http://www.lspace.nildram.co.uk/freeware.html. From... -
pb with serial port
Hello, I have a program that run under Sun. It sends on the serial port this kind of sequence: 02 00 0A 30 30 33 ... On the serial port, i... -
Serial Port and PHP Login
Hello All, I have asked before about serial port programing with PHP and have gotten great advice and now I am stuck again. Okay here is my... -
download via serial port
I used to be able to download files via serial port without problems in windows 9x, ME, NT etc. using DOS programs. It seems there is something... -
Andrew Gabriel #2
Re: Serial port programming
In article <3085409.1057665727@dbforums.com>,
gkk290778 <member32638@dbforums.com> writes:That's because you are trying to open a file in your current>
> Hallo,
> this is my first mail to the list. Here i have a program which
> communicates with the modem (/dev/ttyS0) and gets the output 'OK' on to
> the terminal screen. Iam reading a value from a file called temp and
> concatenating with /dev/ttyS and i am using this file for communicating
> with the modem.
>
> When i open the serial_device with the command
> serial_fd = open("serial_device", O_RDWR | O_NOCTTY);
> i get /dev/ttyS0 no such file or directory error. But when i
> replace with
> serial_fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY); i get proper output.
>
> Actually serial_device contains the value /dev/ttyS0 but why it is not
> opening when i try to access it through serial_fd.
directory called "serial_device", and not the device "/dev/ttyS0".
Take the quotes off...
serial_fd = open(serial_device, O_RDWR | O_NOCTTY);
--
Andrew Gabriel
Andrew Gabriel Guest
-
Rainer Temme #3
Re: Serial port programming
"gkk290778" <member32638@dbforums.com> wrote:Hello gkk290778,>
> Hallo,
> this is my first mail to the list. Here i have a program which
> communicates with the modem (/dev/ttyS0) and gets the output 'OK' on to
> the terminal screen. Iam reading a value from a file called temp and
> concatenating with /dev/ttyS and i am using this file for communicating
> with the modem.
>
> When i open the serial_device with the command
> serial_fd = open("serial_device", O_RDWR | O_NOCTTY);
> i get /dev/ttyS0 no such file or directory error. But when i
> replace with
> serial_fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY); i get proper output.
what you really want might be this:
serial_fd = open(serial_device, O_RDWR | O_NOCTTY);
but what you do is
serial_fd = open("serial_device", O_RDWR | O_NOCTTY);
since serial_device is a char-variable and "serial_device" is a
string-literal,
for the compiler both is correct. (Obviously only one of them does what
you want.)
Regards ... Rainer
Rainer Temme Guest
-
gkk290778 #4
Re: Serial port programming
Oh im sorry i forgot mentioning the same, i tried even this,
serial_fd = open(serial_device, O_RDWR | O_NOCTTY);
got the same result, that's why iam surprised.
kk.
--
Posted via [url]http://dbforums.com[/url]
gkk290778 Guest
-
gkk290778 #5
Re: Serial port programming
I shall make things clear again,
see i have assumed char serial_device[10]="/dev/ttyS";
the reason for not giving a particular port like ttyS0 in the
above stmt.
I have got a file called temp which has value ie either 0 or 1 or 2.
I am doing a concatenation operation like (/dev/ttyS, temp). Which will
give me the result /dev/ttyS0 or /dev/ttyS1 or /dev/ttyS2. The file temp
is a requirement.
W.r.to. my program i get the result as serial_device=/dev/ttyS0
(assuming 0 is the value stored in file temp) successfully,
the corresponding stmt is,
printf("Reading from %s\n", serial_device);
But when i pass the value of serial_device (in our case /dev/ttyS0) like
serial_fd = open(serial_device, O_RDWR | O_NOCTTY);
it get an error, /dev/ttyS0 : no such file or directory.
I think the program is logically correct, but i am not sure where it is
going wrong.
kk.
--
Posted via [url]http://dbforums.com[/url]
gkk290778 Guest
-
Rainer Temme #6
Re: Serial port programming
"gkk290778" <member32638@dbforums.com> wrote:
Hi gkk290778,> I shall make things clear again,
> see i have assumed char serial_device[10]="/dev/ttyS";
> the reason for not giving a particular port like ttyS0 in the
> above stmt.
>
> I have got a file called temp which has value ie either 0 or 1 or 2.
>
> I am doing a concatenation operation like (/dev/ttyS, temp). Which will
> give me the result /dev/ttyS0 or /dev/ttyS1 or /dev/ttyS2. The file temp
> is a requirement.
>
> W.r.to. my program i get the result as serial_device=/dev/ttyS0
> (assuming 0 is the value stored in file temp) successfully,
> the corresponding stmt is,
> printf("Reading from %s\n", serial_device);
>
> But when i pass the value of serial_device (in our case /dev/ttyS0) like
> serial_fd = open(serial_device, O_RDWR | O_NOCTTY);
>
> it get an error, /dev/ttyS0 : no such file or directory.
>
> I think the program is logically correct, but i am not sure where it is
> going wrong.
your variable < char serial_device[10] ="/dev/ttyS"; >
is not long enough to concatenate a single character to it, without
potetially corrupting other variables in stack!
/dev/ttyS gives 9 characters, and a terminating zero-byte is terminating
the string.
therefore all 10 bytes are already filled.
Concatenating anything to it will overwrite something else.
(This might eventually not be harmful in your case, since nowadays
variables
are mainly aligned to 32-bit (or even 64-bit) addresses. Nevertheless,
it's an error).
Regards .. Rainer
Rainer Temme Guest
-
Floyd Davidson #7
Re: Serial port programming
gkk290778 <member32638@dbforums.com> wrote:
And the method you chose to do the concatenation will be a>I shall make things clear again,
>see i have assumed char serial_device[10]="/dev/ttyS";
>the reason for not giving a particular port like ttyS0 in the
>above stmt.
>
>I have got a file called temp which has value ie either 0 or 1 or 2.
>
>I am doing a concatenation operation like (/dev/ttyS, temp). Which will
>give me the result /dev/ttyS0 or /dev/ttyS1 or /dev/ttyS2. The file temp
>is a requirement.
source of trouble every time. That is because
char serial_device[10]="/dev/ttyS";
Does *not* have enough space to concatenate even a single
character to the string.
But in the process you have overwritten whatever data is stored>W.r.to. my program i get the result as serial_device=/dev/ttyS0
> (assuming 0 is the value stored in file temp) successfully,
>the corresponding stmt is,
>printf("Reading from %s\n", serial_device);
just after your string. It may or may not be immediately
significant, but it is an error and it will come back to haunt
you.
>But when i pass the value of serial_device (in our case /dev/ttyS0) like
>serial_fd = open(serial_device, O_RDWR | O_NOCTTY);
/* this array must be able to hold 11 bytes */
char serial_device[11]="/dev/ttyS";
strcat(serial_device, "0");
serial_fd = open(serial_device, O_RDWR | O_NOCTTY);
-->it get an error, /dev/ttyS0 : no such file or directory.
>
>I think the program is logically correct, but i am not sure where it is
>going wrong.
>
>kk.
>
>--
>Posted via [url]http://dbforums.com[/url]
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) [email]floyd@barrow.com[/email]
Floyd Davidson Guest
-
gkk290778 #8
Re: Serial port programming
By giving this stmt the value of port will always be ttyS0
strcat(serial_device, "0");
and whatever changes made to temp file will not be reflected here.
after even changing char serial_device[11]="/dev/ttyS";
If the user changes temp as 1 still the port will be ttyS0 and
not ttyS1.
I have got an application where the user menu selection of the
serial port the subsequent number (like 0 or 1 or 2) gets stored in
the temp file.
Hence i need to get the value from the temp file and concatenated with
the serial_device(/dev/ttyS).
kk.
--
Posted via [url]http://dbforums.com[/url]
gkk290778 Guest
-
Rainer Temme #9
Re: Serial port programming
"gkk290778" <member32638@dbforums.com> wrote:
Hi gkk290778,
see comments within the source ...
??? What is your actual output that you get from this statement???>
> Hallo,
> this is my first mail to the list. Here i have a program which
> communicates with the modem (/dev/ttyS0) and gets the output 'OK' on to
> the terminal screen. Iam reading a value from a file called temp and
> concatenating with /dev/ttyS and i am using this file for communicating
> with the modem.
>
> When i open the serial_device with the command
> serial_fd = open("serial_device", O_RDWR | O_NOCTTY);
> i get /dev/ttyS0 no such file or directory error. But when i
> replace with
> serial_fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY); i get proper output.
>
> Actually serial_device contains the value /dev/ttyS0 but why it is not
> opening when i try to access it through serial_fd.
>
>
> #include<stdio.h>
> #include<sys/ioctl.h>
> #include<unistd.h>
> #include<fcntl.h>
> #include<errno.h>
> #include <termios.h>
> #include <string.h>
> extern int errno;
>
> int main(void){
>
> int comvalue, serial_fd; char buf[10]; int numdevice; int
> modemvalue;
> int r_num, at_num; char w_buf[4], r_buf[100], at_buf[100];
> struct termios options;
> char serial_device[10]="/dev/ttyS";
> if((comvalue=open("temp",O_RDONLY,0))==-1)
> {perror("Unable to open tty device"); return 1; }
>
> fflush(stdout);
> numdevice=read(comvalue,buf,100);
> printf("Got Back %d bytes\n",numdevice);
anything other than 1 will produce an error later on!!!
Note that most editors will padd a newlinecharacter to the end of a
line
so even if you only have a single line containing a 0 in your file,
the contents of buf will be 0\n (2 Bytes)more correct would be buf[numdevice]=0 but most compilers will accept this.> numdevice[buf]=0; printf("You Entered: %s\n",buf);now you have overwritten something in stack ... your variable needs at least>
> modemvalue=atoi(buf);
> if((modemvalue < 0) || (modemvalue > 3)) {
> printf("problem\n");
> exit(1); }
>
> strcat(serial_device,buf);
to be
serial_device[11].
Even better, use a second variable char device_to_open[11]
and
sprintf(device_to_open,"%s%c",serial_device,*buf);
this way you're sure to have only added one character.Have a look at the touput of this line ... modiy it to>
> printf("Reading from %s\n", serial_device);
printf("Reading from [%s]\n", serial_device);
the output should show [ and ] in the same line.
if this is not the case, your variable contains
newline-character...
those will be interpreted to be part of the filename
that you want to open later on!
use serial_fd=open(device_to_open,=RD.......);> // serial_fd = open("serial_device", O_RDWR | O_NOCTTY);
> serial_fd=open("/dev/ttyS0", O_RDWR | O_NOCTTY);
> if(serial_fd < 0) {
> perror(serial_device);
> exit(-1);
> }
> memset(r_buf,0,sizeof(r_buf));
>
> write(serial_fd,"AT\r",3);//reset modem
> sleep(1);
> r_num=read(serial_fd,r_buf,7);
> printf("value of r_num is %d r_buf is %s\n",r_num,r_buf);
> close(comvalue);
> close(serial_fd);
> return 0;
> }
>
> --
> Posted via [url]http://dbforums.com[/url]
Rainer Temme Guest
-
gkk290778 #10
Re: Serial port programming
Thanks Mr. Rainer,
Yes numvalue is more than 1 and a newline character gets added to the
serial_device.
i have included these lines ...
char device_to_open[11];
sprintf(device_to_open,"%s%c",serial_device,*buf);
printf("Reading from [%s]\n", serial_device);
here i am getting device value extending to newline.
you have mentioned this
use serial_fd=open(device_to_open,=RD.......);
now what should be serial_fd, esp. the syntax ....=RD
so that i can avoid the newline character.
--
Posted via [url]http://dbforums.com[/url]
gkk290778 Guest
-
gkk290778 #11
Re: Serial port programming
Hallo everybody,
the answer is found, it is here,
mr. rainer pointed out the newline character and yes it has to be
removed.
What are the changes to the code then,
well i have only added few things to my code posted in the beginning,
they are,
i assigned this char* nl;
printf("You Entered: [%s]\n",buf);
<-- after this line i added this
nl=strrchr(buf, '\r');
if (nl) *nl='\0';
nl=strchr(buf,'\n');
if (nl) *nl='\0';
printf("You Entered again: [%s]\n",buf);
--> the new line character is truncated that's it
now my serial_device shows /dev/ttyS0 or 1 or 2 depending on temp value
without new line character.
The discussion gave me lot of information.
thanks for the same.
kk.
--
Posted via [url]http://dbforums.com[/url]
gkk290778 Guest
-
gkk290778 #12
Re: Serial port programming
Acutally, I am reading a signal from a device connected to serial port,
i initially checked the RS-232 line the results ie., connecting the
device (when it is in switched off ) and after connecting the device
(when the device is turned on)
Note: This device is a Employee card reader, where the time attedance of
all the employees are registered.
The status of my RS-232 lines are as follow (X means on, 0 means off)
LE DTR RTS ST SR CTS CD RNG DSR
(when the device is turned OFF)
X 0 0 0 0 0 0 0
(When the device is turned on )
X 0 0 0 0 0 0 X
Actually the driver for this device is writen in turbo c and according
to the author the device returns a "*" when it encounters a ";" , this
is how it works, i am porting this program to linux (gcc). The situation
is similar to a modem returning a OK when it encounters an AT signal.
I then wrote the program mentioned below, hoping to get an response from
the device,
#include<stdio.h>
#include<sys/ioctl.h>
#include<unistd.h>
#include<fcntl.h>
int main(void){
int serial_fd;
int r_num; char r_buf[100];
serial_fd = open("/dev/ttyS0",O_RDWR | O_NOCTTY);
memset(r_buf,0,sizeof(r_buf));
write(serial_fd,";\r",3);//reset modem
sleep(1);
r_num=read(serial_fd,r_buf,7);
printf("value of r_num is %d r_buf is %s\n",r_num,r_buf);
close(serial_fd);
return 0;
}
I got r_num as 0 and r_buf with no response. What should i add to get
some response from the device.
kk.
--
Posted via [url]http://dbforums.com[/url]
gkk290778 Guest
-
Floyd Davidson #13
Re: Serial port programming
gkk290778 <member32638@dbforums.com> wrote:
Do you actually format your code like that? It might help>Acutally, I am reading a signal from a device connected to serial port,
>i initially checked the RS-232 line the results ie., connecting the
>device (when it is in switched off ) and after connecting the device
>(when the device is turned on)
>Note: This device is a Employee card reader, where the time attedance of
> all the employees are registered.
>
>The status of my RS-232 lines are as follow (X means on, 0 means off)
>
>LE DTR RTS ST SR CTS CD RNG DSR
>
>(when the device is turned OFF)
>
> X 0 0 0 0 0 0 0
>
>
>(When the device is turned on )
> X 0 0 0 0 0 0 X
>
>Actually the driver for this device is writen in turbo c and according
>to the author the device returns a "*" when it encounters a ";" , this
>is how it works, i am porting this program to linux (gcc). The situation
>is similar to a modem returning a OK when it encounters an AT signal.
>
>I then wrote the program mentioned below, hoping to get an response from
>the device,
greatly if you used a consistent indenting style and added a
little more whitespace here and there!
#include<stdio.h>>#include<stdio.h>
>#include<sys/ioctl.h>
>#include<unistd.h>
>#include<fcntl.h>
>int main(void){
> int serial_fd;
> int r_num; char r_buf[100];
> serial_fd = open("/dev/ttyS0",O_RDWR | O_NOCTTY);
> memset(r_buf,0,sizeof(r_buf));
> write(serial_fd,";\r",3);//reset modem
> sleep(1);
> r_num=read(serial_fd,r_buf,7);
> printf("value of r_num is %d r_buf is %s\n",r_num,r_buf);
> close(serial_fd);
> return 0;
> }
>
>I got r_num as 0 and r_buf with no response. What should i add to get
>some response from the device.
#include<sys/ioctl.h>
#include<unistd.h>
#include<fcntl.h>
int
main(void)
{
int serial_fd;
int r_num;
char r_buf[100];
serial_fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY);
/* The following line serves no purpose,
* (when the rest is correctly coded). */
memset(r_buf,0, sizeof(r_buf));
/* at this point you are assuming the serial port is configured
* correctly to talk to your device.
*
* That is almost certainly not correct.
* See the man page for termios.
*/
/* You must have meant to send three characters:
* ";\r\n" would be correct.
*/
write(serial_fd, ";\r", 3); /* reset modem */
sleep(1);
r_num = read(serial_fd, r_buf, 7);
/* you need to use r_num to terminate what ever has been
* written to r_buf if you want to use it as a string
*
* add this line:
*
* r_buf[r_num] = '\0';
*/
printf("value of r_num is %d r_buf is %s\n", r_num, r_buf);
close(serial_fd);
return 0;
}
You have no error checking either. It is very likely, given that
you have no indication of Carrier Detect according to the signal
status you show above, that the call to open() is failing. Either
you'll need to wire the port cable such that it indicates Carrier
Detect when the distant device is operational, or you'll need to
open using the O_NONBLOCK flag.
--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) [email]floyd@barrow.com[/email]
Floyd Davidson Guest
-
gkk290778 #14
Re: Serial port programming
i'm sorry for not presenting my code properly, i have made changes and
the complete program (without include's)it is here,
int main(void){
int serial_fd, status, result;
int r_num; char r_buf[100]; struct termios options;
if((serial_fd = open("/dev/ttyS0",O_RDWR | O_NONBLOCK)) < 0) {
printf("Couldnt open port");
exit(1); }
/* check for serial port status */
ioctl(serial_fd,TIOCMGET,&status);
printf("STATUS %d\n", status);
result=write(serial_fd,";\r\n",3);
printf("result after write %d\n", result);
if(result<0)
{
fputs("write failed\n",stderr);
close(serial_fd);
exit(1); }
usleep(1000);
result=read(serial_fd,r_buf,7); /* reading input buffer and
printing it*/
r_buf[result]='\0';
printf("result after read is %d \n r_buf is %s\n", result,
r_buf);
close(serial_fd);
return 0;
}
I got the following output,
STATUS 16678
result after write 3
result after read is -1
r_buf is Pûÿ¿`@Y,@°H@
Actually i could not able to interpret what it is, my input is ";" and
the device should return a *. I think now the device atleast responds
to my input.
--
Posted via [url]http://dbforums.com[/url]
gkk290778 Guest
-
Floyd Davidson #15
Re: Serial port programming
gkk290778 <member32638@dbforums.com> wrote:
Your code is impossible to read because of the incoherent indenting>i'm sorry for not presenting my code properly, i have made changes and
>the complete program (without include's)it is here,
style (not to mention the other oddness in your formatting).
Once again, I'll reformat this to make it readable. (This style
suits me, but there are many others that are different but still
correct.) The comments I've added are those which have no left
margin.
int
main(void)
{
int serial_fd, status, result;
int r_num;
char r_buf[100];
struct termios options;
if ((serial_fd = open("/dev/ttyS0", O_RDWR | O_NONBLOCK)) < 0) {
printf("Couldnt open port");
exit(1);
}
/*
* You don't check to see if the ioctl() call succeeds or fails.
*?
/* check for serial port status */
ioctl(serial_fd, TIOCMGET, &status);
printf("STATUS %d\n", status);
/*
* The serial port has not been configured. You have no idea
* what state it was last configured for. It might be sending
* at 1200 bps or at 115400 bps, it might have even parity
* enabled, or no parity, and it might have the receiver turned
* off so that nothing can be received.
*/
result = write(serial_fd, ";\r\n", 3);
printf("result after write %d\n", result);
if (result < 0) {
fputs("write failed\n", stderr);
close(serial_fd);
exit(1);
}
usleep(1000);
/*
** As with the write() above, this read() could be reading at some
** bit rate other than what your device is sending, or could have
** any number of odd configuration problems.
**/
result = read(serial_fd,r_buf, 7); /* reading input buffer and printing it*/
/*
** if this read() fails and returns -1, you do *not* want the
** following code to execute. There should be a check, just as
** with the write() call above, to avoid 1) writing to r_buf
** with a negative index, and 2) printing an indeterminant array
** of characters with an unknown or non-existing string terminator.
*/
r_buf[result]='\0';
printf("result after read is %d \n r_buf is %s\n", result, r_buf);
close(serial_fd);
return 0;
}
That has no value. You can use system defined constants to>
>I got the following output,
>STATUS 16678
pull out information on the status of various control leads,
for example; but why?
It would appear that 3 bytes were written! That's good.>result after write 3
That indicates a read error on the device. Given that it has>result after read is -1
not been configured, it's hard to say what would cause a read
error. I suspect things like framing errors, due to incorrect
bit rates, are causing you to see read errors.
That is meaningless garbage that just happened to be what was>r_buf is Pûÿ¿`@Y,@°H@
already in the memory where the buffer was allocated.
There is nothing indicating any response at all.>Actually i could not able to interpret what it is, my input is ";" and
>the device should return a *. I think now the device atleast responds
>to my input.
--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) [email]floyd@barrow.com[/email]
Floyd Davidson Guest
-
gkk290778 #16
Re: Serial port programming
Yes, the buffer value (r_buf) is empty (no garbage) after i added
this stmt.
memset(r_buf,0,sizeof(r_buf));
thus the device is not returning anything
I have also set the baud rates to 9600... */
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
but still the output is
result before read = 3
result after read = 0
r_buf=
Is there anyway to find out why the card reader device not responding to
the call. I have the dos based code (bioscom's )which works well with
the device, i can send it thru email, if someone can figure why the
device is not responding from the device.
--
Posted via [url]http://dbforums.com[/url]
gkk290778 Guest



Reply With Quote

