![]() |
Forum Index : Microcontroller and PC projects : How do I read a single byte from the com port
Author | Message | ||||
Gbbickerton Newbie ![]() Joined: 22/06/2022 Location: United KingdomPosts: 11 |
I am using INPUT #3,var to read a byte from com1 but I am getting an error message ERROR : line is to long VAL(INPUT$(1,#3)) returns a 0 so how do I do it? |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
dat$ = INPUT$(20, #5) ' get up to 20 characters from the serial port dat$ = INPUT$(1, #5) ' get 1 characters from the serial port then you want either x = val(dat$) or x = asc(dat$) Jim VK7JH MMedit |
||||
Gbbickerton Newbie ![]() Joined: 22/06/2022 Location: United KingdomPosts: 11 |
That would be the same as VAL(INPUT$(1,#3)) which returns a 0 |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
Try PRINTing the character to see what it is. for VAL to give something other than zero it has to be characters 1 to 9 asc() will also tell you what the character is. As mentioned in your other thread, make sure you have allowed enough time for the character to arrive before trying to read it. VK7JH MMedit |
||||
Gbbickerton Newbie ![]() Joined: 22/06/2022 Location: United KingdomPosts: 11 |
So does INPUT$(1,#3) return a binary value between 0 and 255 in the form of a string, if so how do I turn it into an integer? |
||||
Gbbickerton Newbie ![]() Joined: 22/06/2022 Location: United KingdomPosts: 11 |
I am sending binary data not text I want to read binary data into variables. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
Did you try x = asc(dat$) INPUT$(1,#3) returns a single byte. You can treat it as a character or a binary number 0 - 255 That is up to you. Jim VK7JH MMedit |
||||
lizby Guru ![]() Joined: 17/05/2016 Location: United StatesPosts: 3378 |
In the other thread, you said you were sending from a PICAXE. PICAXE can be set to send serial with either "idle high" or "idle low". If you don't get the values you expect, try sending from the PICAXE with the other idle setting. PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
Gbbickerton Newbie ![]() Joined: 22/06/2022 Location: United KingdomPosts: 11 |
The Picaxe is set right i can receive ascii text ok. INPUT$(1,#3) returns a string you cannot use var = INPUT$(1,#3) that gives an error its turning the string into an integer that is the problem. It looks like asc() might do the trick, it looks like it gives the binary value for the ascii character. thats not in the picomite manual though. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
Yes it is in the manual: VK7JH MMedit |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4044 |
The description of ASC() can be misleading. All it really does is get the value of the byte (the first char of the string passed to it) and return it as a number (so, in the range 0-255). You could do such as var = asc(input$(1,#3)) (Whereas VAL() turns a sequence of ASCII digits from a string into a number.) If your incoming data is really groups of 4 bytes which you want to treat as 32-bit integers there's going to be a faster way (other than the value1 + value2*256 etc) to dig them out that's likely a bit unreadable. As a Pico is really quite fast, try the readable way and see if it's good eough... John Edited 2024-02-03 10:09 by JohnS |
||||
lizby Guru ![]() Joined: 17/05/2016 Location: United StatesPosts: 3378 |
It's tricky getting 32-bit integers out of the Picaxe, with defined 8-bit and 16-bit integer variables. Why not just send the values as ascii text--easier for a human to understand, and easy enough to covert into integers with VAL? If speed is an issue, increase the baud rate. Better assistance might be available if it was known what the numbers represent, how fast they are coming, and what needs to be done with them. PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |