I learned a thing or two at THIS[^] place, where that author provides this very useful insight...
"...The next part is critical and not obvious. serialPort1 runs in it own separate thread behind the scenes..."
(emphasis added by me).
This was really good and useful information, as you can see how it helped me to understand why I can't send and receive at the same time.
Request: I would like some kind soul to please point me to a few example code snippets that demonstrate how I can start a thread which will send out my byte array over the serial port when the existing background serial report receiver method/thread/whatever gives it an opportunity to do so.
Threaded my code to send a command while the other background event handler (which, as I think I'm learning, is also in a thread) is receiving.
Changed my external box to wait half a second between sending packets.
(Half a second is a long time in this context.)
He (my C# "myPort.Write(blahblahblah)" instruction) still won't execute.
I went even further. I got my external box to receive the first set of data which my C# app sends, but never send another pack back to it, i.e., the external box was purposely not responding, just sitting there so that my app on the PC would not receive data
When that happens, my C# app on the PC will transmit either the start or stop command okay, as often as it wants; i.e., with data received event, the sending works fine.
The moment I start receiving data, I can never send any more; not even one byte.
I used the C# IDE to stop the execution.
He is at this instruction...
There is a green curved arrow in the left hand edge of the window pointing to that instruction.
Hovering over that instruction gives me a message...
"This is the next statement to execute when this thread returns from the current function"
Who arbitrates those various threads ? How do I find out which thread is currently invoked ? Where is this behavior documented ?
How does a software writer get the background serial port received data event handler to back off and let the serial port send data ?
A serial port cannot send and receive at the same time.
There must be some kind of protocol e.g. hardware handshaking, software handshaking or some kind of software protocol.
if You don't know what protocol is used, it is impossible to to make it work.
Here is an example of a protocol that my device uses.
My port is listening and waiting for for bytes to receive.
The device sends three bytes(two address bytes and one data byte and expects to have the data byte as answer immediately.
If the device receives the data byte and checks if this was the byte sent it sends the next three bytes, until there is no more to send.
In the code snippet You can see that the port is sending in the data_received event with no problem,
that is because I know the device is not sending until I answer correct.
This is the code I use to do this.
It only works with the protocol of my device, but it shows how to implement such a protocol:
System.Timers.Timer tmrPortInt = new System.Timers.Timer(800);
comportInt.DataReceived += new SerialDataReceivedEventHandler(portInt_DataReceived);
tmrPortInt.Elapsed += new System.Timers.ElapsedEventHandler(tmrPortInt_Elapsed);
privatevoid portInt_DataReceived(object sender, SerialDataReceivedEventArgs e)
if (tmrPortInt.Enabled) tmrPortInt.Stop();//this is a timer to check if no more data is received,//While data is received within 800 m Sec port keeps listening.// In my case he data is processed in the timer elapsed event(~6000 bytes).int tempAddress;
int bytes = comportInt.BytesToRead;
byte buffer = new byte; //I want to read only three bytes.if (bytes >2)
buffer = new byte[bytes]; //Empty my buffer before filling with received bytes.
comportInt.Read(buffer, 0, 3);
comportInt.Write(buffer, 2, 1);//You can see i am writing within the port_received event.// I send the third byte received.if (receiveByteList == null) receiveByteList = new List<byte>();
tempAddress = (256 * buffer + buffer); // decimal value of two address bytes
receiveBytes[tempAddress] = buffer;
receiveByteList.Add(receiveBytes);// add data byte to list
tmrPortInt.Start();// Prevents the timer to elapse until no more bytes are received.
void tmrPortInt_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
this.Invoke(new EventHandler(Process_MachineData));// This is where I do something With the data received.
Thanks Groover, I do indeed have control over the protocol.
On the small box that I have, (our own embedded system we made here) I can send and receive, whether that is simultaneous or not, I'll let others decide, but I don't have to check anything. So far it works every time.
I want to have the bytes from the small box flood the PC.
If you are correct, then I'm going to have to change my protocol to a start-stop-resume-stop-etc sort of scheme. Yuck; ugly.
In the absence of other advice, I will suppose that yours is correct, and write more code in the small box to do exactly as you've suggested; i.e., insert more protocol between the two systems.
My concept of a "thread" is clearly incomplete. This is evidently not time slicing.
My external box was flooding the serial port (on purpose, we wanted to test it to the max).
Turns out that if we continually send data to the serial port, particularly at BlueTooth speeds, the SerialDataReceivedEventHandler (hope I used the right term) apparently is invoked a whole lot more than the other threads.
i.e., there isn't a round robin tossing out invocations of threads. Evidently the hardware ints on the machine are trumping the software dispatch; that is, if I'm grasping the meaning of threads and events properly (which remains a process in mental development at the moment)
So I put in some space and the RX event handler now (apparently, from observations as a user of the code I'm writing) is proiding enough breathing space for the rest of the app to have a chance to act.
Previously, as soon as the data flood started, the entire app froze, like the buttons wouldn't even click.
Trying to use the split function, but one of the fields I am trying to load has "Last Name, First Name" (with double quotes around it. Is it posible to set an optionaly enclosed by parameter in the split function?
Common sense is admitting there is cause and effect and that you can exert some control over what you understand.
I don't get it. You have a string as "Last Name, First Name" and you want Last Name, First Name without the quotes? Then just strip off the quotes. There is no need to use the split function. Since you want to break the name apart, my solution is actually correct. Remove the quotes, then split based on the comma.
I don't recall seeing that in the original post. I went back and re-read it, and if that is what the OP had intended, its a very poorly worded question . In this case, I'd:
string parts = Split(',')
foreach (string in parts)
Personally, parts 1 and 2 need special comma handling and parts 0 and 3 don't.
If I wanted to get sophisticated, I believe you can use datasets to parse something like that for you, but I'd just do what I said above... split on the commas, strip off the quotes and then process the 4 fields.
i want to add link to my tab pop up to open any web page on click of it as i have to use HTMLanchor tag how to use it in following code to give link with text
GooglePoint GP4= new GooglePoint();
GP4.ID = "4";
GP4.Latitude = 28.496193;
GP4.Longitude = 77.096595;
GP4.InfoHTML = "This is pointing to Building 6 Tower C NIL Labs Ground Floor";//here with this tab i want to give a link like "www.google.com",this is aspx.cs page code
//HtmlAnchor GP5= default(HtmlAnchor);
//GP5.HRef = "http://www.nil-labs.com/";
a webBrowser object and then call the functions using the