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 = newbyte; //I want to read only three bytes.if (bytes > 2)
buffer = newbyte[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) Replace("\"", "");
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 hv written following code to show text on tab click in google map now alone with text in pop up window i want to give link as well how to give that?
code is.... 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"; //HtmlAnchor GP5= default(HtmlAnchor); //GP5.HRef = "http://www.google.com/"; //how to give link to open web page along with " this is pointing to buildin...." text shown in above code.
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/";