
On Wed, Nov 11, 2009 at 6:00 PM, Philippos Apolinarius
closecport :: Int -> IO Int closecport n= return (fromIntegral (c_closecport (fromIntegral n)))
The return here doesn't do what you think it does - semantically, the value of c_closecport is still considered pure and assumed to be referentially transparent, so multiple calls to closecport are allowed to share the value returned, or delay the call until the value is unwrapped, call it multiple times for each use of the value, or anything else. You need to use IO *directly* in the foreign import declaration so that the compiler knows that the function calls can't be shared or inlined or generally messed about with: the IO tells it that order of execution with respect to your other IO actions is important. This one looks the most right: foreign import stdcall unsafe "rs232.h closecport" closecport :: IO () so I think you need to look closer about why it wasn't working for you, and where or how you were using it.