Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : MM 4.0b - Possible bug in Input$()
Author | Message | ||||
Greg Fordyce Senior Member Joined: 16/09/2011 Location: United KingdomPosts: 153 |
I am working on a project to use a MM as a master controller for monitoring batteries in an electric vehicle. The MM is connected to slave boards that send battery voltage data to it. I receive 19 bytes from each slave board, the first 18 bytes is data for 12 cells (The slave has a 12 bit adc), last byte is a crc check, I am ignoring that for now. The problem I was having is the data was getting corrupted. I have posted the first part of my code below, I can post all of it if it helps, but here is the relevant portion; For I = 0 To 18 RawCV(I) = Asc(Input$(1,#1)) 'Read cell data from slave Pause 3 '3ms DELAY REQUIRED FOR CORRECTLY READING DATA FROM SERIAL PORT ' Print RawCV(I)," "; 'Diagnostic output only Next I When I added the line "Print RawCV(I)" to see what was happeneing, the program started behaving as it should. After a bit of messing about I found that just by adding the line "Pause 3" made the program behave as it should. I am using the MM SM1 mono. Unless I am doing something wrong there would appear to be a bug in either Input$() or maybe in the serial port RX buffer. Anyway I have found a workaround. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''' ' 6802 Master Version 0.02 Alpha ' Greg Fordyce 18 Nov 12 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''' Cls Font #2 Print "6802 Master Version 0.02 Alpha" Pause 2000 Open "COM1:9600" As #1 'SlvNo = 6 'Number of slaves in pack SlvNo = 1 'CellNo = 48 'Number of cells in pack CellNo = 12 Dim RawCV(19) 'Raw data stream from slaves Dim StackV(SlvNo,13) 'Individual cell voltages in pack stored in a 2 dim array 'First dimension is slave number (first slave is 0) 'Second dimension is cells in each stack (range 4-12) 'StackV(x,0) is number of cells in each stack. Dim CRCtable(256) 'Lookup table for CRC-8 error checking Dim CellV(CellNo + 1) 'Cell voltage, CellV(1) = cell 1, CellV(2) = cell 2, etc 'first 4 with 8 cells, no.5 = 7, no.6 = 9 StackV(0,0) = 12 'test value Cls Main: Do Print #1,Chr$(16); ' Send STCVAD to slaves (start cell voltage A/D) Pause 20 Print #1,Chr$(4); ' Read cell voltage register from slave Pause 20 Do StackNo = 0 'Pointer for slave currently being read For I = 0 To 18 RawCV(I) = Asc(Input$(1,#1)) 'Read cell data from slave Pause 3 '3ms DELAY REQUIRED FOR CORRECTLY READING DATA FROM SERIAL PORT ' Print RawCV(I)," "; 'Diagnostic output only Next I <snip> P.S. Link to slave boards |
||||
rhamer Senior Member Joined: 06/06/2011 Location: AustraliaPosts: 174 |
You problem is one of timing. The maximise will execute your for next loop much faster than the serial stream can send characters. So what is happening is your loop runs 19 times before all the data has arrived. You need to do something like sit in a while loop until you have all 19 characters. If you try and solve it with delays, it will likely break and give unreliable results as you have no way of knowing or handling any delays that may happen at the sending end. Cheers Rohan Rohan Hamer HAMFIELD Software & Hardware Solutions Makers of the Maximite Expander. http://www.hamfield.com.au |
||||
Greg Fordyce Senior Member Joined: 16/09/2011 Location: United KingdomPosts: 153 |
Thanks Rohan, I have a "Pause 20" before the next loop, I'll increase it to "Pause 200" which will definitely give it enough time to fill the RX buffer. I suppose I should really get a bit more clever and actually check how many bytes are in the buffer before running the next loop. I'll report my results tonight. Greg |
||||
rhamer Senior Member Joined: 06/06/2011 Location: AustraliaPosts: 174 |
You do it like this. Do while rxcharcount < 19 Input If input <> "" then Rxcharcount = rxcharcount + 1 Add it to your array End if Loop Obviously the syntax is not right, as I can't remember it off hand, but you get the idea. This way it will only block further execution for just the time needed, and it doesn't matter if there is a delay at the other end. Cheers Rohan Rohan Hamer HAMFIELD Software & Hardware Solutions Makers of the Maximite Expander. http://www.hamfield.com.au |
||||
Greg Fordyce Senior Member Joined: 16/09/2011 Location: United KingdomPosts: 153 |
Turns out it was my mistake, I was trying to read the RX buffer before it got filled. Increasing the "Pause 20" to "Pause 30" has sorted the problem for now, but I really need to check the buffer is ready before trying to read it. Thanks to Rohan for spotting the error. Greg |
||||
Print this page |