using System;
using System.Collections;
using System.IO;
using System.IO.Ports;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
namespace ndSerialTestHA7S
{
public class Program
{
private const byte R = 82;
private const byte S = 83;
private const byte W = 87;
private const byte s = 115;
private const byte CR = 13;
private static SerialPort _serialPort = null;
private static Queue _serialQueue = new Queue();
private static Object _lockQueue = new Object();
public static AutoResetEvent unblockRead = new AutoResetEvent(false);
public const int _readTimeout = 3000;
public static void Main()
{
_serialPort = new SerialPort(SerialPorts.COM1);
_serialPort.BaudRate = 9600;
_serialPort.DataBits = 8;
_serialPort.StopBits = StopBits.One;
_serialPort.Parity = Parity.None;
_serialPort.Handshake = Handshake.None;
_serialPort.DataReceived += new SerialDataReceivedEventHandler(inboundSerial);
byte[] devAddress = new byte[45];
try
{
_serialPort.Open();
writeSerial(R);
readSerial(devAddress);
log(devAddress);
writeSerial(S);
readSerial(devAddress);
log(devAddress);
writeSerial(s);
readSerial(devAddress);
log(devAddress);
writeSerial(s);
readSerial(devAddress);
log(devAddress);
int i = 1;
}
catch (SystemException se)
{
Debug.Print("Ha7s: " + se.Message);
}
}
private static void inboundSerial(object sender, SerialDataReceivedEventArgs data)
{
while (_serialPort.BytesToRead > 0) // While there is data on the serial bus
{
lock (_lockQueue)
{
_serialQueue.Enqueue((byte)_serialPort.ReadByte());
}
}
unblockRead.Set();
}
private static void writeSerial( byte[] data, int len )
{
for (int i = 0; i < len; i++)
_serialPort.WriteByte(data[i]);
}
private static void writeSerial(byte data)
{
_serialPort.WriteByte(data);
}
private static void log( byte[] buffer )
{
string message = "Log: ";
message += new String(Encoding.UTF8.GetChars(buffer));
Debug.Print(message);
}
private static void zero( byte[] buffer, int l)
{
for (int i = 0; i < l; i++)
buffer[i] = 0;
}
private static int readSerial(byte[] data, byte termChar = CR)
{
zero(data, 20);
int bytesRead = 0;
bool keepChecking = true;
while (keepChecking && (_serialQueue.Count > 0)) // Have something in the queue
{
lock (_lockQueue) // grab lock while dequeing
{
data[bytesRead] = (byte)_serialQueue.Dequeue(); // grab a byte from the queue
}
if (data[bytesRead] == termChar) // Hit terminating character, stop grabbing bits
keepChecking = false;
bytesRead++;
}
while (keepChecking)
{
bool signaled = unblockRead.WaitOne(_readTimeout, false);
if (!signaled) // Timed out, exit loop
keepChecking = false;
else
{
while (keepChecking && (_serialQueue.Count > 0)) // Have something in the queue
{
lock (_lockQueue) // grab lock while dequeing
{
data[bytesRead] = (byte)_serialQueue.Dequeue(); // grab a byte from the queue
}
if (data[bytesRead] == termChar) // Hit terminating character, stop grabbing bits
keepChecking = false;
bytesRead++;
}
}
}
return bytesRead;
}
}
}
I think it must be something in my setup, do I need to have a ground wire added somewhere for the plus2? (as opposed to a plus?) I have tried two plus 2 devices, two different HA7S devices, multiple temp sensors, and always get indeterminate results from the serial queries. Its like something is stepping on the UART queue, or inbound characters. Not at all like the behavior on the plus, very timing dependent. Started off with a fairly simple program to just test the serial commands and can't get any good, or consistent results. Even flashed one unit back to 4.2.2 and have the same issues. Entire test program in message.
Would really appreciate any pointers. Thanks.