i need to test whether serial port sending /receiving data , i have two c# winforms applications.
one application is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel=Microsoft.Office.Interop.Excel;
using System.IO;
using System.Threading;
using System.IO.Ports;
namespace Hand_Held_Data_Transporter
{
public partial class Form1 : Form
{
int[] stream_buffer;
String final = null;
String upload1 = null, upload2 = null, upload3 = null, upload4 = null, upload5 = null, upload6 = null, upload7 = null;
public Form1()
{
InitializeComponent();
progressBar1.Enabled = false;
progressBar2.Enabled = false;
serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
}
private void Browse_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Filter = "All Files(*.*)|*.*";
if (fdlg.ShowDialog() == DialogResult.OK)
{
textBox1.Text = fdlg.FileName;
File.ReadAllText(textBox1.Text);
}
}
private void Upload_Click(object sender, EventArgs e)
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(textBox1.Text, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
progressBar1.Visible = true;
int k = 100 / rowCount;
for (int i = 1; i <= rowCount; i++)
{
for (int j = 1; j <= colCount; j++)
{
if (j == 1)
upload1 = xlRange.Cells[i, j].Value.ToString();
if (j == 2)
upload2 = xlRange.Cells[i, j].Value.ToString();
if (j == 3)
upload3 = xlRange.Cells[i, j].Value.ToString();
if (j == 4)
upload4= xlRange.Cells[i, j].Value.ToString();
if (j == 5)
upload5 = xlRange.Cells[i, j].Value.ToString();
if (j == 6)
upload6= xlRange.Cells[i, j].Value.ToString();
if (j == 7)
upload7 = xlRange.Cells[i, j].Value.ToString();
final = "$ POST," + upload1 + "," + upload2 + "," + upload3 + "," + upload4 + "," + upload5 + "," + upload6 + "," + upload7 + " # ";
}
if (serialPort1.IsOpen)
serialPort1.Write(final);
Thread.Sleep(1000);
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
MessageBox.Show(final);
if (progressBar1.Value < 100)
{
Thread.Sleep(1000);
progressBar1.Value += k;
}
}
progressBar1.Value = 100;
}
private void Form1_Load(object sender, EventArgs e)
{
progressBar1.Visible = false;
progressBar2.Visible = false;
serialPort1.Open();
string[] ArrayComPortsNames = null;
int index = -1;
string ComPortName = null;
ArrayComPortsNames = SerialPort.GetPortNames();
do
{
index += 1;
comboBox1.Items.Add(ArrayComPortsNames[index]);
}
while (!((ArrayComPortsNames[index] == ComPortName)
|| (index == ArrayComPortsNames.GetUpperBound(0))));
Array.Sort(ArrayComPortsNames);
if (index == ArrayComPortsNames.GetUpperBound(0))
{
ComPortName = ArrayComPortsNames[0];
}
comboBox1.Text = ArrayComPortsNames[0];
}
and another application is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace handheld_Responce
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
serialPort1.Open();
serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
}
private void send_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (serialPort1.IsOpen)
{
String RecievedData;
RecievedData = serialPort1.ReadExisting();
if (!(RecievedData == ""))
{
textBox2.Text += RecievedData;
}
}
}
}
}
plz tell me how can i communicate both applications
[edit]Code block added - OriginalGriff[/edit]