SerialPort
dangbinghoo
dangbinghoo at gmail.com
Sun Sep 23 13:11:51 UTC 2018
On Thursday, 20 September 2018 at 10:51:52 UTC, braboar wrote:
> I am going to play with serial port read/write, so I fetched
> serial-port. After that, I wrote simple program:
>
> auto port_name = "/dev/ttyUSB1";
> auto reader = new SerialPort(port_name);
> reader.dataBits(DataBits.data8);
> reader.stopBits(StopBits.one);
> reader.parity(Parity.none);
> reader.speed(BaudRate.BR_230400);
> string[] income;
> reader.read(income);
>
> and the result is
>> serial.device.TimeoutException
>
> Can anybody give me a guide of using serial port?
>
> And one more question: how to outline code blocks? ''' or ---
> can't help me.
here is my code really works:
com is `reader` object in your code.
```
void writeBytes(ubyte[] arr)
{
com.write(arr);
}
void serialThread()
{
ubyte[500] buff;
running = true;
while (running)
{
string str;
size_t readed;
try
{
readed = com.read(buff);
// if (readed > 0) {
//writeln("com port readed " ~
std.conv.to!string(readed));
str = cast(string)(buff[0 .. readed]).idup;
// Don't use writeln for console or string serail
application.
write(str);
// }
}
// must catch for continous reading, otherwize, com
port will block.
catch (TimeoutException e)
{
}
}
}
```
thanks!
More information about the Digitalmars-d-learn
mailing list