装有linux系统的PC和串口微型打印机相连,怎么通过C语言设计一个串口程序发送指令

dailulu13 2016-11-03 11:00:53
装有linux系统的PC和串口微型打印机相连,怎么通过C语言设计一个串口程序,把打印机的控制指令比如(ESC HT )十六进制(1B 09), (ESC &) 十六进制(1C 26) , 这样的指令,通过串口程序发送给打印机达到控制打印机打印汉字的目的,打印机有汉字集。
...全文
305 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
dailulu13 2016-11-03
  • 打赏
  • 举报
回复
引用 1 楼 dailulu13 的回复:
没有人会么。。。。。。求大神
引用 2 楼 Arnold9009 的回复:
之前用过的代码给你贴上来,也是在网上找了一个修改的 #include <stdio.h> /*标准输入输出定义*/ #include <stdlib.h> /*标准函数库定义*/ #include <unistd.h> /*Unix 标准函数定义*/ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> /*文件控制定义*/ #include <termios.h> /*PPSIX 终端控制定义*/ #include <errno.h> /*错误号定义*/ #define COM_DEVICE "/dev/ttyUSB0" static int openSerialDevice() { int fd; fd = open(COM_DEVICE, O_RDWR); return fd; } static void closeSerialDevice(int fd) { close(fd); } /** *@brief 设置串口通信速率 *@param fd 类型 int 打开串口的文件句柄 *@param speed 类型 int 串口速度 *@return void */ static int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,}; static int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200, 300,}; static void set_speed(int fd, int speed){ int i; int status; struct termios Opt; tcgetattr(fd, &Opt); for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) { if (speed == name_arr[i]) { tcflush(fd, TCIOFLUSH); cfsetispeed(&Opt, speed_arr[i]); cfsetospeed(&Opt, speed_arr[i]); status = tcsetattr(fd, TCSANOW, &Opt); if (status != 0) { perror("tcsetattr fd1"); return; } tcflush(fd,TCIOFLUSH); } } } /** *@brief 设置串口数据位,停止位和效验位 *@param fd 类型 int 打开的串口文件句柄 *@param databits 类型 int 数据位 取值 为 7 或者8 *@param stopbits 类型 int 停止位 取值为 1 或者2 *@param parity 类型 char 效验类型 取值为N,E,O,,S */ static int set_Parity(int fd,int databits,int stopbits,char parity) { struct termios options; if ( tcgetattr( fd,&options) != 0) { perror("SetupSerial 1"); return -1; } options.c_cflag &= ~CSIZE; switch (databits) /*设置数据位数*/ { case 7: options.c_cflag |= CS7; break; case 8: options.c_cflag |= CS8; break; default: fprintf(stderr,"Unsupported data size\n"); return -2; } switch (parity) { case 'n': case 'N': options.c_cflag &= ~PARENB; /* Clear parity enable */ options.c_iflag &= ~INPCK; /* Enable parity checking */ break; case 'o': case 'O': options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/ options.c_iflag |= INPCK; /* Disnable parity checking */ break; case 'e': case 'E': options.c_cflag |= PARENB; /* Enable parity */ options.c_cflag &= ~PARODD; /* 转换为偶效验*/ options.c_iflag |= INPCK; /* Disnable parity checking */ break; case 'S': case 's': /*as no parity*/ options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB;break; default: fprintf(stderr,"Unsupported parity\n"); return -3; } /* 设置停止位*/ switch (stopbits) { case 1: options.c_cflag &= ~CSTOPB; break; case 2: options.c_cflag |= CSTOPB; break; default: fprintf(stderr,"Unsupported stop bits\n"); return -4; } options.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); // options.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|INPCK); tcflush(fd,TCIFLUSH); options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/ options.c_cc[VMIN] = 0; /* Update the options and do it NOW */ if (tcsetattr(fd,TCSANOW,&options) != 0){ perror("SetupSerial 3"); return -5; } return 0; } int serial_data_read(int fd, char *buf, int len) { int nRead; nRead = read(fd, buf, len); return nRead; } int serial_data_send(int fd, char *buf, int len) { int nWrite; nWrite = write(fd, buf, len); return nWrite; } #define TEST_SEND_DATA "1234567890-115200" int main(void) { int sfd; int ret; int i; char dataBuf[1024] = {0}; sfd = openSerialDevice(); if (sfd < 0){ printf("open device failed!\n"); return -1; } set_speed(sfd, 115200); ret = set_Parity(sfd, 8, 1, 'N'); if (ret < 0){ printf("set parity error, ret: %d\n", ret); return -2; } while(1){ printf("send data..115200....."); ret = serial_data_send(sfd, TEST_SEND_DATA, sizeof(TEST_SEND_DATA)); printf("%d sent\n", ret); printf("start recive data:\n"); ret = serial_data_read(sfd, dataBuf, 1024); printf("len=%d, data=", ret); for(i = 0; i < ret; i++) printf("%c", dataBuf[i]); printf("\n"); } }
可以发送指令么,比如ASCII码或者十六进制
Arnold9009 2016-11-03
  • 打赏
  • 举报
回复
之前用过的代码给你贴上来,也是在网上找了一个修改的 #include <stdio.h> /*标准输入输出定义*/ #include <stdlib.h> /*标准函数库定义*/ #include <unistd.h> /*Unix 标准函数定义*/ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> /*文件控制定义*/ #include <termios.h> /*PPSIX 终端控制定义*/ #include <errno.h> /*错误号定义*/ #define COM_DEVICE "/dev/ttyUSB0" static int openSerialDevice() { int fd; fd = open(COM_DEVICE, O_RDWR); return fd; } static void closeSerialDevice(int fd) { close(fd); } /** *@brief 设置串口通信速率 *@param fd 类型 int 打开串口的文件句柄 *@param speed 类型 int 串口速度 *@return void */ static int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,}; static int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200, 300,}; static void set_speed(int fd, int speed){ int i; int status; struct termios Opt; tcgetattr(fd, &Opt); for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) { if (speed == name_arr[i]) { tcflush(fd, TCIOFLUSH); cfsetispeed(&Opt, speed_arr[i]); cfsetospeed(&Opt, speed_arr[i]); status = tcsetattr(fd, TCSANOW, &Opt); if (status != 0) { perror("tcsetattr fd1"); return; } tcflush(fd,TCIOFLUSH); } } } /** *@brief 设置串口数据位,停止位和效验位 *@param fd 类型 int 打开的串口文件句柄 *@param databits 类型 int 数据位 取值 为 7 或者8 *@param stopbits 类型 int 停止位 取值为 1 或者2 *@param parity 类型 char 效验类型 取值为N,E,O,,S */ static int set_Parity(int fd,int databits,int stopbits,char parity) { struct termios options; if ( tcgetattr( fd,&options) != 0) { perror("SetupSerial 1"); return -1; } options.c_cflag &= ~CSIZE; switch (databits) /*设置数据位数*/ { case 7: options.c_cflag |= CS7; break; case 8: options.c_cflag |= CS8; break; default: fprintf(stderr,"Unsupported data size\n"); return -2; } switch (parity) { case 'n': case 'N': options.c_cflag &= ~PARENB; /* Clear parity enable */ options.c_iflag &= ~INPCK; /* Enable parity checking */ break; case 'o': case 'O': options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/ options.c_iflag |= INPCK; /* Disnable parity checking */ break; case 'e': case 'E': options.c_cflag |= PARENB; /* Enable parity */ options.c_cflag &= ~PARODD; /* 转换为偶效验*/ options.c_iflag |= INPCK; /* Disnable parity checking */ break; case 'S': case 's': /*as no parity*/ options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB;break; default: fprintf(stderr,"Unsupported parity\n"); return -3; } /* 设置停止位*/ switch (stopbits) { case 1: options.c_cflag &= ~CSTOPB; break; case 2: options.c_cflag |= CSTOPB; break; default: fprintf(stderr,"Unsupported stop bits\n"); return -4; } options.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); // options.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|INPCK); tcflush(fd,TCIFLUSH); options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/ options.c_cc[VMIN] = 0; /* Update the options and do it NOW */ if (tcsetattr(fd,TCSANOW,&options) != 0){ perror("SetupSerial 3"); return -5; } return 0; } int serial_data_read(int fd, char *buf, int len) { int nRead; nRead = read(fd, buf, len); return nRead; } int serial_data_send(int fd, char *buf, int len) { int nWrite; nWrite = write(fd, buf, len); return nWrite; } #define TEST_SEND_DATA "1234567890-115200" int main(void) { int sfd; int ret; int i; char dataBuf[1024] = {0}; sfd = openSerialDevice(); if (sfd < 0){ printf("open device failed!\n"); return -1; } set_speed(sfd, 115200); ret = set_Parity(sfd, 8, 1, 'N'); if (ret < 0){ printf("set parity error, ret: %d\n", ret); return -2; } while(1){ printf("send data..115200....."); ret = serial_data_send(sfd, TEST_SEND_DATA, sizeof(TEST_SEND_DATA)); printf("%d sent\n", ret); printf("start recive data:\n"); ret = serial_data_read(sfd, dataBuf, 1024); printf("len=%d, data=", ret); for(i = 0; i < ret; i++) printf("%c", dataBuf[i]); printf("\n"); } }
dailulu13 2016-11-03
  • 打赏
  • 举报
回复
没有人会么。。。。。。求大神

1,324

社区成员

发帖
与我相关
我的任务
社区描述
主要是开发驱动技术
社区管理员
  • 驱动程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧