|
Thanks Ravi, perfect.
What I have is a box where the user Enters hex bytes, and I have a button next to it that says "Check Syntax"
When he clicks, I need to examine the text in the box (thanks for this refresher)...
string myString = myTextBox.Text;
Then I need to pop up an "OK" box with an exit only or a "Bad" box with an okay on it.
We might want to show him the details of which byte was wrong.
In this case, "OK" text will be
-- 0-thru-9
-- A-thru-F
-- An asterisk (Ascii 02Ah)
-- A question mark (Ascii 03Fh)
Oh, just remembered; we want a comma between each element.
I will have to work out the details of White space (none, one, and multiple instances between the digits) and the single digit problem for values 00h thru 0Fh
|
|
|
|
|
It should be very easy to do what you want to do. But your requirements (as stated in your post) are very vague. Are we talking single hex digits, a string of hex digits, etc. Are the * and ? delimiters or terminators? Are they optional? To many questions. You need to be extremely and unabiguously specific if you're going to code any of this.
Also, this begs the question: why validate when you can sanitize (i.e. fix up) the user's input and therefore be more forgiving to the end user and guarantee that the device receives valid data?
/ravi
modified 30-Mar-14 0:19am.
|
|
|
|
|
Okay, new thread time, will do.
|
|
|
|
|
Hello I'm new at c# and I'm running into a cross thread issue.
I'm reading ASCII data from the serial port using the following code below:
I will like to know how to pass RxString to a different Method without getting a cross thread error?
Thanks
public void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
RxString = serialPort1.ReadLine();
label1.Text = RxString;
}
catch (Exception)
{
MessageBox.Show("Data Receive Error: Close Port & Change Baud Rate" );
System.Threading.Thread.Sleep(5000);
bool dataerror = true;
DataRecError(dataerror);
return;
}
this.Invoke(new EventHandler(DisplayText));
i++;
}
|
|
|
|
|
|
Thanks for the link, but I still cannot get it working.
|
|
|
|
|
The problem is the line
label1.Text = RxString; It must not be executed in a thread different than the main thread.
Try following code:
if (InvokeRequired)
{
this.Invoke(new System.Action(() => { label1.Text = RxString; }));
}
else
{
label1.Text = RxString;
}
|
|
|
|
|
excellent, that worked.
Thank you
|
|
|
|
|
I think you can just always call Invoke (or BeginInvoke) for this type of thing, those calls are harmless if called in the main thread.
|
|
|
|
|
I am not sure. In the example above, I used a lambda which does not call the function again, and that might work.
But with "normal" code like
delegate void VoidDelegate();
void SomeFunction()
{
if (InvokeRequired)
{
this.Invoke(new VoidDelegate(SomeFunction));
return;
}
}
you'll run into a StackOverflow exception when omitting the InvokeRequired... Hence I always use that "safer" version, though it might not be necessary in every case.
|
|
|
|
|
That's a different situation though, that's a 'make this method self-marshalling' rather than 'marshal this method call'. The StackOverflowEx is pretty obvious if you don't guard this. If you put the responsibility on the caller (i.e. they must use control.Invoke(() => control.SomeFunction())), or you provide an explicit SomeFunctionAsync (using BeginInvoke) then it's not an issue.
On a different note you don't need to define a void delegate type, MethodInvoker exists for this already.
|
|
|
|
|
I am coming from Texas instruments Forum (MSP430f47197) due to my curiosity on C# and serial communications!
I just started to make a GUI that can communicate with two energy meters and read their voltage, current, power factor, and the Active power as well.
The GUI itself was easy for me as I saw a couple of Youtube videos and learnt how to make forms in Visual Studio.
The confusion and ambiguity was started when I noticed that I need to make the PC connected to the serial port and most importantly, read the values. Moreover, for one out of two meters I need to write as well and return that back to the GUI!
As I am new in Serial communication and C# stuff, what do you think of this project? Is this doable?
I have the code for one of the energy-meters. The code which is written in the CHIP of the board is partially modified by me and I am almost familiar with.
But the other energy-meter is not. It is just a black-box and I have only some h files! Is this possible to talk to such energy meter?
I would like to know how much time do I need and where is the best point to start with?
I have divided the steps for my project as follows. However your feedback and comments much more appreciate it.
1. Receiving data from the E-meter code through Hyper-terminal or other monitoring software.
2. Receiving the Voltage from the SPM without decimal points via Hyper.
3. Receiving the Voltage from the SPM Using C#.
4. Modifying the Voltage from the SPM Using C# and sending it back to the MCU and showing the modified version of Voltage on the display.
5. Receiving the Voltage from the Radian-meter Using C#.
6. Comparing voltage from SPM to Radian-meter and calculate the offset and sending back to SPM.
P.S.
SPM: energy-meter 1
Radian-meter: energy-meter 2
Voltage: Just as an example to get one value.
|
|
|
|
|
Doable? Sure.
Doable by you? We have no idea since we can't get inside your head and find out exactly what you know and don't know.
This is not a project for a newbie in C#/Windows programming.
There are more to your list of "steps" than what you've got. Two-way Serial communication without blocking the UI and threading issues (updating controls from background threads) are going to be your biggest design roadblocks.
Don't make the mistake of believing that "oh, I'm not using any threads so I'll be OK". With the serial port classes, you're using background threads. You just don't know it.
|
|
|
|
|
In good software design you do not attach a GUI to communicate with electrical systems like that.
1.
First of all start of with a prototype class library that you run in debug. Start creating handlers for communicating with the ports and check the results. (also create unit tests for those). Working with ports means you use machine "resources", which implies cleaning up those resources and also making sure you do not use resources used by other applications (potentially blocking those)
Sending data to the port is probably easy, but reading from it requires some loop OR a callback handler of some sort. There is probably a good tutorial or sample code on the internet, but try to understand before using!
2.
Then you work you way up to business layers or something similar that never communicates to the electrical systems directly, but with the class you wrote in 1. (which you cleaned up, foresaw with sufficient commenting, including intellisense and which is preferably a totally different project. The business layer handles workflow and quality checks of the data. The workflow pumps the data up to the next level.
3.
Create yet again a project that will hold your GUI and that uses the business layer assembly. If you want your GUI to remain responsive you´ll need a backgroundworker.
So for me your steps 1-6 are just one step in the process.
If it is to some interest read my article[^] .
Hope this helps!
|
|
|
|
|
Serial port communication is definitely possible in C#/.Net. It's a low level interface though so you need to know what bits to read and write, handle wait periods and transfer rates and things like that, which require a lot of information about the chips' behaviour (i.e. documentation).
You definitely shouldn't be interacting directly with the ports from UI code. Apart from it being generally good programming practice to separate concerns (and there are at least two obvious distinct parts of this app, serial port I/O and UI; probably three if there's any logic to be done), I/O does lots of things on background threads and that really doesn't play nicely with UIs.
|
|
|
|
|
Ok Guys! Thanks for your comments so far! May you please guide me to a tutorial just to send some amounts through a serial port please?
In other words, I receive my values through Hyper-Terminal but now I just would like to have a window (built by myself) to show those! (As a first step!)
That would be appreciated!
|
|
|
|
|
Hi! I have a proyect where I´m creating .mp3 files with different names each, but I need to download those files in .vox format, so after, another program could read the files, bc it only accepts .vox files. Anyone here have done that before and could help me with some code, or any suggestion would be great. Thanks to all.
|
|
|
|
|
Member 10701353 wrote: Anyone here have done that before and could help me with some code Easiest way is to find a command-line tool and execute that from code;
Process.Start("convert.exe bla.vox bla.mp3"); Alternatively, you'd be googling for a library that implements that functionality; I doubt that you want to implement the conversion yourself.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy Vluggen wrote: I doubt that you want to implement the conversion yourself. Aw
That would actually be interesting, unlike the other two ways.
vox seems pretty simple, decoding mp3 would be the hard part and there are plenty of libraries to do that.
|
|
|
|
|
How to add a context menu in web forms? I want a context menu to be added to a grid and also another context menu outside the grid Without using any third party tool.
Can anyone help me?
|
|
|
|
|
You're going to have to do this via JavaScript. You'll be better off asking this in one of the CodeProject web forums as this has nothing to do with C#.
|
|
|
|
|
Thank you. I will post this question in web forums.
|
|
|
|
|
hi
how chenge close botton in extra tab?
style PropertyView
tanks
|
|
|
|
|
You need to provide more information if you hope to have any meaningful answer to this question.
Describe the technology you are using: WinForms ? WPF ? ASP.NET ?
Tell us what, exactly, is a "Tab" and a "PropertyView" in your application.
“Use the word 'cybernetics,' Norbert, because nobody knows what it means. This will always put you at an advantage in arguments.” Claude Shannon (Information Theory scientist): letter to Norbert Weiner of M.I.T., circa 1940
|
|
|
|
|