![]() |
|
#1
|
|||
|
|||
Problem with string and serial porthi
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:
my source code CPP / C++ / C Code:
when i run this program i get Code:
that seem ok but on the led display, i see nothing... 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? Last edited by LuciWiz : 22-Mar-2005 at 10:32.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#2
|
|||
|
|||
|
Quote:
First of all, when you have a function like this CPP / C++ / C Code:
The argument is a pointer to char. There is no way (no way) that the function can know the size of the array unless you tell it. Note that in your main() function, msg[] is an array of char whose initialization defines the number of chars, so sizeof(msg) in main() gives the number of chars in the message. However, inside your writeopen() function the following code shows the size of a pointer to char (which is apparently four bytes in your system, as it is in mine). CPP / C++ / C Code:
Furthermore when you use any standard library string functions, including printf with "%s" format specifier, they always assume that a "string" is a null-terminated sequence of chars. You do know that there is no "string" data type in C, right? So, since your message has some byte values that are zero, you can't use strlen(), strcpy(), printf("%s"), etc., to do anything useful with the message. Bottom line: you must pass information about the message length to your function. If you want to, you can try the following to see some of the points I have tried to make: CPP / C++ / C Code:
Regards, Dave |
|
#3
|
|||
|
|||
|
thanks a lot for the information you writed
i tried to correct some part of the code with you information now, i have CPP / C++ / C Code:
that display me: Code:
but i see nothing on the led display... Last edited by LuciWiz : 22-Mar-2005 at 11:59.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#4
|
|||
|
|||
|
I could be way off here, but if the character combination '\0' means NULL, then this:
Code:
Code:
|
|
#5
|
|||
|
|||
|
Quote:
\\000\\000\\000\\000\\000 are NULL, not other 0 |
|
#6
|
|||
|
|||
|
Oh, then would that not be:
Code:
|
|
#7
|
|||
|
|||
|
Quote:
There's no way that I can debug your hardware from here. I modified your function to verify that the bytes are getting there OK: CPP / C++ / C Code:
Since the message is getting to the function OK, then if you're absolutely sure that the bytes are correct, then there must be some hardware functionality not operating properly. (By the way, your program has TRIPLEX and your most recent command line example has Triplex, if it matters.) You can capture the command line in a file by doing something like this: Quote:
Then compare the bytes of the file with your message from the program. (Does your message need a newline at the end? The command line that you echo to your output device always has a newline at the end. Just thought I'd mention it.) Regards, Dave |
|
#8
|
|||
|
|||
|
Quote:
before displaying my message, i display this string before my msg... char init[] = {'\0', '\0', '\0', '\0', '\0', '\1', 'Z', '0', '0', '\2', 'E', '$','\4' }; my message is displayed correctely my init and mss array have some octal code... what i need to do to use hexadecimal code? i would like to read a file and display the text file to led display how can i read the file and put the text correctly to msg array? surely there are a better way? |
|
#9
|
|||
|
|||
|
Quote:
Are you saying that following this preamble (from the init[] array) then any sequence of bytes will be displayed? Are the bytes ascii chars or what? As for hex codes: If you have an integral type (int, char, short, etc.) and you want to express it in hex, use something like this: CPP / C++ / C Code:
If you have a string literal and you want to give a hex value for an element, then you can use something like this: CPP / C++ / C Code:
Here is an example that shows two ways to initialize an array of char: (Be sure to pay attention to the size of the first array. An extra byte is used for the terminating null character.) CPP / C++ / C Code:
Regards, Dave |
|
#10
|
|||
|
|||
|
Quote:
Just open the file and read the chars, I guess: CPP / C++ / C Code:
Notes: 1. If the file is created by a Windows-type text editor, it will have \r\n characters at the end of each line (usually even if it has only one line). 2. If the file is created by a UNIX/Linux-type text editor, it will have \n characters at the end of the lines. 3. If the file contains non-ascii bytes, and if, in particular, it may contain '\0', you can't use strcpy(), strcat(), etc. since these will quit when the zero byte is encountered. You can simply copy byte-by-byte using pointer or array notation to put the file's bytes together with whatever control sequences are required to display the stuff. Regards, Dave |
Recent GIDBlog
Developing GUIs with wxPython (Part 2) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The