We are trying to build an connection between Arduino UNO R3 and an Impedance Analyzer (embedded window OS). write a program that can give command to Arduino through USB to control Multiplexer to change channel for Impedance Analyzer.
now the problem is that we don't know how to send command as to Arduino just like an input through USB, and also the program running in windows must be written in VB.
is this possible? if yes, how can i achieve this? thanks very much.
Yeah - just use the serial port. You can mock up behavior by pretending to be your program w/the serial monitor, too. There's tons of information here on how to use the serial port to accept commands and such.
You will find more information about this sort of thing in the Threads in the "Interfacing w/Software on the Computer" section of the Forum.
The examples in serial input basics should also be useful. Just design your VB code to be compatible.
...R
This might help, although it's python not vb. You'll need to figure out how to send something similar in vb.
The python sends codes to move servos... first it sends a start character, then the servo number and the degrees to move to. Once the start character is received at Arduino, it reads the next two characters (ie the servo number and degrees) into an array and then uses those values to drive the appropriate servo.
I know that's not what you want to do, but in essence it's the same thing. You need to read in a character (or a bunch of characters) and act according to what it or they are.
Here's the python code:
#!/usr/bin/env python
################################################
# Module: servo.py
# Created: 2 April 2008
# Author: Brian D. Wendt
# http://principialabs.com/
# Version: 0.3
# License: GPLv3
# http://www.fsf.org/licensing/
'''
Provides a serial connection abstraction layer
for use with Arduino "MultipleSerialServoControl" sketch.
'''
################################################
import serial
# Assign Arduino's serial port address
# Windows example
# usbport = 'COM3'
# Linux example
# usbport = '/dev/ttyUSB0'
# MacOSX example
# usbport = '/dev/tty.usbserial-FTALLOK2'
usbport = 'COM10'
# Set up serial baud rate
ser = serial.Serial(usbport, 9600, timeout=1)
def move(servo, angle):
'''Moves the specified servo to the supplied angle.
Arguments:
servo
the servo number to command, an integer from 1-4
angle
the desired servo angle, an integer from 0 to 180
(e.g.) >>> servo.move(2, 90)
... # "move servo #2 to 90 degrees"'''
if (0 <= angle <= 180):
ser.write(chr(255))
ser.write(chr(servo))
ser.write(chr(angle))
else:
print("Servo angle must be an integer between 0 and 180.\n")
def init():
move(1,90)
move(2,90)
move(3,90)
move(4,90)
move(5,90)
move(6,90)
#init()
while True:
a,b=input("Enter servo number, angle: ")
move(a,b)
And here's the Arduino sketch:
/*
* ------------------------------
* MultipleSerialServoControl
* ------------------------------
*
* Uses the Arduino Serial library
* (http://arduino.cc/en/Reference/Serial)
* and the Arduino Servo library
* (http://arduino.cc/en/Reference/Servo)
* to control multiple servos from a PC using a USB cable.
*
* Dependencies:
* Arduino 0017 or higher
* (http://www.arduino.cc/en/Main/Software)
* Python servo.py module
* (http://principialabs.com/arduino-python-4-axis-servo-control/)
*
* Created: 23 December 2009
* Author: Brian D. Wendt
* (http://principialabs.com/)
* Version: 1.1
* License: GPLv3
* (http://www.fsf.org/licensing/)
*
*/
// Import the Arduino Servo library
#include <Servo.h>
// Create a Servo object for each servo
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
// TO ADD SERVOS:
// Servo servo5;
// etc...
// Common servo setup values
int minPulse = 600; // minimum servo position, us (microseconds)
int maxPulse = 2400; // maximum servo position, us
// User input for servo and position
int userInput[3]; // raw input from serial buffer, 3 bytes
int startbyte; // start byte, begin reading input
int servo; // which servo to pulse?
int pos; // servo angle 0-180
int i; // iterator
// LED on Pin 13 for digital on/off demo
int ledPin = 13;
int pinState = LOW;
void setup()
{
// Attach each Servo object to a digital pin
servo1.attach(9, minPulse, maxPulse);
servo2.attach(3, minPulse, maxPulse);
servo3.attach(4, minPulse, maxPulse);
servo4.attach(5, minPulse, maxPulse);
servo5.attach(10, minPulse, maxPulse);
servo6.attach(11, minPulse, maxPulse);
// TO ADD SERVOS:
// servo5.attach(YOUR_PIN, minPulse, maxPulse);
// etc...
// LED on Pin 13 for digital on/off demo
pinMode(ledPin, OUTPUT);
// Open the serial connection, 9600 baud
Serial.begin(9600);
}
void loop()
{
// Wait for serial input (min 3 bytes in buffer)
if (Serial.available() > 2) {
// Read the first byte
startbyte = Serial.read();
// If it's really the startbyte (255) ...
if (startbyte == 255) {
// ... then get the next two bytes
for (i=0;i<2;i++) {
userInput[i] = Serial.read();
}
// First byte = servo to move?
servo = userInput[0];
// Second byte = which position?
pos = userInput[1];
// Packet error checking and recovery
if (pos == 255) {
servo = 255;
}
// Assign new position to appropriate servo
switch (servo) {
case 1:
servo1.write(pos); // move servo1 to 'pos'
break;
case 2:
servo2.write(pos);
break;
case 3:
servo3.write(pos);
break;
case 4:
servo4.write(pos);
break;
case 5:
servo5.write(pos);
break;
case 6:
servo6.write(pos);
break;
// TO ADD SERVOS:
// case 5:
// servo5.write(pos);
// break;
// etc...
// LED on Pin 13 for digital on/off demo
case 99:
if (pos == 180) {
if (pinState == LOW) {
pinState = HIGH;
}
else {
pinState = LOW;
}
}
if (pos == 0) {
pinState = LOW;
}
digitalWrite(ledPin, pinState);
break;
}//switch
if (pos<10 || pos>170)
{
pinState=HIGH;
}
else
{
pinState=LOW;
}
digitalWrite(ledPin, pinState);
}//startbyte ok
}//serial avail
}//loop