블루투스 연결 성공 코드

From Doowon Kim

Jump to: navigation, search
#include <cfg/crt.h>    /* Floating point configuration. */

#include <string.h>
#include <stdio.h>
#include <io.h>

#include <dev/board.h>
#include <sys/timer.h>

#include <sys/thread.h>
#include <sys/event.h>

#include <dev/irqreg.h>

static HANDLE mutex;

char inbuf1[2];
char inbuf0[2];

//baud rate
u_long baud = 115200;

FILE *uart1;
FILE *uart0;

FILE* initSerial(void) {
	FILE *uart1;
	NutRegisterDevice(&DEV_UART, 0, 0);
	uart1 = fopen("uart1", "r+");
	_ioctl(_fileno(uart1), UART_SETSPEED, &baud);
	return uart1;
}

FILE* initModem(void){
	FILE *uart0;
	NutRegisterDevice(&DEV_UART0, 0, 0);
	uart0 = fopen("uart0", "r+");
	_ioctl(_fileno(uart0), UART_SETSPEED, &baud);
	return uart0;
}
void irqtest(void){
	fputs("\nEnter your name: ", uart1);
}
void cmdWrite(const char* cmd){
	
	int len = strlen(cmd);		// Clac. length of ASCII command
	fprintf(uart1,"\ncmd len : %d\n", len);

	/*find out how many buffer size are used.*/
	int size = 0;
	size += 5; // sof + link + flags + length + nLink
	size += len;

	char* outbuf;		// Buffer for frame
	outbuf = malloc(size);
	memset(outbuf, 0, size);

	char sof = 0xbf;			// Start of Frame
	char link = 0xff;			// 0xff for control channel
	int pos = 0;

	outbuf[pos++] = sof; // SOF
	outbuf[pos++] = link; // Link(0xff = Control, 0x00 = connection 1, etc.)
	outbuf[pos++] = 0x00; // Flags
	outbuf[pos++] = len; // Length

	memcpy(outbuf + pos, cmd, len);

	pos += len; // Move to correct position
	outbuf[pos++] = link ^ 0xff; // nlink
	
//	fprintf(uart0,outbuf);
//	fprintf(uart1,"[Send Message]\n");
//	_write(_fileno(uart1),outbuf,size);
	_write(_fileno(uart0),outbuf,size);

}
void dataWrite(const char* data, char linkId){
	
	int len = strlen(data);		// Clac. length of ASCII command
	fprintf(uart1,"\ndata len : %d\n", len);

	/*find out how many buffer size are used.*/
	int size = 0;
	size += 5; // sof + link + flags + length + nLink
	size += len;

	char* outbuf;		// Buffer for frame
	outbuf = malloc(size);
	memset(outbuf, 0, size);

	char sof = 0xbf;			// Start of Frame
	char link = linkId;			// 0xff for control channel
	int pos = 0;

	outbuf[pos++] = sof; // SOF
	outbuf[pos++] = link; // Link(0xff = Control, 0x00 = connection 1, etc.)
	outbuf[pos++] = 0x00; // Flags
	outbuf[pos++] = len; // Length

	memcpy(outbuf + pos, data, len);

	pos += len; // Move to correct position
	outbuf[pos++] = link ^ 0xff; // nlink
	
//	fprintf(uart0,outbuf);
//	fprintf(uart1,"[Send Message]\n");
//	_write(_fileno(uart1),outbuf,size);
	_write(_fileno(uart0),outbuf,size);
}
/*
Sender
*/
/*THREAD(Sender, arg){
	NutThreadSetPriority(127);
	while(1){
		//fputs("at\r", uart0);
		//fflush(uart0);
		cmdWrite("at\n");
		NutSleep(8500);
	}
}*/
/*
Receiver
*/
THREAD(Receiver, arg){
	NutThreadSetPriority(16);
	while(1){
	//	fgets(inbuf0, sizeof(inbuf0), uart0);
		
	//	fprintf(uart1, inbuf0);
		int got = _read(_fileno(uart0),inbuf0,sizeof(inbuf0));
	//	fprintf(uart1,"[Receive Messsage]\n");
		_write(_fileno(uart1),inbuf0,got);
		//NutSleep(100);
	}
}
int main(void)
{
	uart1 = initSerial();
	uart0 = initModem();
   
	//NutIrqEnable(&sig_SPI);
	//NutRegisterIrqHandler(&sig_SPI,irqtest,0);

	//fputs("SET PROFILE HDP SOURCE 100F WeightScale",uart0);
	//fflush(uart0);
	//fputs("SET CONTROL MUX 1",uart0);
	//fflush(uart0);
	
	//NutThreadCreate("t1", Sender, 0, 512);
    	NutThreadCreate("t2", Receiver, 0, 512);
	while(1){
		NutSleep(10000);
		cmdWrite("call 00:07:80:90:a9:8e 1001 HDP FLOW 803");
		NutSleep(10000);
		cmdWrite("HDP create 1 1 1 1003");
		NutSleep(10000);
		dataWrite("data",0x01);
		NutSleep(10000);
	}
    return 0;
}

Personal tools