linux下与Arduino通信

第一次使用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 ttyACM0

The "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
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zn百里屠苏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值