# COM Port Test Writer - Simulates device sending data

This utility simulates a physical device (like scales) by continuously writing test data to a COM port.

## Features

- ✅ Writes test data continuously to COM port
- ✅ Multiple data types: scales, counter, random, mixed
- ✅ Configurable write interval
- ✅ Easy start/stop with Ctrl+C
- ✅ List available COM ports
- ✅ Command-line arguments

## Setup

1. **Install dependencies:**
   ```bash
   pip install -r requirements.txt
   ```

2. **Configure environment (optional):**
   ```bash
   copy .env.example .env
   # Edit .env with your settings
   ```

## Usage

### List Available COM Ports
```bash
python test_writer.py --list
```

### Basic Usage (using configured port)
```bash
python test_writer.py
```

### Specify COM Port
```bash
python test_writer.py --port COM3
```

### Different Data Types

**Scales Data (default)** - Simulates scale readings
```bash
python test_writer.py --type scales
```

**Counter Data** - Incrementing numbers
```bash
python test_writer.py --type counter
```

**Random Data** - Random numeric values
```bash
python test_writer.py --type random
```

**Mixed Sensor Data** - Temperature, humidity, pressure
```bash
python test_writer.py --type mixed
```

### Custom Write Interval
```bash
python test_writer.py --interval 2.0  # Send every 2 seconds
python test_writer.py --interval 0.5  # Send every 0.5 seconds
```

### Custom Baud Rate
```bash
python test_writer.py --baud 115200
```

## Testing Workflow

1. **Start Serial Bridge** (reads COM port)
   ```bash
   cd serial_bridge
   python app.py
   ```

2. **Start Test Writer** (writes test data)
   ```bash
   cd test_comport_writer
   python test_writer.py --type scales --interval 1.0
   ```

3. **View in React Frontend**
   - Open http://localhost:3000
   - Data should appear in real-time

## Configuration

Edit `.env` for default settings:

```env
COM_PORT=COM1
BAUD_RATE=9600
WRITE_INTERVAL=1.0
TEST_DATA_TYPE=scales
SCALES_MIN=0
SCALES_MAX=100
```

## Example Output

```
==================================================
COM Port Test Writer
==================================================
Port: COM1
Baud Rate: 9600
Data Type: scales
Write Interval: 1.0s
==================================================

✓ Connected to COM1 at 9600 baud
Press Ctrl+C to stop writing data

→ Sent: 45.23 kg
→ Sent: 45.67 kg
→ Sent: 46.12 kg
→ Sent: 46.45 kg
...
(Press Ctrl+C to stop)

✓ Disconnected from COM1
✓ Test writer stopped
```

## Troubleshooting

- **"COM port not found"**: Check `COM_PORT` setting and ensure port exists
- **"Port already in use"**: Another application is using the port (close Serial Bridge temporarily)
- **No data appearing**: Ensure Serial Bridge is running and pointing to same COM port
- **Baud rate mismatch**: Ensure test writer and serial bridge use same baud rate

## Complete System Test

To test the complete flow:

```bash
# Terminal 1: Django Backend
cd backend
venv\Scripts\activate
python manage.py runserver

# Terminal 2: React Frontend
cd frontend
npm start

# Terminal 3: Serial Bridge
cd serial_bridge
venv\Scripts\activate
python app.py

# Terminal 4: Test Writer
cd test_comport_writer
python test_writer.py --type scales --interval 1.0
```

Then visit http://localhost:3000 to see real-time data flow.
