Category Archives: Micro & Hardware

Microcontrollers, electronics, etc.

Korad KA3005P Serial Programming

korad-ka3005pThis is a cheap lab bench power supply that given its flaws is a surprisingly solid piece of equipment. I wouldn’t call this a precision power supply, but its tolerances and ripple are acceptable for ordinary bench work. (And really, if you’re doing precision work, you’ll invest in a precision supply anyhow.)

There are two methods for communicating with the unit: a serial interface and a proprietary binary USB interface. One may use either the DB-9 connector or USB to access the serial interface.

I wrote a Python wrapper for the serial protocol to encapsulate the various tidbits  of information I’ve encountered on the  Internet. The firmware is buggy and there are various gotchas. Hopefully the Python wrapper will address the worst of the problems one might encounter.

How to Access a Local Node Server Using Websockets

Background

The AngularJS web application that I’m working on runs on a remote server, but needs to access laboratory instruments connected to the local computer that is running the web browser. JavaScript running in the web browser runs in a sandbox and is prohibited from accessing local hardware.

We explored several possibilities of how to work around this and found a fairly simple solution. The local computer runs a small Node.js program to act as glue between the instrument and the local web browser. Node.js communicates with the instrument’s USB serial port using the Node serialport plugin.

Node also runs express to serve up a simple AngularJS web application for diagnostics. We also connect socket.io to the express instance to provide an interactive communication pipeline between the Node.js program and the main web application.

The Problem Space

One of the traditional iron clad security paradigms of web programming is that JavaScript served up from a server cannot access another server. This works for nearly all web sites, but there are instances where being able to share resources across servers is desirable. For example, if our web app can communicate with local laboratory instruments it’s a big win for my client.

The Approach

The W3C has published Cross-Origin Resource Sharing specifications which provide a standardized method for doing this. To implement this, the non-origin server (in our case, the Node.js server) has to provide HTTP headers to the web browser indicating that it will accept the cross-origin request.

If these headers are missing, the web browser will not complete the HTTP request. For example, Firefox 29 — in its debug console — will report

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8888/socket.io/1/?t=1402443850582. This can be fixed by moving the resource to the same domain or enabling CORS.

This means that the web browser is denying access to Node.js running on localhost because (a) it is a different destination than the original server (the origin) and (b) the Node.js server is not granting permission for the cross-origin request.

Thus the problem boils down to coaxing socket.io to provide those headers when the web browser connects.

The Snag

I have not been able to get this to work on socket.io version 1.0 and higher. To avoid wasting time I reverted to pre-1.0 thus:

npm install --save socket.io@"<1.0"

In the Node.js program’s main app.js, I added one line to allow connections from any cross-origin server (see line 2 below). Note that this is development code running on an isolated network inaccessible from the Internet. One should think hard before leaving this open to all comers.

var io = require('socket.io').listen(server);
io.set('origins', '*:*');
server.listen(8888);

If you look in the socket.io source file ./lib/manager.js you’ll see the lines:

  if (origin) {
    // https://developer.mozilla.org/En/HTTP_Access_Control
    headers['Access-Control-Allow-Origin'] = origin;
    headers['Access-Control-Allow-Credentials'] = 'true';
  }

This may prove useful during debugging if adding the set('origins' ... call doesn’t work as expected.

Unanswered Questions

This solution doesn’t appear to work for serialport.io version 1.0 and higher.

References

Cross-Origin Resource Sharing official W3C documentation.

Using CORS introduction to CORS.

Enable Cross-Origin Resource Sharing sample code.

Socket.io doesn’t set CORS header(s) on Stackoverflow

 

 

 

 

5V to 3.3V Logic Level Shifting Stragety Notes

Here are some notes on different level-shifting strategies to convert from 5V to 3.3V circuits. As usual, this is for quick-and-dirty stuff.

I searched the Internet, found a couple of appealing strategies, and tried them out. I connected them to an Arduino output that cycled between high and low, and connected an oscilloscope to the output.

Strategy 1: Ordinary Diode

strategy1

This is a very simple strategy with few parts that I found on Rob Faludi’s page XBee Level Shifting.  This is what I saw on the scope:

DS1ET145103448_1

It’s been too many years since I’ve been in school to remember my analogue electronics, so at first this surprised me. Top to bottom is approximately 3.3V, but the huge rise and fall times were not what I expected.

I suspected that the 10k resistor was the culprit, (e.g. see Effects of Varying I2C Pull-Up Resistors), and threw in a 220 ohm resistor as an opposite extreme. The results were much more promising. The falling edge is very sloppy, but the rising edge is pretty crisp.

normal-diode-02-zoom

Strategy #2: Zener Diode

This is a variation on the first, but relying on the zener to limit voltage to 3.3V. The 10k ohm resistor I found on more than one site, so I tried it first of all.

strategy1

This produced the following on the scope:

DS1ET145103448_3

This is atrocious with a 10k resistor in there. Switching to the other extreme, the 220 ohm resistor, I got the following beautiful display. The falling edge has some roundoff on the bottom, but it demonstrates that the zener diode’s a viable quick and dirty level shifter.

DS1ET145103448_4

Strategy 3: Bus Transceiver

The last strategy I tried uses a 74LVC245 bus transceiver.

strategy1

Using an actual IC is a little heavy-handed for a single line, but the results were solid.

DS1ET145103448_5

Strategy #4: Resistor Voltage Divider

Because 3.3V is 2/3 of 5V, it’s easy to create a voltage divider out of resistors.

strategy1

Using 10k resistors produced the familiar shape:

DS1ET145103448_00

While dropping down to the other extreme, 200 ohms, produced a solid waveform:

DS1ET145103448_0

Demonstrating that if you need a dirt simple level shifter, three resistors may do in a pinch. However! Danger! Danger! The impedance of the surrounding circuitry will wreck havoc with the voltage divider. I really do not recommend this method.

Addendum

I have not tried these yet, but there are some notes on the Arduino Playground site here, and this document here.

Updates

2013-02-14
Uploaded cleaner images, added fourth strategy.
2014-01-18
Created addendum.

Installing Rigol DS 1052E Ultrascope Software on Windows 7

The software that comes on CD in useless. The following steps seemed to be the pieces that worked. However, I’m not entirely sure since the entire experience was a bit of a hairball.

  • Go to the National Instruments website and install the latest VISA installer. (At the time of writing is version 5.3.)
  • Go to the Rigol website and install the UltraSigma software. I’m not sure this is needed. However, if will show whether the oscilloscope is recognized.
  • Go to the Rigol website and install the Ultrascope for DS1000E series software.