![]() |
Forum Index : Microcontroller and PC projects : Com port problem
![]() ![]() ![]() ![]() |
|||||
Author | Message | ||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
That little code snippet will not work. When you open a comport the buffer is cleared. Then the for next loop is executed and it probably ends before anything arrives. [code] OPEN "COM1:19200" AS #2 Pin(48) = 1 'Set pin of HC-12 High - ie not connected do if LOC(#2) > 0 then A$=INPUT$(LOC(#2),#2) Print A$ Pause 500 endif loop [/code] Microblocks. Build with logic. |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
I'm such a stupid idiot Yes yesterday it wasn't working, and today I found out that the pins on the Transmit micromite wasn't wired correctly I fixed all that but when I tried the test code I was using PRINT instead of PRINT #2 I was only printing to the console and not the COM port I feel so damn stupid now |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4036 |
Don't feel stupid! We've all (well I have) done something vaguely similar. John |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
THanks Jean and everyone who helped Unfortunately my TTL cable has now broke (not my day at all) so no more testing till I get it fixed sometime. Then I can get back to seeing if the HC-12's work fine now |
||||
Zonker![]() Guru ![]() Joined: 18/08/2012 Location: United StatesPosts: 767 |
All good Lew..!! Everyone here has had the "what am i doing wrong" moment..! Me, several times..! It does help sharpen your troubleshooting skills (and thinking).. I learned that if you can't find the problem, find out what it isn't... Keep checking things and don't be afraid the get out "Mr. Scope"... Some of us are better at hardware, and some firmware... But, working as a group, we can usually get things sorted... Fine group of Gent's we got here..!! ![]() |
||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9590 |
Welcome to the club, Lew! ![]() It's a rite of passage when working with MCU's - look at it that way! Smoke makes things work. When the smoke gets out, it stops! |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
Back to square 1 After sorting my head out, sorting the connections out, sorting out the broken TTL cable I've got 2 working Micromites again. Now the tx unit is definitely 100% transmitting data on the HC-12 Wireless link The receive HC-12 is definitely 100% receiving the data transmitted by Micromite number 1 (tested by putting the receive hc-12 on the TTL cable and reading it on the PC) However I've got myself so confused now that I cannot display anything coming in on the receive micromite. Can Anyone tell me "simple code" to display EVERYTHING coming in on com1 please? As an aside - the data from the transmit unit is only transmitting ONCE every 8 seconds (I set it so it's transmitting the received weather data) |
||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9590 |
Run this: Dim D$ Open "COM1:19200" as #1 DO If LOC(#1)<>0 then 'If COM1 serial port buffer is NOT empty.... D$=INPUT$(LOC(#1),#1) 'then suck everything in the buffer out and stick it in D$.... Print "Data Received: ";D$ 'and print that data on the screen. Endif LOOP 'Do it all again. Untested, but should work fine so long as things are hooked up right. Smoke makes things work. When the smoke gets out, it stops! |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4036 |
In case you'd like to simply copy the chars, instead of Print "Data Received: ";D$ use Print D$; John |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
Thank you so much Grogster (and JohnS) I finally got it working ![]() EDITED: I'm a bigger idiot than I thought I was I had the wiring of the com ports on BOTH micromites wrong!!!!! I totally forgot that while Tx means an output from the Micromite TX on the HC-12 means data OUT I had it the other way round, I was connecting tx to tx like a fool and I did the same on both boards Me thinks it's time for the Chinese to make some more money for new boards! The white data is what the Transmitting micromite is sending but as a string of just the data The black data is the receiving micromite working ![]() |
||||
mikeb![]() Senior Member ![]() Joined: 10/04/2016 Location: AustraliaPosts: 174 |
Don't be so hard on yourself lew. We all do 'odd' things sometimes and it can be extremely difficult to check your own mistakes. It is amazing how your brain can make you see what you want to see sometimes. Check the following link (all of you) and you will see what I mean - I can't believe I can read this There are 10 kinds of people in the world. Those that understand binary and those that don't. |
||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9590 |
Agreed - as I said, Lew, welcome to the club. ![]() The club of people who make mistakes. What was it Billy Joel said? - "You're only Human. You're SUPPOSED to make mistakes." ![]() And mikeb - yeah, I have seen that scrambled word thing before, and it really is an amazing thing - you really CAN read it no problems at all, yet all the letters are totally wrong! But they put so much emphasis on correct spelling...... ![]() Smoke makes things work. When the smoke gets out, it stops! |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
Well if you simspell the firts or alst telter it cebosem omer fidfitulc. ![]() TheThailanguageshasanotherproblementhatisthattheydonotusespaces. Thaipeoplehavenoproblemwiththatthough. Istrugglewithit. Microblocks. Build with logic. |
||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9590 |
Huh?! ![]() Smoke makes things work. When the smoke gets out, it stops! |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
I've just got the Micromite working again after a month away from it My stupid memory problems are a right pain in the rear end - I've had to learn everything again, It's taken me a whole day to get to a stage where I know where I'm at again I now have a problem and I cannot figure it out (as usual) The outdoor Micromite is transmitting to the indoor Micromite using a pair of HC-12 UHF transceiver modules It's working great apart from WHen it prints it puts a line feed after each character so instead of printing "This is" It says t h i s i s The code I'm using on the indoor Micromite is [code]'This is the code on the receiving Micromite CLS RGB(BLUE) OPEN "COM1:19200" AS #2 Pin(48) = 0 'Set pin of HC-12 Connected do if LOC(#2) > 0 then A$=INPUT$(LOC(#2),#2) Print A$ endif loop [/code] I know it's only printing to the console, I put the cls in so I know it's definitely talking to the right Micromite. EDIT: If I just put [code]'This is the code on the receiving Micromite CLS RGB(BLUE) OPEN "COM1:19200" AS #2 Pin(48) = 0 'Set pin of HC-12 Connected do A$=INPUT$(LOC(#2),#2) Print A$ loop [/code] it fails with the error [quote] [6] A$=Input$(Loc(#2),#2) Error: Number out of bounds[/quote] I just need to put everything that COM1 receives into A$ so I can then either print the whole string on one line (so I can check what's being received) or parse it so it makes sense Transmitting code [quote]SetPin 16, DOUT 'HC-12 Power (Connected to COM1) Pin(16) = 0 'HC-12 Power On Open "COM1:19200" As #2 'Open port for HC-12 TX/RX Do Print #2, "Hello this is the HC-12 talking to you" Print "Hello this is the HC-12 talking to you" pause 1000 Loop[/quote] |
||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9590 |
Hi there. ![]() In your first code example, change the line that says: Print A$ so that it has a semi-colon on the end: Print A$; - that will suppress the carriage-return and line-feed, so the next character will be printed to the right of the last. In your next example, you get the error, cos you are trying to suck data from the serial-port buffer, before checking if it has any. As it has none, the values will be zero, so the equavalent code is A$=Input$(0,#2) - and zero is an invalid character count of bytes to get from the serial port buffer, so the code stops with the error you are seeing. To confirm if the serial port buffer is indeed empty, type ? LOC(#2) at the command prompt when you get the error, and press RETURN. If you get a 0 response, the serial port buffer is empty. A non-zero result means there are bytes in the buffer. The thing to remember is that the code only sucks into A$, what is in the serial-port buffer at the time that line of code runs so if your string arrived AFTER that line of code ran, you would still get the error - as there was nothing there to suck out of the buffer at the time that line of code ran. |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
Can't get the function working so went back to LEFT$, RIGHT$ and MID$ but still failing miserably I get the error [quote][10] blah = Left$( A$, 8 ) Error: Expected a number [/quote] In the code it waits till the com port has data before it goes any further so I don't understand why it's failing [code]CLS RGB(BLUE) BOX 9,9,780,460,2,rgb(RED) 'Box round screen Pin(48) = 0 'Set pin of HC-12 High Font 1, 2 ' Set the default font Open "COM1:19200" As #2 'Open port for HC-12 TX/RX OPEN "COM3:9200" AS #3 If LOC(#2)<>0 then LOOP 'If COM1 serial port buffer is NOT empty.... DO A$ = INPUT$(30, #2) blah = LEFT$( A$, 8 ) Print A$; LOOP End [/code] EDIT: if I put line 10 as blah = LEFT$( A$,3,8) It gets the same error |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10223 |
Please, please put OPTION EXPLICIT OPTION DEFAULT NONE at the top of your program (I've said this lots of times before). Everything will then break but it will force you to set the type of all your variables with DIM statements and it help you find bugs much more easily blah is a number, left$ gives a string (the clue is in the $) |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
Thanks Peter, I never can get my head around numbers and strings, yes I know it's simple and I do get it now, but I'll have forgotten once I leave the programming for a couple of days. Sorry BTW the reason I don't use OPTION EXPLICIT OPTION DEFAULT NONE is I'm waiting till the program is actually working then put them in and I should be able to figure out the problems easier that way as I know the program actually works |
||||
atmega8![]() Guru ![]() Joined: 19/11/2013 Location: GermanyPosts: 724 |
"is I'm waiting till the program is actually working then put them in and I should be able to figure out the problems easier that way as I know the program actually works" .......aaarrrggghhhhhh ![]() ![]() ![]() ![]() You should follow this advice, as it helps you during development of your code! Your "reason" is no reason. |
||||
![]() ![]() ![]() ![]() |
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |