|
glennPattonWork wrote: a colleague got some of source hacked it around
That is usually the problem. You may also note that his code looks like it is sending characters direct, which probably means they are still Unicode (i.e. 16 bits) whereas they need to be ASCII (8 bits).
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
Actually the text-oriented methods apply an encoding, that is what the SerialPort.Encoding property is for. However, while the MSDN doc states it defaults at Encoding.ASCII , that is not in accordance with my experience; and that is why I either set the encoding explicitly, or, preferrably, transmit byte arrays, not char arrays or strings.
|
|
|
|
|
The sample that OP said did not work, didn't have any specific encoding, hence my comment.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
I have been using serial ports quite a lot over the years, and I am unaware of any bug in Windows serial ports that could be relevant here.
Anyway, all your code snippets are doing is slow down the communication in different ways.
In my books, when a simple SerialPort.Write(byteArray, startIndex, length) doesn't work properly, then it is probably one of these two things:
- your serial bytes are bumping into each other due to a bit pattern format error (most common would be sending with just the one stop bit whereas the receiver may require 1.5 or 2)
- the receiver is badly designed and has a timing constraint, e.g. it needs more time to process a byte than it takes the byte to travel the serial line. Reducing the baud rate could solve this.
There are a lot of issues with serially receiving data, especially when data rates are high, and both throughput and latency are of high importance. Then Windows is tricky, as are many other systems. However, transmitting should never pose any problems.
Suggestion: try at lower baud rates first. I tend to develop comm protocols at 9600 Bd until they work perfectly, onlyu then turn up the baud rate.
Some random remarks:
- base-board serial ports tend to have good timing behavior; remote ports (such as USB serial cables, or Ethernet serial ports) tend to suffer delays and packetizing phenomena as they try not to send a whole package for each and every byte you want sent. So make sure to log the hardware aspects too when observing system behavior.
- make sure the GROUND signal in your serial cable is connected properly at both ends; a two-wire serial cable should not work, but it may occasionally work, be it intermittently.
- Windows knows how to buffer and dataflow the outgoing data, just like it does the incoming data; if you're unaware of this, it may continue for a while; and then stop sending data because the other side is "actively refusing to accept any more data". This depends on your handshake settings.
- make sure to setup and open the serial port once, then wait a while before exercising anything. Do not open and close all the time (a serial port is not a database!). That is recommended in general; and in particular in Windows where a fast close-open sequence is known to fail often (and that is a documented "feature").
|
|
|
|
|
"There are a lot of issues with serially receiving data, especially when data rates are high..." has got to be the understatement of the year!
I worked for 7 years at a hardware store running 50 serial terminals on one IBM desktop PC. Some of the runs were 300' long, via surplus telephone cable that the owner found and installed before I went to work there. It was my unpleasant job to maintain the system and keep it working, despite the fact that it broke every RS232 standard ever published. What a nightmare!
Will Rogers never met me.
|
|
|
|
|
Roger Wright wrote: "There are a lot of issues ..." has got to be the understatement of the year!
I can't see an understatement here.
Now if I had said: "occasionally a thing or two could go wrong..." that would be an understatement.
Roger Wright wrote: Some of the runs were 300' long
Anything can fail when you use it outside the specified conditions. RS232C is spec'd at a maximum of 15m IIRC. Any failure under unsupported conditions should not reflect on the vendors of the subsystems used (PC, Windows, ...).
|
|
|
|
|
It's (in the one revision I'm most used to) 50' at 9600 bps. We used 300' at 19.2 kbps. But I kept the damned thing working for years, while secretly undermining the boss and installing Cat5 in my spare time.
Will Rogers never met me.
|
|
|
|
|
Undermine with cable, I like that.
|
|
|
|
|
Thats good no bug in Serial Port class, I think the problem is down too small a buffer in the Controller, like I thought. Now all I have to ask is how bad is my hack to get around it, my thinking was a bit inefficient but.....
modified 4-Nov-11 5:12am.
|
|
|
|
|
If the board was my design, I would have started slow. Colleague designed it and the phrase used was "well that is done it works with HyperTerm". Told to "learn how to do comms properly", I don't think that helps at all. My knowledge of Serial Port Class comes from Jan Axelsons book & MSDN I used MSComm & MHComm before that!
modified 4-Nov-11 7:01am.
|
|
|
|
|
Hi,
I'm trying to write a function in a c++/cli dll that will receive an empty array from C# (float[] a), use gcnew to allocate the array and then I need to use the array in C#. I tried regular transfer but after the function returns the array is still null in C#. Current trial is:
C#:
float[] a;
InitArray(a);
c++/cli:
void CLIClass::InitArray(array<float>^ a)
{
a = gcnew array<float>(100);
for(int i=0; i<100; i++)
a[i] = 10.0F;
}
I do not wish to return the array as a return value (the function should actually return 3 arrays).
Thanks!
|
|
|
|
|
Does passing a tracking reference work?
void CLIClass::InitArray(array<float>^% a)
Mark Salsbery
|
|
|
|
|
Yes, all I was missing is the % operator. Thanks!
|
|
|
|
|
I gave him a 5. It's nice when you do that when people help you out here.
|
|
|
|
|
|
your C# code should use the out or ref keyword:
float[] a;
InitArray(out a);
float[] a=null;
InitArray(ref a);
ref would fail here as long as a isn't initialized.
Without ref/out your callee can modify the array content, however it can't replace the array.
|
|
|
|
|
******************************************************
the code works as follows.
If you chose them all, works fine, but not all, of one or more than one election, or choose not at all, the program does not work
stands in this code.
if (dr.Cells [0]. Value.ToString ()! = "true")
How do we solve the problem?
List<int> TestId = new List<int>();
for (int i = 0; i < dataGridView2.RowCount; i++)
{
foreach (DataGridViewRow dr in dataGridView2.Rows)
{
if (dr.Cells[0].Value.ToString() != "true")
{
TestId.Add(Convert.ToInt32(dr.Cells[2].Value.ToString()));
}
}
}
if (baglanti.State == ConnectionState.Closed)
baglanti.Open();
komut.Connection = baglanti;
for (int i = 0; i < TestId.Count; i++)
{
komut.CommandText = "update HASTA_SONUCLARI SET ONAYLANDI='" + true + "' where PROTOKOL_NO='" + TextBox6.Text + "' and TEST_NO='" + TestId + "'";
komut.ExecuteNonQuery();
}
MessageBox.Show("Onaylandı.");
baglanti.Close();
}
}
|
|
|
|
|
what a horrible message. The code isn't formatted properly, there are plenty of unjustified ToString() calls and string comparisons, and "doesn't work" doesn't even start to explain what the problem is.
Please read the forum guidelines ("How to get an answer to your question" at the top of this forum), and see how others post their message. Good questions yield good answers here.
|
|
|
|
|
Actually I wanted to report this message to the Hall of Shame.
Look:
- You run in nested loops several times through your DataGridView
for (int i = 0; i < dataGridView2.RowCount; i++) {
foreach (DataGridViewRow dr in dataGridView2.Rows)
- You compare boolean values as a strings - it will fail with localized versions!
if (dr.Cells[0].Value.ToString() != "true")
By the way, I posted such a coding horror some time ago. It was very hard to find out why the program strangely failed on that one computer.
- You create SQL queries by adding strings together. TextBox6 might contain bad code (SQL injection attack). Use parameterized queries instead.
- You add an array of numbers where one number is required:
and TEST_NO='" + TestId + "'";
instead of
and TEST_NO='" + TestId[i] + "'";
And that looks like using strings instead of numbers...
|
|
|
|
|
Hi,
i want do send an email, with a 'link' including parameters to a pc user. If the user clicks on this link a specific webforms c# application should be started, if it is not running, and than an action should be executed.
Example MailText:
New image received: 'myapplication<imageid=2000>'
Clicking on 'myapplication<imageid=2000>' should (start the application if it is not running and) load my image with id 2000 from db and show it in my application for example.
Is there a easy way to get it work?
The application is not installed locally, it is only available as webforms application.
I did not know how i must define and setup the link in the email in that way, that the user click on the link (with parameters) in the mail is delegated to and handled by my application.
Did someone maybe know a manual or demo project for this case?
Greetings Frank
|
|
|
|
|
Using the Process class[^], you can start a new application in a program of your choice.
Here[^] is a neat sample of how this class has been used to start Notepad.
|
|
|
|
|
It sounds like you want a url link to your web app with a parameter. This can be done simply by including the URL string in the body of the mail. Surely that can't be the issue.
On the other hand if you are wanting to install your application on the recipients hard disk you are talking about the same delivery method as malware, not a good idea.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hello
I am trying to add a service reference to my solution but it propmpts me errors I use the Ip address with web browsers and it works but when i add the service to my solution it gives me this error
the IP address is MYIP:88/sapco/sapcoservice.asmx?wsdl'
Metadata contains a reference that cannot be resolved: 'http://MYIP:88/sapco/sapcoservice.asmx?wsdl'.
There was an error downloading 'http://MYIP:88/sapco/sapcoservice.asmx?wsdl'.
The request failed with HTTP status 404: Not Found.
Metadata contains a reference that cannot be resolved: 'http://MYIP:88/sapco/sapcoservice.asmx'.
Metadata contains a reference that cannot be resolved: 'http://MYIP/sapco/sapcoservice.asmx'.
|
|
|
|
|
You have already posted this in the WCF and Visual Basic forums; please selct one forum only for your questions.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
Can anyone tell me why I cannot get this code to write to my serial port (microchip) ?
If I send "pin1=2" via the simpleserial object, it is received and the microchip code reacts. When I try and send it through the below method, the data is not received by the microchip and it does not react.
Thanks for reading.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.Windows.Forms;
namespace SimpleSerial
{
class test
{
private void serialPortWrite()
{
SerialPort serialPort1 = new SerialPort();
string lineToWrite = "pin1=2";
serialPort1.PortName = "COM8";
serialPort1.BaudRate = 115200;
serialPort1.DataBits = 8;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;
serialPort1.Open();
MessageBox.Show(lineToWrite);
foreach (char chr in lineToWrite)
{
char[] buff = new char[1];
buff[0] = chr;
serialPort1.Write(buff, 0, 1);
}
serialPort1.Close();
}
}
}
|
|
|
|