linux下与Arduino通信

来源:互联网 发布:淘宝买彩票安全吗 编辑:程序博客网 时间:2024/06/07 13:33

第一次使用Arduino IDE时,如果不是要求最新的版本,在Ubuntu的应用商店里即可下载到Arduino的IDE,安装完成后会提示将当前用户添加到dialout用户组中,这样才能下载,选择对话框的ADD即可,或者用下面的语句:
Arduino官网示例

ls -l /dev/ttyACM*you will get something like:crw-rw---- 1 root dialout 188, 0 5 apr 23.01 ttyACM0The "0" at the end of ACM might be a different number, or multiple entries might be returned. The data we need is "dialout" (is the group owner of the file).Now we just need to add our user to the group:sudo usermod -a -G dialout <username>where <username> is your linux user name. You will need to log out and log in again for this change to take effect. 

在arduino pro mini上进行串口通信时,利用FTD1232,下载时需要用DTR data terminal ready,这样就不用在下载时按复位键了。 在linux上与它通信时,有个现象,重新拔插USB后,第一此执行程序时,没反应,再次执行时,就可以了,后来拔掉DTR后,无论是否刚插上USB,都可以直接进行通信。
linux下:

//============================================================================// Name        : SerialPortSend.cpp// Author      : zn// Version     :// Copyright   : Your copyright notice// Description : Hello World in C++, Ansi-style//============================================================================#include <iostream>#include <stdio.h> /* Standard input/output definitions */#include <string.h> /* String function definitions */#include <unistd.h> /* UNIX standard function definitions   write 在这里 */#include <fcntl.h> /* File control definitions  O_RDWR 在这里 */#include <errno.h> /* Error number definitions */#include <termios.h> /* POSIX terminal control definitions */using namespace std;int  ToSerial(){    int  file = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY); //以读写方式打开串口,不控制TTY    if(file < 0)    {        printf("error to open\n");        return 1;    }    usleep(20000);    write(file,"b",2);    usleep(20000);    write(file,"f",2);    usleep(20000);    usleep(20000);    printf("hello!\n");    close(file);    return 0;}int main() {    for(int i=0;i<1;i++)    {        ToSerial();    }    return 0;}

Arduino下:

int x;void setup()  {    Serial.begin(9600);  pinMode(9,OUTPUT); // Enable   pinMode(3,OUTPUT); // Step    pinMode(2,OUTPUT); // Dir  }  void loop()  {    char ch = Serial.read();  switch(ch){  case 'f':     digitalWrite(9,LOW); // Set Enable low     fwd();    Serial.println("receive f");    digitalWrite(9,HIGH); // Set Enable low     break;  case 'b':    digitalWrite(9,LOW); // Set Enable low     bwd();    Serial.println("receive b");    digitalWrite(9,HIGH); // Set Enable low     break;  }}void fwd(){  digitalWrite(2,HIGH); // Set Dir high    for(x = 0; x < 400; x++) // Loop 400 times  1.8*200=360    {      digitalWrite(3,HIGH); // Output high        delayMicroseconds(5000); // Wait  ms        digitalWrite(3,LOW); // Output low        delayMicroseconds(2500); // Wait a ms      } delay(1000); // pause one second  }void bwd(){  digitalWrite(2,LOW); // Set Dir low    for(x = 0; x < 400; x++) // Loop 400 times      {        digitalWrite(3,HIGH); // Output high        delayMicroseconds(5000); // Wait ms        digitalWrite(3,LOW); // Output low        delayMicroseconds(2500); // Wait ms      }delay(1000); // pause one second  }

浅谈linux下的串口通讯开发

linux下查看串口信息

sudo ttylog -d /dev/ttyACM0 -b 38400(这个是波特率)
0 0
原创粉丝点击