{"id":645,"date":"2016-11-23T16:28:20","date_gmt":"2016-11-23T16:28:20","guid":{"rendered":"https:\/\/ibex.tech\/windows-iot\/?p=151"},"modified":"2016-11-23T16:28:20","modified_gmt":"2016-11-23T16:28:20","slug":"rpi-using-the-i2c-interface","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/uwp-programming-in-c\/i2c\/rpi-using-the-i2c-interface","title":{"rendered":"RPi Using The I2C Interface"},"content":{"rendered":"\n<p>See the <a href=\"https:\/\/ibex.tech\/windows-iot\/windows-iot\/programming-in-c\/io-pins\/rpi-io-pins\">IO Pins page here<\/a> for any extensions needed by Visual Studio and to enable the IO pins.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Namespaces<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using Windows.Devices.Enumeration;\nusing Windows.Devices.I2c;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Initialise I2C Port For A Device<\/h4>\n\n\n\n<h4 class=\"wp-block-heading\">Write Bytes<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tbyte[] I2cWriteBuffer;\n\n\t\/\/Write to I2C device\n\tI2cWriteBuffer = new byte[] { 0x01, 0x02 };\n\tOurI2cPort.Write(I2cWriteBuffer);\t\t\t\t\t\t\/\/Writes I2cWriteBuffer.Length bytes\n\n\t\/\/Another way:\n\tOurI2cPort.Write(new byte[] { 0x01, 0x02 });\t\t\t\t\t\t\/\/Writes I2cWriteBuffer.Length bytes<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Write Then Read Bytes<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tbyte[] I2cWriteBuffer;\n\tbyte[] I2cReadBuffer;\n\n\t\/\/Write then read I2C device\n\tI2cWriteBuffer = new byte[] {0x01, 0x02};\n\tI2cReadBuffer = new byte[3];\n\tOurI2cPort.WriteRead(I2cWriteBuffer, I2cReadBuffer);    \/\/Writes I2cWriteBuffer.Length bytes, then does a restart and reads I2cReadBuffer.Length bytes\n\n\t\/\/Another way:\n\tI2cReadBuffer = new byte[1];\n\tOurI2cPort.WriteRead(new byte[] { 0x00 }, I2cReadBuffer);\n\t\/\/ = I2cReadBuffer;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Read Bytes<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tbyte[] I2cReadBuffer;\n\n\t\/\/Just read from I2C device\n\tI2cReadBuffer = new byte[4];\n\tOurI2cPort.Read(I2cReadBuffer);                         \/\/Reads I2cReadBuffer.Length bytes<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Disposing Of A I2C Device Object<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tOurI2cPort.Dispose();<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>See the IO Pins page here for any extensions needed by Visual Studio and to enable the IO pins. Namespaces Initialise I2C Port For A Device Write Bytes Write Then Read Bytes Read Bytes Disposing Of A I2C Device Object<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[],"class_list":["post-645","post","type-post","status-publish","format-standard","hentry","category-i2c"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/645","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=645"}],"version-history":[{"count":0,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/645\/revisions"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}