Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
osie
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
osie
Commits
ca4702ae
Commit
ca4702ae
authored
Oct 28, 2021
by
Ivan Tyagov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
OPC-Ua server should support more than one I2C devices (MOD-IOs). First steps for this.
parent
5bf65e50
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
15 deletions
+21
-15
coupler/opc-ua-server/server.c
coupler/opc-ua-server/server.c
+21
-15
No files found.
coupler/opc-ua-server/server.c
View file @
ca4702ae
...
@@ -20,16 +20,23 @@
...
@@ -20,16 +20,23 @@
// global relay state
// global relay state
uint8_t
myrelay
=
0
;
uint8_t
myrelay
=
0
;
static
int
setRelayState
(
int
command
)
{
// the default addresses of MOD-IOs
const
int
MOD_IO_0_ADDR
=
0x58
;
const
int
MOD_IO_1_ADDR
=
0x59
;
// the block device at host machine
static
const
char
I2C_BLOCK_DEVICE_NAME
[]
=
"/dev/i2c-1"
;
static
int
setRelayState
(
int
command
,
int
i2c_addr
)
{
/*
/*
* Set relays' state over I2C
* Set relays' state over I2C
*/
*/
int
file
;
int
file
;
int
adapter_nr
=
1
;
/* probably dynamically determined */
char
filename
[
20
];
char
filename
[
20
];
// step 1: open device
// step 1: open device
snprintf
(
filename
,
19
,
"/dev/i2c-%d"
,
adapter_nr
);
snprintf
(
filename
,
19
,
I2C_BLOCK_DEVICE_NAME
);
file
=
open
(
filename
,
O_RDWR
);
file
=
open
(
filename
,
O_RDWR
);
if
(
file
<
0
)
{
if
(
file
<
0
)
{
/* ERROR HANDLING; you can check errno to see what went wrong */
/* ERROR HANDLING; you can check errno to see what went wrong */
...
@@ -38,8 +45,7 @@ static int setRelayState(int command) {
...
@@ -38,8 +45,7 @@ static int setRelayState(int command) {
}
}
// step 2: address the slave by its address
// step 2: address the slave by its address
int
addr
=
0x58
;
/* The I2C address */
if
(
ioctl
(
file
,
I2C_SLAVE
,
i2c_addr
)
<
0
)
{
if
(
ioctl
(
file
,
I2C_SLAVE
,
addr
)
<
0
)
{
/* ERROR HANDLING; you can check errno to see what went wrong */
/* ERROR HANDLING; you can check errno to see what went wrong */
printf
(
"Error addressing i2c slave."
);
printf
(
"Error addressing i2c slave."
);
exit
(
1
);
exit
(
1
);
...
@@ -144,11 +150,11 @@ static void afterWriteTime0(UA_Server *server,
...
@@ -144,11 +150,11 @@ static void afterWriteTime0(UA_Server *server,
UA_Int32
hrValue
=
*
(
UA_Int32
*
)
data
->
value
.
data
;
UA_Int32
hrValue
=
*
(
UA_Int32
*
)
data
->
value
.
data
;
if
(
hrValue
>
0
){
if
(
hrValue
>
0
){
myrelay
|=
1UL
<<
0
;
// relay0
myrelay
|=
1UL
<<
0
;
// relay0
setRelayState
(
myrelay
);
setRelayState
(
myrelay
,
MOD_IO_0_ADDR
);
}
}
else
{
else
{
myrelay
&=
~
(
1UL
<<
0
);
// relay0
myrelay
&=
~
(
1UL
<<
0
);
// relay0
setRelayState
(
myrelay
);
setRelayState
(
myrelay
,
MOD_IO_0_ADDR
);
}
}
}
}
}
}
...
@@ -161,11 +167,11 @@ static void afterWriteTime1(UA_Server *server,
...
@@ -161,11 +167,11 @@ static void afterWriteTime1(UA_Server *server,
UA_Int32
hrValue
=
*
(
UA_Int32
*
)
data
->
value
.
data
;
UA_Int32
hrValue
=
*
(
UA_Int32
*
)
data
->
value
.
data
;
if
(
hrValue
>
0
){
if
(
hrValue
>
0
){
myrelay
|=
1UL
<<
1
;
myrelay
|=
1UL
<<
1
;
setRelayState
(
myrelay
);
setRelayState
(
myrelay
,
MOD_IO_0_ADDR
);
}
}
else
{
else
{
myrelay
&=
~
(
1UL
<<
1
);
myrelay
&=
~
(
1UL
<<
1
);
setRelayState
(
myrelay
);
setRelayState
(
myrelay
,
MOD_IO_0_ADDR
);
}
}
}
}
}
}
...
@@ -178,11 +184,11 @@ static void afterWriteTime2(UA_Server *server,
...
@@ -178,11 +184,11 @@ static void afterWriteTime2(UA_Server *server,
UA_Int32
hrValue
=
*
(
UA_Int32
*
)
data
->
value
.
data
;
UA_Int32
hrValue
=
*
(
UA_Int32
*
)
data
->
value
.
data
;
if
(
hrValue
>
0
){
if
(
hrValue
>
0
){
myrelay
|=
1UL
<<
2
;
myrelay
|=
1UL
<<
2
;
setRelayState
(
myrelay
);
setRelayState
(
myrelay
,
MOD_IO_0_ADDR
);
}
}
else
{
else
{
myrelay
&=
~
(
1UL
<<
2
);
myrelay
&=
~
(
1UL
<<
2
);
setRelayState
(
myrelay
);
setRelayState
(
myrelay
,
MOD_IO_0_ADDR
);
}
}
}
}
}
}
...
@@ -195,11 +201,11 @@ static void afterWriteTime3(UA_Server *server,
...
@@ -195,11 +201,11 @@ static void afterWriteTime3(UA_Server *server,
UA_Int32
hrValue
=
*
(
UA_Int32
*
)
data
->
value
.
data
;
UA_Int32
hrValue
=
*
(
UA_Int32
*
)
data
->
value
.
data
;
if
(
hrValue
>
0
){
if
(
hrValue
>
0
){
myrelay
|=
1UL
<<
3
;
myrelay
|=
1UL
<<
3
;
setRelayState
(
myrelay
);
setRelayState
(
myrelay
,
MOD_IO_0_ADDR
);
}
}
else
{
else
{
myrelay
&=
~
(
1UL
<<
3
);
myrelay
&=
~
(
1UL
<<
3
);
setRelayState
(
myrelay
);
setRelayState
(
myrelay
,
MOD_IO_0_ADDR
);
}
}
}
}
}
}
...
@@ -279,7 +285,7 @@ static void stopHandler(int sign) {
...
@@ -279,7 +285,7 @@ static void stopHandler(int sign) {
int
main
(
void
)
{
int
main
(
void
)
{
// set all relays to OFF at startup
// set all relays to OFF at startup
setRelayState
(
0x00
);
setRelayState
(
0x00
,
MOD_IO_0_ADDR
);
signal
(
SIGINT
,
stopHandler
);
signal
(
SIGINT
,
stopHandler
);
signal
(
SIGTERM
,
stopHandler
);
signal
(
SIGTERM
,
stopHandler
);
...
@@ -298,7 +304,7 @@ int main(void) {
...
@@ -298,7 +304,7 @@ int main(void) {
UA_Server_delete
(
server
);
UA_Server_delete
(
server
);
// set all relays to OFF at startup
// set all relays to OFF at startup
setRelayState
(
0x00
);
setRelayState
(
0x00
,
MOD_IO_0_ADDR
);
return
retval
==
UA_STATUSCODE_GOOD
?
EXIT_SUCCESS
:
EXIT_FAILURE
;
return
retval
==
UA_STATUSCODE_GOOD
?
EXIT_SUCCESS
:
EXIT_FAILURE
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment