I'm developing an application which communicates with my electronic circuit via Port 4. I can send data from the PC to the circuit without a problem; I then get data back from the circuit. However, when I try to get data from my circuit for the second time, I receive incorrect data from it. Any pointers on how to solve this?
This is C# code:
byte[] Sent_Byte = {1,2,3,4,5,6};
byte[] Received_Byte = new byte[10];
private void button_sendData_Click(object sender, EventArgs e)
{
// I send this data because the circuit is ready to get data
serialPort1.Write("G");
serialPort1.Write(Sent_Byte, 0, 6);
}
private void button_getData_Click(object sender, EventArgs e)
{
// I send this data because the circuit is ready to send data
serialPort1.Write("A");
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
serialPort1.Read(Received_Byte, 0, 10);
}
This is Arduino code:
char Control_OP=0;
char Received_Data[6];
byte Sent_Data[10];
void loop()
{
while(Serial.available())
{
Control_OP = Serial.read(); // determines whether receiving data or sending data
if(Control_OP=='G') // receiving data
{
Number=Serial.readBytes(Received_Data,6);
}
else if(Control_OP=='A') // sending data
{
Serial.write(Sent_Data,10);
}
}
}
I found the solution.
I changed this code
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
serialPort1.Read(Received_Byte, 0, 10);
}
into this one
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
for (int i = 0; i <Received_Byte.Length; i++)
{
Received_Byte[i] = Convert.ToByte(serialPort1.ReadByte());
}
}
Thanks everyone for helping me
Related
I use NAudio to record datas from microphone, then i need to playback audio without writing a wav file yet.
Play / Pause / Stop work well, but how can I set back the position to the beginning of bwp and play back from start the audio.
I can't write a wav file yet, because I need to play back the file, navigate throught it with a slider, then erase the end of buffer with new recorded datas, then save the modified file.
private void btn_Start_Click(object sender, EventArgs e)
{
if (sourceList.SelectedItems.Count == 0)
return;
int deviceNumber = sourceList.SelectedItems[0].Index;
wo = new WaveOutEvent();
wi = new WaveIn();
wi.DeviceNumber = deviceNumber;
wi.WaveFormat = new WaveFormat(44100, WaveIn.GetCapabilities(deviceNumber).Channels);
wi.DataAvailable += new EventHandler<WaveInEventArgs>(wi_DataAvailable);
bwp = new BufferedWaveProvider(wi.WaveFormat);
bwp.BufferDuration = new TimeSpan(1, 0, 0);
bwp.DiscardOnBufferOverflow = false;
wi.StartRecording();
}
private void wi_DataAvailable(object sender, WaveInEventArgs e)
{
bwp.AddSamples(e.Buffer, 0, e.BytesRecorded);
}
private void btn_Stop_Click(object sender, EventArgs e)
{
wi.StopRecording();
wo.Init(bwp);
}
private void btn_InitWaveOut_Click(object sender, EventArgs e)
{
wo.Play();
}
private void btn_StopWaveOut_Click(object sender, EventArgs e)
{
wo.Stop();
}
private void btn_PauseWaveOut_Click(object sender, EventArgs e)
{
wo.Pause();
}
The BufferedWaveProvider is not designed to support repositioning. If you want that you should make your own IWaveProvider derived class that holds onto all bytes received to allow repositioning. Obviously you'd want to be careful about how much memory you use up as audio data can grow quite large over time.
I'm currently working on a C# application and where I read Serial Data send through the USB port, where this data is to be shown on a textbox then eventually into a database. The code I have right now has it refresh to read serial data coming in every 2 seconds, but I cannot get the data onto the textBox. I'm fairly new to C# development so I am unsure as to what the best way to output my data onto a textbox or how to fix my problem in the first place. My code is below.
public partial class Form1 : Form
{
SerialPort mySerialPort = new SerialPort("COM3");
OleDbConnection connection = new OleDbConnection();
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.RtsEnable = true;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
if (mySerialPort.IsOpen == false)
{
mySerialPort.Open();
}
}
public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
//gets data values from serial port
SerialPort sp = (SerialPort)sender;
string data = sp.ReadExisting();
string time = GetTimestamp(DateTime.Now);
int indata;
long timeStamp;
//parses strings to integers
indata = Int32.Parse(data);
timeStamp = Int64.Parse(time);
//writes to console
Console.WriteLine(indata);
Console.WriteLine(timeStamp);
//writes to text box
textBox1.Text = data;
Thread.Sleep(2000);
}
private void button3_Click(object sender, EventArgs e)
{
mySerialPort.Close();
}
private void label1_Click(object sender, EventArgs e)
{
}
public static string GetTimestamp(DateTime value)
{
return value.ToString("yyyyMMddHHmmss");
}
private void label1_Click_1(object sender, EventArgs e)
{
}
}
The event DataReceived is handled with the method DataReceivedHandler on a different thread than the control textBox1 is created. So when running your program you should have get a System.InvalidOperationException. Which states exactly what I just described.
To get out of this dilemma you can use the Control.BeginInvoke method which would:
Execute the specified delegate asynchronously on the thread that the control's underlying handle was created on.
Basically it would drag the piece of code down to the thread that cretated the control. In this case the main thread. This is how you would use it:
//writes to text box
textBox1.BeginInvoke(new Action(() => { textBox1.Text = data; }));
I hope it helps.
What I'm trying to do is send 1 in ASCII value, my mk waiting for that char.
But when I press a button nothing happens. But When I send a byte to toggle a PORT, everything is works. SO I wonder how to send in ASCII, 1 value.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) // Here i send a byte to MK
{
// serialPort1.RtsEnable = true; serialPort1.DtrEnable = true;
// var content = new List<byte>();
// content.AddRange(Encoding.ASCII.GetBytes("1"));
// content.Add(3); // ASCII ETX
//byte[] buffer = content.ToArray();
// serialPort1.Write(buffer, 0, buffer.Length);
serialPort1.Write("1");
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e) // choosing a right com port
{
serialPort1.PortName = textBox1.Text;
serialPort1.BaudRate = Convert.ToInt32(textBox2.Text);
}
string rs;
byte re;
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) // Da
{
try
{
//rs = serialPort1.ReadByte();
//re = Convert.ToByte(serialPort1.ReadByte());
rs = serialPort1.ReadExisting();
// System.Text.Encoding.ASCII.GetString(new[] { re });
this.Invoke(new EventHandler(type));
}
catch (System.TimeoutException) { }
}
void type(object s,EventArgs e) // receive data
{
textBox4.Text += rs;
}
private void button3_Click(object sender, EventArgs e) // OPen port
{
serialPort1.Open();
}
private void button4_Click(object sender, EventArgs e) // Close port
{
serialPort1.Close();
}
}
You can write a string to the serial port in that way: the string must contain only ASCII characters in the range 0x00 to 0x7F since the ASCII encoding is used by default (you can change the encoding by setting the SerialPort.Encoding property).
As others have said in the comments, you need to append the ETX character to match the commented-out code, which you can do as follows:
serialPort1.Write("1\x03");
Or, more generally, if you want to build a string containing control characters, you can do something like:
const char STX = '\x02';
const char ETX = '\x03';
... etc, other control characters you want to use
...
var s = String.Format("1{0}", ETX);
serialPort1.Write(s);
The following code is called several times to read data from a serial port in response to different commands sent to an attached chip & pin terminal.
However it fails to report any further responses after the 1st command.
Although there is data there, for the following commands, is not picked up or read.
How can I fix that?
I'm using ReadExisting serial port property.
private void MySerialReader()
{
ip_serialport.Open();
ip_serialport.DataReceived += serialPort_DataReceived;
//ip_serialport.Close(); //This causes the problems
}
public string RxString;
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
RxString = ip_serialport.ReadExisting();
this.Invoke(new EventHandler(DisplayText_RX)); //<<-- ????!!!
}
private void DisplayText_RX(object sender, EventArgs e)
{
RX_Box.Items.Add(RxString);
}
I think if you use timer then you can read remaining data
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
RxString = ip_serialport.ReadExisting();
this.Invoke(new EventHandler(DisplayText_RX));
}
private void DisplayText_RX(object sender, EventArgs e)
{
RX_Box.Items.Add(RxString);
if(RxString!="")
timer.start();
}
void timer_Tick(object sender, EventArgs e)
{
if(serialport.isopen)//check for open
{
RxString = ip_serialport.ReadExisting();
this.Invoke(new EventHandler(DisplayText_RX));
}
timer.stop();
}
I have two Arduinos that allow messages exchange via Serial Port using Serial Monitor.
If I use Serial Monitor in both sides everything works fine. If I use my C# application nothing happens. I tried to send from Serial Monitor for C# App and it works but not the reverse.
// ...
comPort1.Open();
// ...
private void comPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
this.Invoke(new EventHandler(processData));
}
private void processData(object sender, EventArgs e)
{
string inData = comPort1.ReadExisting();
msgBoxLog.AppendText(inData);
}
// ...
private void sendButton_Click(object sender, EventArgs e)
{
string my_str = "my string";
msgBoxLog.AppendText(msgBox.Text + my_str);
comPort1.Write(msgBox.Text);
}
RtsEnable and DtrEnable are both Enabled
Well, with Console.Write(msgBox.Text); I realized it was just a silly problem, I was not sending msgBox.Text as I wanted. It should be:
private void sendButton_Click(object sender, EventArgs e)
{
string my_str = "my string";
comPort1.Write(msgBox.Text); //Console.Write(msgBox.Text);
msgBoxLog.AppendText(msgBox.Text + my_str);
}