Serial tty devices
Hi,
I am trying to communicate with a serial device by sending packets to it using echo command and the device is responding as expected. But I am trying to read the response the device is sending me back and that is where I am stuck. I am able to see the response on another terminal running screen, the response is valid but the problem is that I am unable to catch that data. Moreover that data is in hex form and I need a way to type cast the data. Any suggestions would be helpful for me. |
The simplest thing to do is exactly what you are doing, performing echo from one command prompt and performing cat on another command prompt. You can redirect the cat to a file to capture the responses completely, be they printable, versus not. Sounds like the characters are not printable, or you need translation to decode what you're seeing. So what I would do there is to write a short program to listen to the serial port, and output whatever is received from it in a coherent manner; which is to say that the program would decode the data and print it out in a read-able manner.
Minicom is a Linux terminal program, typically you have to run it using sudo, or as root; however I do not believe it gives you any better output translations than what you're seeing from the command prompt performing the cat. Perhaps there are other Linux terminal programs which will convert to HEX. Here are some program tips for what I'd do in C if you were so inclined: Substitute "/dev/ttyUSB0" for whatever serial resource you are using on your system: Code:
int rdHandle; Code:
char buffer[4096]; Code:
int i; |
Quote:
|
you may try:
a=$(od -t x1 <serial device>) and you will get the result in a (just you need to process it), but it also depends how many bytes do you want to evaluate. |
Quote:
|
Quote:
So to avoid calling a c program to perform the tasks I wish I am preferring to implement my code functionality in shell script completely. |
actually your explanation is not clear (for me). Probably you missed something, or I misunderstood something.
The program you invoked should work in the same way any time, therefore its execution must not depend on that fact (if it was executed from a shell or by an init script). I would add set -xv at the beginning of your script to see what's happening and compare the two cases. You will see what causes the differences... |
One of the things to watch out for with using serial devices is the thing on the other end of the serial device...
Does it use modem signals? Does respond to cts/rts signals? One of the things that happens is that cts is off (not clear to send) if the terminal is closed... thus the device stops. Having the serial line open (ie cat) allows another process to send data, and the response to be read. But note: this takes two processes. Serial lines are asynchronous devices. Normal commands are synchronous. Now you CAN get the effect by opening ANOTHER file desriptor (not stdin/stdout) to the terminal (see bash manpage for redirection for other than file descriptors 0,1,2). You can then use this file descriptor to send data, and read responses (also bash coprocesses may help, by having a coprocess ready to read a response, the the other process sends a string). |
Quote:
More surprisingly this same script and the same program works as expected on raspberry pie which is running a linux kernel based on debian. This is making me think of what is gone wrong on zedboard and I could not identify the problem and hence I am exploring the ways to either execute the program forcefully using a while loop or implementing the entire c code in shell script. |
I see, so probably it runs too early, the system has not been fully initialized?
How it is configured in init.d? Probably it should be executed a bit later? |
Quote:
|
in that case I can suggest only to insert set -xv in the beginning of the script, save the log and check it. Probably you will find some useful info in it.
|
Quote:
Code:
# Your script - example The 'S' is for Starting the script, as opposed to a 'K' which you would use in say runlevels 2, 4, or 6 as the system is going down if you needed to cleanup anything. The '99' is the important part; which is the numerical ordering of when the script gets run. I honestly don't know if you can use one, three, or more digit numbers; I've always used 2 digit numbers, but they work in counting order, therefore 01, 02, 03, 04, ... 97, 98, 99 would be run in sequence; if symbolic links existed for every number. I should also state some added ignorance on my part which is that there is also rcS.d (Letter S) and I've successfully placed start scripts there versus the rc5.d (number 5) directory. I don't know the reason, but have heard that use of the rcS.d (Letter S) tree is discouraged. |
All times are GMT -5. The time now is 11:39 PM. |