Saturday, May 17, 2014

Use the Raspberry Pi Serial Port to Connect to a Device

This example shows how to create a connection to a serial device, write data to the device, and read data from the device.

By default, the serial console in the customized version of Raspian Wheezy on your Raspberry Pi™ hardware is disabled. To use the serialdev, the serial console must be disabled.
Excessive voltage and current can damage the Raspberry Pi hardware. Observe the manufacturer's precautions for handling the Raspberry Pi hardware and connecting it to other devices. For more information, see http://www.raspberrypi.org/technical-help-and-resource-documents.
Create a connection to the Raspberry Pi board.
mypi = raspi;
Show the location of the Tx and Rx pins, GPIO 14 (UART0_TXD) and GPIO 15 (UART0_RXD), on the GPIO header.
showPins(mypi)

Raspberry Pi hardware uses +3.3V. Do not connect Raspberry Pi hardware directly to devices that use higher voltages.
Connect the Raspberry Pi board to a +3.3V serial device.
  • To receive data, connect the GPIO 15 (UART0_RXD) pin on the Raspberry Pi board to the TxD pin on the serial device.
  • To transmit data, connect the GPIO 14 (UART0_TXD) pin on the Raspberry Pi board to the RxD pin on the serial device.
  • Connect a ground pin, GND, on the Raspberry Pi board to the GND pin on the serial device.
  • Connect a +3.3V pin on the Raspberry Pi board to the VCC pin on the serial device.
You can check the status of the serial console.
system(mypi, 'rpi-serial-console status')
ans =

Serial console on /dev/ttyAMA0 is enabled
By default, the serial console is enabled in the custom version of Raspian Wheezy on the Raspberry Pi hardware.
If the serial console is enabled, and you want to use serialdev, disable the console and reboot the Raspberry Pi hardware.
system(mypi, 'sudo rpi-serial-console disable');
system(mypi, 'sudo shutdown -r now');
clear mypi;
When the Raspberry Pi hardware finishes rebooting, you can use serialdev to exchange data with serial devices.
Before continuing, research the manufacturer's product information to determine which baud rate, data bits, parity, and stop bit settings the serial device supports.
Use serialdev to create a connection to the serial device and assign the connection to a handle.
myserialdevice = serialdev(mypi,'/dev/ttyAMA0')
myserialdevice = 

  serialdev with properties:

    BaudRate: 115200
    DataBits: 8
      Parity: 'none'
    StopBits: 1
     Timeout: 10
In this example, the connection uses the default values for baud rate (115200), data bits (8), parity ('none'), and stop bit (1).
If the serial device requires nondefault values, use a set of optional arguments to override those defaults.
myserialdevice = serialdev(mypi,'/dev/ttyAMA0',115200,8,'none',2)
myserialdevice = 

  serialdev with properties:

    BaudRate: 115200
    DataBits: 8
      Parity: 'none'
    StopBits: 2
     Timeout: 10
This example overrides the default value of StopBits by setting it to 2. It uses the other arguments to maintain the correct sequence of arguments to the left of the rightmost overriding value.
You can write a values the serial device.
write(myserialdevice,[10 12],'uint16')
This example writes two values to the serial device. It overrides the default precision, uint8, by setting it to uint16.
You can also read an array of values from the serial port.
output = read(myserialdevice,100);
This example reads a 100-element array of uint8 values from the serial device.
If the serial connection times out during read operations, you can adjust the time out period by assigning a new value to the Timeout property.
myserialdevice.Timeout = 20
myserialdevice = 

  serialdev with properties:

    BaudRate: 115200
    DataBits: 8
      Parity: 'none'
    StopBits: 1
     Timeout: 20
To use the serial console, enable the serial console and reboot the Raspberry Pi hardware.
system(mypi, 'sudo rpi-serial-console enable');
system(mypi, 'sudo shutdown -r now');
clear mypi;
When the Raspberry Pi hardware finishes rebooting, you can communicate with the Linux® command interface over a USB to TTL serial cable that is connected to the serial pins on the GPIO header. However, you cannot use serialdev until you disable the console.

No comments:

Post a Comment