![]() |
| | #1 |
| Registered User Join Date: Mar 2005
Posts: 5
| Problem with string and serial port in c programming (linux), i search a way to read the setting of the serial port (speed, parity...) and change it. i need also to write to a led display (alpha sign communications protocole) with bash i do: Code: echo -e \\000\\000\\000\\000\\000\\001Z00\\002AA Triplex\\004 >/dev/ttyS2 Code: #include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h> /*atoi, system, getenv */
#include <unistd.h> /*sleep, close */
#include <dirent.h> /*opendir, readdir, closedir */
#include <string.h>
#include <fcntl.h> /*open */
#include <sys/stat.h>
#include <sys/errno.h>
char *ledisplay;
/* Opening port one for read and write */
int writeopen(char msg[])
{
int fd1;
int wr;
fd1 = open(ledisplay, O_RDWR | O_NOCTTY | O_NDELAY );
if (fd1 == -1)
{
fprintf(stderr, " %s open_port: Unable to open %s\n", strerror(errno), ledisplay);
}
else
{
fcntl(fd1, F_SETFL, 0);
printf(" Port 1 has been sucessfully opened and %d is the file descriptor\n",fd1);
printf("sizeof %d\n", sizeof(msg));
wr=write(fd1, msg, sizeof(msg));
printf(" Bytes sent are %d \n",wr);
if (wr < 0)
fputs("write() of n bytes failed!\n", stderr);
else
printf(" Stuff has been written into port 1\n");
close(fd1);
}
return (fd1);
}
int readopen()
{
int fd1;
int rd;
char *buff=NULL;
fd1 = open(ledisplay, O_RDONLY | O_NOCTTY | O_NDELAY );
if (fd1 == -1)
{
fprintf(stderr, "%s open_port: Unable to open %s\n", strerror(errno), ledisplay);
}
else
{
fcntl(fd1, F_SETFL, 0);
printf(" Port 1 has been sucessfully opened and %d is the file descriptor\n",fd1);
rd=read(fd1, buff, 100);
printf(" Bytes recieved are %d \n",rd);
printf("%s\n",buff);
close(fd1);
}
return (fd1);
}
int main()
{
int result;
//echo -e \\000\\000\\000\\000\000\\001Z00\\002AA TRIPLEX \\004 >/dev/ttyS2
char msg[] = {'\0', '\0', '\0', '\0', '\0', '\1', 'Z', '0', '0', '\2', 'A', 'A', ' ', 'T', 'R', 'I', 'P', 'L', 'E', 'X', ' ', '\4' };
if((result=(readRtuConfig()))!=-1)
{
printf("port série utilisé: %s\n", ledisplay);
printf("msg = %s\n",msg);
if((result=(writeopen(msg)))!=-1)
{
printf("ok\n");
}
}
return 1;
}
Code: Port 1 has been sucessfully opened and 3 is the file descriptor Bytes sent are 4 Stuff has been written into port 1 ok surely, char msg[] = {'\0', '\0', '\0', '\0', '\0', '\1', 'Z', '0', '0', '\2', 'A', 'A', ' ', 'T', 'R', 'I', 'P', 'L', 'E', 'X', '\4' }; is not equivalent to this bash command echo -e \\000\\000\\000\\000\\000\\001Z00\\002AA Triplex\\004 >/dev/ttyS2 any idea? |
| collinm is offline | |
| | #2 |
| Registered User Join Date: Oct 2001
Posts: 2,936
| >Bytes sent are 4 Notice you're only writing out 4 bytes to your port, even though your message is much longer. Once you pass an array to a function, all you get is the size of a pointer. You need to pass the size of the array to the function: Code: if((result=(writeopen(msg, sizeof(msg))))!=-1) I don't know bash, so I can't tell you whether msg matches what you are sending using bash. If you can tell us what bash actually sends, then we can tell you what msg should be.
__________________ http://www.freechess.org |
| swoopy is offline | |
| | #3 |
| SleepWalker Join Date: Apr 2004
Posts: 70
| There's tcgetattr (...) what you can use to get serial configuration... "man termios" gives you more information about it and there's Serial-Programming-HOWTO in tldp.org... If your schedule is busy enough, hit "setserial" on your console, read quick info and do what you need to.
__________________ -- Add Your Signature Here -- |
| tjohnsson is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Send image (.jpg) file over serial port | aSpareName | Networking/Device Communication | 2 | 06-30-2009 09:35 AM |
| Lame null append cause buffer to crash | cmoo | C Programming | 8 | 12-29-2008 03:27 AM |
| Reading and writing to a serial port | SwarfEye | C Programming | 2 | 08-18-2006 12:28 AM |
| Problem of serial programming | fingerling54 | Linux Programming | 5 | 05-11-2005 11:05 AM |
| DOS, Serial, and Touch Screen | jon_nc17 | A Brief History of Cprogramming.com | 0 | 01-08-2003 04:59 PM |