|
Hi Luc,
I tried what you suggested and it still isn't working, its behavior remains unchanged. I also tried sending the serial data as a "(10)" instead of a "(13)" and that did not work either, it actually wouldn't read anything with the 10. Should I try and send a different ascii character or stick with the 13?
|
|
|
|
|
I suggest you validate the incoming bytes somehow. I see basically two ways to do that:
1.
use a terminal emulator (such as good old HyperTerminal) and see what comes in.
2.
better, use SerialPort class in binary mode, basically something along these lines:
SerialPort port=new SerialPort();
...
...
Thread.Sleep(5000);
int DIM=300;
byte[] bytes=new byte[DIM];
int n=port.Read(bytes,0,DIM);
for(int i=0; i<n; i++) {
byte b=bytes[i];
char c=b;
log("bytes["+i+"]=0x"+b.ToString("X2")+"="+c);
}
where log is a simple logging method, could be:
public static void log(string s) {
Console.WriteLine(s);
}
|
|
|
|
|
Hi Luc,
Here is what it looks like with the OE program, so it is working correctly. There has to be something I'm missing here?
<
>
http://img221.imageshack.us/img221/4316/serialterminal.png[^]
Here is my code after all kinds of tweaks or attempts to get this to work, if you'd like anything else just let me know. Thanks!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace SimpleSerial
{
public partial class Form1 : Form
{
string RxString;
public Form1()
{
InitializeComponent();
string[] theSerialPortNames = System.IO.Ports.SerialPort.GetPortNames();
}
private void buttonStart_Click(object sender, EventArgs e)
{
serialPort1.PortName = "COM7";
serialPort1.BaudRate = 115200;
serialPort1.Handshake = Handshake.None;
serialPort1.Open();
if (serialPort1.IsOpen)
{
buttonStart.Enabled = false;
buttonStop.Enabled = true;
textBox1.ReadOnly = false;
}
}
private void buttonStop_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
buttonStart.Enabled = true;
buttonStop.Enabled = false;
textBox1.ReadOnly = true;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort1.IsOpen) serialPort1.Close();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(!serialPort1.IsOpen) return;
char[] buff = new char[1];
buff[0] = e.KeyChar;
serialPort1.Write(buff, 0, 1);
e.Handled = true;
}
private void DisplayText(object sender, EventArgs e)
{
textBox1.AppendText(RxString);
}
private void serialPort1_DataReceived
(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadExisting();
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;
serialPort1.DataBits = 8;
serialPort1.NewLine = "(13)";
this.Invoke(new EventHandler(DisplayText));
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void comboBox1_Click(object sender, EventArgs e)
{
}
}
}
|
|
|
|
|
Hi,
some comments:
1.
as your terminal emulator is working, it would be time to try the binary mode I have shown earlier. It has the advantage of (a) showing the exact bytes you're getting, and (b) you're using your own code (as opposed to the terminal emulator).
2.
you should set the port's properties right when you open it, and not in its DataReceived handler, i.e. after having gotten some data already.
3.
turbosupramk3 wrote: serialPort1.NewLine = "(13)";
that is non-sense. You need a CR character, which is represented by \r in C-like languages. cfr my first reply.
4.
I wouldn't use a single TextBox for both input and output, at least until everything works well. I also wouldn't spend all that effort in the GUI stuff until such moment that the serial comm is working fine.
I think you're getting closer to the solution!
|
|
|
|
|
Hey Luc,
I tried \r and \n too with no luck then I was trying the string value and I tried Chr(13) as well with no luck.
I will try the binary thing tonight and see what bits I am getting
|
|
|
|
|
Has the textbox a multiline option?
|
|
|
|
|
No. I am only concatenating two string
like...
string strValue="abc" + ";" + "xyz";
|
|
|
|
|
I have had luck writing serial data from a PIC microcontroller to a textbox using the following code in C#:
private void serialPort1_DataReceived_1(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
txtToDisplay = serialPort1.ReadExisting();
DisplayText();
}
public void DisplayText()
{
if (txtIn.InvokeRequired)
{
this.BeginInvoke(new MethodInvoker(DisplayText));
}
else
{
txtIn.AppendText(txtToDisplay);
}
}
Also, I set the comm port parameters in the same place I open the port, rather than the receive event:
private void GetComPorts()
{
foreach (string s in SerialPort.GetPortNames() )
{
lbPort.Items.Add(s);
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
this.lbPort.SelectedIndex = this.lbPort.TopIndex;
this.lbRate.SelectedIndex = this.lbRate.TopIndex;
this.lbProtocol.SelectedIndex = this.lbProtocol.TopIndex;
string crlf = Environment.NewLine;
serialPort1.BaudRate = Int32.Parse(lbRate.Text);
serialPort1.PortName = lbPort.Text;
serialPort1.Open();
if (serialPort1.IsOpen)
{
btnOpen.Enabled = false;
btnClose.Enabled = true;
txtIn.AppendText(string.Format("Port {0} opened successfully." + crlf, serialPort1.PortName));
}
}
Anyway, this seems to work in my situation. Also, my PIC code is using "\r\n" so I am actually sending a {10} and a {13} pair.
Hope this helps,
Adam
|
|
|
|
|
Hi Adam,
Thanks! I will try that at home and see how it works with my uC. I did not try sending a 10 and 13, that might be worth a try as well!
|
|
|
|
|
Hey Adam,
Looks like it was searching for a 10 and 13, but it wanted them as a 13, then a 10.
Thanks to everyone who was kind enough to respond and offer help to me. I'll probably have more questions as time goes on and now that I have a baseline I can start to implement the other good pieces of advice in this thread
|
|
|
|
|
Do you wite firmware on microcomputer? try to change 0x0d to (0x0a 0x0d)
|
|
|
|
|
Try looking for a linefeed (10) character instead. If it's coming from a Unix/Linux box then that's what will be sent.
|
|
|
|
|
Hi,
I am using something like this to get all my running processes:
Process[] processlist = Process.GetProcesses();
foreach(Process theprocess in processlist)
{
Console.WriteLine("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id);
}
My problem is that I want to know the filename (EXE) of a process, and the pathname it was started from. For example, if there is a process Notepad.exe running, I want to determine in which folder that file actually resides, i.e. something like "C:\Windows\system32\notepad.exe". How can I get that information?
I already tried to get the member StartInfo. but it doesn't contain a link to the folder.
How can I do this?
|
|
|
|
|
Message Closed
modified 23-Nov-14 5:45am.
|
|
|
|
|
qwertz321 wrote: theprocess.MainModule.FileName
Good one
My vote 5.
|
|
|
|
|
You can also use
theprocess.Modules[indexno].FileName
|
|
|
|
|
that is much more expensive and in no way any better than what qwertz already provided.
|
|
|
|
|
Luc Pattyn wrote: that is much more expensive and in no way any better than what qwertz already provided.
Yes, that's why I vote 5 for qwertz and during writing my answer I have mention you can also use and code works very well. I just try to give an alternate answer.
|
|
|
|
|
Good its just look like a fire A complete + Answer
|
|
|
|
|
ShilpaKumari wrote: Good its just look like a fire A complete + Answer
Not a Matter
some people Down vote or comment without testing code, which is not good in any corner.
|
|
|
|
|
RaviRanjankr wrote: some people Down vote or comment without testing code, which is not good in any corner.
agree with you
|
|
|
|
|
RaviRanjankr wrote: theprocess.Modules[indexno].FileName
We need to handle Exception in this case.
|
|
|
|
|
MDNadeemAkhter wrote: We need to handle Exception in this case.
Yes, in both cases either you are using MainModules or Modules you need to handle exception..
|
|
|
|
|
Hi,
I have two datagridview, one is invisible. When i click the first datagridview then the other will visible, and i need to select a value from that grid. How can i drag rows in the new grid when i press down arrow?
Thankyou
|
|
|
|
|
Have a Look
http://www.codeproject.com/KB/webforms/DragAndDropGridView.aspx
http://www.telerik.com/community/forums/aspnet/grid/drag-and-drop-grid-rows.aspx
http://jqueryui.com/demos/sortable/#connect-lists
http://forums.silverlight.net/forums/p/164753/372597.aspx
http://themechanicalbride.blogspot.com/2009/08/new-with-silverlight-toolkit-drag-and.html
http://aspdotnetcodebook.blogspot.com/2009/04/how-to-move-selected-record-from-one.html
|
|
|
|