Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Serial! It's what's for breakfast

Serial! It's what's for breakfast

Me: I want to write a program that talks to a serial port. Reality: What is serial port? How does work?

This talk aims to fill in that reality gap. I'll cover the basics of what serial is, how it works (RTS/CTS, stop signals, buffers), and possibly go over how to write/interact with Unix serial ports (in C or walk through part of the python pyserial library. TBD).

All serial info to be based on the RS-232 protocol.

Presented at bangbangcon 2014
Talk recording: http://youtube.com/watch?v=J1CQz8XyWoo

67867d2677e7dd983000441ff0b1c089?s=128

NeiL saitug

May 18, 2014
Tweet

Transcript

  1. SERIAL! It's what's for breakfast lisa neigut | @niftynei Animation

    by Paul Lau & Roy Stanfield
  2. import serial ser = serial.Serial('/dev/tty.usbserial-FTELSI2D', 9600) ser.write("some stuff") byte =

    ser.read()
  3. None
  4. None
  5. serial | parallel

  6. None
  7. None
  8. None
  9. None
  10. pins, wtf!

  11. RX Received Data Pin 2 TX Transmitted Data Pin 3

    GRD Ground Pin 5 RTS Ready to Send Pin 7 CTS Clear to Send Pin 8
  12. TX / RX

  13. None
  14. GRD

  15. 00100110

  16. frames!

  17. 00100110|0|11

  18. 00100110|0|11

  19. #define CS5 0x00000000 /* 5 bits (pseudo) */ #define CS6

    0x00000100 /* 6 bits */ #define CS7 0x00000200 /* 7 bits */ #define CS8 0x00000300 /* 8 bits */
  20. 00100110|0|11

  21. parity: odd, even, mark, space, none

  22. 00100110|0|11

  23. stop: 1, 1.5, 2

  24. 8N1 5E2

  25. speed!

  26. baud rate

  27. 9600 bps 115200 bps

  28. import serial ser = serial.Serial('/dev/tty.usbserial-FTELSI2D', 9600) ser.write("some stuff") byte =

    ser.read()
  29. None
  30. None
  31. <3 <3 <3

  32. 33<

  33. control flow!

  34. RTS / CTS

  35. RTS Ready to Send Pin 7 CTS Clear to Send

    Pin 8
  36. XON 17 XOFF 18

  37. but how!

  38. /* * Defaults on "first" open. */ #define TTYDEF_IFLAG (BRKINT

    | ICRNL | IMAXBEL | IXON | IXANY) #define TTYDEF_OFLAG (OPOST | ONLCR) #define TTYDEF_LFLAG (ECHO | ICANON | ISIG | IEXTEN | ECHOE|ECHOKE|ECHOCTL) #define TTYDEF_CFLAG (CREAD | CS8 | HUPCL) #define TTYDEF_SPEED (B9600)
  39. /usr/include/sys/termios.h /usr/include/sys/ttydefaults.h

  40. None
  41. None
  42. None