1. 程式人生 > >一個串列埠通訊程式,適用於3g模組傳送簡單AT指令

一個串列埠通訊程式,適用於3g模組傳送簡單AT指令

原始碼如下:

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>

int tty_fd;


int max(int a,int b)
{
    return a>b?a:b;
}


int
set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop) { struct termios newtio,oldtio; if ( tcgetattr( fd,&oldtio) != 0) { perror("SetupSerial 1"); return -1; } bzero( &newtio, sizeof( newtio ) ); newtio.c_cflag |= CLOCAL | CREAD; newtio.c_cflag &= ~CSIZE; switch
( nBits ) { case 7: newtio.c_cflag |= CS7; break; case 8: newtio.c_cflag |= CS8; break; } switch( nEvent ) { case 'O': //奇校驗 newtio.c_cflag |= PARENB; newtio.c_cflag |= PARODD; newtio.c_iflag |= (INPCK | ISTRIP); break
; case 'E': //偶校驗 newtio.c_iflag |= (INPCK | ISTRIP); newtio.c_cflag |= PARENB; newtio.c_cflag &= ~PARODD; break; case 'N': //無校驗 newtio.c_cflag &= ~PARENB; break; } switch( nSpeed ) { case 2400: cfsetispeed(&newtio, B2400); cfsetospeed(&newtio, B2400); break; case 4800: cfsetispeed(&newtio, B4800); cfsetospeed(&newtio, B4800); break; case 9600: cfsetispeed(&newtio, B9600); cfsetospeed(&newtio, B9600); break; case 115200: cfsetispeed(&newtio, B115200); cfsetospeed(&newtio, B115200); break; default: cfsetispeed(&newtio, B9600); cfsetospeed(&newtio, B9600); break; } if( nStop == 1 ) { newtio.c_cflag &= ~CSTOPB; } else if ( nStop == 2 ) { newtio.c_cflag |= CSTOPB; } newtio.c_cc[VTIME] = 0; newtio.c_cc[VMIN] = 0; tcflush(fd,TCIFLUSH); if((tcsetattr(fd,TCSANOW,&newtio))!=0) { perror("com set error"); return -1; } // printf("set done!\n"); return 0; } int open_port(int fd,int comport) { // char *dev[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"}; // long vdisable; if (comport==0) { //printf("open /dev/ttyUSB0"); fd = open( "/dev/ttyUSB0", O_RDWR|O_NOCTTY|O_NDELAY); if (-1 == fd) { perror("Can't Open Serial Port"); return(-1); } else { // printf("open ttyUSB0 .....\n"); } } else if(comport==1) { fd = open( "/dev/ttyUSB1", O_RDWR|O_NOCTTY|O_NDELAY); if (-1 == fd) { perror("Can't Open Serial Port"); return(-1); } else { // printf("open ttyUSB1 .....\n"); } } else if (comport==2) { fd = open( "/dev/ttyUSB2", O_RDWR|O_NOCTTY|O_NDELAY); if (-1 == fd) { perror("Can't Open Serial Port"); return(-1); } else { // printf("open ttyUSB2 .....\n"); } } if(fcntl(fd, F_SETFL, 0)<0) { printf("fcntl failed!\n"); } else { //printf("fcntl=%d\n",fcntl(fd, F_SETFL,0)); } if(isatty(STDIN_FILENO)==0) { printf("standard input is not a terminal device\n"); } else { // printf("isatty success!\n"); } // printf("fd-open=%d\n",fd); return fd; } int main(int argc, char *argv[]) { int maxfd; fd_set rd; int nread,i; char tmp_buff[8]; char buff[1024]; char send_buf[1024]; int offset; char recvbuf[1024]; struct timeval timeout={0,50000}; int timeout_flg = 0; int recv_flg = 0; // pthread_t check_ack_thread; // pthread_attr_t check_ack_thread_attr; if((tty_fd=open_port(tty_fd,1))<0) { perror("open_port error"); return 0; } if((i=set_opt(tty_fd,115200,8,'N',1))<0) { perror("set_opt error"); return 0; } // printf("fd=%d\n",tty_fd); FD_ZERO(&rd); strcpy(send_buf,argv[1]); // printf("uart >> "); send_buf[strlen(send_buf)] = '\r'; send_buf[strlen(send_buf) + 1] = '\n'; send_buf[strlen(send_buf) + 2] = 0; write(tty_fd, send_buf, strlen(send_buf)); memset(send_buf,0,sizeof(send_buf)); FD_SET(tty_fd, &rd); FD_SET(fileno(stdin), &rd); maxfd = max(fileno(stdin), tty_fd) + 1; if(select(maxfd, &rd, NULL,NULL,NULL) < 0) { perror("select error\n"); } if(FD_ISSET(tty_fd, &rd)){ while((nread = read(tty_fd, buff, sizeof(buff))) > 0) { printf("%s", buff); memset(buff, 1 , sizeof(buff)); } exit(1); } close (tty_fd); return 0; }

相關推薦

一個串列通訊程式用於3g模組傳送簡單AT指令

原始碼如下: #include <stdio.h> #include <string.h> #include <sys/types.h> #include <errno.h> #include <sys

一個C串列通訊程式

#include <stdio.h>#include <dos.h>#include <conio.h>#include <string.h> #define COM232  0x2f8#define COMINT  0x0b

C語言RL78 serial bootloader和C#語言bootloader PC端串列通訊程式

            瞭解更多關於bootloader 的C語言實現,請加我QQ: 1273623966 (驗證資訊請填 bootloader),歡迎諮詢或定製bootloader(線上升級程式)。   前段時間完成的hyperboot_rl78, 是專門為Renesas 16-bit微控制器RL78 通

串列通訊解決serialport.DataReceived接收到的資料不完整

private SerialPort serialport = new SerialPort(); private StringBuilder builder = new StringBuilder(); // private String

NanoPC-T3 plus 串列通訊程式程式碼

我是用的是NanoPC-T3plus 串列埠UART0(除錯串列埠接收的資料不全,不知道什麼原因),將程式碼改成程式中的const char *DeviceName = "/dev/ttyS0"的ttyS0改成ttySAC0即可使用;   本程式摘自友善之臂最新Linux示例(光碟

STC15F2K60S2串列通訊程式

//********************************************** //STC15F2K60S2串列埠通訊程式,將串列埠接受到的資料從串列埠發出去 //採用22.1184M晶振,波特率115200 //為方便大家除錯,特附該程式的專案工程檔案下載地址,下載開啟即

STC15F2K60S2串列通訊程式程式碼

//********************************************** //STC15F2K60S2串列埠通訊程式,將串列埠接受到的資料從串列埠發出去 //採用22.1184M晶振,波特率115200 //為方便大家除錯,特附該程式的專案工程

用C語言實現串列通訊程式

1. 首先安裝虛擬串列埠vspd軟體,用於建立虛擬串列埠進行互聯除錯2.開啟vspd軟體,選擇COM1和COM4,點選Add pair,把COM1和COM4互聯3.開啟虛擬串列埠助手,設定串列埠號為COM1,波特率為19200,其他設定使用預設值,點選連線4.用C語言編寫串列

有關於串列通訊程式的編寫

在dos/win95/win98的年代,作業系統對串列埠是不保護的,也就是說將串列埠的的資源完全開放給使用者,使用者可以用直接操作硬體的函式(比如說 TC2.0下的inport()和outport()函式) 跟串列埠直接打交道,這時候使用者使用直接操作串列埠的函式怎樣"折磨"串列埠都是沒有問題的,作業系統根本

stm32串列通訊程式之UART2(TTL)

一、硬體連線二、串列埠助手設定:三、keil下程式設計1.要新增的工程檔案二要編寫的user檔案1.main.c#include "printf.h" int main() { printf_init(); printf("

Cortex A-8裸機串列通訊程式

main.c:實現串列埠簡單的傳送的功能 #include "s5pc100.h" int main() { UART0.ULCON0 = 0x03; UART0.UCON0 = 0x05;

如何用Delphi開發串列通訊程式

       Delphi 是新一代視覺化開發工具,它具有功能強大、簡便易用和程式碼執行速度快等特點,Delphi系列開發工具在國際各媒體上曾獲得過三十多項大獎,是全球公認的快速應用開發工具技術的先驅者,他越來越在構架企業資訊系統方面發揮著重要作用。 它的功能很強大,但在對串列埠通訊的控制上Delphi的幫助

用VS2010寫了一個串列示例程式(使用API寫的)[email protec

為了配合GNSS的調式,今天用VS2010寫了一個串列埠示例程式(使用API寫的)。#include "MySerial.h" #include <TCHAR.H> #include &l

VS 串列通訊SerialPort官方demo

網上有很多解決方案: 主流大概三種: 1、使用mscomm控制元件,是微軟以前提供的控制元件,現在已經放棄了。當然在vs中可以手動整合這個控制元件,也能使用,但是需要註冊。 2、自己程式設計實現串列埠通訊(這個以後有時間寫個部落格) 3、使用.net framework 中

C#串列通訊程式SerialPort類

上篇文章主要是關於串列埠通訊中微控制器部分的程式編寫。在實際應用中,常常還要有上位機的配合才能完成工作。寫串列埠通訊的上位機程式,常常應用的程式語言有C++,VB,C#等。VB沒有學過,C++編寫視窗應用程式接觸也不多,暑假自己看了一些C#的書,也有了一些瞭解,所以就用C#

VC++USB及串列通訊程式(附工程)

 ResetEvent(USBReadOverlapped.hEvent);  bResult = ReadFile(HidHandle, USBRxBuff, dwBytesRead, &dwBytesRead, &USBReadOverlapped);  if (!bResult)  {

C++編寫串列通訊程式

宣告:本部落格的內容主要是本人學習其他串列埠通訊部落格之後的總結,主要參考的部落格地址如下: 串列埠通訊一般分為同步和非同步兩種方式,本部落格主要講述非同步通訊程式的編寫,其程式設計步驟主要分為四步驟: 一、開啟串列埠  Win32系統把檔案的概念進

vs2013用qt編寫串列通訊程式

1. 檔案->新建專案-> QT5 project->QT application,命名為HelloSerialPort,點選確定,在彈出來的嚮導介面中選擇下一步->勾選openGL,下一步->點選finish 2. 在mai

linux環境下C語言實現非阻塞方式讀取字串資料的串列測試程式串列工具的編寫

一、前言 1.1 關於串列埠測試工具,網上已經有整合好的應用程式提供大家使用,但其只提供功能介面,內部具體怎麼實現的還需要自己去探索; 1.2 關於串列埠通訊的測試程式在網上已經是數見不鮮,但也不排除很多是直接“參考”別人的(ctrl+c),而且很多程式

樹莓派ASCII指令串列通訊(十六進位制傳送

對於指令文字 ENQ STX 08 PS 0500 F0 ETX的ASCII傳送 首先按照我上一篇部落格開啟串列埠 >>ser.write("\x05\x02\x30\x38\x50\x53\x20\x20\x30\x35\x30\x30\x46\x30\x0