Sie sind auf Seite 1von 1

TM

4 Port UART
Arduino MEGA sample code
Revised 7/10/15

//This sample code was written on an Arduino MEGA 2560.


//It will allow you to control up to 4 Atlas Scientific devices through 1 serial RX/TX line.
//To open a channel (marked on the board as PRB 1 to PRB 4) send the number of the channel, a colon and the command
//ending with a carriage return.

//1:r<CR>
//2:i<CR>
//3:c<CR>
//4:r<CR>

//To open a channel and not send a command just send the channel number followed by a colon.

//1:<CR>
//3:<CR>

5V
MADE IN
ITALY 22
GND

9
8

7
6
5
4
3
2
1
0
13
12

10
11
AREF

SDA 20
RX3 15

RX2 17

RX1 19

SCL 21
TX3 14

TX2 16

TX1 18

24
PWM
RX0
TX0

26
L COMMUNICATION 28
30 31
TX
ON 32 33
RX
34 35
36 37
38 39
40 41
42 43
44 45

MEGA 46 47
DIGITAL

48 49
50 51
52 53
POWER ANALOG IN
RESET
IOREF

GND

GND

A10

A12
A13
A14
A15
A11
3V3

GND
VIN

A0
A1

A2
A3
A4
A5

A7

A8
A9
5V

X Y PRB
0 0 1

0 1 2

1 0 3
1 1 4

int Pin_x = 2; //Arduino pin 2 to control pin x


int Pin_y = 3; //Arduino pin 3 to control pin y

char computerdata[20]; //A 20 byte character array to hold incoming data from a pc/mac/other
char sensordata[30]; //A 30 byte character array to hold incoming data from the sensors
byte computer_bytes_received=0; //We need to know how many characters bytes have been received
byte sensor_bytes_received=0; //We need to know how many characters bytes have been received

char *channel; //Char pointer used in string parsing


char *cmd; //Char pointer used in string parsing

void setup(){
Serial.begin(9600); //set baud rate for the hardware serial port 0 to 9600
Serial1.begin(9600); //set baud rate for the hardware serial port 1 to 9600
pinMode(Pin_x, OUTPUT); //Set the digital pin as output.
pinMode(Pin_y, OUTPUT); //Set the digital pin as output.
}

void serialEvent(){
computer_bytes_received=Serial.readBytesUntil(13,computerdata,20); //This interrupt will trigger when the data coming from
computerdata[computer_bytes_received]=0; //the serial monitor(pc/mac/other) is received
} //We read the data sent from the serial monitor
//(pc/mac/other) until we see a <CR>.
//We also count how many characters have been received
//We add a 0 to the spot in the array just after the last
//character we received.. This will stop us from
//transmitting incorrect data that may have been left
//in the buffer

void serialEvent1(){
sensor_bytes_received=Serial1.readBytesUntil(13,sensordata,30); //This interrupt will trigger when the data coming from
sensordata[sensor_bytes_received]=0; //the serial monitor(pc/mac/other) is received
} //we read the data sent from the Atlas Scientific device
//until we see a <CR>. We also count how many character
//have been received
//we add a 0 to the spot in the array just after the last
//character we received. This will stop us from
//transmitting incorrect data that may have been left
//in the buffer

void loop(){

if(computer_bytes_received!=0){ //If computer_bytes_received does not equal zero


channel=strtok(computerdata, ":"); //Let's parse the string at each colon
cmd=strtok(NULL, ":"); //Let's parse the string at each colon
open_channel(); //Call the function "open_channel" to open the correct data path
Serial1.print(cmd); //Send the command from the computer to the Atlas Scientific device using serial port 1
Serial1.print("\r"); //After we send the command we send a carriage return <CR>
computer_bytes_received=0; //Reset the var computer_bytes_received to equal 0
}

if(sensor_bytes_received!=0){ //If data has been transmitted from an Atlas Scientific device
Serial.println(sensordata); //let’s transmit the data received from the Atlas Scientific device to the serial monitor
sensor_bytes_received=0; //Reset the var sensor_bytes_received to equal 0
}
}

void open_channel(){ //This function controls what UART port is opened.

switch (*channel) { //Looking to see what channel to open

case '1': //If *channel==1 then we open channel 1


digitalWrite(Pin_x, LOW); //Pin_x and pin_y control what channel opens
digitalWrite(Pin_y, LOW); //Pin_x and pin_y control what channel opens
break; //Exit switch case

case '2':
digitalWrite(Pin_x, LOW);
digitalWrite(Pin_y, HIGH);
break;

case '3':
digitalWrite(Pin_x, HIGH);
digitalWrite(Pin_y, LOW);
break;

case '4':
digitalWrite(Pin_x, HIGH);
digitalWrite(Pin_y, HIGH);
break;
}

Click here to download the *.ino file


Atlas-Scientific.com Copyright © Atlas Scientific LLC All Rights Reserved

Das könnte Ihnen auch gefallen