This page describes how to use the built-in serial ports (also knownas USARTs). For more information about serial ports, seeUSART.
Contents
First, decide which serial port you wish to use, and connect itspins to the device you’re communicating with. (The TXand RX pins for a serial port are labeled on your board’s silkscreen;for example, serial port 2 has pins labeled “TX2” and “RX2”).
Blog post Serial RS232 connections in Python. Import time import serial # configure the serial connections (the parameters differs on the device you are connecting to) ser = serial.Serial( port='/dev/ttyUSB1', baudrate=9600, parity=serial.PARITYODD, stopbits=serial.STOPBITSTWO, bytesize=serial.SEVENBITS ) ser.isOpen print 'Enter your commands below.rnInsert 'exit' to leave. PureBasic Forum PureBasic web site PureBasic blog page: FAQ: Last visit was: Thu Jul 23, 2020 11:02 pm. Hi, how can I read the serial port as the GW-Basic program. Network Serial Port Kit. Network Serial Port Kit allows you to connect any serial port devices over the TCP/IP network or Internet. Creation of local virtual serial ports pairs are available as well.For example you can convert usual PC to 'Serial Port Server' so then you can connect any serial device directly to a TCP/IP network and communicate with that device from any other PC in the same.
The variable for controlling a serial port is the word
Serial
,plus the serial port’s number. For example, you can control serialport 1 with the variable Serial1
, serial port 2 with Serial2
,and so on.In order to get started using your serial port, you’ll first need toturn it on. Do this by calling your serial port’s
begin()
function, giving it the baud rate you wish it to communicate at. Ifyou’re not sure what baud rate to use, 9600 is a safe (although slow)value to try. Put this call to begin()
in your setup(),like in the following example:Now that your serial port is set up, it’s time to start communicating.
One common use for serial ports is to print strings and otherdebugging information to a computer. You can print numbers or stringsusing
print()
and println()
, like this:![Purebasic Serial Port Example Purebasic Serial Port Example](/uploads/1/1/8/8/118894039/882287686.jpg)
This sort of communication can go both ways: you can send charactersfrom a computer to a serial port as well. You can check how manycharacters are waiting for you to read using the
available()
function, and read them out one at a time using read()
. Thefollowing example program uses these functions to “echo” back anythingsent to Serial2
:This section gives a full listing of functions available for use withserial ports.
All of the
Serial[1,2,3]
objects are instances of theHardwareSerial
class, which is documented in this section. (Thismeans that you can use any of these functions on any of Serial1
,Serial2
, and Serial3
).HardwareSerial
¶Serial port class. Predefined instances are
Serial1
,Serial2
, and Serial3
.HardwareSerial::
begin
(unsigned int baud)¶Set up a
HardwareSerial
object for communications. This methodmust be called before attempting to use the HardwareSerial
object (typically, you call this in your setup() function).HardwareSerial::
end
()¶Disables the USART associated with this object, allowing anyassociated communication pins to be used for other purposes.
HardwareSerial::
available
()¶Returns the number of bytes available for reading.
unsigned char HardwareSerial::read()
Returns the next available, unread character. If there are noavailable characters (you can check this with
available
), the call will block until onebecomes available.HardwareSerial::
flush
()¶Throw away the contents of the serial port’s receiver (RX) buffer.That is, clears any buffered characters, so that the next characterread is guaranteed to be new.
void HardwareSerial::print(unsigned char b)
Print the given byte over the USART.
HardwareSerial::
print
(char c)¶Print the given character over the USART. 7-bit clean charactersare typically interpreted as ASCII text.
HardwareSerial::
print
(const char *str)¶Print the given null-terminated string over the USART.
HardwareSerial::
print
(int n)¶Print the argument’s digits over the USART, in decimal format.Negative values will be prefixed with a
'-'
character.HardwareSerial::
print
(unsigned int n)¶Print the argument’s digits over the USART, in decimal format.
HardwareSerial::
print
(long n)¶Print the argument’s digits over the USART, in decimal format.Negative values will be prefixed with a
'-'
character.HardwareSerial::
print
(unsigned long n)¶Print the argument’s digits over the USART, in decimal format.
HardwareSerial::
print
(long n, int base)¶Print the digits of
n
over the USART, in base base
(whichmay be between 2 and 16). The base
value 2 corresponds tobinary, 8 to octal, 10 to decimal, and 16 to hexadecimal. Negativevalues will be prefixed with a '-'
character.HardwareSerial::
print
(double n)¶Print
n
, accurate to 2 digits after the decimal point.HardwareSerial::
println
(char c)¶Like
print(c)
, followed by 'rn'
.HardwareSerial::
println
(const char *c)¶Like
print(c)
, followed by 'rn'
.void HardwareSerial::println(unsigned char b)
Like
print(b)
, followed by 'rn'
.HardwareSerial::
println
(int n)¶Like
print(n)
, followed by 'rn'
.HardwareSerial::
println
(unsigned int n)¶Like
print(n)
, followed by 'rn'
.HardwareSerial::
println
(long n)¶Like
print(n)
, followed by 'rn'
.HardwareSerial::
println
(unsigned long n)¶Like
print(n)
, followed by 'rn'
.HardwareSerial::
println
(long n, int base)¶Like
print(n,b)
, followed by 'rn'
.HardwareSerial::
println
(double n)¶Like
print(n)
, followed by 'rn'
.HardwareSerial::
println
()¶Prints
'rn'
over the USART.void HardwareSerial::write(unsigned char ch)
Sends one character over the USART. This function is currentlyblocking.
![Purebasic Serial Port Example Purebasic Serial Port Example](/uploads/1/1/8/8/118894039/459938710.jpg)
This is a low-level function. One of the
print()
orprintln()
functions is likely to be more useful when printingmultiple characters, when formatting numbers for printing, etc.HardwareSerial::
write
(const char *str)¶Send the given null-terminated character string over the USART.
This is a low-level function. One of the
print()
orprintln()
functions is likely to be more useful when printingmultiple characters, when formatting numbers for printing, etc.HardwareSerial::
write
(void *buf, unsigned int size)¶Writes the first
size
bytes of buf
over the USART. Eachbyte is transmitted as an individual character.This is a low-level function. One of the
print()
orprintln()
functions is likely to be more useful when printingmultiple characters, when formatting numbers for printing, etc.HardwareSerial::
txPin
()¶Purebasic Serial Port Examples
Return the number of the TX (transmit) pin.
Purebasic Serial Port Example 1
HardwareSerial::
rxPin
()¶Return the number of the RX (receive) pin.
Purebasic Book
Unlike the Arduino, none of the Maple’s serial ports is connected tothe USB port on the Maple board. If you want to communicate using thebuilt-in USB port, use SerialUSB instead. Youwill need an additional USB-to-serial adapter to communicate between aUSART and your computer.
License and Attribution
Portions of this page were adapted from the Arduino ReferenceDocumentation, whichis released under a Creative Commons Attribution-ShareAlike 3.0License.