实现的功能:
使用linuxC语言通过串口编程和Socket编程,以及解析从串口收到的数据,并且把数据封装成http格式通过post方式可以实现远程发送数据到服务器,并且能够接收服务器发送过来的命令。
关于HTTP请求报文格式
关键代码如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "cssl.h"
char* dev_path;
char* ip = NULL;
int port;
int sockfd;
void sendDatawenshidu(uint8_t *buf,int length)
{
printf("buf:%s
",buf);
char page[] = "addWendu";
char content[4096];
char content_page[50];
char content_host[50];
char content_len[50];
sprintf(content_page,"POST /%s HTTP/1.1
",page);
sprintf(content_host,"HOST: %s:%d
",ip,port);
char content_type[] = "Content-Type: application/x-www-form-urlencoded
";
sprintf(content_len,"Content-Length: %d
",strlen(buf));
sprintf(content,"%s%s%s%s%s",content_page,content_host,content_type,content_len,buf);
send(sockfd,content,strlen(content),0);
}
void sendDataguangzhao(uint8_t *buf,int length)
{
printf("buf:%s
",buf);
char page[] = "Guangzhao";
char content[4096];
char content_page[50];
char content_len[50];
char content_host[50];
sprintf(content_page,"POST /%s HTTP/1.1
",page);
sprintf(content_host,"HOST: %s:%d
",ip,port);
char content_type[] = "Content-Type: application/x-www-form-urlencoded
";
sprintf(content_len,"Content-Length: %d
",strlen(buf));
sprintf(content,"%s%s%s%s%s",content_page,content_host,content_type,content_len,buf);
send(sockfd,content,strlen(content),0);
}
void sendDataGas(uint8_t *buf,int length)
{
printf("buf:%s
",buf);
char page[] = "Gas";
char content[4096];
char content_page[50];
char content_host[50];
char content_len[50];
sprintf(content_page,"POST /%s HTTP/1.1
",page);
sprintf(content_host,"HOST: %s:%d
",ip,port);
char content_type[] = "Content-Type: application/x-www-form-urlencoded
";
sprintf(content_len,"Content-Length: %d
",strlen(buf));
sprintf(content,"%s%s%s%s%s",content_page,content_host,content_type,content_len,buf);
send(sockfd,content,strlen(content),0);
}
static void callback(int id, uint8_t *buf,int length)
{
int i;
char dtype[2],data[50];
printf("the date from serial:%s
", buf);
printf("
read serial success: length = %d
", length);
sprintf(dtype,"%d",buf[2]);
switch(dtype[0]){
case '1':
sprintf(data,"wendu=%d&shidu=%d",buf[5],buf[6]);
puts(data);
sendDatawenshidu(data,strlen(data));
break;
case '2':
sprintf(data,"gz=%d",buf[5]);
puts(data);
sendDataguangzhao(data,strlen(data));
break;
case '3':
sprintf(data,"gs=%d",buf[5]);
puts(data);
sendDataGas(data,strlen(data));
break;
default:
puts("the date is error");
break;
}
}
int main( int argc, char **argv ){
dev_path = (char*)argv[1];
ip = (char*)argv[2];
port = atoi(argv[3]);
char buffer[64];
if( argc < 4 ){
fprintf( stderr,"usage: %s dev_path,ip and post
",argv[0] );
exit(1);
}
cssl_t *ser;
cssl_start();
ser=cssl_open(dev_path,callback,0,38400,8,0,1);
if (!ser){
printf("%s
",cssl_geterrormsg());
}
printf("Serial connection successful, DEV = %s
", dev_path);
sockfd = socket( AF_INET,SOCK_STREAM,0 );
if ( sockfd < 0 )
{
fprintf( stderr, "socket:%s
", strerror(errno));
exit(1);
}
else{
printf("success to connect the service
");
}
struct sockaddr_in sockaddr;
memset( &sockaddr,0,sizeof(sockaddr) );
sockaddr.sin_family = AF_INET;
sockaddr.sin_port = htons ( atoi (port) );
inet_pton(AF_INET,ip,&sockaddr.sin_addr.s_addr);
socklen_t len = sizeof(sockaddr);
if( connect(sockfd,(struct sockaddr*)&sockaddr,len) <0 ){
fprintf(stderr, "connect: %s
", strerror(errno) );
exit(1);
}
memset( buffer,0,sizeof(buffer) );
ssize_t n;
while(1){
if( (n = read(sockfd,buffer,1024)) > 0) {
printf("receive from service :");
puts(buffer);
printf("
");
cssl_putstring(ser,buffer);
printf("the serial receive the commend from service !
");
}
}
for(;;);
close(sockfd);
return 0;
}
具体详细的项目代码可以到笔者github仓库下载:
https://github.com/Wlain/-linuxC-Socket-http-post-.git