Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 7 months ago.
thread
I try to create 3 threads to read GPS, ODB, bluetooth,but the thread often be dead when it power on,
the code follow:
- include "mbed.h"
- include "rtos.h"
DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4);
enum enDevice{ BT = 1, OBD = 2, GPS = 3 };
typedef struct stBuffer_t{ enum enDevice enName; char cHead; char acBuffer[100]; char cEnd; }Bufer_t, *PBufer_t;
MemoryPool<Bufer_t, 106> mpool; Queue<Bufer_t, 106> queue;
/***************/ /* NAME: thread_readBt */ /* */ /* DESCRIPTIONS: read bluetooth */ /* */ /* Calling Period: 100ms */ /* */ /***************/ void thread_writeBt(void const *args) { myBluetooth cBt; cBt.baud(BtBaud);
while (true) { osEvent evt = queue.get(); if (evt.status == osEventMessage) { Bufer_t *pstBuffer = (Bufer_t *)evt.value.p; cBt.sendData(&pstBuffer->cHead, 0); cBt.sendData(pstBuffer->acBuffer, 0); cBt.sendData(&pstBuffer->cEnd, 0); mpool.free(pstBuffer);
led2 = !led2; Thread::wait(1); } } }
/***************/ /* NAME: thread_readBt */ /* */ /* DESCRIPTIONS: read bluetooth */ /* */ /* Calling Period: 100ms */ /* */ /***************/ void thread_readBt(void const *args) { while (true) { led2 = !led2;
Thread::wait(100); } }
/***************/ /* NAME: thread_readGps */ /* */ /* DESCRIPTIONS: read gps */ /* */ /* Calling Period: 100ms */ /* */ /***************/ void thread_readGps(void const *args) { int nCnt = 0; myGPS cMyGps(GpsUartTx, GpsUartRx, GpsName, GpsRst, GpsOnOff); cMyGps.baud(GpsBaud);
while (true) { led3 = !led3; Bufer_t *pstBuffer = mpool.alloc(); pstBuffer->enName = GPS; pstBuffer->cHead = ' '; nCnt = cMyGps.getData(pstBuffer->acBuffer, 0); pstBuffer->cEnd = ' '; if(nCnt > 0){ queue.put(pstBuffer); } Thread::wait(100); } }
/***************/ /* NAME: thread_readObd */ /* */ /* DESCRIPTIONS: read OBD */ /* */ /* Calling Period: 100ms */ /* */ /***************/ void thread_readObd(void const *args) { int nCnt = 0; myOBD cMyObd; cMyObd.baud(GpsBaud);
while (true) { led4 = !led4; Bufer_t *pstBuffer = mpool.alloc(); pstBuffer->enName = OBD; pstBuffer->cHead = '#'; nCnt = cMyGps.getData(pstBuffer->acBuffer, 0); nCnt = 1; pstBuffer->acBuffer[0] = '1'; pstBuffer->acBuffer[1] = '2'; pstBuffer->acBuffer[2] = '3'; pstBuffer->acBuffer[3] = '4'; pstBuffer->acBuffer[4] = '5'; pstBuffer->acBuffer[5] = '6'; pstBuffer->acBuffer[6] = '7'; pstBuffer->acBuffer[7] = '8'; pstBuffer->acBuffer[8] = '9'; pstBuffer->acBuffer[9] = '0'; pstBuffer->acBuffer[10] = 'a'; pstBuffer->acBuffer[11] = 'b'; pstBuffer->acBuffer[12] = 'c'; pstBuffer->acBuffer[13] = 'd'; pstBuffer->acBuffer[14] = 'e'; pstBuffer->cEnd = '#'; if(nCnt > 0){ queue.put(pstBuffer); }
Thread::wait(100); } }
/***************/ /* NAME: main */ /* */ /* DESCRIPTIONS: This is the main function with 1 sec tasks. */ /* */ /* Calling Period: 1S */ /* */ /***************/ int main() { Thread* pThreadBt = new Thread(thread_writeBt); Thread* pThreadGps = new Thread(thread_readGps); Thread* pThreadObd = new Thread(thread_readObd); Thread thread1(thread_writeBt); Thread thread2(thread_readGps); Thread thread3(thread_readObd);
myBluetooth cBt; cBt.baud(BtBaud);
while (true) { osEvent evt = queue.get(); if (evt.status == osEventMessage) { Bufer_t *pstBuffer = (Bufer_t *)evt.value.p; cBt.sendData(&pstBuffer->cHead, 0); cBt.sendData(pstBuffer->acBuffer, 0); cBt.sendData(&pstBuffer->cEnd, 0); mpool.free(pstBuffer);
led2 = !led2; Thread::wait(1); } } }