89 lines
2.9 KiB
Markdown

# Getting Started
This guide describes the actual usage flow for AISU 830T Card Reader SDK v0.1.0.
## 1. Install the package
In Unity Package Manager, choose **Add package from git URL...** and enter:
```text
https://git.aisu.xin/aisu-sdk/card-reader-830t-package.git
```
The Package ID is `xin.aisu.card-reader-830t`. The minimum Unity version is 2021.3, and the current supported platform is Windows.
## 2. Import the Basic Sample
Open the package details, find the **Samples** section, and click **Import** for **Basic**. Open the imported `Scenes/CardReaderSample.unity` scene.
## 3. Add CardReaderManager
Create a GameObject in the scene and add the `CardReaderManager` component. Keep one Manager instance in the scene.
## 4. Configure the serial port
Configure `SerialOptions` in the Manager Inspector:
- `PortName`: the actual Windows COM port, for example `COM3`.
- `BaudRate`, `Parity`, `DataBits`, and `StopBits`: use the reader's settings.
- Keep the other defaults, or configure `ReadTimeout`, `WriteTimeout`, `AutoReconnect`, `ReconnectInterval`, and `MaxPendingSendCount` as needed.
## 5. Open and listen for events
Register listeners with `AddListener`, then call `CardReaderApi.Open()`:
```csharp
using Aisu.CardReader;
using UnityEngine;
public sealed class CardReaderUsage : MonoBehaviour
{
private void OnEnable()
{
CardReaderApi.OnConnected.AddListener(OnConnected);
CardReaderApi.OnCardReceived.AddListener(OnCardReceived);
CardReaderApi.OnError.AddListener(OnError);
}
private void Start()
{
CardReaderApi.Open();
}
private static void OnConnected()
{
Debug.Log("Reader connected.");
}
private static void OnCardReceived(CardInfo card)
{
Debug.Log($"{card.CardType}: {card.CardNumber}");
}
private static void OnError(CardReaderErrorEventArgs error)
{
Debug.LogError(error.Exception.Message);
}
private void OnDisable()
{
CardReaderApi.OnConnected.RemoveListener(OnConnected);
CardReaderApi.OnCardReceived.RemoveListener(OnCardReceived);
CardReaderApi.OnError.RemoveListener(OnError);
CardReaderApi.Close();
}
}
```
Once connected, present a card to the reader and handle `OnCardReceived`. `CardInfo.CardNumber` is a decimal string with a default minimum width of 10 digits; shorter values are left-padded with `0`, and longer values are not truncated.
## 6. Close
Call `CardReaderApi.Close()` to close the current connection and stop automatic reconnect. You can call `Open()` again later.
You can also call `OpenReader()` and `CloseReader()` on the current Manager.
## Lifetime
`CardReaderManager` releases serial resources when it is destroyed or when the application exits. Register events in `OnEnable` and remove them with `RemoveListener` in `OnDisable`. Without an active Manager, `CardReaderApi.Open()`, `Close()`, and event access throw `InvalidOperationException`.