Serial port (RS232) Communications
Posted: Wed Jul 25, 2012 6:32 am
I've been trying to get a T4A to talk over serial to an external device via RS232 without luck. For some reason I'm not every receiving any bytes on the T4A.
This is the code in my script file:
On a PC, I'm running the following test code in a C# application:
Does anyone know what I'm doing wrong?
This is the code in my script file:
Code: Select all
namespace Neo.ApplicationFramework.Generated
{
using System;
using System.Drawing;
using Neo.ApplicationFramework.Tools;
using Neo.ApplicationFramework.Common.Graphics.Logic;
using Neo.ApplicationFramework.Controls;
using Neo.ApplicationFramework.Interfaces;
using System.Threading;
using System.IO;
using System.IO.Ports;
public partial class Test
{
static Thread Test_COM = new Thread(new ThreadStart(Test_COM_Thread));
static volatile bool Alive = true;
public static volatile StreamWriter FileIO = null;
public static volatile bool Write = false;
public static volatile string FileName = "";
void IR_Bus_Created(System.Object sender, System.EventArgs e)
{
Test_COM.Name = "Test COM";
Test_COM.Priority = ThreadPriority.BelowNormal;
Test_COM.IsBackground = true;
Test_COM.Start();
}
~Test()
{
Alive = false;
Test_COM.Join();
}
public static bool IsWriting
{
get { return (FileIO != null); }
}
static void Test_COM_Thread()
{
TestPort tst_port = new TestPort();
tst_port.Open();
while (Alive)
{
Thread.Sleep(20);
if (Write && (FileIO == null))
{
FileIO = new StreamWriter(FileName, false, System.Text.Encoding.ASCII);
}
// read port
tst_port.Read();
if ((!Write) && (FileIO != null))
{
FileIO.Close();
FileIO = null;
}
}
if (FileIO != null)
{
FileIO.Close();
FileIO = null;
}
tst_port.Close();
}
public static string ToHex(byte[] data)
{
int i = data.Length;
int j = 0;
char[] hex = new char[i << 1];
for (i--; i >= 0; i--)
{
hex[j] = ToHex((byte)(data[i] >> 4));
j++;
hex[j] = ToHex(data[i]);
j++;
}
return new string(hex);
}
public static char ToHex(byte nibble)
{
nibble &= 0x0F;
if (nibble < 10)
nibble += (byte)'0';
else
nibble += (byte)'A' - 10;
return (char)nibble;
}
}
public class TestPort : IDisposable
{
private SerialPort port = null;
public void Open()
{
if (!IsOpen)
{
// Instantiate the communications port (RS232)
port = new SerialPort("COM1", 38400, Parity.None, 8, StopBits.One);
// Open the port for communications
port.Open();
}
}
public void Close()
{
if (port != null)
{
// Close the port
port.Close();
port = null;
}
}
public void Dispose()
{
Close();
}
public bool IsOpen
{
get { return ((port != null) && (port.IsOpen)); }
}
public void Read()
{
if (!IsOpen) return;
int BytesReceived;
byte[] BusMsg;
BytesReceived = port.BytesToRead;
if (BytesReceived == 0) return;
BusMsg = new byte[BytesReceived];
port.Read(BusMsg, 0, BytesReceived);
if (Test.FileIO != null)
{
Test.FileIO.WriteLine(Test.ToHex(BusMsg));
}
}
}
}
Code: Select all
private void button1_Click(object sender, EventArgs e)
{
try
{
using (SerialPort port = new SerialPort("COM1", 38400, Parity.None, 8, StopBits.One))
{
port.Open();
byte[] data = new byte[] { 1, 2, 3, 4, 5 };
port.Write(data, 0, data.Length);
MessageBox.Show("Message sent");
}
}
catch (Exception ex)
{
MessageBox.Show("ERROR: " + ex.Message);
}
}