Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
mitkSerialCommunicationTest.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include <iostream>
19 
31 int main()
32 {
33  std::cout << "Test Program for mitkSerialCommunication.cpp\n";
34  std::cout << "Using com port COM7\n"; // current test uses hard coded COM7
35 // std::cout << "Using com port /dev/ttyUSB1\n";
36 
38  serial->SetBaudRate(mitk::SerialCommunication::BaudRate115200);
39  serial->SetPortNumber(mitk::SerialCommunication::COM7);
40  //serial->SetDeviceName("/dev/ttyUSB1");
41  serial->SetDataBits(mitk::SerialCommunication::DataBits8);
42  serial->SetParity(mitk::SerialCommunication::Even);
43  serial->SetStopBits(mitk::SerialCommunication::StopBits1);
44  serial->SetHardwareHandshake(mitk::SerialCommunication::HardwareHandshakeOff);
45  serial->SetSendTimeout(2000);
46  serial->SetReceiveTimeout(2000);
47  if (serial->OpenConnection() == false)
48  {
49  std::cout << "Error Opening connection to com port #" << serial->GetPortNumber() << std::endl;
50  return -1;
51  }
52  std::string message = "Hello World\r\n";
53  if (serial->Send(message) == false)
54  {
55  std::cout << "Error sending string '" << message << "'" << std::endl;
56  serial->CloseConnection();
57  return -1;
58  }
59  message = "";
60  serial->Send(std::string("Oh, Hello again, dort am Fluss wo die Baeume steh'n, will ich Dir in die Augen seh'n, ob ich da bleiben kann.\r\n"));
61 
62  std::cout << "Waiting to receive 4 characters. [readtimeout = 0! --> blocking --> waiting until 4 characters are send in windows implementation!]\n";
63  if (serial->Receive(message, 4) == false)// receive 4 bytes
64  std::cout << "Error receiving message. Only " << message.size() << " characters received: '" << message << "'.\n";
65  else
66  std::cout << "Received message: '" << message << "'.\n";
67 
68  std::cout << "Setting Receive timeout to 6 seconds, waiting to receive 10 more characters.\n";
69  serial->CloseConnection();
70  serial->SetReceiveTimeout(6000);
71  serial->OpenConnection();
72  if (serial->Receive(message, 10) == false)// receive 10 bytes
73  std::cout << "Error receiving message. Received " << message.size() << " characters: '" << message << "'.\n";
74  else
75  std::cout << "Received message: '" << message << "' without errors.\n";
76 
77  std::cout << "sending a new message.\n";
78  serial->Send(std::string("all your base are belong to us\r\n"));
79  std::cout << "Waiting to receive 20 characters.\n";
80  serial->Receive(message, 20);
81  std::cout << "Received " << message.size() << " characters. Message: '" << message << "'.\n";
82  serial->CloseConnection();
83  std::cout << "closing connection.\n";
84  std::cout << "good bye.\n";
85  return 0;
86 }
itk::SmartPointer< Self > Pointer
static Pointer New()
int main()
SerialCommunicationTest - send and receive bytes over a com port.