資源簡介
unity 安卓串口接發代碼:unity環境在android手機調用手機串口,該解壓包內附詳細圖文教程,按教程來操作即可。

代碼片段和文件信息
//包含頭文件
#include?
#include?
#include?
#include?
#include?
#ifndef?_WIN32?
#include?
#include?
#include?
#else
#include?
#pragma?warning?(disable:4996)
#endif
#include?
#include?
#include?
int fd_?=?-1;
extern?“C“?{
int?set_opt(int?fd?int?nSpeed?int?nBits?char?nEvent?int?nStop?int?min_btye);
}
long?long??getMilisec()
{
#ifndef?_WIN32?
struct?timeval?now;
gettimeofday(&now?NULL);
long?long??t_mili?=?((long?long)now.tv_sec)?*?1000?+?now.tv_usec?/?1000;
return?t_mili;
#else
return?0;
#endif
}
//打印
//#include?“android/log.h“
//static?const?char?*TAG?=?“HelloWorldScene“;
//#define?LOGI(fmt?args...)?__android_log_print(ANDROID_LOG_INFO?TAG?fmt?##args)
//#define?LOGD(fmt?args...)?__android_log_print(ANDROID_LOG_DEBUG?TAG?fmt?##args)
//#define?LOGE(fmt?args...)?__android_log_print(ANDROID_LOG_ERROR?TAG?fmt?##args)
extern?“C“??{
bool?Serial_Open()
{
if?(fd_?>?0)//already?open
return?true;
#ifndef?_WIN32
//char?*dev[]?=?{?“/dev/ttySAC0“?“/dev/ttySAC1“?“/dev/ttySAC2“?“/dev/ttySAC3“?};
//long?vdisable;
fd_?=?open(“/dev/ttyS1“?O_RDWR?|?O_NOCTTY?|?O_NDELAY);
if?(fd_?==?-1)?{
return?false;
}
//LOGE(“Open?Serial?Port?%s\n“dev[comport]);
//恢復串口為阻塞狀態
if?(fcntl(fd_?F_SETFL?0)?0)?{
return?false;
}
/*測試是否為終端設備*/
if?(isatty(STDIN_FILENO)?==?0)?{
//LOGE(“standard?input?is?not?a?terminal?device\n“);
}
set_opt(fd_?9600?8?‘N‘?1?200);
#else
fd_?=?open(“/dev/ttyS1“?O_RDWR);
if?(fd_?==?-1)?{
return?false;
}
#endif
return?fd_?>?0;
}
void?Serial_Close()
{
if?(fd_?>?0)
{
close(fd_);
fd_?=?-1;
}
}
int?Serial_SendData(const?unsigned?char*?data?int?size)
{
if?(fd_?0)//not?open
return?-1;
int?ret?=?write(fd_?data?size);
return?ret;
}
int?Serial_RecvData(unsigned?char*?buff?int?len)
{
if?(fd_?0)//not?open
return?-1;
memset(buff?0?len);
int?readSize?=?read(fd_?buff?len);
return?readSize;
}
/*****************************
?*?功能:設置串口函數
?*?入口:(fd,波特率,數據位,奇偶校驗,停止位)
?*****************************/
int?set_opt(int?fd?int?nSpeed?int?nBits?char?nEvent?int?nStop?int?min_btye)
{
#ifndef?_WIN32
struct?termios?newtio?oldtio;
/*保存測試現有串口參數設置,在這里如果串口號出錯,會有相關的出錯信息*/
if?(tcgetattr(fd?&oldtio)?!=?0)?{
//?LOGE(“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
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-01-02?14:34??Unity使用安卓串口(附教程)\
?????目錄???????????0??2018-01-02?14:34??Unity使用安卓串口(附教程)\test2\
?????目錄???????????0??2018-01-02?14:34??Unity使用安卓串口(附教程)\test2\jni\
?????文件????????1193??2017-12-27?13:22??Unity使用安卓串口(附教程)\test2\jni\Android.mk
?????文件??????????34??2017-12-27?13:23??Unity使用安卓串口(附教程)\test2\jni\Application.mk
?????文件????????4601??2017-12-28?13:25??Unity使用安卓串口(附教程)\test2\jni\serial.cpp
?????目錄???????????0??2018-01-02?14:34??Unity使用安卓串口(附教程)\test2\libs\
?????目錄???????????0??2018-01-02?14:34??Unity使用安卓串口(附教程)\test2\libs\armeabi\
?????目錄???????????0??2018-01-02?14:34??Unity使用安卓串口(附教程)\test2\libs\armeabi-v7a\
?????文件???????13504??2018-01-02?14:06??Unity使用安卓串口(附教程)\test2\libs\armeabi-v7a\libserialport.so
?????文件???????13456??2018-01-02?14:06??Unity使用安卓串口(附教程)\test2\libs\armeabi\libserialport.so
?????目錄???????????0??2018-01-02?14:34??Unity使用安卓串口(附教程)\test2\libs\x86\
?????文件????????5208??2018-01-02?14:06??Unity使用安卓串口(附教程)\test2\libs\x86\libserialport.so
?????目錄???????????0??2018-01-02?14:34??Unity使用安卓串口(附教程)\test2\obj\
?????目錄???????????0??2018-01-02?14:34??Unity使用安卓串口(附教程)\test2\obj\local\
?????目錄???????????0??2018-01-02?14:34??Unity使用安卓串口(附教程)\test2\obj\local\armeabi\
?????目錄???????????0??2018-01-02?14:34??Unity使用安卓串口(附教程)\test2\obj\local\armeabi-v7a\
?????文件???????49960??2018-01-02?14:06??Unity使用安卓串口(附教程)\test2\obj\local\armeabi-v7a\libserialport.so
?????文件???????????8??2018-01-02?14:06??Unity使用安卓串口(附教程)\test2\obj\local\armeabi-v7a\libstdc++.a
?????目錄???????????0??2018-01-02?14:34??Unity使用安卓串口(附教程)\test2\obj\local\armeabi-v7a\objs\
?????目錄???????????0??2018-01-02?14:34??Unity使用安卓串口(附教程)\test2\obj\local\armeabi-v7a\objs\serialport\
?????文件???????14452??2018-01-02?14:06??Unity使用安卓串口(附教程)\test2\obj\local\armeabi-v7a\objs\serialport\serial.o
?????文件???????11384??2018-01-02?14:06??Unity使用安卓串口(附教程)\test2\obj\local\armeabi-v7a\objs\serialport\serial.o.d
?????文件???????53660??2018-01-02?14:06??Unity使用安卓串口(附教程)\test2\obj\local\armeabi\libserialport.so
?????文件???????????8??2018-01-02?14:06??Unity使用安卓串口(附教程)\test2\obj\local\armeabi\libstdc++.a
?????目錄???????????0??2018-01-02?14:34??Unity使用安卓串口(附教程)\test2\obj\local\armeabi\objs\
?????目錄???????????0??2018-01-02?14:34??Unity使用安卓串口(附教程)\test2\obj\local\armeabi\objs\serialport\
?????文件???????13916??2018-01-02?14:06??Unity使用安卓串口(附教程)\test2\obj\local\armeabi\objs\serialport\serial.o
?????文件???????11380??2018-01-02?14:06??Unity使用安卓串口(附教程)\test2\obj\local\armeabi\objs\serialport\serial.o.d
?????目錄???????????0??2018-01-02?14:34??Unity使用安卓串口(附教程)\test2\obj\local\x86\
?????文件???????12408??2018-01-02?14:06??Unity使用安卓串口(附教程)\test2\obj\local\x86\libserialport.so
............此處省略6個文件信息
評論
共有 條評論