Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
Urumbu
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Neil Gershenfeld
Urumbu
Commits
99858e5e
Commit
99858e5e
authored
Apr 20, 2021
by
Neil Gershenfeld
Browse files
Options
Downloads
Patches
Plain Diff
wip
parent
2b549761
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
serialtimetest/serialtimetest.c
+30
-6
30 additions, 6 deletions
serialtimetest/serialtimetest.c
serialtimetest/serialtimetest.py
+24
-4
24 additions, 4 deletions
serialtimetest/serialtimetest.py
with
54 additions
and
10 deletions
serialtimetest/serialtimetest.c
+
30
−
6
View file @
99858e5e
...
...
@@ -19,9 +19,11 @@
#include
<stdlib.h>
//
int
speed
,
port
;
float
delay
;
unsigned
char
msg0
=
0
;
unsigned
char
msg1
=
255
;
unsigned
char
buf
[
4
]
=
{
0
,
100
,
200
,
255
};
//unsigned char buf[4] = {0,100,200,255};
unsigned
char
buf
[
1
]
=
{
200
};
//
void
timer_handler
(
int
signum
)
{
write
(
port
,
&
buf
,
4
);
...
...
@@ -39,11 +41,14 @@ void timer_handler (int signum) {
}
//
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
argc
!=
3
)
{
printf
(
"command line: serialtimetest port speed
\n
"
);
if
(
argc
!=
4
)
{
printf
(
"command line: serialtimetest port speed
delay
\n
"
);
return
-
1
;
}
//
// set up port
//
delay
=
atof
(
argv
[
3
]);
speed
=
atoi
(
argv
[
2
]);
port
=
open
(
argv
[
1
],
O_RDWR
|
O_NOCTTY
|
O_NDELAY
);
fcntl
(
port
,
F_SETFL
,
0
);
...
...
@@ -61,16 +66,35 @@ int main(int argc,char *argv[]) {
cfsetospeed
(
&
term
,
speed
);
tcsetattr
(
port
,
TCSANOW
,
&
term
);
//
// alarm version
//
struct
sigaction
sa
;
struct
itimerval
timer
;
memset
(
&
sa
,
0
,
sizeof
(
sa
));
sa
.
sa_handler
=
&
timer_handler
;
sigaction
(
SIGALRM
,
&
sa
,
NULL
);
timer
.
it_value
.
tv_sec
=
0
;
timer
.
it_value
.
tv_usec
=
100
;
timer
.
it_value
.
tv_usec
=
delay
*
1.0e6
;
timer
.
it_interval
.
tv_sec
=
0
;
timer
.
it_interval
.
tv_usec
=
100
;
timer
.
it_interval
.
tv_usec
=
delay
*
1.0e6
;
setitimer
(
ITIMER_REAL
,
&
timer
,
NULL
);
//
while
(
1
);
/*
//
// loop version
//
struct timeval time;
double newtime,oldtime;
gettimeofday(&time,NULL);
oldtime = time.tv_sec+time.tv_usec/1.0e6;
while (1) {
gettimeofday(&time,NULL);
newtime = time.tv_sec+time.tv_usec/1.0e6;
if ((newtime-oldtime) >= delay) {
oldtime = newtime;
//write(port,&buf,4);
write(port,&buf,1);
}
}
*/
}
This diff is collapsed.
Click to expand it.
serialtimetest/serialtimetest.py
+
24
−
4
View file @
99858e5e
...
...
@@ -10,16 +10,36 @@
# warranty is provided, and users accept all liability.
#
import
serial
,
sys
,
time
,
signal
if
(
len
(
sys
.
argv
)
!=
3
):
print
(
"
command line: serialtest.py port speed
"
)
if
(
len
(
sys
.
argv
)
!=
4
):
print
(
"
command line: serialtest.py port speed
delay
"
)
sys
.
exit
()
device
=
sys
.
argv
[
1
]
baud
=
int
(
sys
.
argv
[
2
])
print
(
'
open
'
+
device
+
'
at
'
,
baud
)
delay
=
float
(
sys
.
argv
[
3
])
print
(
'
open
'
+
device
+
'
at
'
+
str
(
baud
)
+
'
delay
'
+
str
(
delay
))
port
=
serial
.
Serial
(
device
,
baudrate
=
baud
,
timeout
=
0
)
msg0
=
b
"
\x00
"
msg1
=
b
"
\xff
"
flag
=
0
'''
#
# loop version
#
oldtime = time.time()
while (1):
newtime = time.time()
if ((newtime-oldtime) >= delay):
oldtime = newtime
if (flag == 0):
port.write(msg0)
flag = 1
else:
port.write(msg1)
flag = 0
'''
#
# alarm version
#
def
handler
(
signum
,
stack
):
global
flag
if
(
flag
==
0
):
...
...
@@ -29,6 +49,6 @@ def handler(signum,stack):
port
.
write
(
msg1
)
flag
=
0
signal
.
signal
(
signal
.
SIGALRM
,
handler
)
signal
.
setitimer
(
signal
.
ITIMER_REAL
,
1
,
0.0002
)
signal
.
setitimer
(
signal
.
ITIMER_REAL
,
1
,
delay
)
while
(
1
):
0
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment