# \[dev\] i2c openSync seems to work async way

**URL:** <https://discourse.nodered.org/t/dev-i2c-opensync-seems-to-work-async-way/51017>\
**Category:** Developing Nodes\
**Created:** [14 September 2021 14:19 UTC](https://discourse.nodered.org/t/dev-i2c-opensync-seems-to-work-async-way/51017 "2021-09-14T14:19:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [14 September 2021 14:19 UTC](https://discourse.nodered.org/t/dev-i2c-opensync-seems-to-work-async-way/51017/1 "2021-09-14T14:19:17Z")

</div>

I have a big problem:

- the compoment I'm developing / fixing is almost finished, but the error handling does not work the proper way.

I've opened a ticket at the i2c developer's fork, but I'm not sure if it's a component error, or NR problem?

> <https://github.com/fivdi/i2c-bus/issues/105>
>
> Hi,
> 
> I'm fixing / enhacing currently an MPC chip node for NodeRed: https://git…hub.com/PizzaProgram/mcp23017chip
> I'm inserting lot's of try .. catch lines to make the old code of @willhey stable.
> 
> \# But it seems, the code runs a few lines more, and does not stop at .openSync error !
> 
> \`\`\`javascript
> try {
> if (log2consol) console.log("myTimer: opening bus... Time: " + new Date( \_readTime ).toISOString().slice(11, -1) );
> let \_aBus = null;
> 
> // start async error handling: \<\< does not work either
> // this.on('error', function( i2cErr ) { timerErrorHandler(i2cErr) } );
> let ipA;
> let ipB;
> 
> \_aBus = i2cModule.openSync(globalChipNode.busNum + 999); // \<\< FORCING an ERROR ... should stop at this line ! 
> if (\_aBus) { // \<\< 2. problem: \_aBus is not null, even, if it could not open the bus !
> if (log2consol) console.log("OK. Bus Opened. Now reading A+B banks... Typeof \_aBUS:" + typeof(\_aBus));
> \_processState = 1;
> ipA = \_aBus.readByteSync(\_addr, 0x12);
> ipB = \_aBus.readByteSync(\_addr, 0x13);
> if (log2consol) console.log("Read success ipA=" + ipA + " ipB=" + ipB);
> 				
> \_processState = 3;
> \_aBus.closeSync();
> ...
> \`\`\`
> 
> 2. problem: \_aBus is not null, even, if it could not open the bus !

---

<div class="post-metadata">

**Author:** ![PizzaProgram](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pizzaprogram/32/19431_2.png) [@PizzaProgram](https://discourse.nodered.org/u/PizzaProgram)\
**Post date:** [17 September 2021 08:10 UTC](https://discourse.nodered.org/t/dev-i2c-opensync-seems-to-work-async-way/51017/2 "2021-09-17T08:10:59Z")

</div>

The i2c code developer answered: [.openSync() works async way · Issue #105 · fivdi/i2c-bus · GitHub](https://github.com/fivdi/i2c-bus/issues/105#issuecomment-921191652)

As it turns out, the `openSync` command does not do anything, just creates an object.  
The i2c bus is opened **only at the first attempt to write** a byte to the bus.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [1 October 2021 08:11 UTC](https://discourse.nodered.org/t/dev-i2c-opensync-seems-to-work-async-way/51017/3 "2021-10-01T08:11:55Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
