Building a demux circuit, programming in c#

Hey guys, I picked a heck of a headache for a first project. The intent is to use an arduino and a bank of 4051's as a demux/channel addressing circuit. I'm using it to address pixels on oled devices. I'm trying to write the program to run the computer side in C#, cause that seemed to have the easiest serial communication methods. So, here's what I need to do.

c# - output a 6 character long string of 1's and 0's, read back the data from the arduino. I've managed to output data, but i can't get it to read it back because i'm doing something wrong. it seems to want to send data out in the form of strings, but read it back as int. If i could output it as an int that'd be great, cause it'd make life easier on the arduino side.

arduino - read in a string, into an array, break it down into 6 separate, '0' or '1's, use an if statement to determine state and write a digital pin high -- if (dP0 == 49) { digitalWrite (devPin0, HIGH);}.

I got it working with single channels, sending out a 1-byte 0 or 1 as a char and checking to see if it was an ascii 1 (49), and then turning on a single channel. Now I need to send out a string of 1's or 0's and have it turn on the appropriate pin. Here's the arduino code.

int dataStreamInt; char dataStreamChar [7]; char dP0; char dP1; char dP2; char pP0; char pP1; char pP2; int devPin0 = 3; // s0 on device select 4051 int devPin1 = 4; // s1 on device select 4051 int devPin2 = 5; // s2 on device select 4051 int pixPin0 = 6; // s0 on pix tree int pixPin1 = 7; // s1 on pix tree int pixPin2 = 8; // s2 on pix tree

void setup() // run once, when the sketch starts { Serial.begin(9600); // init serial

pinMode(devPin0, OUTPUT); // sets the digital pin as output pinMode(devPin1, OUTPUT); pinMode(devPin2, OUTPUT); pinMode(pixPin0, OUTPUT); pinMode(pixPin1, OUTPUT); pinMode(pixPin2, OUTPUT); }

void loop() // run over and over again { // wait for serial input if (Serial.available() > 0) { // read incoming data dataStreamInt =; itoa (dataStreamInt, dataStreamChar, 10); dP0 = dataStreamChar[1]; dP1 = dataStreamChar[2]; dP2 = dataStreamChar[3]; pP0 = dataStreamChar[4]; pP1 = dataStreamChar[5]; pP2 = dataStreamChar[6];

// set output pins

if (dP0 == 49) {digitalWrite (devPin0, HIGH);} else {digitalWrite (devPin0, LOW);} if (dP1 == 49) {digitalWrite (devPin1, HIGH);} else {digitalWrite (devPin1, LOW);} if (dP2 == 49) {digitalWrite (devPin2, HIGH);} else {digitalWrite (devPin2, LOW);} if (pP0 == 49) {digitalWrite (pixPin0, HIGH);} else {digitalWrite (pixPin0, LOW);} if (pP1 == 49) {digitalWrite (pixPin1, HIGH);} else {digitalWrite (pixPin1, LOW);} if (pP2 == 49) {digitalWrite (pixPin2, HIGH);} else {digitalWrite (pixPin2, LOW);}

// display output for testing



I think something's not working in the whole converting int to string thing. That's the most painful mismatch. C# only wants to output as string, and the arduino only wants to input as int. That, or I've got something seriously F'd up.

the C# code

namespace SerialPortTest { public partial class Form1 : Form { string dataStreamInt = "100100"; string dP0 = "0"; string dP1 = "0"; string dP2 = "0"; string pP0 = "0"; string pP1 = "0"; string pP2 = "0";

public Form1() { InitializeComponent();


// section to send data to serial private void Write1_Click(object sender, EventArgs e) { // configure serial port serialPort1.PortName = "COM9"; serialPort1.BaudRate = 9600; serialPort1.DataBits = 8; serialPort1.Parity = System.IO.Ports.Parity.None; serialPort1.StopBits = System.IO.Ports.StopBits.One;

// open the port serialPort1.Open ();

// temp value for dP0 to test serial port com dP0 = "100100";

// write data serialPort1.Write(dataStreamInt);

// close the port serialPort1.Close(); } }

Hey, Locus,

The C# looks fine, although strings dP0 through pP2 seem to be unused.

One problem on the Arduino side is that you are misunderstanding how works. It actually returns the next byte (value 0-255) if one is available in the port buffer, or -1 if not. (In other words, it is not synthesizing binary integers from the text stream.)

I'd suggest a rewrite using a helper function -- perhaps something like this:

byte GetNextByte()
  while (true)
    if (Serial.available())
  return 0;

... in loop()
dP0 = (char)GetNextByte();
dP1 = (char)GetNextByte();
dP2 = (char)GetNextByte();
pP0 = (char)GetNextByte();
pP1 = (char)GetNextByte();
pP2 = (char)GetNextByte();