Skip to content

spider-gazelle/crystal-bacnet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BACnet Support for Crystal Lang

CI

it supports

  • the IPv4 data link layer (BACnet/IP)
  • the websocket data link layer (BACnet/SC)

Installation

Add the dependency to your shard.yml:

dependencies:
  bacnet:
    github: spider-gazelle/crystal-bacnet

Usage

require "bacnet"
require "socket"

# ======================
# Create transport layer
# ======================
server = UDPSocket.new
server.bind "0.0.0.0", 0xBAC0

# ======================
# Hook up the client to the transport
# ======================
client = BACnet::Client::IPv4.new

client.on_transmit do |message, address|
  if address.address == Socket::IPAddress::BROADCAST
    # Broadcast this message, might need to be a unicast to a BBMD
  else
    server.send message, to: address
  end
end

spawn do
  # Feed data to the client
  loop do
    break if server.closed?
    bytes, client_addr = server.receive

    message = IO::Memory.new(bytes).read_bytes(BACnet::Message::IPv4)
    client.received message, client_addr
  end
end

# ======================
# Collect device information
# ======================
registry = BACnet::Client::DeviceRegistry.new(client)
registry.on_new_device do |device|
  puts "FOUND NEW DEVICE: #{device.vendor_name} #{device.name} #{device.model_name}"
end

# broadcast a who_is out onto the network
client.who_is