Welcome to %s forums

BrainModular Users Forum

Login Register

How to read from serial port..

Create your own modules in C++
Post Reply
Gizzeta
Member
Posts: 42
Location: Paris
Contact:

Unread post by Gizzeta » 09 May 2009, 00:12

here again!
I'm trying to create a module in C++, that read datas send on the serial port (com4) by an external device.
I programmed the device so that it sends an ascii char whose bits represent the state of 8 phisical port, every time some of it change his state. Outside Usine it seems to work fine.
I have utilized the CreateFile and ReadFile function in the main process function. It works, but the cpu usage in the patch is 100%, and there's a substantial delay. What is strange is that only the patch cpu usage is at max, not the main usine cpu usage (the green one) that is at 5%.

Somebody can advise me about other methods to read from a serial port? (fstream maybe, but I don't understend it very well...)

thanks for help

User avatar
senso
Site Admin
Posts: 4424
Location: France
Contact:

Unread post by senso » 09 May 2009, 09:44

Not so easy to explain how to read serial port. It's not so easy but they are many c++ library's that you can find on the net?
I a word, it's just a window service that you open and start.
The only thing I recommend is to create a separate user module just to read data's from the port that you can connect to any other module.

about the cpu: the indicator calculates the time the patch takes to achieve the 'proceed' function. So any disk access take, at least 10ms which is 10 times larger than the maximum allowed calculation time. During this time the CPU is waiting for the hard-disk. So the global cpu time is 0% but the time it takes is 10000%!!!

Gizzeta
Member
Posts: 42
Location: Paris
Contact:

Unread post by Gizzeta » 12 May 2009, 15:16

Ok, now it's clear for the cpu monitor.

So I have a general question on Usine, just for better undrstending.
I guess it has 2 differents threads: one for the audio and one for te datas.
I guess also that (ie.) if sampling rate is at 44.1kHz and block size is 128 samples, the process function is called every 128 / 44100 = 3 ms (...around), right?
And what about the other thread? It call "process" function too (I don't think...)? If not, other datas treatments, when are they done? with the audio treatment?

About the libraryes on the net, they all use CreateFile and ReadFile, I've not found the window's service you speak about... :(

User avatar
senso
Site Admin
Posts: 4424
Location: France
Contact:

Unread post by senso » 12 May 2009, 15:59

all patches are calculated every BLOC samples.
most of data's are processed in an secondary thread but some some of them are treated immediately. In the SDK this can be set with the parameter option

Code: Select all

enum TFastCallBackType {
	ctNormal,       
	ctImmediate,    
	ctAsynchronous  
};
typically :
- ctNormal for most data, the response depends on the setup/refresh speed option
- ctImmediate for example the play button of the sampler module
- ctAsynchronous for a 'file open' button, very slow...

sorry about the windows service, you re right it's a kind of file emulation.
but to help you, you have to create an independent thread which reads data's from the ports and stores them into a buffer.
then into the process of Usine, try to read the buffer.
also, be careful to 'lock' (notion of 'critical section') the buffer to avoid thread conflicts.

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests