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;
using System.IO.Ports;
using System.Threading;
namespace serial_port_read_write_data
{
public partial class Form1 : Form
{
static string file;
SerialPort _serialPort = null;
List<int> list = new List<int>();
AutoResetEvent eventAutoReset = null;
SerialDataReceivedEventHandler eventSerialData = null;
delegate void SetTextCallback(string text);
public Form1()
{
InitializeComponent();
}
private void btnbrowse_Click(object sender, EventArgs e)
{
ofdbrowsefile.Filter = "wav File(*.wav)|*.wav";
if (ofdbrowsefile.ShowDialog() == DialogResult.OK)
{
file = ofdbrowsefile.FileName;
lblfirstfile.Text = Path.GetFileName(file);
}
else
{
MessageBox.Show("Incorrect file !"); return;
}
}
private void btnwavfilewrite_Click(object sender, EventArgs e)
{
/* if (stringComparer.Equals("quit", message))
{
_continue = false;
}
else
{*/
try
{
if (file != null)
{
int Position = 0, i = 0, readbyte = 0, headersize = 0;
byte[] bytes = new byte[4];
FileStream fspcmfl = new FileStream(file, FileMode.Open, FileAccess.Read);
if (fspcmfl == null)
return;
byte[] RpcmwholeFile = new byte[fspcmfl.Length];
int length = fspcmfl.Read(RpcmwholeFile, 0, RpcmwholeFile.Length);
int length1 = Convert.ToInt32(fspcmfl.Length);
fspcmfl.Seek(16, 0);
fspcmfl.Read(bytes, 0, 4);
fspcmfl.Close();
int Subchunk1Size = BitConverter.ToInt32(bytes, 0);
if (Subchunk1Size < 16)
MessageBox.Show("This is not a valid wav file");
else
switch (Subchunk1Size)
{
case 16:
headersize = 44;
break;
case 18:
headersize = 46;
break;
default:
MessageBox.Show("Header contains extra data and is larger than 46 bytes");
break;
}
Position = headersize;
length = length - headersize;
pgbwavfilewrite.Maximum = length1;
pgbwavfilewrite.Minimum = 0;
while (length > 0)
{
if (length > 255)
{
byte[] bReadConfigToWrite = new byte[255];
bReadConfigToWrite[0] = (int)'W';
bReadConfigToWrite[1] = 255;
bReadConfigToWrite[2] = (int)'M';
for (i = 3; i < 255; Position++)
{
bReadConfigToWrite[i] = RpcmwholeFile[Position];
i++;
pgbwavfilewrite.Value = Position;
lablperwavwrite.Text = (pgbwavfilewrite.Value * 100 / pgbwavfilewrite.Maximum).ToString() + "%";
length--;
}
_serialPort.Write(bReadConfigToWrite, 0, bReadConfigToWrite.Length);
// string s = _serialPort.ReadExisting();
/* bool res = false;
res = eventAutoReset.WaitOne(500, false);
if (res == true)
{
//res = false;
}*/
// bReadConfigValuesCopy = bReadConfigToWrite;
}
else if (length <= 255)
{
byte[] bReadConfigToWrite = new byte[length + 3];
bReadConfigToWrite[0] = (int)'W';
bReadConfigToWrite[1] = 3;
bReadConfigToWrite[2] = (int)'M';
readbyte = length + 3;
for (i = 3; i < readbyte; Position++)
{
bReadConfigToWrite[i] = RpcmwholeFile[Position];
i++;
pgbwavfilewrite.Value = Position + 1;
lablperwavwrite.Text = (pgbwavfilewrite.Value * 100 / pgbwavfilewrite.Maximum).ToString() + "%";
length--;
}
_serialPort.Write(bReadConfigToWrite, 0, bReadConfigToWrite.Length);
}
Thread.Sleep(500);
}
string f = Path.GetFileName(file);
if (f == lblfirstfile.Text)
lblfirstfile.ForeColor = Color.Green;
else if (f == lblsecndfile.Text)
lblsecndfile.ForeColor = Color.Green;
}
else
{
MessageBox.Show("Please select pcm file !"); return;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
// }
}
private void Form1_Load(object sender, EventArgs e)
{
gbapproachfiles.Enabled = true;
string[] sPortNames = SerialPort.GetPortNames();
if (sPortNames == null)
return;
foreach (string sPort in sPortNames)
{
cmbPort.Items.Add(sPort);
}
cmbPort.SelectedIndex = 0;
}
delegate void DelegateDisplay(byte[] aa);
void serialPort_DataReceived(object s, SerialDataReceivedEventArgs e)
{
try
{
int bytes = _serialPort.BytesToRead;
byte[] byte_buffer = new byte[bytes];
_serialPort.Read(byte_buffer, 0, bytes);
Display(byte_buffer);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void Display(byte[] inputData)
{
textBox1.Invoke(new DelegateDisplay(showdata), inputData);
}
public void showdata(byte[] a)
{
string result = Encoding.UTF8.GetString(a);
textBox1.Text += result;
}
private void btnConnectCOM_Click(object sender, EventArgs e)
{
_serialPort = new SerialPort();
// Allow the user to set the appropriate properties.
_serialPort = new SerialPort(cmbPort.Text, Convert.ToInt32(cmbBaudRate.Text));
_serialPort.DataBits = 8;
_serialPort.Encoding = ASCIIEncoding.ASCII;
_serialPort.Handshake = Handshake.None;
_serialPort.Parity = Parity.None;
_serialPort.StopBits = StopBits.One;
_serialPort.ReceivedBytesThreshold = 2;
// Set the read/write timeouts
_serialPort.ReadTimeout = 10000;
_serialPort.Open();
if (_serialPort.IsOpen)
{
btnConnectCOM.Enabled = false;
}
eventSerialData = new SerialDataReceivedEventHandler(serialPort_DataReceived);
_serialPort.DataReceived += eventSerialData;
eventAutoReset = new AutoResetEvent(false);
// _serialPort.DataReceived += serialPort_DataReceived;
// objSerial.DataReceived += eventSerialData;
// eventAutoReset = new AutoResetEvent(false);
}
private void btnDisconnectCOM_Click(object sender, EventArgs e)
{
if (_serialPort.IsOpen)
{
_serialPort.Close();
_serialPort.Dispose();
btnConnectCOM.Enabled = true;
btnDisconnectCOM.Enabled = false;
}
}
private void btnbrwsesecndfile_Click(object sender, EventArgs e)
{
string fs1 = "D://Projects/test1.txt";
int i = 0,j=0;
FileStream fspcmfl = new FileStream(fs1, FileMode.Open, FileAccess.Read);
byte[] bReadWholeFile = new byte[fspcmfl.Length];
int iCountOfRead = 0;
pgbwavfilewrite.Maximum = bReadWholeFile.Length + 3; ;
pgbwavfilewrite.Minimum = 0;
iCountOfRead = fspcmfl.Read(bReadWholeFile, 0, bReadWholeFile.Length);
byte[] bReadConfigToWrite = new byte[bReadWholeFile.Length+6];
bReadConfigToWrite[0] = (int)'W';
bReadConfigToWrite[1] = 32;
bReadConfigToWrite[2] = (int)'M';
for (i = 3; i < bReadWholeFile.Length+3; i++)
{
bReadConfigToWrite[i] = bReadWholeFile[j];
j++;
pgbwavfilewrite.Value = i+1;
lablperwavwrite.Text = (pgbwavfilewrite.Value * 100 / pgbwavfilewrite.Maximum).ToString() + "%";
}
bReadConfigToWrite[i] = (int)'W';
bReadConfigToWrite[i+1] = 3;
bReadConfigToWrite[i+2] = (int)'E';
_serialPort.Write(bReadConfigToWrite, 0, bReadConfigToWrite.Length);
bool f = false;
eventAutoReset.Set();
Thread.Sleep(1000);
f = eventAutoReset.WaitOne(1000, false);
eventAutoReset.Set();
Thread.Sleep(1000);
// Thread.Sleep(4000);
/*List<string> receivedata = new List<string>();
string s = _serialPort.ReadExisting();
receivedata.Add(s);
listBox1.DataSource = receivedata;*/
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Comments
Post a Comment