NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » clearing data in serail buffer

February 09, 2010
by sask55
sask55's Avatar

I am very new to C and micro programming .I am attempting to set up two way communications between my pc and the micro. Eventually between VBA applications on the pc and couple of digital calliper units and stepper motors connected to the micro. I have successfully connected one of the digital callipers to input to the micro. I also have the micro reporting on the serial USB to the VBA application. I need the code to clear the buffer before each new reading is sent to the uart. When my application in vba calls for a current calliper reading I get a very long list of all the reading that have occurred since the last call. I need to reset the uart buffer before the micro sends the current reading each time. An equivalent example in the Nerdkit sample code is the tempsensor data sent to the serial. If I make a call from my VBA application to the serial port when the tempsensor is connected and running I get a list of every reading since the last call. I need to reset or clear the last temp before sending the current one to the uart, so that if the VBA application calls data from the serial I receive only the last (most current) reading.

I know this should be very simple I just cannot seam to figure it out.

Darryl

February 11, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi sask55,

The readings you are getting are not getting buffered at the microcontroller, they are getting buffered either at the serial module on your computer, or on the software level in VB. I'm not familiar with VB, and don't know if there is an easy way to clear the incoming buffer (perhaps somebody else who knows can jump in). From a design perspective though, I would consider taking a different approach to this problem. Instead of clearing your buffer, and then taking the next reading you get. I suggest you actually use all the readings you get. Keep reading in the numbers you get in over the serial port and average them, maybe 10 or even 100 at a time in order to get a more accurate temperature reading.

If don't want to be polling the MCU continuously for the temperature reading, you could also take a different approach, and send a character from the computer to the MCU, as a signal for the MCU to send you X temperature sensor samples. This way you only get data when you need it.

Hope that helps.

Humberto

February 11, 2010
by treymd
treymd's Avatar

I think probably what humberto suggested is the best route, rather than just continuously dumping the reading to serial, you could have the MCU keep a running average and then you send a request to the MCU for that one piece of data. Alternatively if you just want one sample, you can essentially do that as well. Just send your custom request to the MCU and get a single response in return.

Basically, if the data is dumped to serial as fast as the MCU can manage, your VB code is going to get more than one sample every time, unless you are somehow sampling at the same rate that the MCU is writing to the port. (Not impossible) So even if you do manage to dump the buffer momentarily, the chances of you getting a single sample when you go to read it are slim.

Another less elegant possibility is to take all of the data read from serial in VB and split that up by some delimiter (like vblf if each reading is terminated by a carriage return and line feed.) Throw the split data into an array and you can then select the last item in the array, or even average together the samples in VB rather than asking the MCU to do it.

February 11, 2010
by treymd
treymd's Avatar

One other route that I didn't mention is that there is likely to be some event that can be fired off in VB to respond to data becoming available on the port. (I'm sure there probably is one) You could use an event handler to keep track of the current readings, average them if preferred, and then read whatever variable that event is keeping the running average in.

Near the bottom of this post is an example of serial event handling: http://www.codeworks.it/net/VBNetRs232.htm

(I admittedly do not know how VB handles multiple threads accessing the same variable, but be aware that in most languages multiple threads accessing the same data can produce some nasty results. You may want to add some code in to "lock" the shared variable from being written to while you are reading it. Perhaps even pausing the "MCU reader" event until you have read the variable)

February 11, 2010
by sask55
sask55's Avatar

Thanks for the thoughts.

I was just using the tempsensor project as an example of the type continually changing data stream that I am getting from my digital calliper. The calliper does not use Digimatic protocol. They use a Chinese scale protocol in witch there is a continues series of data sent out on the calliper data line The Chinese scales do not handshake the data output. The scale data is sent continuously with a fixed, ~300ms period. Therefore the micro is triggered on an interrupt to receive a new calliper position reading about 3 times per second. I am not interested in averaging where the calliper has been before the current reading. I am using the callipers to verify the positions of the sliding table that is being moved by stepper motors. There are two steppers and two callipers X and Y ( up down, side to side).

I have successfully been able to control the steppers through the miro with control sequences from the pc (using VBA code). I can read the raw data into the micro from the callipers and am working on C code to translate this Chinese data protocol to a position reading in inches. I think that I will be able to resolve my earlier issue that was resulting in multiple reading building up in a serial buffer between calls for a position reading by the PC. I intend to try and write a C code on the miro that will not send any output data to the uart until there is a call for the current position made from the VBA application on the pc. In this way I hope that I can disregard most of the data coming from the callipers to the miro by repeatedly overwriting the same variable with each new position the callipers send, as the position of the callipers is changed by the movement of the stepper motors controlled by the pc. When the VBA application requires a verification of the callipers position I hope to be able to send code to the miro to place the latest (current) position of the calliper of interest on the uart serial port and then read it back into the pc. I will trigger a read from the port prier to requesting a position in order to clear the serial buffer and avoid the situation I was getting where there was a great many reading there each time I made a call. In short my problem was sending all of the position readings sent from the callipers to the uart. I should just send the ones that I am interested in.

I anticipate I may have some issues with collisions on the interrupts. I currently only have one calliper and the serial involved. I hope to have at least two callipers three would be ideal (X,Y,Z). The VBA application is sending a continues stream of control data to the mirco to energize the correct sequence of motor coils to move the stepper in the correct direction and speed and stop in the desired locations. The callipers reading are intended to verify that the location is correct.

I think I have just about got this working.

Darryl

Post a Reply

Please log in to post a reply.

Did you know that inductors try to keep their current constant over short periods of time? Learn more...