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) !
本文介绍了使用C语言进行简单串口通信的方法,包括代码示例,适用于基础的串口数据传输需求。
最低0.47元/天 解锁文章

987

被折叠的 条评论
为什么被折叠?



