Commit 6b74b1d6 authored by Mart Lubbers's avatar Mart Lubbers
Browse files

add write operation

parent 60e89ab8
...@@ -30,4 +30,6 @@ TTYreadc :: !*TTY -> (!Char, !*TTY) ...@@ -30,4 +30,6 @@ TTYreadc :: !*TTY -> (!Char, !*TTY)
TTYreadline :: !*TTY -> (!String, !*TTY) TTYreadline :: !*TTY -> (!String, !*TTY)
TTYwrite :: !*TTY !String -> !*TTY
TTYerror :: !*env -> (!String, !*env) TTYerror :: !*env -> (!String, !*env)
implementation module TTY implementation module TTY
import Data.Maybe import Data.Maybe
import StdFunc import StdFunc
...@@ -56,11 +55,6 @@ TTYclose f w = code { ...@@ -56,11 +55,6 @@ TTYclose f w = code {
ccall ttyclose "I:I:A" ccall ttyclose "I:I:A"
} }
TTYerror :: !*env -> (!String, !*env)
TTYerror w = code {
ccall ttyerror ":S:A"
}
TTYreadc :: !*TTY -> (!Char, !*TTY) TTYreadc :: !*TTY -> (!Char, !*TTY)
TTYreadc w = code { TTYreadc w = code {
ccall ttyreadc "I:VII" ccall ttyreadc "I:VII"
...@@ -71,11 +65,22 @@ TTYreadline t = code { ...@@ -71,11 +65,22 @@ TTYreadline t = code {
ccall ttyreadline "I:VSI" ccall ttyreadline "I:VSI"
} }
TTYwrite :: !*TTY !String -> !*TTY
TTYwrite s e = code {
ccall ttywrite "IS:I"
}
TTYerror :: !*env -> (!String, !*env)
TTYerror w = code {
ccall ttyerror ":S:A"
}
Start :: *World -> (!String, *World) Start :: *World -> (!String, *World)
Start w Start w
#! (ok, tty, w) = TTYopen "/dev/ttyUSB0" zero w #! (ok, tty, w) = TTYopen "/dev/ttyUSB0" zero w
| not ok = TTYerror w | not ok = TTYerror w
#! (c, tty) = TTYreadline tty #! (c, tty) = TTYreadline tty
#! tty = TTYwrite tty "Hello World"
#! (ok, w) = TTYclose tty w #! (ok, w) = TTYclose tty w
| not ok = TTYerror w | not ok = TTYerror w
#! (s, w) = TTYerror w #! (s, w) = TTYerror w
......
...@@ -29,7 +29,8 @@ static char *cleanStringToCString(CleanString s) ...@@ -29,7 +29,8 @@ static char *cleanStringToCString(CleanString s)
return cs; return cs;
} }
void ttyopen(CleanString fn, int baudrate, int bytesize, int parity, int stopbits, int xonoff, int *status, FILE **f) void ttyopen(CleanString fn, int baudrate, int bytesize, int parity,
int stopbits, int xonoff, int *status, FILE **f)
{ {
struct termios tio; struct termios tio;
char *cs_fn = cleanStringToCString(fn); char *cs_fn = cleanStringToCString(fn);
...@@ -49,15 +50,14 @@ void ttyopen(CleanString fn, int baudrate, int bytesize, int parity, int stopbit ...@@ -49,15 +50,14 @@ void ttyopen(CleanString fn, int baudrate, int bytesize, int parity, int stopbit
//Parity //Parity
tio.c_cflag |= PARENB | PARODD | CMSPAR; tio.c_cflag |= PARENB | PARODD | CMSPAR;
tio.c_cflag -= PARENB | PARODD | CMSPAR; tio.c_cflag -= PARENB | PARODD | CMSPAR;
if(parity == 1){ if(parity == 1)
tio.c_cflag |= PARENB | PARODD; tio.c_cflag |= PARENB | PARODD;
} else if(parity == 2){ else if(parity == 2)
tio.c_cflag |= PARENB; tio.c_cflag |= PARENB;
} else if(parity == 3){ else if(parity == 3)
tio.c_cflag |= PARODD | PARENB | CMSPAR; tio.c_cflag |= PARODD | PARENB | CMSPAR;
} else if(parity == 4){ else if(parity == 4)
tio.c_cflag |= PARENB | CMSPAR; tio.c_cflag |= PARENB | CMSPAR;
}
//Stopbits //Stopbits
tio.c_cflag |= CSTOPB; tio.c_cflag |= CSTOPB;
tio.c_cflag -= stopbits == 0 ? 0 : CSTOPB; tio.c_cflag -= stopbits == 0 ? 0 : CSTOPB;
...@@ -121,6 +121,15 @@ void ttyreadline(FILE *fd, CleanString *result, FILE **fdo) ...@@ -121,6 +121,15 @@ void ttyreadline(FILE *fd, CleanString *result, FILE **fdo)
free(buf); free(buf);
} }
int ttywrite(FILE *fd, CleanString s, FILE **fdo)
{
char *cs_s = cleanStringToCString(s);
fwrite(s, 1, strlen(cs_s), fd);
free(cs_s);
*fdo = fd;
}
int ttyclose(FILE *fd) int ttyclose(FILE *fd)
{ {
return fclose(fd) == 0 ? 1 : 0; return fclose(fd) == 0 ? 1 : 0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment