ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
write() returns the number of bytes and -1 on error. So if (iOut >0) is backwards.
if (iOut < 0){
printf("no commmunication.");
}
else{
printf("Wrote %d of %d bytes", iOut, sizeof(cmd));
}
return (0);
Second, you don't need the double backslashes on your string (\\001), just one (\001). And remember it's octal, so use \xnn when you mean hex. You need the double backslash on the echo command because the shell interprets the backslash as well.
There's one more issue with that example code, the cmd[] array begins with a \0, so strlen() can't be used to calculate the number of bytes to transmit in the write() function. Previous poster correctly said sizeof(cmd) would work, but I think it should be sizeof(cmd)-1 to be most accurate, the compiler includes the implicit termination null in sizeof() a char array.
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main (void)
{
int iOut = 0;
int fd = 0;
char *sCompPort="/dev/ttyS2";
//char cmd[70];
//echo -e \\000\\000\\000\\000\000\\001Z00\\002AA LINUX \\004 >/dev/ttyS2
char cmd[]="\\000\\000\\000\\000\\000\\001Z00\\002AA linux \\004";
//return
//port open
//strlen(cmd) 43
//Wrote 43 of 43 bytes
//char cmd[]="\000\000\000\000\000\001Z00\002AA linux\004";
//return
//port open
//strlen(cmd) 0
//Wrote 0 of 19 bytes
fd = open(sCompPort, O_RDWR | O_NOCTTY);
if (fd >0){
printf("port open\n");
printf("strlen(cmd) %d\n",strlen(cmd) );
iOut = write(fd, cmd, strlen(cmd));
close(fd);
}
else{
printf("not able to open port.\n");
}
if (iOut < 0){
printf("no commmunication.\n");
}
else{
printf("Wrote %d of %d bytes\n", iOut, sizeof(cmd)-1);
}
return (0);
}
data was sending to the serial port... the port is connected to a display pannel...
there are some code to initialize the connection, centering the text, close connection... in bash mode that work fine... but not in this c program...
nothing is interpreted... all is displayed to the display pannel...
Your literal strings contain double backslashes. If you intend to use the notation for octal bytes in your literal strings, remove one of the backslashes from each octal byte expression in each string:
Code:
char cmd[]="\000\000\000\000\000\001Z00\002AA linux \004";
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.