void sio_cleanup(sio_s *sio) { if (sio_isopen(sio)){ sio_flush(sio, SIO_IN | SIO_OUT ); sio_close(sio); } }
//初始化端口 BOOL CComm_Thread::Init_ComPort() { COApp* pApp=(COApp*)AfxGetApp(); unsigned int x_Baud[]= {B300,B600,B1200,B2400,B4800,B9600,B19200,B38400,B57600}; if(CommLink) return false; if(sio_open(pApp->x_Port)!=0) return false; sio_ioctl(pApp->x_Port,x_Baud[pApp->x_Order],P_NONE|BIT_8|STOP_1); sio_SetReadTimeouts(pApp->x_Port,200,200); sio_flush(pApp->x_Port,2); return true; }
/*************************************************************** 【函数功能】: 关闭串口 【输 入】: 无 【输 出】: 执行结果:0正确 【说 明】: ***************************************************************/ int CGyro::Close() { if (m_bPortOpened) { if (m_bOutEnabled) { Enable(false); m_bOutEnabled = false; } sio_flush(m_iPort,0); // 仅清空Input缓冲区 sio_close(m_iPort); // 等待发送缓冲区为空后close } m_bPortOpened = false; //m_iErrCnt = 0; cout<<"[-] Gyro: Stoped!"<<endl; return 0; }
void fob_autoconfig(char *hemisphere) { int i; unsigned char axis = 0, sign = 0; sio_putc(0x50); // Change Value sio_putc(0x32); // FBB Auto-Configuration p.131 sio_putc(fob_num_birds); //sio_putc(3); sio_flush(); if (hemisphere) { switch (hemisphere[0]) { case 'f': break; case 'l': axis = 6; sign = 1; break; default: break; } } usleep(700000); for (i = 0; i < fob_num_sensors; ++i) { fob_RS232toFOB(fob_sensors[i]); sio_putc(0x59); // Position/Angles (FoBman p.90) fob_RS232toFOB(fob_sensors[i]); fob_hemisphere(axis, sign); } sio_putc(0x50); // Change Value sio_putc(0x23); // Group mode (p.129); sio_putc(1); }
/*************************************************************** 【函数功能】: 关闭串口 【输 入】: 无 【输 出】: 执行结果:0正确 【说 明】: ***************************************************************/ int CSensor::Close() { if (m_bPortOpened) { // 关闭定时器 // EnableTimer(false); // 关闭发送线程 if(m_hSendThreadHandle != INVALID_HANDLE_VALUE) { m_bSendThreadStopFlag = true; Sleep(1); WaitForSingleObject(m_hSendThreadHandle,INFINITE); //CloseHandle(m_hSendThreadHandle); m_hSendThreadHandle = INVALID_HANDLE_VALUE; } // 清空发送缓冲 sio_flush(m_iPort,0); // 仅清空Input缓冲区 sio_close(m_iPort); // 等待发送缓冲区为空后close } m_bPortOpened = false; cout<<"[-] Sensor: Stoped !"<<endl; // 复位错误统计变量值 //m_sErr_USensor.err = RESULT_ERR_SENSOR_OK; //m_sErr_USensor.err_cnt = 0; //m_sErr_USensor.cmd = 0; //m_sErr_Voltage.err = RESULT_ERR_SENSOR_OK; //m_sErr_Voltage.err_cnt = 0; //m_sErr_Voltage.cmd = 0; //m_sErr_Current.err = RESULT_ERR_SENSOR_OK; //m_sErr_Current.err_cnt = 0; //m_sErr_Current.cmd = 0; //m_sErr_IOconfig.err = RESULT_ERR_SENSOR_OK; //m_sErr_IOconfig.err_cnt = 0; //m_sErr_IOconfig.cmd = 0; return 0; }
int main(void) { int autoboot; ino_t ino; dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base); v86.ctl = V86_FLAGS; dsk.drive = *(uint8_t *)PTOV(ARGS); dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD; dsk.unit = dsk.drive & DRV_MASK; dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1; bootinfo.bi_version = BOOTINFO_VERSION; bootinfo.bi_size = sizeof(bootinfo); bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */ bootinfo.bi_extmem = memsize(); bootinfo.bi_memsizes_valid++; /* Process configuration file */ autoboot = 1; if ((ino = lookup(PATH_CONFIG))) fsread(ino, cmd, sizeof(cmd)); if (*cmd) { printf("%s: %s", PATH_CONFIG, cmd); if (parse()) autoboot = 0; /* Do not process this command twice */ *cmd = 0; } /* * Try to exec stage 3 boot loader. If interrupted by a keypress, * or in case of failure, try to load a kernel directly instead. */ if (autoboot && !*kname) { memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3)); if (!keyhit(3*SECOND)) { load(); memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL)); } } /* Present the user with the boot2 prompt. */ for (;;) { printf("\nFreeBSD/i386 boot\n" "Default: %u:%s(%u,%c)%s\n" "boot: ", dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit, 'a' + dsk.part, kname); if (ioctrl & IO_SERIAL) sio_flush(); if (!autoboot || keyhit(5*SECOND)) getstr(); else putchar('\n'); autoboot = 0; if (parse()) putchar('\a'); else load(); } }
int main(void) { uint8_t autoboot; ufs_ino_t ino; size_t nbyte; dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base); v86.ctl = V86_FLAGS; v86.efl = PSL_RESERVED_DEFAULT | PSL_I; dsk.drive = *(uint8_t *)PTOV(ARGS); dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD; dsk.unit = dsk.drive & DRV_MASK; dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1; bootinfo.bi_version = BOOTINFO_VERSION; bootinfo.bi_size = sizeof(bootinfo); /* Process configuration file */ autoboot = 1; if ((ino = lookup(PATH_CONFIG)) || (ino = lookup(PATH_DOTCONFIG))) { nbyte = fsread(ino, cmd, sizeof(cmd) - 1); cmd[nbyte] = '\0'; } if (*cmd) { memcpy(cmddup, cmd, sizeof(cmd)); if (parse()) autoboot = 0; if (!OPT_CHECK(RBX_QUIET)) printf("%s: %s", PATH_CONFIG, cmddup); /* Do not process this command twice */ *cmd = 0; } /* * Try to exec stage 3 boot loader. If interrupted by a keypress, * or in case of failure, try to load a kernel directly instead. */ if (!kname) { kname = PATH_BOOT3; if (autoboot && !keyhit(3*SECOND)) { load(); kname = PATH_KERNEL; } } /* Present the user with the boot2 prompt. */ for (;;) { if (!autoboot || !OPT_CHECK(RBX_QUIET)) printf("\nFreeBSD/x86 boot\n" "Default: %u:%s(%u,%c)%s\n" "boot: ", dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit, 'a' + dsk.part, kname); if (DO_SIO) sio_flush(); if (!autoboot || keyhit(3*SECOND)) getstr(); else if (!autoboot || !OPT_CHECK(RBX_QUIET)) putchar('\n'); autoboot = 0; if (parse()) putchar('\a'); else load(); } }
/* --------------------------------------- 打开 RS232 通讯接口 --------------------------------------- */ static bool_t qst_com_rs232 ( __CR_IN__ void_t* parm, __CR_IN__ uint_t argc, __CR_IN__ ansi_t** argv ) { int32u baud; uint_t port, bits; uint_t stop, parity; /* --------------- */ const ansi_t* sstop; const ansi_t* sparity; /* 参数解析 <串口号> [波特率] [数据位] [校验位] [停止位] */ if (argc < 2) return (FALSE); bits = 8; baud = 115200UL; stop = CR_SIO_STOP10; parity = CR_SIO_NOP; sstop = "1"; sparity = "no"; port = str2intxA(argv[1]); if (argc > 2) { baud = str2intx32A(argv[2]); if (argc > 3) { bits = str2intxA(argv[3]); if (argc > 4) { sparity = argv[4]; if (str_cmpA(argv[4], "no") == 0) parity = CR_SIO_NOP; else if (str_cmpA(argv[4], "odd") == 0) parity = CR_SIO_ODD; else if (str_cmpA(argv[4], "even") == 0) parity = CR_SIO_EVEN; else if (str_cmpA(argv[4], "mark") == 0) parity = CR_SIO_MARK; else if (str_cmpA(argv[4], "space") == 0) parity = CR_SIO_SPCE; else sparity = "no"; if (argc > 5) { sstop = argv[5]; if (str_cmpA(argv[5], "1") == 0) stop = CR_SIO_STOP10; else if (str_cmpA(argv[5], "1.5") == 0) stop = CR_SIO_STOP15; else if (str_cmpA(argv[5], "2") == 0) stop = CR_SIO_STOP20; else sstop = "1"; } } } } sQstComm* ctx = (sQstComm*)parm; /* 关闭当前接口并打开串口 */ if (!sio_open(port)) return (FALSE); sio_setup(port, baud, bits, parity, stop); sio_set_buffer(port, 1024, 1024); sio_set_rd_timeout(port, 0, 0, QCOM_CUTDOWN); sio_set_wr_timeout(port, 0, QCOM_SNDTOUT); sio_clear_error(port); sio_flush(port, CR_SIO_FLU_RT); /* 设置工作参数 */ qst_com_close(parm, argc, argv); ctx->comm.obj.port = port; ctx->comm.send = qst_rs232_send; /* 启动接收线程 */ ctx->comm.thrd = thread_new(0, qst_rs232_main, parm, FALSE); if (ctx->comm.thrd == NULL) { sio_close(port); return (FALSE); } TRY_FREE(ctx->comm.title); ctx->comm.title = str_fmtA(" - COM%u, %u, %u, %s, %s", port, baud, bits, sparity, sstop); qst_update_title(ctx); return (TRUE); }
void fob_flush(void) { sio_flush(); }
int main(u_int argc, const char *argv[], const char *envv[], uint64_t memsize) { uint8_t autoboot; ufs_ino_t ino; size_t nbyte; /* Arguments from Miniboot. */ beri_argc = argc; beri_argv = argv; beri_envv = envv; beri_memsize = memsize; dmadat = &__dmadat; #if 0 /* XXXRW: more here. */ v86.ctl = V86_FLAGS; v86.efl = PSL_RESERVED_DEFAULT | PSL_I; dsk.drive = *(uint8_t *)PTOV(ARGS); #endif dsk.type = TYPE_DEFAULT; #if 0 dsk.unit = dsk.drive & DRV_MASK; dsk.slice = *(uint8_t *)PTOV(ARGS + 1) + 1; #endif bootinfo.bi_version = BOOTINFO_VERSION; bootinfo.bi_size = sizeof(bootinfo); /* Process configuration file */ autoboot = 1; if ((ino = lookup(PATH_CONFIG)) || (ino = lookup(PATH_DOTCONFIG))) { nbyte = fsread(ino, cmd, sizeof(cmd) - 1); cmd[nbyte] = '\0'; } if (*cmd) { memcpy(cmddup, cmd, sizeof(cmd)); if (parse()) autoboot = 0; if (!OPT_CHECK(RBX_QUIET)) printf("%s: %s", PATH_CONFIG, cmddup); /* Do not process this command twice */ *cmd = 0; } /* * Try to exec stage 3 boot loader. If interrupted by a keypress, * or in case of failure, try to load a kernel directly instead. */ if (!kname) { kname = PATH_LOADER; if (autoboot && !keyhit(3*SECOND)) { boot_fromfs(); kname = PATH_KERNEL; } } /* Present the user with the boot2 prompt. */ for (;;) { if (!autoboot || !OPT_CHECK(RBX_QUIET)) printf("\nFreeBSD/mips boot\n" "Default: %s%ju:%s\n" "boot: ", dev_nm[dsk.type], dsk.unitptr, kname); #if 0 if (ioctrl & IO_SERIAL) sio_flush(); #endif if (!autoboot || keyhit(3*SECOND)) getstr(); else if (!autoboot || !OPT_CHECK(RBX_QUIET)) putchar('\n'); autoboot = 0; if (parse()) putchar('\a'); else load(); } }
//嗅探模式读取应答通讯报文 int CComm_Thread::CommRecv(unsigned char *m_data) { COApp * pApp = (COApp *)AfxGetApp(); unsigned char x_ucLength; unsigned char x_ucDataR[100],x_ucCrc[2]; //数据缓存初始化 x_ucLength = 0; memset(x_ucCrc,0,sizeof(x_ucCrc)); memset(x_ucDataR,0,sizeof(x_ucDataR)); //开始进行嗅探读取数据 for(int k1=0; k1<10; k1++) { if(sio_read(pApp->x_Port,(char *)x_ucDataR,1) > 0) break; Sleep(10); } //嗅探失败退出 if(k1 >=10) { pApp->test.Format("嗅探失败!"); SendMessage(pApp->m_pMainWnd->m_hWnd,WM_MSGCOMMANS,0x10,0x00); return x_ucLength; } //提取应答命令中的命令码,判断数据长度 if(sio_read(pApp->x_Port,(char *)&x_ucDataR[1],1) != 1) return x_ucLength; //根据命令码的不同,读取相应的后续内容数据 switch(x_ucDataR[1]) { case 0x03: //数据提取 //读取数据长度 if(sio_read(pApp->x_Port,(char *)&x_ucDataR[2],1) == 1) { //判断数据是否超界 if(x_ucDataR[2] > 100) { sio_flush(pApp->x_Port,2); return 0; } //读取后续数据 x_ucLength = x_ucDataR[2]+2; if(sio_read(pApp->x_Port,(char *)&x_ucDataR[3],x_ucLength) == x_ucLength) { //应答长度合法 x_ucLength += 3; //数据读取完毕,校验数据 CRC16(x_ucDataR,x_ucLength-2,x_ucCrc); if((x_ucDataR[x_ucLength-2] != x_ucCrc[0]) || (x_ucDataR[x_ucLength-1] != x_ucCrc[1])) { pApp->test.Format("校验错误!"); SendMessage(pApp->m_pMainWnd->m_hWnd,WM_MSGCOMMANS,0x10,0x00); x_ucLength = 0; } } else { //应答长度非法 x_ucLength = 0; } } break; case 0x10: //参数设定 if(sio_read(pApp->x_Port,(char *)&x_ucDataR[2],6) == 6) { //长度合法 x_ucLength = 8; //数据读取完毕,校验数据 CRC16(x_ucDataR,x_ucLength-2,x_ucCrc); if((x_ucDataR[x_ucLength-2] != x_ucCrc[0]) || (x_ucDataR[x_ucLength-1] != x_ucCrc[1])) { pApp->test.Format("校验错误!"); SendMessage(pApp->m_pMainWnd->m_hWnd,WM_MSGCOMMANS,0x10,0x00); x_ucLength = 0; } } break; default: x_ucLength = 0; break; } //拷贝输出应答报文 memcpy(m_data,x_ucDataR,x_ucLength); //TEST //pApp->test.Format("收到报文: ← %02X %02X %02X %02X %02X %02X %02X %02X",x_ucDataR[0],x_ucDataR[1],x_ucDataR[2],x_ucDataR[3],x_ucDataR[4],x_ucDataR[5],x_ucDataR[6],x_ucDataR[7]); //SendMessage(pApp->m_pMainWnd->m_hWnd,WM_MSGCOMMANS,0x17,0x00); //TEST //清除缓冲区内的垃圾 sio_flush(pApp->x_Port,2); return x_ucLength; }
int main(void) { char cmd[512], cmdtmp[512]; int autoboot, dskupdated; ufs_ino_t ino; dmadat = (void *)(roundup2(__base + (int32_t)&_end, 0x10000) - __base); v86.ctl = V86_FLAGS; v86.efl = PSL_RESERVED_DEFAULT | PSL_I; dsk.drive = *(uint8_t *)PTOV(ARGS); dsk.type = dsk.drive & DRV_HARD ? TYPE_AD : TYPE_FD; dsk.unit = dsk.drive & DRV_MASK; dsk.part = -1; dsk.start = 0; bootinfo.bi_version = BOOTINFO_VERSION; bootinfo.bi_size = sizeof(bootinfo); bootinfo.bi_basemem = 0; /* XXX will be filled by loader or kernel */ bootinfo.bi_extmem = memsize(); bootinfo.bi_memsizes_valid++; /* Process configuration file */ if (gptinit() != 0) return (-1); autoboot = 1; *cmd = '\0'; for (;;) { *kname = '\0'; if ((ino = lookup(PATH_CONFIG)) || (ino = lookup(PATH_DOTCONFIG))) fsread(ino, cmd, sizeof(cmd)); if (*cmd != '\0') { memcpy(cmdtmp, cmd, sizeof(cmdtmp)); if (parse(cmdtmp, &dskupdated)) break; if (dskupdated && gptinit() != 0) break; if (!OPT_CHECK(RBX_QUIET)) printf("%s: %s", PATH_CONFIG, cmd); *cmd = '\0'; } if (autoboot && keyhit(3)) { if (*kname == '\0') memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3)); break; } autoboot = 0; /* * Try to exec stage 3 boot loader. If interrupted by a * keypress, or in case of failure, try to load a kernel * directly instead. */ if (*kname != '\0') load(); memcpy(kname, PATH_BOOT3, sizeof(PATH_BOOT3)); load(); memcpy(kname, PATH_KERNEL, sizeof(PATH_KERNEL)); load(); gptbootfailed(&dsk); if (gptfind(&freebsd_ufs_uuid, &dsk, -1) == -1) break; dsk_meta = 0; } /* Present the user with the boot2 prompt. */ for (;;) { if (!OPT_CHECK(RBX_QUIET)) { printf("\nFreeBSD/x86 boot\n" "Default: %u:%s(%up%u)%s\n" "boot: ", dsk.drive & DRV_MASK, dev_nm[dsk.type], dsk.unit, dsk.part, kname); } if (ioctrl & IO_SERIAL) sio_flush(); *cmd = '\0'; if (keyhit(0)) getstr(cmd, sizeof(cmd)); else if (!OPT_CHECK(RBX_QUIET)) putchar('\n'); if (parse(cmd, &dskupdated)) { putchar('\a'); continue; } if (dskupdated && gptinit() != 0) continue; load(); } /* NOTREACHED */ }
static void * sior_rpc_server(u32 funcno, void * data, int size) { int res = 0, c; size_t s; char * p; struct init_arguments_t * i; switch(funcno) { case SIOR_INIT: i = (struct init_arguments_t *) data; sio_init(i->baudrate, i->lcr_ueps, i->lcr_upen, i->lcr_usbl, i->lcr_umode); break; case SIOR_PUTC: c = *((int *) data); res = sio_putc(c); break; case SIOR_GETC: res = sio_getc(); break; case SIOR_GETCBLOCK: res = sio_getc_block(); break; case SIOR_WRITE: p = *((char **) data) + IOP_MEM; s = *(((size_t *) data) + 1); DI(); ee_kmode_enter(); res = sio_write(p, s); ee_kmode_exit(); EI(); break; case SIOR_READ: p = *((char **) data) + IOP_MEM; s = *(((size_t *) data) + 1); DI(); ee_kmode_enter(); res = sio_read(p, s); ee_kmode_exit(); EI(); break; case SIOR_PUTS: p = *((char **) data) + IOP_MEM; DI(); ee_kmode_enter(); res = sio_puts(p); ee_kmode_exit(); EI(); break; case SIOR_PUTSN: p = *((char **) data) + IOP_MEM; DI(); ee_kmode_enter(); res = sio_putsn(p); ee_kmode_exit(); EI(); break; case SIOR_GETS: p = *((char **) data) + IOP_MEM; DI(); ee_kmode_enter(); (char*)res = sio_gets(p); ee_kmode_exit(); EI(); break; case SIOR_FLUSH: sio_flush(); break; } *((int *) data) = res; return data; }