Click here to Skip to main content
15,895,606 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi All,

Asked a stupid question earlier about string arrays and found the answer myself after typing the question (thanks also to Manfred R. Bilhy) so I hoping to do this again. I have to send a group of strings from the serial port to a waiting board.
I have comms to the board I can sent and recieve data as well it is just I can't get it to upload all the data:
C#
for (int b = 0; b <= 5; b++)
         {
             Command = CommandA[1] + '\n' + '\r';
             rtbIncoming.SelectionStart = rtbIncoming.Text.Length;
             rtbIncoming.ScrollToCaret();
             Length_Command = Command.Length;
             for (int Counter = 0; Counter <= Length_Command + 1; Counter++)
             {
                 if (Counter == Command.Length)
                 {
                     return;
                 }
                 Command_Sent = Command.Substring(Counter, 1);
                 myComPort.Write(Command_Sent);

             }

That is the for loop thats being the problem the method of writing out works (bit of a kludge I worked out ages ago.
Using a Serial Port Monitor I can tell it replies to my command with 0D or Carriage Return, the text OK and an OD and a > prompt.
Anybody Any Ideas?
Glenn
Posted
Comments
Ed Nutting 9-Dec-11 8:56am    
Your code seems self defaeting - you run the second loop from 0 to Command Length + 1 yet in the loop you have an if Counter == Length_Command. Meaning the loop only really runs from 0 to Command.Length : did you mean this?
Jephunneh Malazarte 9-Dec-11 9:19am    
what is the possible value for your CommandA[1]?
glennPattonWork3 9-Dec-11 9:32am    
to Edward Nutting, That works like I said its a kludge to get around the awkward way this microcontrollers on board UART runs, if I didn't send individual characters the board ignors it.
to Jephunneh Malaztre CommandA[1] contains "SLD:0002:4181246867"
Timberbird 9-Dec-11 9:42am    
What is the outer (for (int b = 0; b <= 5; b++)) loop for? return will exit this method as soon as you reach Length_Command. Should Command value be changed inside that second loop?
glennPattonWork3 9-Dec-11 9:48am    
Thanks, that looks like it, a quick test shows it is getting past if I change the return to a break!!!

We use Advanced Serial Port Monitor by AGG its quite good. I added the dreaded Thread.Sleep and got an odd reaction. I hate to say it but I think the board is not playing nicely!
 
Share this answer
 
If your board wait an only character, may be this loop is too fast, so your microcontroller program considere it a many chars. Have you tried to add a sleep between successive writes ?

Addy Tas, suggest to read in another thread, i agree. But the mono-threaded microcontroller probably do not need that. I have the intuition you can simply block while getting your replies.
You can easily wait using a loop like this one :

C#
while(myComPort.BytesToRead ==0) 
   ;


Which is a dangerous inifnite loop if nothing happend, can be worked around with something like this (i don't have the code here, but it should be something like that)

C#
public int WaitForData(int NbDataToWait, int Timeout)
{
    int timeWaited = 0;
    int timeToWait = 1; // 1 ms
    while (timeWaited < Timeout && MyCompPort.BytesToRead < NbDataToWait)
    {
        timeWaited += timeToWait ;
        System.Threading.Thread.Sleep(timeToWait );
    }
    return timeWaited;
}
 
Share this answer
 
Comments
glennPattonWork3 12-Dec-11 6:10am    
I have used a similar routine in the past, but experience some problems with the BytesToRead method I will when my PC sorts it self out give it a go!, a question though the System.Threading.Sleep() is it no an evil Thread.Sleep on the UI thread?
Romain TAILLANDIER 12-Dec-11 7:54am    
Thread.sleep is not recommanded on UI thread that's right. But if it sleep a very few time (less than 200-300), user can't observe the delay. You can add Application.DoEvents() in the sleep loop, to let the user have the control, and the UI refreshing good. You also can freeze the UI while the program communicate with the peripheral, simply by activate a wait cursor (this.Cursor = Cursors.Wait;)

According to my experience, i drove tens of rfid peripherals using C#, massively constructed on waiting for ByteToRead > 0, and i never experienced problems using this, since the SerialPort class exists on compact framework 2. (didn't exists in CF1).
Most of the peripheral i drive are self maid one based on microchip PIC microcontrollers. Do you now ASPYCOM[^]?
It allow you to spy a com port and see if your program and your peripheral communicate as waited

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900