{"id":674,"date":"2010-01-08T10:03:56","date_gmt":"2010-01-08T00:33:56","guid":{"rendered":"http:\/\/www.clearchain.com\/blog\/?p=674"},"modified":"2010-01-08T10:03:56","modified_gmt":"2010-01-08T00:33:56","slug":"using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate","status":"publish","type":"post","link":"https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate","title":{"rendered":"Using Serial Devices in FreeBSD \/ How to set a terminal baud rate"},"content":{"rendered":"<p>Recently I was working on a php command line program that required access to a serial port.<\/p>\n<p>Initially developed under Linux the program was then shifted to it&#8217;s permanent location on a FreeBSD server. This is where we first started having problems. Initially we discovered the server didn&#8217;t have a native serial port. We fixed this using a USB to serial converter\/dongle (FTDI Chipset). This was fine as FreeBSD has the ufdti kernel module. Upon loading the module new devices appears in \/dev<\/p>\n<pre>crw-rw----  1 uucp  dialer    0, 157 Oct  6 08:39 \/dev\/cuaU0\r\ncrw-rw----  1 uucp  dialer    0, 158 Oct  6 08:39 \/dev\/cuaU0.init\r\ncrw-rw----  1 uucp  dialer    0, 159 Oct  6 08:39 \/dev\/cuaU0.lock\r\ncrw-rw-rw-  1 root  wheel     0, 154 Jan  8 10:50 \/dev\/ttyU0\r\ncrw-------  1 root  wheel     0, 155 Oct  6 08:39 \/dev\/ttyU0.init\r\ncrw-------  1 root  wheel     0, 156 Oct  6 08:39 \/dev\/ttyU0.lock<\/pre>\n<p>We attempted to connect to our device using screen (screen \/dev\/ttyU0 115200) and everything worked as expected. We could send AT commands to the device all ok.<br \/>\nWe then stopped screen and ran our php program. It ended up hanging on a fgets call to the serial port. This is really strange we though.<br \/>\nNext we queried the port to find out what baud rate it was set at:<\/p>\n<pre>&gt;stty -f \/dev\/ttyu0\r\nspeed 9600 baud;\r\nlflags: echoe echoke echoctl\r\noflags: tab0\r\ncflags: cs8 -parenb<\/pre>\n<p>Strange we thought as we&#8217;d just connected with screen at 115200. Under linux we use screen to set the baud rate, all other programs accessing the port use the port at 115200. So what had set it back to 9600 baud?<br \/>\nWe tried to use stty to set the speed:<\/p>\n<pre>&gt;stty -f \/dev\/ttyU0 speed 115200<\/pre>\n<pre>&gt;stty -f \/dev\/ttyu0\r\nspeed 9600 baud;\r\nlflags: echoe echoke echoctl\r\noflags: tab0\r\ncflags: cs8 -parenb<\/pre>\n<p>What on earth was happening? We set the speed to 115200 but directly quering the port again indicated it was still at 9600 baud? At this point we were perplexed.<br \/>\nEventually we found the solution. The newer FreeBSD terminal drivers provide the *.init devices, in this case \/dev\/ttyU0.init . These devices indicate the terminal settings to be applied to the terminal when the device is closed. Whilst Linux leaves the device in the same state the last program put the port into, FreeBSD restores the terminals state to what ever is specified in the init file. So a quick command:<\/p>\n<pre>&gt; stty -f \/dev\/ttyU0.init -icanon -isig -echo echoe echok echoke echoctl -icrnl -ixany -imaxbel ignpar -opost -onlcr -oxtabs cs8 -parenb -hupcl clocal<\/pre>\n<p>And then to check:<\/p>\n<pre>&gt; stty -f \/dev\/ttyU0\r\nspeed 115200 baud;\r\nlflags: -icanon -isig -echo echoe echok echoke echoctl\r\niflags: -icrnl -ixany -imaxbel ignpar\r\noflags: -opost -onlcr -oxtabs\r\ncflags: cs8 -parenb -hupcl clocal<\/pre>\n<p>Excellent. The terminal was now configured exactly how we wanted. We ran the program and it worked like a charm!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I was working on a php command line program that required access to a serial port. Initially developed under Linux the program was then shifted to it&#8217;s permanent location on a FreeBSD server. This is where we first started having problems. Initially we discovered the server didn&#8217;t have a native serial port. We fixed<a href=\"https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate\"> <font size=-2>[..more..]<\/font><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,6],"tags":[282,281,441,280,279,283],"class_list":["post-674","post","type-post","status-publish","format-standard","hentry","category-freebsd","category-unisa","tag-baud-rate","tag-baudrate","tag-freebsd","tag-ftdi","tag-serial","tag-stty"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Using Serial Devices in FreeBSD \/ How to set a terminal baud rate - ClearChain<\/title>\n<meta name=\"description\" content=\"Recently I was working on a php command line program that required access to a serial port. Initially developed under Linux the program was then shifted\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin Close\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate\"},\"author\":{\"name\":\"Benjamin Close\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/aef6faa2c32188398139db9270ca1c98\"},\"headline\":\"Using Serial Devices in FreeBSD \/ How to set a terminal baud rate\",\"datePublished\":\"2010-01-08T00:33:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate\"},\"wordCount\":338,\"commentCount\":3,\"keywords\":[\"baud rate\",\"baudrate\",\"FreeBSD\",\"ftdi\",\"serial\",\"stty\"],\"articleSection\":[\"FreeBSD\",\"UniSA\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate\",\"url\":\"https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate\",\"name\":\"Using Serial Devices in FreeBSD \/ How to set a terminal baud rate - ClearChain\",\"isPartOf\":{\"@id\":\"https:\/\/www.clearchain.com\/blog\/#website\"},\"datePublished\":\"2010-01-08T00:33:56+00:00\",\"author\":{\"@id\":\"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/aef6faa2c32188398139db9270ca1c98\"},\"description\":\"Recently I was working on a php command line program that required access to a serial port. Initially developed under Linux the program was then shifted\",\"breadcrumb\":{\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.clearchain.com\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using Serial Devices in FreeBSD \/ How to set a terminal baud rate\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/#website\",\"url\":\"https:\/\/www.clearchain.com\/blog\/\",\"name\":\"ClearChain\",\"description\":\"-= Daily Happenings =-\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.clearchain.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/aef6faa2c32188398139db9270ca1c98\",\"name\":\"Benjamin Close\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/19dca0aa372edfa901b93c556dfda2e78ad4434558fe4d139598e086315d714a?s=96&d=mm&r=pg\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/19dca0aa372edfa901b93c556dfda2e78ad4434558fe4d139598e086315d714a?s=96&d=mm&r=pg\",\"caption\":\"Benjamin Close\"},\"sameAs\":[\"http:\/\/www.clearchain.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using Serial Devices in FreeBSD \/ How to set a terminal baud rate - ClearChain","description":"Recently I was working on a php command line program that required access to a serial port. Initially developed under Linux the program was then shifted","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate","twitter_misc":{"Written by":"Benjamin Close","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate#article","isPartOf":{"@id":"https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate"},"author":{"name":"Benjamin Close","@id":"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/aef6faa2c32188398139db9270ca1c98"},"headline":"Using Serial Devices in FreeBSD \/ How to set a terminal baud rate","datePublished":"2010-01-08T00:33:56+00:00","mainEntityOfPage":{"@id":"https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate"},"wordCount":338,"commentCount":3,"keywords":["baud rate","baudrate","FreeBSD","ftdi","serial","stty"],"articleSection":["FreeBSD","UniSA"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate","url":"https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate","name":"Using Serial Devices in FreeBSD \/ How to set a terminal baud rate - ClearChain","isPartOf":{"@id":"https:\/\/www.clearchain.com\/blog\/#website"},"datePublished":"2010-01-08T00:33:56+00:00","author":{"@id":"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/aef6faa2c32188398139db9270ca1c98"},"description":"Recently I was working on a php command line program that required access to a serial port. Initially developed under Linux the program was then shifted","breadcrumb":{"@id":"https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.clearchain.com\/blog\/posts\/using-serial-devices-in-freebsd-how-to-set-a-terminal-baud-rate#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.clearchain.com\/blog"},{"@type":"ListItem","position":2,"name":"Using Serial Devices in FreeBSD \/ How to set a terminal baud rate"}]},{"@type":"WebSite","@id":"https:\/\/www.clearchain.com\/blog\/#website","url":"https:\/\/www.clearchain.com\/blog\/","name":"ClearChain","description":"-= Daily Happenings =-","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.clearchain.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/aef6faa2c32188398139db9270ca1c98","name":"Benjamin Close","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.clearchain.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/19dca0aa372edfa901b93c556dfda2e78ad4434558fe4d139598e086315d714a?s=96&d=mm&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/19dca0aa372edfa901b93c556dfda2e78ad4434558fe4d139598e086315d714a?s=96&d=mm&r=pg","caption":"Benjamin Close"},"sameAs":["http:\/\/www.clearchain.com"]}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/posts\/674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/comments?post=674"}],"version-history":[{"count":2,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/posts\/674\/revisions"}],"predecessor-version":[{"id":676,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/posts\/674\/revisions\/676"}],"wp:attachment":[{"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/media?parent=674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/categories?post=674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clearchain.com\/blog\/wp-json\/wp\/v2\/tags?post=674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}