Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 14:32 26 Nov 2024 Privacy Policy
Jump to

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 : How do I read a single byte from the com port

Author Message
Gbbickerton
Newbie

Joined: 22/06/2022
Location: United Kingdom
Posts: 11
Posted: 07:49pm 02 Feb 2024
Copy link to clipboard 
Print this post

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: Australia
Posts: 6098
Posted: 07:56pm 02 Feb 2024
Copy link to clipboard 
Print this post

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   MMBasic Help
 
Gbbickerton
Newbie

Joined: 22/06/2022
Location: United Kingdom
Posts: 11
Posted: 08:00pm 02 Feb 2024
Copy link to clipboard 
Print this post

That would be the same as VAL(INPUT$(1,#3)) which returns a 0
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6098
Posted: 08:06pm 02 Feb 2024
Copy link to clipboard 
Print this post

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   MMBasic Help
 
Gbbickerton
Newbie

Joined: 22/06/2022
Location: United Kingdom
Posts: 11
Posted: 08:17pm 02 Feb 2024
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 11
Posted: 08:19pm 02 Feb 2024
Copy link to clipboard 
Print this post

I am sending binary data not text I want to read binary data into variables.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6098
Posted: 08:24pm 02 Feb 2024
Copy link to clipboard 
Print this post

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   MMBasic Help
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3150
Posted: 09:14pm 02 Feb 2024
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 11
Posted: 10:03pm 02 Feb 2024
Copy link to clipboard 
Print this post

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: Australia
Posts: 6098
Posted: 10:56pm 02 Feb 2024
Copy link to clipboard 
Print this post

  Quote  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.


Yes it is in the manual:
  Quote  ASC( string$ ) Returns the ASCII code (i.e. byte value) for the first letter in ‘string$’.

VK7JH
MMedit   MMBasic Help
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3801
Posted: 12:05am 03 Feb 2024
Copy link to clipboard 
Print this post

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 States
Posts: 3150
Posted: 12:31am 03 Feb 2024
Copy link to clipboard 
Print this post

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
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024