|
I'm working on a project in which I'm working with my serial ports. When I open my serial port after that I'll have to send 4 requests. SO for sending request my code is:
private string ByteArrayToHexString(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length * 3);
foreach (byte b in data)
sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
return sb.ToString().ToUpper();
}
private void senddata1(String str)
{
byte[] data = HexStringToByteArray(str);
comport.Write(data, 0, data.Length);
}
This function will create a method to send data. On timer tick event I'm sending this Data:
private void timer1_Tick(object sender, EventArgs e)
{
senddata1(s1);
senddata1(s2);
senddata1(s3);
senddata1(s4);
}
Note:after every senddata1() function system will get a response.
So the problem is When I'm sending data I'm not getting the response because system is unable to read all senddata. I want to manage my program in a way so that I can send data after every 250 mili second. Like this:
SendData1(s1);
get response from the system
senddata1(s2);
get response from the system
get response is being handled in like this:
comport.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
So is there a way I can manage my program. Please share your opinions.
modified 16-Jan-14 4:46am.
|
|
|
|
|
Since you send all 4 strings directly after each other, you cannot receive the responses in between, of course. Send one string, receive the response, send next string, receive, etc. After having received the response for the last string, restart the timer.
|
|
|
|
|
Do you really mean 250 seconds ? My guess is: you mean milliseconds.
Why don't you set a boolean flag in the comport.DataReceived EventHandler, and use a 'while loop in your code after each send ? Here's a very rough sketch [1]:
using System.Diagnostics;
private bool hasResponded = false;
private Stopwatch theStopWatch;
private int waitHowLong1 = 250;
private int waitHowLong2 = 3000;
private void SerialDataReceivedEventHandler(dynamic port_DataReceived)
{
hasResponded = true;
}
private void senddata1(String str)
{
hasResponded = false;
theStopWatch = Stopwatch.StartNew();
while(true)
{
if (theStopWatch.ElapsedMilliseconds >= waitHowLong1) break;
}
Console.WriteLine("250 ms. wait done");
theStopWatch.Stop();
theStopWatch.Reset();
theStopWatch.Start();
while (true)
{
if (theStopWatch.ElapsedMilliseconds >= waitHowLong2) break;
}
theStopWatch.Stop();
Console.WriteLine("3000 ms. wait done");
if (! hasResponded) Console.WriteLine("failed to handle update");
} [1] Note: the above is untested code: it will compile as shown here (certain lines commented out, and missing parameter Type for SerialDataReceivedEventHandler "fudged"), but there's no guarantee beyond that; use this code as a source of ideas only.
[2] examples of using System.Diagnostic.Stopwatch: [^]
[3] Note: if you are working with .NET 4.5, and Windows 8, by all means use the new Task.Delay method with Wait argument in System.Threading.Tasks: [^].
“But I don't want to go among mad people,” Alice remarked.
“Oh, you can't help that,” said the Cat: “we're all mad here. I'm mad. You're mad.”
“How do you know I'm mad?” said Alice.
“You must be," said the Cat, or you wouldn't have come here.” Lewis Carroll
|
|
|
|
|
May I know how can I solve with the problem describe?
Problem:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
* The problem occured when debug with the code below in MVC C#:
<p>Last Checked on : <a href="/Members/utility">@Utility</a>ConvertToDateTime(@Model[0].Timestamp).ToString("dd/MM/yyyy") <a href="/Members/utility">@Utility</a>ConvertToDateTime(@Model[0].Timestamp).ToString("HH:mm:ss")</p>
|
|
|
|
|
I suspect @Modelcollection is empty, the out of range error indicates that it cannot find @Model0]
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Please check whether model collection is empty or not
|
|
|
|
|
hi there trying to interop WCF with unmanaged C++ clients.
First question is, where can you download sproxy.exe[^]
|
|
|
|
|
admittedly Im only using VS2005(*), but sproxy is installed already for me under VC (... Microsoft Visual Studio 8\VC\Bin\) , I haven't downloaded/installed it
It may be you need a particular 'Version' of Visual Studio VS<wxyz> eg Professional/Ultimate/Architect
(*) Professional version
'g'
|
|
|
|
|
There is no place to download it from.
It's installed with Visual Studio 2003 and 2005. It's not needed in later releases of Visual Studio as the ATL Server Library was deprecated and now lives in a retirement home here [^] on CodePlex.
|
|
|
|
|
|
$5 to make you hola
Member for 7 years and that's your first post?
|
|
|
|
|
Hola, spanish hello, gui developer using linux and shouting, what else needs to be said
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
|
I dint understand. Please explain me the meaning of this.
Thanks
|
|
|
|
|
Hi,
m a starter C# programming.I am working on Lab Machine and I'm being told to make a software to interface and fetch data from machine.
Please help I really need to retrieve information from this machine and store in our database also to show it in our WPF application.
I have to interface machines like Advia,Coulter,Liaision,Sysmex.
Anyone having knowledge on the same please respond back.
TIA.
|
|
|
|
|
Each of these machines will have an API that you should be able to query. Please don't cross post your questions.
|
|
|
|
|
Yeah thats what i want to know more in detail as how to start with by calling that API.
.
Need some examples or source code on it
|
|
|
|
|
Then you're going to have to get the documentation on what these machines exposes from the manufacturer of those machines.
|
|
|
|
|
Yeah, those Guide i do have. But that looks very sophsiticated in reading.
Being new into it,i am not able to start on it
|
|
|
|
|
No one is going to give you the code. If you have the manuals, you had better start reading.
The first thing you are going to have to do is work out what the data is that you actually want to read and start from there.
|
|
|
|
|
If you can't understand the documentation, how are you going to understand the code anyone gives you??
|
|
|
|
|
If you dont have any thing on it, so dont respond with you such invaluable suggestions
|
|
|
|
|
In truth Dave is quite correct, If someone supplied the code to do what you are after how would you support / change the code if you don't understand the documentation?
Have a try with the documentation to produce something that extracts the data and learn from it, it's the best way in my opinion.
if your stuck on something then post the code that doesn't work or work as expected (that is relevant to the problem) with as much detail as you can and you will get a better response.
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|
|
What it seems is that you expect that quite a few people here are experienced in operating the API of those machines...
The truth is quite the opposite I assure you. Not only do 99.9% of us not have a single bit of experience in those machines, we also do not have access to the API documentation.
But, being the great developers and awesome mentors we are, we are willing to help, the problem is that, again, we have zero information other than what you give us. If you want help, you need to throw us a bone here. Post up the code from the API documentation and start asking questions. We are asking for code, it doesn't have to be your code but we need something, anything, and some specific questions from you in order to help.
We don't have these machines sitting on our desks, in our closets, or at our facility. We don't have access to your computer, hard drive, or network. We can't see the documentation sitting on your desk and certainly can't get what you already have. Help us help you.
|
|
|
|
|
Ron Beyer wrote: Help us help you. Very well said.
/ravi
|
|
|
|