|
Because the dll is a com object accessed through the idl interface it will not accept StringBuilder in place of string as the parameter type.
Argument '1': cannot convert from 'out System.Text.StringBuilder' to 'out string'
Am I missing something?
|
|
|
|
|
Sorry, Probably my mistake.
If interfacing to COM does not need an explicit prototype, then you can´t simply change the parameter types; for C# calling non-COM native code I believe it would work the way I had in mind.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
modified on Sunday, June 12, 2011 9:19 AM
|
|
|
|
|
can you use IXMLHTTPRequest in C#?
|
|
|
|
|
Hi!
I am writing a small application which shall communicate with a picaxe microship through the serialport. I have got the communication up and running using the SerialPort class, but got a little disappointed due to the speed.
The picaxe is connected to a potentiometer. The picaxe then reads the value and sends it as one byte(0 - 255)at a given frequency through the serialport to my computer. Then a textbox shows the value.
The problem is that the update speed seems to vary(it seems in the textbox). Sometimes it's pretty much realtime, other times it has up to 2 seconds lag. When it's good it may be good for a long time, a minute or so, and the same if it's slow. I don't know if this can be blamed at Windows(XP), or my programming, or if it's nothing to do with it.
I have heard(read) that the SerialPort class in .NET isn't exactly fast, but honestly, it's just one byte! I have tried many different frequencys for sending the byte at the PICAXE, from 4 times a second to a thousand, but it's like the port is slow.
I have read something about using one thread to scan the port in addition to the one that serves the Window itself, but with the eventhandler for datarecieved I believe this shouldn't be necessary.
I will paste some of my code below, and I will appreciate if someone gives me some feedback.
Thanks you for your time !
Jon, Norway.
The code:
public void initializePort()
{
serialPort.PortName = "COM5";
serialPort.BaudRate = 4800;
serialPort.DataBits = 8;
serialPort.ReadTimeout = 1000;
serialPort.DataReceived += new SerialDataReceivedEventHandler(readPic);
serialPort.Open();
serialPort.DtrEnable = true;
}
public void readPic(object sender, SerialDataReceivedEventArgs e)
{
int fromPic = serialPort.ReadByte();
String line = Convert.ToString(fromPic);
if (display.InvokeRequired)
{
display.Invoke(new MethodInvoker(delegate() { setDispText(line); }));
}
else
{
display.Text = line;
}
}
public void setDispText(String s)
{
display.Text = s;
}
|
|
|
|
|
I ran into issues with performance sending a handful of bytes at a time with custom serial port classes (VS2k3, so no SerialPort class available). I've seen equally miserable performance in an mfc app, so it's not .net specific. Unfortunately I can't recall what I finally did to make it perform acceptably; aside from the fact that I spent several weeks banging my head off the wall over it.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
Hi,
IMO DataReceived will not necessarily fire for each byte that is received; basically when one or more bytes are available, you will get an event, and they expect you to obtain all available data each time. If you don't try getting it all, the extra data will sit in the input buffer, and your code will lag getting it out (the ReadExisting method is pretty useful for that provided your data is text, yours isn't); in the end you will loose data unless handshaking has been organized, in which case you only will get a potentially big latency (depending on buffer size).
I do have some more remarks:
1. "from 4 times a second to a thousand" is not compatible with 4800Baud, you need at least 9600Baud to be able to communicate the data at 1000 bytes/s.
2. the line display.Text = line; will never execute since InvokeRequired will always be true
3. if your serial port happens to be a USB-to-serial cable, be aware that those do buffer some characters, so they can transmit data in small packets, making better use of USB bandwidth.
4. I trust you did not enable handshaking and left ReceivedBytesThreshold at 1.
And some suggestions:
1. add a StopWatch to your code, and show both its value (up to the milliseconds) and the incoming data in a Console.WriteLine inside your DataReceived handler; that way you will see how the data comes in, and then decide whether or not it gets processed as it should.
Alternatively, add an incrementing counter to DataReceived or setDispText and have it display in another Label (using Invoke again when in DataReceived).
2. add an ErrorReceived handler, maybe something is going wrong and currently gets ignored.
[ADDED]
If you are mainly interested in the most recent value, there are alternative approaches to reading binary bytes:
1. use SerialPort.BytesToRead and Read(byte[],...) to read what is available, then process the last byte only.
2. instead of wiring DataReceived, just launch a thread that performs a synchronous ReadByte() inside an "eternal" loop, and choose an appropriate ReadTimeout value (maybe 100msec is fine). Such polling will provide better "real-time" behavior (never trust Windows to do any of that) but it will be more expensive though.
[/ADDED]
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Have you tried using BeginReadByte() from SerialPort.BaseStream?
Alternatively, try using SerialPort.Read()
Another possibility is tweaking the SerialPort.BaudRate
|
|
|
|
|
You are definitely not the first to encounter problems trying to interface Windows to some real time device or kit. Having heard/had this complaint many times, though not in .NET (as I've never used .NET), I've seen two possible solutions - none of which I suspect you want to hear.
1. Design your program so that it doesn't rely on real time reading of your port. For instance consider: Do you need to update a window with the potentiomer value 9800/sec when the human eye only scans at a lower frequency? (Can't remember what frequency but 18 or 180 scans/sec seems to come to mind).
2. I had a similar problem trying to make a 'software-scope' out of my old PC and ended up buying VToolsD in order to write a Device Driver and buying a blank PCI card so I could solder on some buffers, 555 timer etc and interface directly to the PCI bus - and my device driver. My device driver - much more responsive than my earlier program attempts - then recorded an array of the sample time and value which it passed to the main program.
|
|
|
|
|
hello ,
my application either run automatically on startup or can be manually executed by user.
is there a way to know who triggered the application ? i want to add a note about it in a log file my application creates.
thanks in advance,
avi
|
|
|
|
|
|
Well I know a dirty workaround: give the auto-startup entry a command line argument and use that.
Doesn't give any kind of guarantee, but it will "usually" be accurate and it's easy to implement.
|
|
|
|
|
|
shabya wrote: know who triggered the application ?
Depends on how you define "who". Computer Name or User Name?
I Love KongFu~
|
|
|
|
|
hi,
i ment by who : the computer itself on startup or manual activation from srart->programs -> ...
|
|
|
|
|
shabya wrote: the computer itself on startup or manual activation from srart->programs ->
Well, in this case, Environment.UserName is exactly what you are looking for. Jimmanuel already give you the anwser
I Love KongFu~
|
|
|
|
|
why is it not writing to screen the first line/value in the txt file this code?
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
class MainClass
{
public static void Main(string[] args)
{
string inFile;
string line;
Console.WriteLine("Input file name to open!");
Console.WriteLine("Sample: d:\\filename.txt");
inFile = Convert.ToString(Console.ReadLine());
StreamReader sr = File.OpenText(inFile);
line = sr.ReadLine();
List<string> lines = new List<string>();
while ((line = sr.ReadLine()) != null)
{
lines.Add(line);
}
foreach (string fi in lines)
{
Console.WriteLine(fi);
}
Console.ReadKey();
sr.Close();
}
}</string></string>
Everything is ok but there aren't first line
|
|
|
|
|
wwwxyz wrote: line = sr.ReadLine(); List lines = new List(); //add all the lines to a list while ((line = sr.ReadLine()) != null)
Because you are reading the fisrt line before the loop and you are not adding it to Lines
Just lines.Add(line); add this before the while loop and after the List lines = new List(); . and it will be fine.
|
|
|
|
|
thanks for reply
Problem is solved but there are two "lines.Add(line);" in code anymore.
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
class MainClass
{
public static void Main(string[] args)
{
string inFile;
string line;
Console.WriteLine("Input file name to open!");
Console.WriteLine("Sample: d:\\filename.txt");
inFile = Convert.ToString(Console.ReadLine());
StreamReader sr = File.OpenText(inFile);
line = sr.ReadLine();
List<string> lines = new List<string>();
lines.Add(line);
while ((line = sr.ReadLine()) != null)
{
lines.Add(line);
}
foreach (string fi in lines)
{
Console.WriteLine(fi);
}
Console.ReadKey();
sr.Close();
}
}
</string></string>
|
|
|
|
|
Or you can simply remove the first line = sr.ReadLine(); (which is outside of the while loop) before List lines = new List(); in the original code. (means the code in your first post). I think that line of code is not needed.
and also, its good to check wether the file exist or not before reading/opening. you can implement try-catch block for that.
|
|
|
|
|
just wonderful
thank you very much
I removed the first line = sr.ReadLine(); which is outside of the while loop.
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
class MainClass
{
public static void Main(string[] args)
{
string inFile;
string line;
Console.WriteLine("Input file name to open!");
Console.WriteLine("Sample: d:\\filename.txt");
inFile = Convert.ToString(Console.ReadLine());
StreamReader sr = File.OpenText(inFile);
List<string> lines = new List<string>();
while ((line = sr.ReadLine()) != null)
{
lines.Add(line);
}
foreach (string fi in lines)
{
Console.WriteLine(fi);
}
Console.ReadKey();
sr.Close();
}
}
</string></string>
|
|
|
|
|
Or simply:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
class MainClass {
public static void Main() {
Console.WriteLine("Input file name to open!");
Console.WriteLine("Sample: d:\\filename.txt");
File.ReadAllLines(Console.ReadLine()).ToList().ForEach(Console.WriteLine);
}
}
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
|
Hi
I am using the Zedgraph addin to plot some curves. I would like to add labels to each curve individually instead of having a legend on the right-hand side. Can anyone shed some light on this?
Thanks,
Alex
|
|
|
|
|
have you consulted Zedgraph genie, I mean, Wikki[^], how about this[^]
Yusuf
Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]
|
|
|
|
|
Hi Yusuf,
Thanks for the tip, I have looked there and there is someone else with the same problem but I don't see a solution. There seems to be a function with pie charts but still no idea for line graphs?
Alex
|
|
|
|