robotworkshop
Posts: 24
Joined: Thu Aug 30, 2012 7:19 pm

Using the GPIO Serial port ttyAMA0

Fri Sep 14, 2012 3:28 pm

I have a couple (hopefully quick) questions about using the onboard serial port on the GPIO connector on the RP. I've already built a small adapter with a MAX3232 chip so I have the RX and TX using proper RS-232 voltage levels to connect to my device. Now that it is connected I need to start using it.

From searching the forums I found a great thread about using this port and it mentioned that I should make a couple config changes to prevent the boot console from using it:

http://www.raspberrypi.org/phpBB3/viewt ... 44&t=15683

This thread also has some great details:

http://www.raspberrypi.org/phpBB3/viewt ... 34&t=12000

After going through all that I still have a couple questions:

- If I just want to echo the contents of a txt file to /dev/ttyAMA0 is there a way to set the port to 9600 8N1 from the command line? What is the default speed of this port and how can it be set to 9600 if needed?

- When using the serial port does it support XON/XOFF directly or is that only supported in the Pyton library for using the port? Is it also supported in C or is that something I have to code for myself when using the port in C?

- Is minicom part of the default dist of Linux for the RP? If not, where can I get that and how do I install it? I'd really like to get minicom up and running so I can verify that my serial connection is working as I expect it to.

- Can anyone point me to other examples of using the serial port? Some simple ones I can use from the command line would be ideal.

With the boot config files altered to stop using the ttyAMA0 I am able to receive data from it. As a quick test I just typed:

cat /dev/ttyAMA0

and then turned on my serial device. It was able to display all the startup details ok so I can see that the RX portion works. That was at 9600 baud. Under normal operation I can just ignore all that and would really only be looking for the XON/XOFF codes coming back (unless the serial driver can do that for me)

If you can help answer any of these questions I would appreciate it.

Robert

robotworkshop
Posts: 24
Joined: Thu Aug 30, 2012 7:19 pm

Re: Using the GPIO Serial port ttyAMA0

Fri Sep 14, 2012 3:38 pm

I found the answer to the question about minicom here:

http://codeandlife.com/2012/07/29/ardui ... unication/

It looks like I just need to install that package. I'm going to give that a try this evening.

User avatar
mister_wavey
Posts: 98
Joined: Sun Sep 02, 2012 8:23 am
Location: Abergavenny, Wales, UK

Re: Using the GPIO Serial port ttyAMA0

Fri Sep 14, 2012 3:43 pm

see the 2nd section of this blog: http://www.trainelectronics.com/RaspberryPi/ - he is sending data down his rs232 cable (which I also built successfully) in the same way I think you want to. I didn't see anything about baud rates though.. (I hope this isnt a duplication of the links you gave earlier, I didn't read those).

User avatar
joan
Posts: 16259
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Using the GPIO Serial port ttyAMA0

Fri Sep 14, 2012 3:49 pm

Use the command

stty -F /dev/ttyAMA0 9600

to set the baud rate to 9600 from the command line.

Use the command

man stty

to see other options.

robotworkshop
Posts: 24
Joined: Thu Aug 30, 2012 7:19 pm

Re: Using the GPIO Serial port ttyAMA0

Fri Sep 14, 2012 3:55 pm

That link is very helpful. Thank You!! One tip I saw there was how to send the carriage return and linefeed from the command lie:

echo "hello" > /dev/ttyAMA0

To send with a carriage return and line feed:

echo -e "hello \r\n" > /dev/ttyAMA0

Also, thanks for the note about the stty command. I'll start reading up on the man page for that. By using that to set the default to 9600 8N1 for output from the command line that will let me do most of my testing with what I have setup now. Once I confirm it is working I can try working on some scripts and eventually some higher level code for my robot.

Thanks everyone!

Robert

robotworkshop
Posts: 24
Joined: Thu Aug 30, 2012 7:19 pm

Re: Using the GPIO Serial port ttyAMA0

Fri Sep 14, 2012 10:19 pm

Using all the suggestions I was able to confirm the port is working and I can send and receive data at 9600 baud via the command line. I still need to look into the XON/XOFF for handshaking but this really helps me get started.

Thank You,

Robert

SiriusHardware
Posts: 520
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK

Re: Using the GPIO Serial port ttyAMA0

Mon Nov 12, 2012 8:54 pm

Sorry for bumping an old thread but this naturally follows on from the above:

Assuming I have modified the appropriate configuration files to stop the console from using ttyAMA0 so that I can use it myself:-

Assuming I have somehow managed to set the baudrate and the rest of the serial format to what I want it to be:-

How (from the command line) can I copy an entire text file (actually an Intel Hex file) to the ttyAMA0 serial port, preserving all non-alphanumeric characters intact and ignoring the absence of hardware handshaking?

And if that's possible, then how would I word a batch file called, let's say, serialTX, which would allow a command of the form

serialTX hexfilename

to be run?

User avatar
joan
Posts: 16259
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Using the GPIO Serial port ttyAMA0

Mon Nov 12, 2012 9:11 pm

cat hexfile >/dev/ttyAMA0

create a file called serialTX containing
#!/bin/bash
cat $1 >/dev/ttyAMA0
chmod +x serialTX

then

./serialTX hexfile

You could copy serialTX to /usr/local/bin and then you could just say

serialTX hexfile

SiriusHardware
Posts: 520
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK

Re: Using the GPIO Serial port ttyAMA0

Mon Nov 12, 2012 9:28 pm

joan wrote:cat hexfile >/dev/ttyAMA0

create a file called serialTX containing
#!/bin/bash
cat $1 >/dev/ttyAMA0
chmod +x serialTX

then

./serialTX hexfile

You could copy serialTX to /usr/local/bin and then you could just say

serialTX hexfile
Thanks for that very clear explanation - one other thing I'd like to clarify - in the application we're talking about there may be other files with the same 'main' names, but different extensions ie, filename.asm, filename.lst, filename.sym, and finally the file I actually want this batch file to copy to the serial port, filename.hex.

How can I tweak the example you've given above so that when I type 'serialTX filename', it knows I always mean filename.hex without my having to include the '.hex' portion of the filename in the typed command?

And incidentally, does the batch file have to have a particular suffix/extension to be recognised as a batch file (as in DOS .bat?), or is the inclusion of the shebang: (#!/bin/bash) enough?

User avatar
joan
Posts: 16259
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Using the GPIO Serial port ttyAMA0

Mon Nov 12, 2012 9:31 pm

The chmod +x tells the system its executable. The shebang tells the system to execute it using a particular interpreter.

Instead of $1 use $1.hex

SiriusHardware
Posts: 520
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK

Re: Using the GPIO Serial port ttyAMA0

Mon Nov 12, 2012 9:38 pm

joan wrote:The chmod +x tells the system its executable. The shebang tells the system to execute it using a particular interpreter.

Instead of $1 use $1.hex
Much appreciated, thanks - from this I can see that the '$1' is used to convey the first parameter which followed the batch file name (ie, the hex file name) into the batch file itself - I didn't actually realise that until now.

Return to “Interfacing (DSI, CSI, I2C, etc.)”