简单的串口通信代码

本文介绍了使用C语言进行简单串口通信的方法,包括代码示例,适用于基础的串口数据传输需求。
摘要由CSDN通过智能技术生成
python代码
#!/usr/bin/python2.7
import sys
import serial
import time


try:
 ser = serial.Serial('/dev/ttyUSB0', 9600)
except Exception, e:
 print 'open serial failed.%s'%e
 exit(1)

 # echo
print 'open serial success'
ser.write('B')
time.sleep(3)
ser.write('b')
ser.close()

C代码

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
  
int main()
{
  int fd, wr_num, rd_num, status;
  
  char send_buf[] = "B",send_buf2[] = "b", recv_buf[200];
  
  fd = open("/dev/ttyUSB0", O_RDWR);
  if (fd == -1)
    {
    printf("can't not open the COM1! \n");
    }
  else
    {
    printf("Open COM1 success! \n");
    }
  
  struct termios Opt;
  if (tcgetattr(fd, &Opt) !
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值